Skip to content

Commit

Permalink
refactor: cover fastify deprecated syntax (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Jun 16, 2022
1 parent 1928dd0 commit 3167d6a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
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));

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>>;

0 comments on commit 3167d6a

Please sign in to comment.