From 679dcda9709376f37cc58a60f74d12d324d93e4e Mon Sep 17 00:00:00 2001 From: with-heart Date: Thu, 28 Apr 2022 07:08:02 -0400 Subject: [PATCH] feat(REST): enable setting default authPrefix (#7853) --- packages/rest/src/lib/REST.ts | 6 ++++++ packages/rest/src/lib/RequestManager.ts | 2 +- packages/rest/src/lib/utils/constants.ts | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/rest/src/lib/REST.ts b/packages/rest/src/lib/REST.ts index 581090b8e..ddf955eaa 100644 --- a/packages/rest/src/lib/REST.ts +++ b/packages/rest/src/lib/REST.ts @@ -29,6 +29,12 @@ export interface RESTOptions { * @default 'https://discord.com/api' */ api: string; + /** + * The authorization prefix to use for requests, useful if you want to use + * bearer tokens + * @default 'Bot' + */ + authPrefix: 'Bot' | 'Bearer'; /** * The cdn path * @default 'https://cdn.discordapp.com' diff --git a/packages/rest/src/lib/RequestManager.ts b/packages/rest/src/lib/RequestManager.ts index cae592258..cc27acd24 100644 --- a/packages/rest/src/lib/RequestManager.ts +++ b/packages/rest/src/lib/RequestManager.ts @@ -351,7 +351,7 @@ export class RequestManager extends EventEmitter { throw new Error('Expected token to be set for this request, but none was present'); } - headers.Authorization = `${request.authPrefix ?? 'Bot'} ${this.#token}`; + headers.Authorization = `${request.authPrefix ?? this.options.authPrefix} ${this.#token}`; } // If a reason was set, set it's appropriate header diff --git a/packages/rest/src/lib/utils/constants.ts b/packages/rest/src/lib/utils/constants.ts index c463f3098..27e334a4f 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 = { agent: {}, api: 'https://discord.com/api', + authPrefix: 'Bot', cdn: 'https://cdn.discordapp.com', headers: {}, invalidRequestWarningInterval: 0,