2018-11-02 13:47:44 +09:00
|
|
|
import * as fs from 'fs';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { ILocalUser } from '../../models/entities/user';
|
2018-11-02 13:47:44 +09:00
|
|
|
import { IEndpointMeta } from './endpoints';
|
2019-02-22 11:46:58 +09:00
|
|
|
import { ApiError } from './error';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { App } from '../../models/entities/app';
|
2019-04-23 22:35:26 +09:00
|
|
|
import { SchemaType } from '../../misc/schema';
|
2018-11-02 13:47:44 +09:00
|
|
|
|
2020-02-18 19:05:11 +09:00
|
|
|
// TODO: defaultが設定されている場合はその型も考慮する
|
2018-11-02 13:47:44 +09:00
|
|
|
type Params<T extends IEndpointMeta> = {
|
2019-04-13 01:43:22 +09:00
|
|
|
[P in keyof T['params']]: NonNullable<T['params']>[P]['transform'] extends Function
|
|
|
|
? ReturnType<NonNullable<T['params']>[P]['transform']>
|
|
|
|
: ReturnType<NonNullable<T['params']>[P]['validator']['get']>[0];
|
2018-11-02 13:47:44 +09:00
|
|
|
};
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
export type Response = Record<string, any> | void;
|
|
|
|
|
2019-04-23 22:35:26 +09:00
|
|
|
type executor<T extends IEndpointMeta> =
|
2020-02-15 21:33:32 +09:00
|
|
|
(params: Params<T>, user: T['requireCredential'] extends true ? ILocalUser : ILocalUser | null, app: App, file?: any, cleanup?: Function) =>
|
2019-04-28 19:56:41 +09:00
|
|
|
Promise<T['res'] extends undefined ? Response : SchemaType<NonNullable<T['res']>>>;
|
2019-04-23 22:35:26 +09:00
|
|
|
|
|
|
|
export default function <T extends IEndpointMeta>(meta: T, cb: executor<T>)
|
2020-02-15 21:33:32 +09:00
|
|
|
: (params: any, user: T['requireCredential'] extends true ? ILocalUser : ILocalUser | null, app: App, file?: any) => Promise<any> {
|
|
|
|
return (params: any, user: T['requireCredential'] extends true ? ILocalUser : ILocalUser | null, app: App, file?: any) => {
|
2018-11-02 13:47:44 +09:00
|
|
|
function cleanup() {
|
|
|
|
fs.unlink(file.path, () => {});
|
|
|
|
}
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
if (meta.requireFile && file == null) return Promise.reject(new ApiError({
|
|
|
|
message: 'File required.',
|
|
|
|
code: 'FILE_REQUIRED',
|
|
|
|
id: '4267801e-70d1-416a-b011-4ee502885d8b',
|
|
|
|
}));
|
2018-11-02 13:47:44 +09:00
|
|
|
|
|
|
|
const [ps, pserr] = getParams(meta, params);
|
|
|
|
if (pserr) {
|
|
|
|
if (file) cleanup();
|
|
|
|
return Promise.reject(pserr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(ps, user, app, file, cleanup);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
function getParams<T extends IEndpointMeta>(defs: T, params: any): [Params<T>, ApiError | null] {
|
2018-11-04 21:11:54 +09:00
|
|
|
if (defs.params == null) return [params, null];
|
|
|
|
|
2018-11-02 13:47:44 +09:00
|
|
|
const x: any = {};
|
2019-04-13 01:43:22 +09:00
|
|
|
let err: ApiError | null = null;
|
2018-11-02 13:47:44 +09:00
|
|
|
Object.entries(defs.params).some(([k, def]) => {
|
|
|
|
const [v, e] = def.validator.get(params[k]);
|
|
|
|
if (e) {
|
2019-02-22 11:46:58 +09:00
|
|
|
err = new ApiError({
|
|
|
|
message: 'Invalid param.',
|
|
|
|
code: 'INVALID_PARAM',
|
|
|
|
id: '3d81ceae-475f-4600-b2a8-2bc116157532',
|
|
|
|
}, {
|
|
|
|
param: k,
|
|
|
|
reason: e.message
|
|
|
|
});
|
2018-11-02 13:47:44 +09:00
|
|
|
return true;
|
|
|
|
} else {
|
2018-11-04 23:13:35 +09:00
|
|
|
if (v === undefined && def.hasOwnProperty('default')) {
|
2018-11-02 13:47:44 +09:00
|
|
|
x[k] = def.default;
|
|
|
|
} else {
|
|
|
|
x[k] = v;
|
|
|
|
}
|
|
|
|
if (def.transform) x[k] = def.transform(x[k]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return [x, err];
|
|
|
|
}
|