From 8372a1cf1b1f327174b0506b268bdab5cd7ebc37 Mon Sep 17 00:00:00 2001 From: "Zaid \"Nico" Date: Thu, 25 Feb 2021 11:22:51 -0500 Subject: [PATCH 1/2] fix: add a RequestBodyData type and replace in appropriate areas --- libs/rest/src/Bucket.ts | 4 ++-- libs/rest/src/Fetch.ts | 8 ++++++-- libs/rest/src/RestManager.ts | 14 +++++++------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/libs/rest/src/Bucket.ts b/libs/rest/src/Bucket.ts index f22bfa5..bae63e2 100644 --- a/libs/rest/src/Bucket.ts +++ b/libs/rest/src/Bucket.ts @@ -1,4 +1,4 @@ -import { discordFetch, DiscordFetchOptions, StringRecord } from './Fetch'; +import { discordFetch, DiscordFetchOptions, StringRecord, RequestBodyData } 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); diff --git a/libs/rest/src/Fetch.ts b/libs/rest/src/Fetch.ts index d198b99..0489613 100644 --- a/libs/rest/src/Fetch.ts +++ b/libs/rest/src/Fetch.ts @@ -20,10 +20,14 @@ export interface File { export type StringRecord = Record; +export interface RequestBodyData { + [key: string]: string | number | boolean | 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; @@ -38,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 cbec354..d1d3dca 100644 --- a/libs/rest/src/RestManager.ts +++ b/libs/rest/src/RestManager.ts @@ -4,7 +4,7 @@ import { EventEmitter } from 'events'; import { Headers } from 'node-fetch'; import { Mutex, MemoryMutex } from './mutex'; import AbortController from 'abort-controller'; -import type { DiscordFetchOptions, File, StringRecord } from './Fetch'; +import type { DiscordFetchOptions, File, RequestBodyData, StringRecord } from './Fetch'; /** * Options for constructing a rest manager @@ -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); @@ -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 }); } } From 501745caef8fc72b2cfcf684486871e4b51929f4 Mon Sep 17 00:00:00 2001 From: "Zaid \"Nico" Date: Thu, 25 Feb 2021 12:47:44 -0500 Subject: [PATCH 2/2] update IRouter typings --- libs/routers/src/IRouter.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libs/routers/src/IRouter.ts b/libs/routers/src/IRouter.ts index e3fae2f..877db8e 100644 --- a/libs/routers/src/IRouter.ts +++ b/libs/routers/src/IRouter.ts @@ -1,9 +1,9 @@ -import type { File, StringRecord } from '@cordis/rest'; +import type { File, StringRecord, RequestBodyData } from '@cordis/rest'; export type IRouter = { get(options?: { query?: Q }): T; - delete(options?: { data?: D; reason?: string }): T; - patch(options: { data: D; reason?: string }): T; - put(options: { data: D; reason?: string }): T; - post(options: { data: D; reason?: string; files?: File[] }): T; + delete(options?: { data?: D; reason?: string }): T; + patch(options: { data: D; reason?: string }): T; + put(options: { data: D; reason?: string }): T; + post(options: { data: D; reason?: string; files?: File[] }): T; } & { [key: string]: IRouter };