Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: cover fastify deprecated syntax #105

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/http-framework/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export * from './lib/structures/Command';
export * from './lib/structures/CommandStore';
export * from './lib/structures/InteractionHandler';
export * from './lib/structures/InteractionHandlerStore';
export type { FastifyObjectOptions } from './lib/utils/FastifyObjectOptions';
13 changes: 7 additions & 6 deletions packages/http-framework/src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import tweetnacl from 'tweetnacl';
import { HttpCodes } from './api/HttpCodes';
import type { IIdParser } from './components/IIdParser';
import { StringIdParser } from './components/StringIdParser';
import type { FastifyObjectOptions } from './utils/FastifyObjectOptions';
import { CommandStore } from './structures/CommandStore';
import { InteractionHandlerStore } from './structures/InteractionHandlerStore';

Expand Down Expand Up @@ -50,11 +51,11 @@ export class Client extends EventEmitter {
* Starts the HTTP server, listening for HTTP interactions.
* @param options The listen options.
*/
public async listen(options: ListenOptions) {
this.server = Fastify(options.serverOptions);
this.server.post(options.postPath ?? process.env.HTTP_POST_PATH ?? '/', this.handleHttpMessage.bind(this));
public async listen({ serverOptions, postPath, port, address, ...listenOptions }: ListenOptions) {
this.server = Fastify(serverOptions);
this.server.post(postPath ?? process.env.HTTP_POST_PATH ?? '/', this.handleHttpMessage.bind(this));

Check failure

Code scanning / CodeQL

Missing rate limiting

This route handler performs [authorization](1), but is not rate-limited.

await this.server.listen(options.port, options.address);
await this.server.listen({ ...listenOptions, port, host: address });
}

protected async handleHttpMessage(request: FastifyRequest, reply: FastifyReply): Promise<FastifyReply> {
Expand Down Expand Up @@ -140,11 +141,11 @@ export interface LoadOptions {
baseUserDirectory?: string | null;
}

export interface ListenOptions {
export interface ListenOptions extends FastifyObjectOptions {
/**
* The port at which the server will listen for requests.
*/
port: number | string;
port: number;

/**
* The address at which the server will be started.
Expand Down
46 changes: 46 additions & 0 deletions packages/http-framework/src/lib/utils/FastifyObjectOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { FastifyInstance } from 'fastify';

export type FastifyObjectOptions = Omit<Exclude<OverloadedParameters<FastifyInstance['listen']>[0], FastifyUnnecessaryUnionTypes>, 'host'>;

/**
* The union types extracted by {@link Overloads}
* that we do not want to include in {@link FastifyObjectOptions}
*/
type FastifyUnnecessaryUnionTypes = string | number | ((...args: any[]) => any) | undefined;

/**
* Extracts the overloads from methods and transforms them into a union
* source: {@link https://github.com/microsoft/TypeScript/issues/32164#issuecomment-811608386}
*/
type Overloads<T extends (...args: any[]) => any> = T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
(...args: infer A6): infer R6;
}
? ((...args: A1) => R1) | ((...args: A2) => R2) | ((...args: A3) => R3) | ((...args: A4) => R4) | ((...args: A5) => R5) | ((...args: A6) => R6)
: T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
}
? ((...args: A1) => R1) | ((...args: A2) => R2) | ((...args: A3) => R3) | ((...args: A4) => R4) | ((...args: A5) => R5)
: T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; (...args: infer A3): infer R3; (...args: infer A4): infer R4 }
? ((...args: A1) => R1) | ((...args: A2) => R2) | ((...args: A3) => R3) | ((...args: A4) => R4)
: T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2; (...args: infer A3): infer R3 }
? ((...args: A1) => R1) | ((...args: A2) => R2) | ((...args: A3) => R3)
: T extends { (...args: infer A1): infer R1; (...args: infer A2): infer R2 }
? ((...args: A1) => R1) | ((...args: A2) => R2)
: T extends { (...args: infer A1): infer R1 }
? (...args: A1) => R1
: never;

/**
* Extracts the overloaded parameters from methods
* source: {@link https://github.com/microsoft/TypeScript/issues/32164#issuecomment-811608386}
*/
type OverloadedParameters<T extends (...args: any[]) => any> = Parameters<Overloads<T>>;