add support for form bodies for mastodon apis
this separates registration of mastodon endpoints to another register scope so that form body parsing wouldn't affect misskey apis
This commit is contained in:
parent
04c1c0e1e6
commit
9443b99399
1 changed files with 43 additions and 26 deletions
|
@ -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<string, unknown>,
|
||||
Querystring: Record<string, unknown>,
|
||||
}>('/' + 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<string, unknown>,
|
||||
Querystring: Record<string, unknown>,
|
||||
}>('/' + 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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue