refactor: fix types

This commit is contained in:
syuilo 2023-02-09 10:46:01 +09:00
parent 7afee5977f
commit 21331e53fe
6 changed files with 17 additions and 13 deletions

View File

@ -1,3 +1,4 @@
import { IncomingMessage } from 'node:http';
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import fastifyAccepts from '@fastify/accepts'; import fastifyAccepts from '@fastify/accepts';
import httpSignature from '@peertube/http-signature'; import httpSignature from '@peertube/http-signature';
@ -19,6 +20,7 @@ import { QueryService } from '@/core/QueryService.js';
import { UtilityService } from '@/core/UtilityService.js'; import { UtilityService } from '@/core/UtilityService.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import { IActivity } from '@/core/activitypub/type.js';
import type { FastifyInstance, FastifyRequest, FastifyReply, FastifyPluginOptions } from 'fastify'; import type { FastifyInstance, FastifyRequest, FastifyReply, FastifyPluginOptions } from 'fastify';
import type { FindOptionsWhere } from 'typeorm'; import type { FindOptionsWhere } from 'typeorm';
@ -97,7 +99,8 @@ export class ActivityPubServerService {
return; return;
} }
this.queueService.inbox(request.body, signature); // TODO: request.bodyのバリデーション
this.queueService.inbox(request.body as IActivity, signature);
reply.code(202); reply.code(202);
} }
@ -413,20 +416,21 @@ export class ActivityPubServerService {
@bindThis @bindThis
public createServer(fastify: FastifyInstance, options: FastifyPluginOptions, done: (err?: Error) => void) { public createServer(fastify: FastifyInstance, options: FastifyPluginOptions, done: (err?: Error) => void) {
fastify.addConstraintStrategy({ // addConstraintStrategy の型定義がおかしいため
(fastify.addConstraintStrategy as any)({
name: 'apOrHtml', name: 'apOrHtml',
storage() { storage() {
const store = {}; const store = {} as any;
return { return {
get(key) { get(key: string) {
return store[key] ?? null; return store[key] ?? null;
}, },
set(key, value) { set(key: string, value: any) {
store[key] = value; store[key] = value;
}, },
}; };
}, },
deriveConstraint(request, ctx) { deriveConstraint(request: IncomingMessage) {
const accepted = accepts(request).type(['html', ACTIVITY_JSON, LD_JSON]); const accepted = accepts(request).type(['html', ACTIVITY_JSON, LD_JSON]);
const isAp = typeof accepted === 'string' && !accepted.match(/html/); const isAp = typeof accepted === 'string' && !accepted.match(/html/);
return isAp ? 'ap' : 'html'; return isAp ? 'ap' : 'html';

View File

@ -10,9 +10,9 @@ import { getIpHash } from '@/misc/get-ip-hash.js';
import type { ILocalUser } from '@/models/entities/User.js'; import type { ILocalUser } from '@/models/entities/User.js';
import { IdService } from '@/core/IdService.js'; import { IdService } from '@/core/IdService.js';
import { TwoFactorAuthenticationService } from '@/core/TwoFactorAuthenticationService.js'; import { TwoFactorAuthenticationService } from '@/core/TwoFactorAuthenticationService.js';
import { bindThis } from '@/decorators.js';
import { RateLimiterService } from './RateLimiterService.js'; import { RateLimiterService } from './RateLimiterService.js';
import { SigninService } from './SigninService.js'; import { SigninService } from './SigninService.js';
import { bindThis } from '@/decorators.js';
import type { FastifyRequest, FastifyReply } from 'fastify'; import type { FastifyRequest, FastifyReply } from 'fastify';
@Injectable() @Injectable()
@ -131,7 +131,7 @@ export class SigninApiService {
createdAt: new Date(), createdAt: new Date(),
userId: user.id, userId: user.id,
ip: request.ip, ip: request.ip,
headers: request.headers, headers: request.headers as any,
success: false, success: false,
}); });

View File

@ -33,7 +33,7 @@ export class SigninService {
createdAt: new Date(), createdAt: new Date(),
userId: user.id, userId: user.id,
ip: request.ip, ip: request.ip,
headers: request.headers, headers: request.headers as any,
success: true, success: true,
}).then(x => this.signinsRepository.findOneByOrFail(x.identifiers[0])); }).then(x => this.signinsRepository.findOneByOrFail(x.identifiers[0]));

View File

@ -162,7 +162,7 @@ export class SignupApiService {
token: secret, token: secret,
}; };
} catch (err) { } catch (err) {
throw new FastifyReplyError(400, err); throw new FastifyReplyError(400, typeof err === 'string' ? err : (err as Error).toString());
} }
} }
} }
@ -195,7 +195,7 @@ export class SignupApiService {
return this.signinService.signin(request, reply, account as ILocalUser); return this.signinService.signin(request, reply, account as ILocalUser);
} catch (err) { } catch (err) {
throw new FastifyReplyError(400, err); throw new FastifyReplyError(400, typeof err === 'string' ? err : (err as Error).toString());
} }
} }
} }

View File

@ -27,7 +27,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
return { return {
params: Object.entries(ep.params.properties ?? {}).map(([k, v]) => ({ params: Object.entries(ep.params.properties ?? {}).map(([k, v]) => ({
name: k, name: k,
type: v.type.charAt(0).toUpperCase() + v.type.slice(1), type: v.type ? v.type.charAt(0).toUpperCase() + v.type.slice(1) : 'string',
})), })),
}; };
}); });

View File

@ -155,7 +155,7 @@ export class ClientServerService {
}); });
serverAdapter.setBasePath(bullBoardPath); serverAdapter.setBasePath(bullBoardPath);
fastify.register(serverAdapter.registerPlugin(), { prefix: bullBoardPath }); (fastify.register as any)(serverAdapter.registerPlugin(), { prefix: bullBoardPath });
//#endregion //#endregion
fastify.register(fastifyView, { fastify.register(fastifyView, {