2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import * as fs from 'node:fs';
|
2023-06-25 21:13:15 +09:00
|
|
|
import _Ajv from 'ajv';
|
2023-03-10 14:22:37 +09:00
|
|
|
import type { Schema, SchemaType } from '@/misc/json-schema.js';
|
2023-08-16 17:51:28 +09:00
|
|
|
import type { MiLocalUser } from '@/models/entities/User.js';
|
|
|
|
import type { MiAccessToken } from '@/models/entities/AccessToken.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { ApiError } from './error.js';
|
|
|
|
import type { IEndpointMeta } from './endpoints.js';
|
|
|
|
|
2023-06-25 21:13:15 +09:00
|
|
|
const Ajv = _Ajv.default;
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
const ajv = new Ajv({
|
|
|
|
useDefaults: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
ajv.addFormat('misskey:id', /^[a-zA-Z0-9]+$/);
|
|
|
|
|
|
|
|
export type Response = Record<string, any> | void;
|
|
|
|
|
2022-12-03 19:42:05 +09:00
|
|
|
type File = {
|
|
|
|
name: string | null;
|
|
|
|
path: string;
|
|
|
|
};
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
// TODO: paramsの型をT['params']のスキーマ定義から推論する
|
2023-02-22 14:51:34 +09:00
|
|
|
type Executor<T extends IEndpointMeta, Ps extends Schema> =
|
2023-08-16 17:51:28 +09:00
|
|
|
(params: SchemaType<Ps>, user: T['requireCredential'] extends true ? MiLocalUser : MiLocalUser | null, token: MiAccessToken | null, file?: File, cleanup?: () => any, ip?: string | null, headers?: Record<string, string> | null) =>
|
2022-09-18 03:27:08 +09:00
|
|
|
Promise<T['res'] extends undefined ? Response : SchemaType<NonNullable<T['res']>>>;
|
|
|
|
|
|
|
|
export abstract class Endpoint<T extends IEndpointMeta, Ps extends Schema> {
|
2023-08-16 17:51:28 +09:00
|
|
|
public exec: (params: any, user: T['requireCredential'] extends true ? MiLocalUser : MiLocalUser | null, token: MiAccessToken | null, file?: File, ip?: string | null, headers?: Record<string, string> | null) => Promise<any>;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
2023-02-22 14:51:34 +09:00
|
|
|
constructor(meta: T, paramDef: Ps, cb: Executor<T, Ps>) {
|
2022-09-18 03:27:08 +09:00
|
|
|
const validate = ajv.compile(paramDef);
|
|
|
|
|
2023-08-16 17:51:28 +09:00
|
|
|
this.exec = (params: any, user: T['requireCredential'] extends true ? MiLocalUser : MiLocalUser | null, token: MiAccessToken | null, file?: File, ip?: string | null, headers?: Record<string, string> | null) => {
|
2022-09-18 03:27:08 +09:00
|
|
|
let cleanup: undefined | (() => void) = undefined;
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (meta.requireFile) {
|
|
|
|
cleanup = () => {
|
2022-12-03 19:42:05 +09:00
|
|
|
if (file) fs.unlink(file.path, () => {});
|
2022-09-18 03:27:08 +09:00
|
|
|
};
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (file == null) return Promise.reject(new ApiError({
|
|
|
|
message: 'File required.',
|
|
|
|
code: 'FILE_REQUIRED',
|
|
|
|
id: '4267801e-70d1-416a-b011-4ee502885d8b',
|
|
|
|
}));
|
|
|
|
}
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
const valid = validate(params);
|
|
|
|
if (!valid) {
|
|
|
|
if (file) cleanup!();
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
const errors = validate.errors!;
|
|
|
|
const err = new ApiError({
|
|
|
|
message: 'Invalid param.',
|
|
|
|
code: 'INVALID_PARAM',
|
|
|
|
id: '3d81ceae-475f-4600-b2a8-2bc116157532',
|
|
|
|
}, {
|
|
|
|
param: errors[0].schemaPath,
|
|
|
|
reason: errors[0].message,
|
|
|
|
});
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
return cb(params as SchemaType<Ps>, user, token, file, cleanup, ip, headers);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|