From d1758c74b00a3f83c39745cd9af147a7f8f2b12b Mon Sep 17 00:00:00 2001 From: ckohen Date: Fri, 8 Oct 2021 07:30:01 -0700 Subject: [PATCH] feat(RequestManager): support setting global headers in options (#70) Co-authored-by: Advaith Co-authored-by: Vlad Frangu --- packages/rest/src/lib/REST.ts | 5 +++++ packages/rest/src/lib/RequestManager.ts | 1 + packages/rest/src/lib/utils/constants.ts | 1 + 3 files changed, 7 insertions(+) diff --git a/packages/rest/src/lib/REST.ts b/packages/rest/src/lib/REST.ts index d220fb6..1899c16 100644 --- a/packages/rest/src/lib/REST.ts +++ b/packages/rest/src/lib/REST.ts @@ -17,6 +17,11 @@ export interface RESTOptions { * @default 'https://cdn.discordapp.com' */ cdn: string; + /** + * Additional headers to send for all API requests + * @default {} + */ + headers: Record; /** * The extra offset to add to rate limits in milliseconds * @default 50 diff --git a/packages/rest/src/lib/RequestManager.ts b/packages/rest/src/lib/RequestManager.ts index 911336e..a8697b5 100644 --- a/packages/rest/src/lib/RequestManager.ts +++ b/packages/rest/src/lib/RequestManager.ts @@ -197,6 +197,7 @@ export class RequestManager extends EventEmitter { // Create the required headers const headers: RequestHeaders = { + ...this.options.headers, 'User-Agent': `${DefaultUserAgent} ${options.userAgentAppendix}`.trim(), }; diff --git a/packages/rest/src/lib/utils/constants.ts b/packages/rest/src/lib/utils/constants.ts index 4f87780..8715de4 100644 --- a/packages/rest/src/lib/utils/constants.ts +++ b/packages/rest/src/lib/utils/constants.ts @@ -9,6 +9,7 @@ export const DefaultUserAgent = `DiscordBot (${Package.homepage}, ${Package.vers export const DefaultRestOptions: Required = { api: 'https://discord.com/api', cdn: 'https://cdn.discordapp.com', + headers: {}, offset: 50, retries: 3, timeout: 15_000,