From 6dcc3800e0d0456c70efd9561903e21aad131b9e Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 24 Oct 2018 18:32:19 +0900 Subject: [PATCH] Improve some API definitions --- .../api/endpoints/drive/folders/create.ts | 37 +++++++++----- .../api/endpoints/drive/folders/show.ts | 24 ++++++--- .../api/endpoints/drive/folders/update.ts | 49 +++++++++++++------ 3 files changed, 78 insertions(+), 32 deletions(-) diff --git a/src/server/api/endpoints/drive/folders/create.ts b/src/server/api/endpoints/drive/folders/create.ts index f83fe46cd..cca25b059 100644 --- a/src/server/api/endpoints/drive/folders/create.ts +++ b/src/server/api/endpoints/drive/folders/create.ts @@ -2,8 +2,11 @@ import $ from 'cafy'; import ID from '../../../../../misc/cafy-id'; import DriveFolder, { isValidFolderName, pack } from '../../../../../models/drive-folder'; import { publishDriveStream } from '../../../../../stream'; import { ILocalUser } from '../../../../../models/user'; +import getParams from '../../../get-params'; export const meta = { + stability: 'stable', + desc: { 'ja-JP': 'ドライブのフォルダを作成します。', 'en-US': 'Create a folder of drive.' @@ -11,25 +14,37 @@ export const meta = { requireCredential: true, - kind: 'drive-write' + kind: 'drive-write', + + params: { + name: $.str.optional.pipe(isValidFolderName).note({ + default: 'Untitled', + desc: { + 'ja-JP': 'フォルダ名', + 'en-US': 'Folder name' + } + }), + + parentId: $.type(ID).optional.nullable.note({ + desc: { + 'ja-JP': '親フォルダID', + 'en-US': 'Parent folder ID' + } + }) + } }; export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - // Get 'name' parameter - const [name = '無題のフォルダー', nameErr] = $.str.optional.pipe(isValidFolderName).get(params.name); - if (nameErr) return rej('invalid name param'); - - // Get 'parentId' parameter - const [parentId = null, parentIdErr] = $.type(ID).optional.nullable.get(params.parentId); - if (parentIdErr) return rej('invalid parentId param'); + const [ps, psErr] = getParams(meta, params); + if (psErr) return rej(psErr); // If the parent folder is specified let parent = null; - if (parentId) { + if (ps.parentId) { // Fetch parent folder parent = await DriveFolder .findOne({ - _id: parentId, + _id: ps.parentId, userId: user._id }); @@ -41,7 +56,7 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = // Create folder const folder = await DriveFolder.insert({ createdAt: new Date(), - name: name, + name: ps.name, parentId: parent !== null ? parent._id : null, userId: user._id }); diff --git a/src/server/api/endpoints/drive/folders/show.ts b/src/server/api/endpoints/drive/folders/show.ts index bb25bcba3..f01c75d95 100644 --- a/src/server/api/endpoints/drive/folders/show.ts +++ b/src/server/api/endpoints/drive/folders/show.ts @@ -1,26 +1,38 @@ import $ from 'cafy'; import ID from '../../../../../misc/cafy-id'; import DriveFolder, { pack } from '../../../../../models/drive-folder'; import { ILocalUser } from '../../../../../models/user'; +import getParams from '../../../get-params'; export const meta = { + stability: 'stable', + desc: { - 'ja-JP': '指定したドライブのフォルダの情報を取得します。' + 'ja-JP': '指定したドライブのフォルダの情報を取得します。', + 'en-US': 'Get specified folder of drive.' }, requireCredential: true, - kind: 'drive-read' + kind: 'drive-read', + + params: { + folderId: $.type(ID).note({ + desc: { + 'ja-JP': '対象のフォルダID', + 'en-US': 'Target folder ID' + } + }) + } }; export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - // Get 'folderId' parameter - const [folderId, folderIdErr] = $.type(ID).get(params.folderId); - if (folderIdErr) return rej('invalid folderId param'); + const [ps, psErr] = getParams(meta, params); + if (psErr) return rej(psErr); // Get folder const folder = await DriveFolder .findOne({ - _id: folderId, + _id: ps.folderId, userId: user._id }); diff --git a/src/server/api/endpoints/drive/folders/update.ts b/src/server/api/endpoints/drive/folders/update.ts index 479dd2fd4..b041a1592 100644 --- a/src/server/api/endpoints/drive/folders/update.ts +++ b/src/server/api/endpoints/drive/folders/update.ts @@ -2,8 +2,11 @@ import $ from 'cafy'; import ID from '../../../../../misc/cafy-id'; import DriveFolder, { isValidFolderName, pack } from '../../../../../models/drive-folder'; import { publishDriveStream } from '../../../../../stream'; import { ILocalUser } from '../../../../../models/user'; +import getParams from '../../../get-params'; export const meta = { + stability: 'stable', + desc: { 'ja-JP': '指定したドライブのフォルダの情報を更新します。', 'en-US': 'Update specified folder of drive.' @@ -11,18 +14,40 @@ export const meta = { requireCredential: true, - kind: 'drive-write' + kind: 'drive-write', + + params: { + folderId: $.type(ID).note({ + desc: { + 'ja-JP': '対象のフォルダID', + 'en-US': 'Target folder ID' + } + }), + + name: $.str.optional.pipe(isValidFolderName).note({ + desc: { + 'ja-JP': 'フォルダ名', + 'en-US': 'Folder name' + } + }), + + parentId: $.type(ID).optional.nullable.note({ + desc: { + 'ja-JP': '親フォルダID', + 'en-US': 'Parent folder ID' + } + }) + } }; export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { - // Get 'folderId' parameter - const [folderId, folderIdErr] = $.type(ID).get(params.folderId); - if (folderIdErr) return rej('invalid folderId param'); + const [ps, psErr] = getParams(meta, params); + if (psErr) return rej(psErr); // Fetch folder const folder = await DriveFolder .findOne({ - _id: folderId, + _id: ps.folderId, userId: user._id }); @@ -30,22 +55,16 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = return rej('folder-not-found'); } - // Get 'name' parameter - const [name, nameErr] = $.str.optional.pipe(isValidFolderName).get(params.name); - if (nameErr) return rej('invalid name param'); - if (name) folder.name = name; + if (ps.name) folder.name = ps.name; - // Get 'parentId' parameter - const [parentId, parentIdErr] = $.type(ID).optional.nullable.get(params.parentId); - if (parentIdErr) return rej('invalid parentId param'); - if (parentId !== undefined) { - if (parentId === null) { + if (ps.parentId !== undefined) { + if (ps.parentId === null) { folder.parentId = null; } else { // Get parent folder const parent = await DriveFolder .findOne({ - _id: parentId, + _id: ps.parentId, userId: user._id });