From f34b5ea1d68db3667cf408af06617009d0631083 Mon Sep 17 00:00:00 2001 From: Advaith Date: Mon, 3 May 2021 19:17:27 -0700 Subject: [PATCH 1/2] feat(ApiRequest): support setting global headers in HTTPOptions --- src/rest/APIRequest.js | 2 +- src/util/Constants.js | 1 + typings/index.d.ts | 17 +++++++++++------ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/rest/APIRequest.js b/src/rest/APIRequest.js index d5e4ec183c3d..a56566695b8b 100644 --- a/src/rest/APIRequest.js +++ b/src/rest/APIRequest.js @@ -33,7 +33,7 @@ class APIRequest { ? this.client.options.http.api : `${this.client.options.http.api}/v${this.client.options.http.version}`; const url = API + this.path; - let headers = {}; + let headers = this.client.options.http.headers ?? {}; if (this.options.auth !== false) headers.Authorization = this.rest.getAuth(); if (this.options.reason) headers['X-Audit-Log-Reason'] = encodeURIComponent(this.options.reason); diff --git a/src/util/Constants.js b/src/util/Constants.js index 7516b78ff24c..528f4e0445d6 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -80,6 +80,7 @@ exports.DefaultOptions = { * @property {string} [cdn='https://cdn.discordapp.com'] Base url of the CDN * @property {string} [invite='https://discord.gg'] Base url of invites * @property {string} [template='https://discord.new'] Base url of templates + * @property {Object} [headers] Additional headers to send for all API requests */ http: { version: 8, diff --git a/typings/index.d.ts b/typings/index.d.ts index c217855c8f26..3088b2bb1938 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1378,7 +1378,7 @@ declare module 'discord.js' { public eval(fn: (client: Client) => T): Promise; public fetchClientValue(prop: string): Promise; public kill(): void; - public respawn(options?: { delay?: number, timeout?: number }): Promise; + public respawn(options?: { delay?: number; timeout?: number }): Promise; public send(message: any): Promise; public spawn(timeout?: number): Promise; @@ -1411,7 +1411,7 @@ declare module 'discord.js' { public broadcastEval(fn: (client: Client) => T, shard: number): Promise; public fetchClientValues(prop: string): Promise; public fetchClientValues(prop: string, shard: number): Promise; - public respawnAll(options?: { shardDelay?: number, respawnDelay?: number, timeout?: number }): Promise; + public respawnAll(options?: { shardDelay?: number; respawnDelay?: number; timeout?: number }): Promise; public send(message: any): Promise; public static singleton(client: Client, mode: ShardingManagerMode): ShardClientUtil; @@ -1448,11 +1448,15 @@ declare module 'discord.js' { public fetchClientValues(prop: string): Promise; public fetchClientValues(prop: string, shard: number): Promise; public respawnAll(options?: { - shardDelay?: number, - respawnDelay?: number, - timeout?: number, + shardDelay?: number; + respawnDelay?: number; + timeout?: number; + }): Promise>; + public spawn(options?: { + amount?: number | 'auto'; + delay?: number; + timeout?: number; }): Promise>; - public spawn(options?: { amount?: number | 'auto', delay?: number, timeout?: number }): Promise>; public on(event: 'shardCreate', listener: (shard: Shard) => void): this; @@ -2810,6 +2814,7 @@ declare module 'discord.js' { cdn?: string; invite?: string; template?: string; + headers?: Record; } type ImageSize = 16 | 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096; From 82677580b9a444e557db22b4b6194fb328e8959b Mon Sep 17 00:00:00 2001 From: Advaith Date: Tue, 4 May 2021 07:56:21 -0700 Subject: [PATCH 2/2] fix: clone object Co-authored-by: Vlad Frangu --- src/rest/APIRequest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rest/APIRequest.js b/src/rest/APIRequest.js index a56566695b8b..c4487bf2a708 100644 --- a/src/rest/APIRequest.js +++ b/src/rest/APIRequest.js @@ -33,7 +33,7 @@ class APIRequest { ? this.client.options.http.api : `${this.client.options.http.api}/v${this.client.options.http.version}`; const url = API + this.path; - let headers = this.client.options.http.headers ?? {}; + let headers = { ...this.client.options.http.headers }; if (this.options.auth !== false) headers.Authorization = this.rest.getAuth(); if (this.options.reason) headers['X-Audit-Log-Reason'] = encodeURIComponent(this.options.reason);