2018-11-02 06:47:44 +02:00
|
|
|
import * as fs from 'fs';
|
2021-08-19 15:55:45 +03:00
|
|
|
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';
|
2018-11-02 06:47:44 +02:00
|
|
|
|
2021-05-28 03:34:42 +03:00
|
|
|
type NonOptional<T> = T extends undefined ? never : T;
|
|
|
|
|
2021-03-24 04:05:37 +02:00
|
|
|
type SimpleUserInfo = {
|
|
|
|
id: ILocalUser['id'];
|
2022-02-05 23:24:06 +02:00
|
|
|
createdAt: ILocalUser['createdAt'];
|
2021-03-24 04:05:37 +02:00
|
|
|
host: ILocalUser['host'];
|
|
|
|
username: ILocalUser['username'];
|
|
|
|
uri: ILocalUser['uri'];
|
|
|
|
inbox: ILocalUser['inbox'];
|
|
|
|
sharedInbox: ILocalUser['sharedInbox'];
|
|
|
|
isAdmin: ILocalUser['isAdmin'];
|
|
|
|
isModerator: ILocalUser['isModerator'];
|
|
|
|
isSilenced: ILocalUser['isSilenced'];
|
|
|
|
};
|
|
|
|
|
2018-11-02 06:47:44 +02:00
|
|
|
type Params<T extends IEndpointMeta> = {
|
2021-11-12 03:52:10 +02:00
|
|
|
[P in keyof T['params']]: NonNullable<T['params']>[P]['transform'] extends () => any
|
2019-04-12 19:43:22 +03:00
|
|
|
? ReturnType<NonNullable<T['params']>[P]['transform']>
|
2021-05-28 03:34:42 +03:00
|
|
|
: NonNullable<T['params']>[P]['default'] extends null | number | string
|
|
|
|
? NonOptional<ReturnType<NonNullable<T['params']>[P]['validator']['get']>[0]>
|
|
|
|
: ReturnType<NonNullable<T['params']>[P]['validator']['get']>[0];
|
2018-11-02 06:47:44 +02:00
|
|
|
};
|
|
|
|
|
2019-02-22 04:46:58 +02:00
|
|
|
export type Response = Record<string, any> | void;
|
|
|
|
|
2019-04-23 16:35:26 +03:00
|
|
|
type executor<T extends IEndpointMeta> =
|
2021-11-12 03:52:10 +02:00
|
|
|
(params: Params<T>, user: T['requireCredential'] extends true ? SimpleUserInfo : SimpleUserInfo | null, token: AccessToken | null, file?: any, cleanup?: () => any) =>
|
2019-04-28 13:56:41 +03:00
|
|
|
Promise<T['res'] extends undefined ? Response : SchemaType<NonNullable<T['res']>>>;
|
2019-04-23 16:35:26 +03:00
|
|
|
|
|
|
|
export default function <T extends IEndpointMeta>(meta: T, cb: executor<T>)
|
2021-03-24 04:05:37 +02:00
|
|
|
: (params: any, user: T['requireCredential'] extends true ? SimpleUserInfo : SimpleUserInfo | null, token: AccessToken | null, file?: any) => Promise<any> {
|
|
|
|
return (params: any, user: T['requireCredential'] extends true ? SimpleUserInfo : SimpleUserInfo | null, token: AccessToken | null, file?: any) => {
|
2018-11-02 06:47:44 +02:00
|
|
|
function cleanup() {
|
|
|
|
fs.unlink(file.path, () => {});
|
|
|
|
}
|
|
|
|
|
2019-02-22 04:46:58 +02: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 06:47:44 +02:00
|
|
|
|
|
|
|
const [ps, pserr] = getParams(meta, params);
|
|
|
|
if (pserr) {
|
|
|
|
if (file) cleanup();
|
|
|
|
return Promise.reject(pserr);
|
|
|
|
}
|
|
|
|
|
2020-03-28 11:07:41 +02:00
|
|
|
return cb(ps, user, token, file, cleanup);
|
2018-11-02 06:47:44 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:43:22 +03:00
|
|
|
function getParams<T extends IEndpointMeta>(defs: T, params: any): [Params<T>, ApiError | null] {
|
2018-11-04 14:11:54 +02:00
|
|
|
if (defs.params == null) return [params, null];
|
|
|
|
|
2018-11-02 06:47:44 +02:00
|
|
|
const x: any = {};
|
2019-04-12 19:43:22 +03:00
|
|
|
let err: ApiError | null = null;
|
2018-11-02 06:47:44 +02:00
|
|
|
Object.entries(defs.params).some(([k, def]) => {
|
|
|
|
const [v, e] = def.validator.get(params[k]);
|
|
|
|
if (e) {
|
2019-02-22 04:46:58 +02:00
|
|
|
err = new ApiError({
|
|
|
|
message: 'Invalid param.',
|
|
|
|
code: 'INVALID_PARAM',
|
|
|
|
id: '3d81ceae-475f-4600-b2a8-2bc116157532',
|
|
|
|
}, {
|
|
|
|
param: k,
|
2021-12-09 16:58:30 +02:00
|
|
|
reason: e.message,
|
2019-02-22 04:46:58 +02:00
|
|
|
});
|
2018-11-02 06:47:44 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
2021-11-12 03:52:10 +02:00
|
|
|
if (v === undefined && Object.prototype.hasOwnProperty.call(def, 'default')) {
|
2018-11-02 06:47:44 +02:00
|
|
|
x[k] = def.default;
|
|
|
|
} else {
|
|
|
|
x[k] = v;
|
|
|
|
}
|
|
|
|
if (def.transform) x[k] = def.transform(x[k]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return [x, err];
|
|
|
|
}
|