diff --git a/packages/backend/src/server/api/ApiServerService.ts b/packages/backend/src/server/api/ApiServerService.ts index 60b5c396f..2a4bd054d 100644 --- a/packages/backend/src/server/api/ApiServerService.ts +++ b/packages/backend/src/server/api/ApiServerService.ts @@ -7,6 +7,7 @@ import { Inject, Injectable } from '@nestjs/common'; import cors from '@fastify/cors'; import multipart from '@fastify/multipart'; import fastifyCookie from '@fastify/cookie'; +import fastifyFormbody from '@fastify/formbody'; import { ModuleRef } from '@nestjs/core'; import type { Config } from '@/config.js'; import type { InstancesRepository, AccessTokensRepository } from '@/models/_.js'; @@ -63,6 +64,13 @@ export class ApiServerService { done(); }); + fastify.register(this.createMisskeyServer); + fastify.register(this.createMastodonServer); + done(); + } + + @bindThis + public createMisskeyServer(fastify: FastifyInstance, options: FastifyPluginOptions, done: (err?: Error) => void) { for (const endpoint of endpoints) { const ep = { name: endpoint.name, @@ -106,32 +114,6 @@ export class ApiServerService { } } - const createEndpoint = (endpoint: IMastodonEndpoint): IMastodonEndpoint & { exec: any } => ({ - name: endpoint.name, - method: endpoint.method, - meta: endpoint.meta, - params: endpoint.params, - exec: this.moduleRef.get(`mep:${endpoint.method}:${endpoint.name}`, { strict: false }).exec, - }); - const groupedMastodonEndpoints = Array.from(Map.groupBy(mastodonEndpoints.map(createEndpoint), endpoint => endpoint.name)) - .map(([name, endpoints]) => ({ name, endpoints: new Map(endpoints.map(endpoint => [endpoint.method, endpoint])) })); - for (const { name, endpoints } of groupedMastodonEndpoints) { - fastify.all<{ - Params: { endpoint: string; }, - Body: Record, - Querystring: Record, - }>('/' + name, async (request, reply) => { - const ep = endpoints.get(request.method); - if (!ep) { - reply.code(405); - reply.send(); - return; - } - await this.apiCallService.handleMastodonRequest(ep, request, reply); - return reply; - }); - } - fastify.post<{ Body: { username: string; @@ -212,4 +194,39 @@ export class ApiServerService { done(); } + + @bindThis + public createMastodonServer(fastify: FastifyInstance, options: FastifyPluginOptions, done: (err?: Error) => void) { + fastify.register(fastifyFormbody); + + const createEndpoint = (endpoint: IMastodonEndpoint): IMastodonEndpoint & { exec: any } => ({ + name: endpoint.name, + method: endpoint.method, + meta: endpoint.meta, + params: endpoint.params, + exec: this.moduleRef.get(`mep:${endpoint.method}:${endpoint.name}`, { strict: false }).exec, + }); + + const groupedMastodonEndpoints = Array.from(Map.groupBy(mastodonEndpoints.map(createEndpoint), endpoint => endpoint.name)) + .map(([name, endpoints]) => ({ name, endpoints: new Map(endpoints.map(endpoint => [endpoint.method, endpoint])) })); + + for (const { name, endpoints } of groupedMastodonEndpoints) { + fastify.all<{ + Params: { endpoint: string; }, + Body: Record, + Querystring: Record, + }>('/' + name, async (request, reply) => { + const ep = endpoints.get(request.method); + if (!ep) { + reply.code(405); + reply.send(); + return; + } + await this.apiCallService.handleMastodonRequest(ep, request, reply); + return reply; + }); + } + + done(); + } }