2022-02-27 03:07:39 +01:00
|
|
|
import * as fs from 'node:fs';
|
2022-02-20 05:15:40 +01:00
|
|
|
import Ajv from 'ajv';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { ILocalUser } from '@/models/entities/user.js';
|
|
|
|
import { IEndpointMeta } from './endpoints.js';
|
|
|
|
import { ApiError } from './error.js';
|
|
|
|
import { Schema, SchemaType } from '@/misc/schema.js';
|
|
|
|
import { AccessToken } from '@/models/entities/access-token.js';
|
2018-11-02 05:47:44 +01:00
|
|
|
|
2021-03-24 03:05:37 +01:00
|
|
|
type SimpleUserInfo = {
|
|
|
|
id: ILocalUser['id'];
|
2022-02-05 22:24:06 +01:00
|
|
|
createdAt: ILocalUser['createdAt'];
|
2021-03-24 03:05:37 +01: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'];
|
2022-02-15 15:08:50 +01:00
|
|
|
showTimelineReplies: ILocalUser['showTimelineReplies'];
|
2021-03-24 03:05:37 +01:00
|
|
|
};
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
export type Response = Record<string, any> | void;
|
|
|
|
|
2022-02-19 06:05:32 +01:00
|
|
|
// TODO: paramsの型をT['params']のスキーマ定義から推論する
|
|
|
|
type executor<T extends IEndpointMeta, Ps extends Schema> =
|
|
|
|
(params: SchemaType<Ps>, user: T['requireCredential'] extends true ? SimpleUserInfo : SimpleUserInfo | null, token: AccessToken | null, file?: any, cleanup?: () => any) =>
|
2019-04-28 12:56:41 +02:00
|
|
|
Promise<T['res'] extends undefined ? Response : SchemaType<NonNullable<T['res']>>>;
|
2019-04-23 15:35:26 +02:00
|
|
|
|
2022-02-19 06:05:32 +01:00
|
|
|
const ajv = new Ajv({
|
|
|
|
useDefaults: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
ajv.addFormat('misskey:id', /^[a-z0-9]+$/);
|
|
|
|
|
|
|
|
export default function <T extends IEndpointMeta, Ps extends Schema>(meta: T, paramDef: Ps, cb: executor<T, Ps>)
|
2021-03-24 03:05:37 +01:00
|
|
|
: (params: any, user: T['requireCredential'] extends true ? SimpleUserInfo : SimpleUserInfo | null, token: AccessToken | null, file?: any) => Promise<any> {
|
2022-02-19 06:05:32 +01:00
|
|
|
|
|
|
|
const validate = ajv.compile(paramDef);
|
|
|
|
|
2021-03-24 03:05:37 +01:00
|
|
|
return (params: any, user: T['requireCredential'] extends true ? SimpleUserInfo : SimpleUserInfo | null, token: AccessToken | null, file?: any) => {
|
2018-11-02 05:47:44 +01:00
|
|
|
function cleanup() {
|
|
|
|
fs.unlink(file.path, () => {});
|
|
|
|
}
|
|
|
|
|
2019-02-22 03:46:58 +01: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 05:47:44 +01:00
|
|
|
|
2022-02-19 06:05:32 +01:00
|
|
|
const valid = validate(params);
|
|
|
|
if (!valid) {
|
2018-11-02 05:47:44 +01:00
|
|
|
if (file) cleanup();
|
|
|
|
|
2022-02-19 06:05:32 +01:00
|
|
|
const errors = validate.errors!;
|
|
|
|
const err = new ApiError({
|
2019-02-22 03:46:58 +01:00
|
|
|
message: 'Invalid param.',
|
|
|
|
code: 'INVALID_PARAM',
|
|
|
|
id: '3d81ceae-475f-4600-b2a8-2bc116157532',
|
|
|
|
}, {
|
2022-02-19 06:05:32 +01:00
|
|
|
param: errors[0].schemaPath,
|
|
|
|
reason: errors[0].message,
|
2019-02-22 03:46:58 +01:00
|
|
|
});
|
2022-02-19 06:05:32 +01:00
|
|
|
return Promise.reject(err);
|
2018-11-02 05:47:44 +01:00
|
|
|
}
|
2022-02-19 06:05:32 +01:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
return cb(params as SchemaType<Ps>, user, token, file, cleanup);
|
2022-02-19 06:05:32 +01:00
|
|
|
};
|
2018-11-02 05:47:44 +01:00
|
|
|
}
|