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