refactor(backend): encapsulate any affection in ApiCallService.ts

This commit is contained in:
Kisaragi Marine 2024-03-14 19:50:07 +09:00
parent 4711e1a33f
commit c167caf3e7
No known key found for this signature in database
GPG key ID: C6631564CD2110E4

View file

@ -89,11 +89,18 @@ export class ApiCallService implements OnApplicationShutdown {
}
@bindThis
public handleRequest(
endpoint: IEndpoint & { exec: any },
public async handleRequest<Ret extends string | number | Record<string, unknown> | null | undefined>(
endpoint: IEndpoint & { exec: (
data: Record<string, unknown> | undefined,
user: MiLocalUser | null | undefined,
token: MiAccessToken | null | undefined,
file: { name: string, path: string } | null,
ip: string,
headers: any,
) => Promise<Ret> },
request: FastifyRequest<{ Body: Record<string, unknown> | undefined, Querystring: Record<string, unknown> }>,
reply: FastifyReply,
): void {
): Promise<void> {
const body = request.method === 'GET'
? request.query
: request.body;
@ -126,7 +133,14 @@ export class ApiCallService implements OnApplicationShutdown {
@bindThis
public async handleMultipartRequest(
endpoint: IEndpoint & { exec: any },
endpoint: IEndpoint & { exec: (
data: unknown,
user: MiLocalUser | null | undefined,
token: MiAccessToken | null | undefined,
file: { name: string, path: string } | null,
ip: string,
headers: any,
) => Promise<string | number | Record<string, unknown> | null | undefined> },
request: FastifyRequest<{ Body: Record<string, unknown>, Querystring: Record<string, unknown> }>,
reply: FastifyReply,
): Promise<void> {
@ -173,8 +187,18 @@ export class ApiCallService implements OnApplicationShutdown {
});
}
/**
*
* @param reply Fastifyのレスポンス
* @param x
* `null``undefined`204
* `number``y`
* `string`bodyとしJSON化してbodyとする
* @param y
* @private
*/
@bindThis
private send(reply: FastifyReply, x?: any, y?: ApiError) {
private send(reply: FastifyReply, x?: number | string | Record<string, unknown> | null | undefined, y?: ApiError) {
if (x == null) {
reply.code(204);
reply.send();
@ -220,15 +244,15 @@ export class ApiCallService implements OnApplicationShutdown {
}
@bindThis
private async call<Data>(
private async call<Data, Ret>(
ep: IEndpoint & { exec: (
data: Data,
user: MiLocalUser,
user: MiLocalUser | null | undefined,
token: MiAccessToken | null | undefined,
file: { name: string, path: string } | null,
ip: any,
headers: any
) => Promise<any> },
ip: string,
headers: any,
) => Promise<Ret> },
user: MiLocalUser | null | undefined,
token: MiAccessToken | null | undefined,
data: Data,
@ -351,8 +375,10 @@ export class ApiCallService implements OnApplicationShutdown {
if ((ep.meta.requireFile || request.method === 'GET') && ep.params.properties) {
for (const k of Object.keys(ep.params.properties)) {
const param = ep.params.properties![k];
// @ts-expect-error TS7053: Element implicitly has an any type because expression of type string can't be used to index type unknown
if (['boolean', 'number', 'integer'].includes(param.type ?? '') && typeof data[k] === 'string') {
try {
// @ts-expect-error TS7053: Element implicitly has an any type because expression of type string can't be used to index type unknown
data[k] = JSON.parse(data[k]);
} catch (e) {
throw new ApiError({