import * as fs from 'fs'; import { ILocalUser } from '@/models/entities/user'; import { IEndpointMeta } from './endpoints'; import { ApiError } from './error'; import { SchemaType } from '@/misc/schema'; import { AccessToken } from '@/models/entities/access-token'; type NonOptional = T extends undefined ? never : T; type SimpleUserInfo = { id: ILocalUser['id']; host: ILocalUser['host']; username: ILocalUser['username']; uri: ILocalUser['uri']; inbox: ILocalUser['inbox']; sharedInbox: ILocalUser['sharedInbox']; isAdmin: ILocalUser['isAdmin']; isModerator: ILocalUser['isModerator']; isSilenced: ILocalUser['isSilenced']; }; type Params = { [P in keyof T['params']]: NonNullable[P]['transform'] extends () => any ? ReturnType[P]['transform']> : NonNullable[P]['default'] extends null | number | string ? NonOptional[P]['validator']['get']>[0]> : ReturnType[P]['validator']['get']>[0]; }; export type Response = Record | void; type executor = (params: Params, user: T['requireCredential'] extends true ? SimpleUserInfo : SimpleUserInfo | null, token: AccessToken | null, file?: any, cleanup?: () => any) => Promise>>; export default function (meta: T, cb: executor) : (params: any, user: T['requireCredential'] extends true ? SimpleUserInfo : SimpleUserInfo | null, token: AccessToken | null, file?: any) => Promise { return (params: any, user: T['requireCredential'] extends true ? SimpleUserInfo : SimpleUserInfo | null, token: AccessToken | null, file?: any) => { function cleanup() { fs.unlink(file.path, () => {}); } if (meta.requireFile && file == null) return Promise.reject(new ApiError({ message: 'File required.', code: 'FILE_REQUIRED', id: '4267801e-70d1-416a-b011-4ee502885d8b', })); const [ps, pserr] = getParams(meta, params); if (pserr) { if (file) cleanup(); return Promise.reject(pserr); } return cb(ps, user, token, file, cleanup); }; } function getParams(defs: T, params: any): [Params, ApiError | null] { if (defs.params == null) return [params, null]; const x: any = {}; let err: ApiError | null = null; Object.entries(defs.params).some(([k, def]) => { const [v, e] = def.validator.get(params[k]); if (e) { err = new ApiError({ message: 'Invalid param.', code: 'INVALID_PARAM', id: '3d81ceae-475f-4600-b2a8-2bc116157532', }, { param: k, reason: e.message }); return true; } else { if (v === undefined && Object.prototype.hasOwnProperty.call(def, 'default')) { x[k] = def.default; } else { x[k] = v; } if (def.transform) x[k] = def.transform(x[k]); return false; } }); return [x, err]; }