From b09b287735003c8d3fa29d9b0da9d6872f26860c Mon Sep 17 00:00:00 2001 From: didinele Date: Wed, 3 Mar 2021 12:09:26 +0200 Subject: [PATCH] fix(rest/router): generic types for making requests --- libs/rest/src/Bucket.ts | 10 +++++----- libs/rest/src/Fetch.ts | 4 ++-- libs/rest/src/RestManager.ts | 26 +++++++++++++------------- libs/routers/src/IRouter.ts | 10 +++++----- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/libs/rest/src/Bucket.ts b/libs/rest/src/Bucket.ts index bae63e2..1ef535c 100644 --- a/libs/rest/src/Bucket.ts +++ b/libs/rest/src/Bucket.ts @@ -1,4 +1,4 @@ -import { discordFetch, DiscordFetchOptions, StringRecord, RequestBodyData } from './Fetch'; +import { discordFetch, DiscordFetchOptions } from './Fetch'; import { CordisRestError, HTTPError } from './Error'; import { halt } from '@cordis/common'; import type { RestManager } from './RestManager'; @@ -54,7 +54,7 @@ export class Bucket { * Makes a request to Discord * @param req Request options */ - public async make(req: DiscordFetchOptions): Promise { + public async make(req: DiscordFetchOptions): Promise { this.manager.emit('request', req); const timeout = setTimeout(() => req.controller.abort(), this.manager.abortAfter); @@ -83,10 +83,10 @@ export class Bucket { this.manager.emit('ratelimit', this.route, req.path, false, retryAfter); await this.mutex.set(this.route, { timeout: retryAfter }); - return this._retry(req); + return this._retry(req); } else if (res.status >= 500 && res.status < 600) { await halt(1000); - return this._retry(req); + return this._retry(req); } else if (!res.ok) { throw new HTTPError(res.clone(), await res.text()); } @@ -105,7 +105,7 @@ export class Bucket { * @param req Request options * @param res Response given */ - private _retry(req: DiscordFetchOptions): Promise { + private _retry(req: DiscordFetchOptions): Promise { if (req.failures) req.failures++; else req.failures = 1; diff --git a/libs/rest/src/Fetch.ts b/libs/rest/src/Fetch.ts index 0489613..f484130 100644 --- a/libs/rest/src/Fetch.ts +++ b/libs/rest/src/Fetch.ts @@ -27,7 +27,7 @@ export interface RequestBodyData { /** * Presents the base options that may be needed for making a request to Discord */ -export interface DiscordFetchOptions { +export interface DiscordFetchOptions { path: string; method: string; headers: Headers; @@ -42,7 +42,7 @@ export interface DiscordFetchOptions(options: DiscordFetchOptions) => { +export const discordFetch = (options: DiscordFetchOptions) => { let { path, method, headers, controller, query, files, data } = options; let queryString: string | null = null; diff --git a/libs/rest/src/RestManager.ts b/libs/rest/src/RestManager.ts index d1d3dca..f0411e0 100644 --- a/libs/rest/src/RestManager.ts +++ b/libs/rest/src/RestManager.ts @@ -30,14 +30,14 @@ export interface RestManager { * Fired when a request is being started (pre-ratelimit checking) * @event */ - on(event: 'request', listener: (request: Partial) => any): this; + on(event: 'request', listener: (request: Partial>) => any): this; /** * Fired when Discord responds to a request * @event */ on( event: 'response', - listener: (request: Partial, response: any, ratelimit: Partial) => any + listener: (request: Partial>, response: any, ratelimit: Partial) => any ): this; /** * Fired when a rate limit is (about to be) hit. @@ -46,19 +46,19 @@ export interface RestManager { on(event: 'ratelimit', listener: (bucket: string, endpoint: string, prevented: boolean, waitingFor: number) => any): this; /** @internal */ - once(event: 'request', listener: (request: Partial) => any): this; + once(event: 'request', listener: (request: Partial>) => any): this; /** @internal */ once( event: 'response', - listener: (request: Partial, response: any, ratelimit: Partial) => any + listener: (request: Partial>, response: any, ratelimit: Partial) => any ): this; /** @internal */ once(event: 'ratelimit', listener: (bucket: string, endpoint: string, prevented: boolean, waitingFor: number) => any): this; /** @internal */ - emit(event: 'request', request: Partial): boolean; + emit(event: 'request', request: Partial>): boolean; /** @internal */ - emit(event: 'response', request: Partial, response: any, ratelimit: Partial): boolean; + emit(event: 'response', request: Partial>, response: any, ratelimit: Partial): boolean; /** @internal */ emit(event: 'ratelimit', bucket: string, endpoint: string, prevented: boolean, waitingFor: number): boolean; } @@ -66,7 +66,7 @@ export interface RestManager { /** * Options used for making a request */ -export interface RequestOptions { +export interface RequestOptions { /** * Path you're requesting */ @@ -139,7 +139,7 @@ export class RestManager extends EventEmitter { * Prepares a request to Discord, associating it to the correct Bucket and attempting to prevent rate limits * @param options Options needed for making a request; only the path is required */ - public make(options: RequestOptions): Promise { + public make(options: RequestOptions): Promise { const route = Bucket.makeRoute(options.method, options.path); let bucket = this._buckets.get(route); @@ -165,7 +165,7 @@ export class RestManager extends EventEmitter { * @param options Other options for the request */ /* istanbul ignore next */ - public get(path: string, options: { query?: Q } = {}): Promise { + public get(path: string, options: { query?: Q } = {}): Promise { return this.make({ path, method: 'get', ...options }); } @@ -175,7 +175,7 @@ export class RestManager extends EventEmitter { * @param options Other options for the request */ /* istanbul ignore next */ - public delete(path: string, options: { data?: D; reason?: string } = {}): Promise { + public delete(path: string, options: { data?: D; reason?: string } = {}): Promise { return this.make({ path, method: 'delete', ...options }); } @@ -185,7 +185,7 @@ export class RestManager extends EventEmitter { * @param options Other options for the request */ /* istanbul ignore next */ - public patch(path: string, options: { data: D; reason?: string }): Promise { + public patch(path: string, options: { data: D; reason?: string }): Promise { return this.make({ path, method: 'patch', ...options }); } @@ -195,7 +195,7 @@ export class RestManager extends EventEmitter { * @param options Other options for the request */ /* istanbul ignore next */ - public put(path: string, options: { data: D; reason?: string }): Promise { + public put(path: string, options: { data: D; reason?: string }): Promise { return this.make({ path, method: 'put', ...options }); } @@ -205,7 +205,7 @@ export class RestManager extends EventEmitter { * @param options Other options for the request */ /* istanbul ignore next */ - public post(path: string, options: { data: D; reason?: string; files: File[] }): Promise { + public post(path: string, options: { data: D; reason?: string; files: File[] }): Promise { return this.make({ path, method: 'post', ...options }); } } diff --git a/libs/routers/src/IRouter.ts b/libs/routers/src/IRouter.ts index e498501..61213aa 100644 --- a/libs/routers/src/IRouter.ts +++ b/libs/routers/src/IRouter.ts @@ -1,9 +1,9 @@ import type { File, StringRecord, RequestBodyData } from '@cordis/rest'; export type IRouter = { - get(options?: { query?: Q }): Promise; - delete(options?: { data?: D; reason?: string }): Promise; - patch(options: { data: D; reason?: string }): Promise; - put(options: { data: D; reason?: string }): Promise; - post(options: { data: D; reason?: string; files?: File[] }): Promise; + get(options?: { query?: Q }): Promise; + delete(options?: { data?: D; reason?: string }): Promise; + patch(options: { data: D; reason?: string }): Promise; + put(options: { data: D; reason?: string }): Promise; + post(options: { data: D; reason?: string; files?: File[] }): Promise; } & { [key: string]: IRouter };