From 162ace2fd6f4683a056e08b3c0c3b199d115b8de Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 23 Oct 2018 06:59:52 +0900 Subject: [PATCH] Improve some API definitions --- .../api/endpoints/drive/files/delete.ts | 29 +++++++++++++------ src/server/api/endpoints/drive/files/show.ts | 29 +++++++++++++------ 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/server/api/endpoints/drive/files/delete.ts b/src/server/api/endpoints/drive/files/delete.ts index fb7340df38..e29e330aa8 100644 --- a/src/server/api/endpoints/drive/files/delete.ts +++ b/src/server/api/endpoints/drive/files/delete.ts @@ -3,8 +3,11 @@ import DriveFile from '../../../../../models/drive-file'; import del from '../../../../../services/drive/delete-file'; import { publishDriveStream } from '../../../../../stream'; import { ILocalUser } from '../../../../../models/user'; +import getParams from '../../../get-params'; export const meta = { + stability: 'stable', + desc: { 'ja-JP': 'ドライブのファイルを削除します。', 'en-US': 'Delete a file of drive.' @@ -12,23 +15,31 @@ export const meta = { requireCredential: true, - kind: 'drive-write' + kind: 'drive-write', + + params: { + fileId: $.type(ID).note({ + desc: { + 'ja-JP': '対象のファイルID', + 'en-US': 'Target file ID' + } + }) + } }; -export default async (params: any, user: ILocalUser) => { - // Get 'fileId' parameter - const [fileId, fileIdErr] = $.type(ID).get(params.fileId); - if (fileIdErr) throw 'invalid fileId param'; +export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { + const [ps, psErr] = getParams(meta, params); + if (psErr) return rej(psErr); // Fetch file const file = await DriveFile .findOne({ - _id: fileId, + _id: ps.fileId, 'metadata.userId': user._id }); if (file === null) { - throw 'file-not-found'; + return rej('file-not-found'); } // Delete @@ -37,5 +48,5 @@ export default async (params: any, user: ILocalUser) => { // Publish file_deleted event publishDriveStream(user._id, 'file_deleted', file._id); - return; -}; + res(); +}); diff --git a/src/server/api/endpoints/drive/files/show.ts b/src/server/api/endpoints/drive/files/show.ts index e35c5e3ca2..49d6027add 100644 --- a/src/server/api/endpoints/drive/files/show.ts +++ b/src/server/api/endpoints/drive/files/show.ts @@ -1,8 +1,11 @@ import $ from 'cafy'; import ID from '../../../../../misc/cafy-id'; import DriveFile, { pack } from '../../../../../models/drive-file'; import { ILocalUser } from '../../../../../models/user'; +import getParams from '../../../get-params'; export const meta = { + stability: 'stable', + desc: { 'ja-JP': '指定したドライブのファイルの情報を取得します。', 'en-US': 'Get specified file of drive.' @@ -10,24 +13,32 @@ export const meta = { requireCredential: true, - kind: 'drive-read' + kind: 'drive-read', + + params: { + fileId: $.type(ID).note({ + desc: { + 'ja-JP': '対象のファイルID', + 'en-US': 'Target file ID' + } + }) + } }; -export default async (params: any, user: ILocalUser) => { - // Get 'fileId' parameter - const [fileId, fileIdErr] = $.type(ID).get(params.fileId); - if (fileIdErr) throw 'invalid fileId param'; +export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { + const [ps, psErr] = getParams(meta, params); + if (psErr) return rej(psErr); // Fetch file const file = await DriveFile .findOne({ - _id: fileId, + _id: ps.fileId, 'metadata.userId': user._id, 'metadata.deletedAt': { $exists: false } }); if (file === null) { - throw 'file-not-found'; + return rej('file-not-found'); } // Serialize @@ -35,5 +46,5 @@ export default async (params: any, user: ILocalUser) => { detail: true }); - return _file; -}; + res(_file); +});