From 7f9459b4353a2357b4376b6bd0caf53ec64318c3 Mon Sep 17 00:00:00 2001 From: ITOH Date: Mon, 3 Apr 2023 17:51:47 +0200 Subject: [PATCH] feat(rest): add `isProxied` (#2957) Adds the `isProxied` property to rest. This effectively removes the `startWith('https://discord.com/api')` check. --- packages/rest/src/manager.ts | 9 ++++++--- packages/rest/src/types.ts | 6 ++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/rest/src/manager.ts b/packages/rest/src/manager.ts index bdc5fd614..f20185e59 100644 --- a/packages/rest/src/manager.ts +++ b/packages/rest/src/manager.ts @@ -62,11 +62,14 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage ) } + const baseUrl = options.proxy?.baseUrl ?? 'https://discord.com/api' + const rest: RestManager = { token: options.token, applicationId, version: options.version ?? 10, - baseUrl: options.proxy?.baseUrl ?? 'https://discord.com/api', + baseUrl, + isProxied: !baseUrl.startsWith('https://discord.com/api'), maxRetryCount: Infinity, globallyRateLimited: false, processingRateLimitedPaths: false, @@ -396,8 +399,8 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage }, async makeRequest(method, route, options) { - if (!rest.baseUrl.startsWith('https://discord.com') && route[0] === '/') { - const result = await fetch(`${rest.baseUrl}${route}`, rest.createRequestBody(method, options)) + if (rest.isProxied) { + const result = await fetch(`${rest.baseUrl}/v${rest.version}${route}`, rest.createRequestBody(method, options)) if (!result.ok) { const err = (await result.json().catch(() => {})) as Record diff --git a/packages/rest/src/types.ts b/packages/rest/src/types.ts index 7c2baad0d..3309a21bc 100644 --- a/packages/rest/src/types.ts +++ b/packages/rest/src/types.ts @@ -136,6 +136,12 @@ export interface RestManager { * @default https://discord.com/api */ baseUrl: string + /** + * `true` if the `baseUrl` does not start with `https://discord.com/api`. + * + * Mostly used only for intern functions. + */ + isProxied: boolean /** The authorization header to attach when sending requests to the proxy. */ authorization?: string /** The maximum amount of times a request should be retried. Defaults to Infinity */