Improve some API definitions

This commit is contained in:
syuilo 2018-10-24 18:32:19 +09:00
parent 44e9be5a1c
commit 6dcc3800e0
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
3 changed files with 78 additions and 32 deletions

View file

@ -2,8 +2,11 @@ import $ from 'cafy'; import ID from '../../../../../misc/cafy-id';
import DriveFolder, { isValidFolderName, pack } from '../../../../../models/drive-folder'; import DriveFolder, { isValidFolderName, pack } from '../../../../../models/drive-folder';
import { publishDriveStream } from '../../../../../stream'; import { publishDriveStream } from '../../../../../stream';
import { ILocalUser } from '../../../../../models/user'; import { ILocalUser } from '../../../../../models/user';
import getParams from '../../../get-params';
export const meta = { export const meta = {
stability: 'stable',
desc: { desc: {
'ja-JP': 'ドライブのフォルダを作成します。', 'ja-JP': 'ドライブのフォルダを作成します。',
'en-US': 'Create a folder of drive.' 'en-US': 'Create a folder of drive.'
@ -11,25 +14,37 @@ export const meta = {
requireCredential: true, 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) => { export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
// Get 'name' parameter const [ps, psErr] = getParams(meta, params);
const [name = '無題のフォルダー', nameErr] = $.str.optional.pipe(isValidFolderName).get(params.name); if (psErr) return rej(psErr);
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');
// If the parent folder is specified // If the parent folder is specified
let parent = null; let parent = null;
if (parentId) { if (ps.parentId) {
// Fetch parent folder // Fetch parent folder
parent = await DriveFolder parent = await DriveFolder
.findOne({ .findOne({
_id: parentId, _id: ps.parentId,
userId: user._id userId: user._id
}); });
@ -41,7 +56,7 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
// Create folder // Create folder
const folder = await DriveFolder.insert({ const folder = await DriveFolder.insert({
createdAt: new Date(), createdAt: new Date(),
name: name, name: ps.name,
parentId: parent !== null ? parent._id : null, parentId: parent !== null ? parent._id : null,
userId: user._id userId: user._id
}); });

View file

@ -1,26 +1,38 @@
import $ from 'cafy'; import ID from '../../../../../misc/cafy-id'; import $ from 'cafy'; import ID from '../../../../../misc/cafy-id';
import DriveFolder, { pack } from '../../../../../models/drive-folder'; import DriveFolder, { pack } from '../../../../../models/drive-folder';
import { ILocalUser } from '../../../../../models/user'; import { ILocalUser } from '../../../../../models/user';
import getParams from '../../../get-params';
export const meta = { export const meta = {
stability: 'stable',
desc: { desc: {
'ja-JP': '指定したドライブのフォルダの情報を取得します。' 'ja-JP': '指定したドライブのフォルダの情報を取得します。',
'en-US': 'Get specified folder of drive.'
}, },
requireCredential: true, 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) => { export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
// Get 'folderId' parameter const [ps, psErr] = getParams(meta, params);
const [folderId, folderIdErr] = $.type(ID).get(params.folderId); if (psErr) return rej(psErr);
if (folderIdErr) return rej('invalid folderId param');
// Get folder // Get folder
const folder = await DriveFolder const folder = await DriveFolder
.findOne({ .findOne({
_id: folderId, _id: ps.folderId,
userId: user._id userId: user._id
}); });

View file

@ -2,8 +2,11 @@ import $ from 'cafy'; import ID from '../../../../../misc/cafy-id';
import DriveFolder, { isValidFolderName, pack } from '../../../../../models/drive-folder'; import DriveFolder, { isValidFolderName, pack } from '../../../../../models/drive-folder';
import { publishDriveStream } from '../../../../../stream'; import { publishDriveStream } from '../../../../../stream';
import { ILocalUser } from '../../../../../models/user'; import { ILocalUser } from '../../../../../models/user';
import getParams from '../../../get-params';
export const meta = { export const meta = {
stability: 'stable',
desc: { desc: {
'ja-JP': '指定したドライブのフォルダの情報を更新します。', 'ja-JP': '指定したドライブのフォルダの情報を更新します。',
'en-US': 'Update specified folder of drive.' 'en-US': 'Update specified folder of drive.'
@ -11,18 +14,40 @@ export const meta = {
requireCredential: true, 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) => { export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
// Get 'folderId' parameter const [ps, psErr] = getParams(meta, params);
const [folderId, folderIdErr] = $.type(ID).get(params.folderId); if (psErr) return rej(psErr);
if (folderIdErr) return rej('invalid folderId param');
// Fetch folder // Fetch folder
const folder = await DriveFolder const folder = await DriveFolder
.findOne({ .findOne({
_id: folderId, _id: ps.folderId,
userId: user._id userId: user._id
}); });
@ -30,22 +55,16 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
return rej('folder-not-found'); return rej('folder-not-found');
} }
// Get 'name' parameter if (ps.name) folder.name = ps.name;
const [name, nameErr] = $.str.optional.pipe(isValidFolderName).get(params.name);
if (nameErr) return rej('invalid name param');
if (name) folder.name = name;
// Get 'parentId' parameter if (ps.parentId !== undefined) {
const [parentId, parentIdErr] = $.type(ID).optional.nullable.get(params.parentId); if (ps.parentId === null) {
if (parentIdErr) return rej('invalid parentId param');
if (parentId !== undefined) {
if (parentId === null) {
folder.parentId = null; folder.parentId = null;
} else { } else {
// Get parent folder // Get parent folder
const parent = await DriveFolder const parent = await DriveFolder
.findOne({ .findOne({
_id: parentId, _id: ps.parentId,
userId: user._id userId: user._id
}); });