monkeeShark/src/server/api/endpoints/drive/folders/create.ts
syuilo 52774bbe64
Introduce OpenAPI specs (#4351)
* wip

* wip

* wip

* Update index.ts

* Update gen-openapi-spec.ts

* Update api.ja-JP.md

* Fix

* Improve doc

* Update gen-openapi-spec.ts

* Update redoc.html

* Improve doc

* Update gen-openapi-spec.ts

* Improve doc

* Update CHANGELOG.md
2019-02-23 11:20:58 +09:00

81 lines
1.7 KiB
TypeScript

import $ from 'cafy';
import ID, { transform } from '../../../../../misc/cafy-id';
import DriveFolder, { isValidFolderName, pack } from '../../../../../models/drive-folder';
import { publishDriveStream } from '../../../../../services/stream';
import define from '../../../define';
import { ApiError } from '../../../error';
export const meta = {
stability: 'stable',
desc: {
'ja-JP': 'ドライブのフォルダを作成します。',
'en-US': 'Create a folder of drive.'
},
tags: ['drive'],
requireCredential: true,
kind: 'drive-write',
params: {
name: {
validator: $.optional.str.pipe(isValidFolderName),
default: 'Untitled',
desc: {
'ja-JP': 'フォルダ名',
'en-US': 'Folder name'
}
},
parentId: {
validator: $.optional.nullable.type(ID),
transform: transform,
desc: {
'ja-JP': '親フォルダID',
'en-US': 'Parent folder ID'
}
}
},
errors: {
noSuchFolder: {
message: 'No such folder.',
code: 'NO_SUCH_FOLDER',
id: '53326628-a00d-40a6-a3cd-8975105c0f95'
},
}
};
export default define(meta, async (ps, user) => {
// If the parent folder is specified
let parent = null;
if (ps.parentId) {
// Fetch parent folder
parent = await DriveFolder
.findOne({
_id: ps.parentId,
userId: user._id
});
if (parent === null) {
throw new ApiError(meta.errors.noSuchFolder);
}
}
// Create folder
const folder = await DriveFolder.insert({
createdAt: new Date(),
name: ps.name,
parentId: parent !== null ? parent._id : null,
userId: user._id
});
const folderObj = await pack(folder);
// Publish folderCreated event
publishDriveStream(user._id, 'folderCreated', folderObj);
return folderObj;
});