@discordeno/rest 19.0.0-next.50282f9 → 19.0.0-next.51fd668

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  /**
3
2
  * A invalid request bucket is used in a similar manner as a leaky bucket but a invalid request bucket can be refilled as needed.
4
3
  * It's purpose is to make sure the bot does not hit the limit to getting a 1 hr ban.
@@ -14,8 +13,8 @@ export interface InvalidRequestBucketOptions {
14
13
  max?: number;
15
14
  /** The time that discord allows to make the max number of invalid requests. Defaults to 10 minutes */
16
15
  interval?: number;
17
- /** timer to reset to 0 */
18
- timeoutId?: NodeJS.Timeout;
16
+ /** When the timeout for the bucket has started at. */
17
+ resetAt?: number;
19
18
  /** how safe to be from max. Defaults to 1 */
20
19
  safety?: number;
21
20
  /** The request statuses that count as an invalid request. */
@@ -25,19 +24,19 @@ export interface InvalidRequestBucketOptions {
25
24
  }
26
25
  export interface InvalidRequestBucket {
27
26
  /** current invalid amount */
28
- current: number;
27
+ invalidRequests: number;
29
28
  /** max invalid requests allowed until ban. Defaults to 10,000 */
30
29
  max: number;
31
30
  /** The time that discord allows to make the max number of invalid requests. Defaults to 10 minutes */
32
31
  interval: number;
33
- /** timer to reset to 0 */
34
- timeoutId: NodeJS.Timeout | undefined;
32
+ /** When the timeout for this bucket has started at. */
33
+ resetAt: number | undefined;
35
34
  /** how safe to be from max. Defaults to 1 */
36
35
  safety: number;
37
36
  /** The request statuses that count as an invalid request. */
38
37
  errorStatuses: number[];
39
38
  /** The amount of requests that were requested from this bucket. */
40
- requested: number;
39
+ activeRequests: number;
41
40
  /** The requests that are currently pending. */
42
41
  waiting: Array<(value: void | PromiseLike<void>) => void>;
43
42
  /** Whether or not the waiting queue is already processing. */
@@ -1 +1 @@
1
- {"version":3,"file":"invalidBucket.d.ts","sourceRoot":"","sources":["../src/invalidBucket.ts"],"names":[],"mappings":";AAEA;;;;;;GAMG;AACH,wB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EnB,+DAA+D;IAC/D,eAAe,EAAE,MAAM,MAAM,CAAA;IAC7B,mDAAmD;IACnD,gBAAgB,EAAE,MAAM,OAAO,CAAA;IAC/B,yCAAyC;IACzC,yBAAyB,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IAC9C,uDAAuD;IACvD,cAAc,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IACnC,iIAAiI;IACjI,sBAAsB,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,KAAK,IAAI,CAAA;CACrE"}
@@ -7,21 +7,25 @@ import { delay, logger } from '@discordeno/utils';
7
7
  * @returns RefillingBucket
8
8
  */ export function createInvalidRequestBucket(options) {
9
9
  const bucket = {
10
- current: options.current ?? 0,
10
+ invalidRequests: options.current ?? 0,
11
11
  max: options.max ?? 10000,
12
- interval: options.interval ?? 600000,
13
- timeoutId: options.timeoutId,
12
+ interval: options.interval ?? 600_000,
13
+ resetAt: options.resetAt,
14
14
  safety: options.safety ?? 1,
15
15
  errorStatuses: options.errorStatuses ?? [
16
16
  401,
17
17
  403,
18
18
  429
19
19
  ],
20
- requested: options.requested ?? 0,
20
+ activeRequests: options.requested ?? 0,
21
21
  processing: false,
22
22
  waiting: [],
23
23
  requestsAllowed: function() {
24
- return bucket.max - bucket.current - bucket.requested - bucket.safety;
24
+ if (bucket.resetAt !== undefined && Date.now() > bucket.resetAt) {
25
+ bucket.invalidRequests = 0;
26
+ bucket.resetAt = Date.now() + bucket.interval;
27
+ }
28
+ return bucket.max - bucket.invalidRequests - bucket.activeRequests - bucket.safety;
25
29
  },
26
30
  isRequestAllowed: function() {
27
31
  return bucket.requestsAllowed() > 0;
@@ -31,7 +35,7 @@ import { delay, logger } from '@discordeno/utils';
31
35
  return await new Promise(async (resolve)=>{
32
36
  // If whatever amount of requests is left is more than the safety margin, allow the request
33
37
  if (bucket.isRequestAllowed()) {
34
- bucket.requested++;
38
+ bucket.activeRequests += 1;
35
39
  resolve();
36
40
  } else {
37
41
  bucket.waiting.push(resolve);
@@ -48,34 +52,28 @@ import { delay, logger } from '@discordeno/utils';
48
52
  bucket.processing = true;
49
53
  while(bucket.waiting.length > 0){
50
54
  logger.info(`[InvalidBucket] processing waiting queue while loop ran with ${bucket.waiting.length} remaining.`);
51
- if (bucket.isRequestAllowed()) {
52
- bucket.requested++;
53
- // Resolve the next item in the queue
54
- bucket.waiting.shift()?.();
55
- } else {
56
- await delay(1000);
55
+ if (bucket.resetAt !== undefined && !bucket.isRequestAllowed()) {
56
+ await delay(bucket.resetAt - Date.now());
57
57
  }
58
+ bucket.activeRequests += 1;
59
+ // Resolve the next item in the queue
60
+ bucket.waiting.shift()?.();
58
61
  }
59
62
  // Mark as false so next pending request can be triggered by new loop.
60
63
  bucket.processing = false;
61
64
  },
62
65
  handleCompletedRequest: function(code, sharedScope) {
63
66
  // Since request is complete, we can remove one from requested.
64
- bucket.requested--;
67
+ bucket.activeRequests -= 1;
65
68
  // Since it is as a valid request, we don't need to do anything
66
69
  if (!bucket.errorStatuses.includes(code)) return;
67
70
  // Shared scope is not considered invalid
68
71
  if (code === 429 && sharedScope) return;
69
72
  // INVALID REQUEST WAS MADE
70
- // Mark a request has been invalid
71
- bucket.current++;
72
- // If a timeout was not started, start a timeout to reset this bucket
73
- if (bucket.timeoutId === undefined) {
74
- bucket.timeoutId = setTimeout(()=>{
75
- bucket.current = 0;
76
- bucket.timeoutId = undefined;
77
- }, bucket.interval);
73
+ if (bucket.resetAt === undefined) {
74
+ bucket.resetAt = Date.now() + bucket.interval;
78
75
  }
76
+ bucket.invalidRequests += 1;
79
77
  }
80
78
  };
81
79
  return bucket;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/invalidBucket.ts"],"sourcesContent":["import { delay, logger } from '@discordeno/utils'\n\n/**\n * A invalid request bucket is used in a similar manner as a leaky bucket but a invalid request bucket can be refilled as needed.\n * It's purpose is to make sure the bot does not hit the limit to getting a 1 hr ban.\n *\n * @param options The options used to configure this bucket.\n * @returns RefillingBucket\n */\nexport function createInvalidRequestBucket(options: InvalidRequestBucketOptions): InvalidRequestBucket {\n const bucket: InvalidRequestBucket = {\n current: options.current ?? 0,\n max: options.max ?? 10000,\n interval: options.interval ?? 600000,\n timeoutId: options.timeoutId,\n safety: options.safety ?? 1,\n errorStatuses: options.errorStatuses ?? [401, 403, 429],\n requested: options.requested ?? 0,\n processing: false,\n\n waiting: [],\n\n requestsAllowed: function () {\n return bucket.max - bucket.current - bucket.requested - bucket.safety\n },\n\n isRequestAllowed: function () {\n return bucket.requestsAllowed() > 0\n },\n\n waitUntilRequestAvailable: async function () {\n // eslint-disable-next-line no-async-promise-executor\n return await new Promise(async (resolve) => {\n // If whatever amount of requests is left is more than the safety margin, allow the request\n if (bucket.isRequestAllowed()) {\n bucket.requested++\n resolve()\n } else {\n bucket.waiting.push(resolve)\n await bucket.processWaiting()\n }\n })\n },\n\n processWaiting: async function () {\n // If already processing, that loop will handle all waiting requests.\n if (bucket.processing) {\n return\n }\n\n // Mark as processing so other loops don't start\n bucket.processing = true\n\n while (bucket.waiting.length > 0) {\n logger.info(`[InvalidBucket] processing waiting queue while loop ran with ${bucket.waiting.length} remaining.`)\n if (bucket.isRequestAllowed()) {\n bucket.requested++\n // Resolve the next item in the queue\n bucket.waiting.shift()?.()\n } else {\n await delay(1000)\n }\n }\n\n // Mark as false so next pending request can be triggered by new loop.\n bucket.processing = false\n },\n\n handleCompletedRequest: function (code, sharedScope) {\n // Since request is complete, we can remove one from requested.\n bucket.requested--\n // Since it is as a valid request, we don't need to do anything\n if (!bucket.errorStatuses.includes(code)) return\n // Shared scope is not considered invalid\n if (code === 429 && sharedScope) return\n\n // INVALID REQUEST WAS MADE\n\n // Mark a request has been invalid\n bucket.current++\n // If a timeout was not started, start a timeout to reset this bucket\n if (bucket.timeoutId === undefined) {\n bucket.timeoutId = setTimeout(() => {\n bucket.current = 0\n bucket.timeoutId = undefined\n }, bucket.interval)\n }\n },\n }\n\n return bucket\n}\n\nexport interface InvalidRequestBucketOptions {\n /** current invalid amount */\n current?: number\n /** max invalid requests allowed until ban. Defaults to 10,000 */\n max?: number\n /** The time that discord allows to make the max number of invalid requests. Defaults to 10 minutes */\n interval?: number\n /** timer to reset to 0 */\n timeoutId?: NodeJS.Timeout\n /** how safe to be from max. Defaults to 1 */\n safety?: number\n /** The request statuses that count as an invalid request. */\n errorStatuses?: number[]\n /** The amount of requests that were requested from this bucket. */\n requested?: number\n}\n\nexport interface InvalidRequestBucket {\n /** current invalid amount */\n current: number\n /** max invalid requests allowed until ban. Defaults to 10,000 */\n max: number\n /** The time that discord allows to make the max number of invalid requests. Defaults to 10 minutes */\n interval: number\n /** timer to reset to 0 */\n timeoutId: NodeJS.Timeout | undefined\n /** how safe to be from max. Defaults to 1 */\n safety: number\n /** The request statuses that count as an invalid request. */\n errorStatuses: number[]\n /** The amount of requests that were requested from this bucket. */\n requested: number\n /** The requests that are currently pending. */\n waiting: Array<(value: void | PromiseLike<void>) => void>\n /** Whether or not the waiting queue is already processing. */\n processing: boolean\n\n /** Gives the number of requests that are currently allowed. */\n requestsAllowed: () => number\n /** Checks if a request is allowed at this time. */\n isRequestAllowed: () => boolean\n /** Waits until a request is available */\n waitUntilRequestAvailable: () => Promise<void>\n /** Begins processing the waiting queue of requests. */\n processWaiting: () => Promise<void>\n /** Handler for whenever a request is validated. This should update the requested values or trigger any other necessary stuff. */\n handleCompletedRequest: (code: number, sharedScope: boolean) => void\n}\n"],"names":["delay","logger","createInvalidRequestBucket","options","bucket","current","max","interval","timeoutId","safety","errorStatuses","requested","processing","waiting","requestsAllowed","isRequestAllowed","waitUntilRequestAvailable","Promise","resolve","push","processWaiting","length","info","shift","handleCompletedRequest","code","sharedScope","includes","undefined","setTimeout"],"mappings":"AAAA,SAASA,KAAK,EAAEC,MAAM,QAAQ,oBAAmB;AAEjD;;;;;;CAMC,GACD,OAAO,SAASC,2BAA2BC,OAAoC,EAAwB;IACrG,MAAMC,SAA+B;QACnCC,SAASF,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K;QAC3B;QAEAY,wBAAwB,SAAUC,IAAI,EAAEC,WAAW,EAAE;YACnD,+DAA+D;YAC/DtB,OAAOO,SAAS;YAChB,+DAA+D;YAC/D,IAAI,CAACP,OAAOM,aAAa,CAACiB,QAAQ,CAACF,OAAO;YAC1C,yCAAyC;YACzC,IAAIA,SAAS,OAAOC,aAAa;YAEjC,2BAA2B;YAE3B,kCAAkC;YAClCtB,OAAOC,OAAO;YACd,qEAAqE;YACrE,IAAID,OAAOI,SAAS,KAAKoB,WAAW;gBAClCxB,OAAOI,SAAS,GAAGqB,WAAW,IAAM;oBAClCzB,OAAOC,OAAO,GAAG;oBACjBD,OAAOI,SAAS,GAAGoB;gBACrB,GAAGxB,OAAOG,QAAQ;YACpB,CAAC;QACH;IACF;IAEA,OAAOH;AACT,CAAC"}
1
+ {"version":3,"sources":["../src/invalidBucket.ts"],"sourcesContent":["import { delay, logger } from '@discordeno/utils'\n\n/**\n * A invalid request bucket is used in a similar manner as a leaky bucket but a invalid request bucket can be refilled as needed.\n * It's purpose is to make sure the bot does not hit the limit to getting a 1 hr ban.\n *\n * @param options The options used to configure this bucket.\n * @returns RefillingBucket\n */\nexport function createInvalidRequestBucket(options: InvalidRequestBucketOptions): InvalidRequestBucket {\n const bucket: InvalidRequestBucket = {\n invalidRequests: options.current ?? 0,\n max: options.max ?? 10000,\n interval: options.interval ?? 600_000, // 10 minutes\n resetAt: options.resetAt,\n safety: options.safety ?? 1,\n errorStatuses: options.errorStatuses ?? [401, 403, 429],\n activeRequests: options.requested ?? 0,\n processing: false,\n\n waiting: [],\n\n requestsAllowed: function () {\n if (bucket.resetAt !== undefined && Date.now() > bucket.resetAt) {\n bucket.invalidRequests = 0\n bucket.resetAt = Date.now() + bucket.interval\n }\n\n return bucket.max - bucket.invalidRequests - bucket.activeRequests - bucket.safety\n },\n\n isRequestAllowed: function () {\n return bucket.requestsAllowed() > 0\n },\n\n waitUntilRequestAvailable: async function () {\n // eslint-disable-next-line no-async-promise-executor\n return await new Promise(async (resolve) => {\n // If whatever amount of requests is left is more than the safety margin, allow the request\n if (bucket.isRequestAllowed()) {\n bucket.activeRequests += 1\n resolve()\n } else {\n bucket.waiting.push(resolve)\n await bucket.processWaiting()\n }\n })\n },\n\n processWaiting: async function () {\n // If already processing, that loop will handle all waiting requests.\n if (bucket.processing) {\n return\n }\n\n // Mark as processing so other loops don't start\n bucket.processing = true\n\n while (bucket.waiting.length > 0) {\n logger.info(`[InvalidBucket] processing waiting queue while loop ran with ${bucket.waiting.length} remaining.`)\n\n if (bucket.resetAt !== undefined && !bucket.isRequestAllowed()) {\n await delay(bucket.resetAt - Date.now())\n }\n\n bucket.activeRequests += 1\n // Resolve the next item in the queue\n bucket.waiting.shift()?.()\n }\n\n // Mark as false so next pending request can be triggered by new loop.\n bucket.processing = false\n },\n\n handleCompletedRequest: function (code, sharedScope) {\n // Since request is complete, we can remove one from requested.\n bucket.activeRequests -= 1\n // Since it is as a valid request, we don't need to do anything\n if (!bucket.errorStatuses.includes(code)) return\n // Shared scope is not considered invalid\n if (code === 429 && sharedScope) return\n\n // INVALID REQUEST WAS MADE\n if (bucket.resetAt === undefined) {\n bucket.resetAt = Date.now() + bucket.interval\n }\n\n bucket.invalidRequests += 1\n },\n }\n\n return bucket\n}\n\nexport interface InvalidRequestBucketOptions {\n /** current invalid amount */\n current?: number\n /** max invalid requests allowed until ban. Defaults to 10,000 */\n max?: number\n /** The time that discord allows to make the max number of invalid requests. Defaults to 10 minutes */\n interval?: number\n /** When the timeout for the bucket has started at. */\n resetAt?: number\n /** how safe to be from max. Defaults to 1 */\n safety?: number\n /** The request statuses that count as an invalid request. */\n errorStatuses?: number[]\n /** The amount of requests that were requested from this bucket. */\n requested?: number\n}\n\nexport interface InvalidRequestBucket {\n /** current invalid amount */\n invalidRequests: number\n /** max invalid requests allowed until ban. Defaults to 10,000 */\n max: number\n /** The time that discord allows to make the max number of invalid requests. Defaults to 10 minutes */\n interval: number\n /** When the timeout for this bucket has started at. */\n resetAt: number | undefined\n /** how safe to be from max. Defaults to 1 */\n safety: number\n /** The request statuses that count as an invalid request. */\n errorStatuses: number[]\n /** The amount of requests that were requested from this bucket. */\n activeRequests: number\n /** The requests that are currently pending. */\n waiting: Array<(value: void | PromiseLike<void>) => void>\n /** Whether or not the waiting queue is already processing. */\n processing: boolean\n\n /** Gives the number of requests that are currently allowed. */\n requestsAllowed: () => number\n /** Checks if a request is allowed at this time. */\n isRequestAllowed: () => boolean\n /** Waits until a request is available */\n waitUntilRequestAvailable: () => Promise<void>\n /** Begins processing the waiting queue of requests. */\n processWaiting: () => Promise<void>\n /** Handler for whenever a request is validated. This should update the requested values or trigger any other necessary stuff. */\n handleCompletedRequest: (code: number, sharedScope: boolean) => void\n}\n"],"names":["delay","logger","createInvalidRequestBucket","options","bucket","invalidRequests","current","max","interval","resetAt","safety","errorStatuses","activeRequests","requested","processing","waiting","requestsAllowed","undefined","Date","now","isRequestAllowed","waitUntilRequestAvailable","Promise","resolve","push","processWaiting","length","info","shift","handleCompletedRequest","code","sharedScope","includes"],"mappings":"AAAA,SAASA,KAAK,EAAEC,MAAM,QAAQ,oBAAmB;AAEjD;;;;;;CAMC,GACD,OAAO,SAASC,2BAA2BC,OAAoC,EAAwB;IACrG,MAAMC,SAA+B;QACnCC,iBAAiBF,QAAQG,OAAO,IAAI;QACpCC,KAAKJ,QAAQI,GAAG,IAAI;QACpBC,UAAUL,QAAQK,QAAQ,IAAI;QAC9BC,SAASN,QAAQM,OAAO;QACxBC,QAAQP,QAAQO,MAAM,IAAI;QAC1BC,eAAeR,QAAQQ,aAAa,IAAI;YAAC;YAAK;YAAK;SAAI;QACvDC,gBAAgBT,QAAQU,SAAS,IAAI;QACrCC,YAAY,KAAK;QAEjBC,SAAS,EAAE;QAEXC,iBAAiB,WAAY;YAC3B,IAAIZ,OAAOK,OAAO,KAAKQ,aAAaC,KAAKC,GAAG,KAAKf,OAAOK,OAAO,EAAE;gBAC/DL,OAAOC,eAAe,GAAG;gBACzBD,OAA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,OAAO;YAC1C,yCAAyC;YACzC,IAAIA,SAAS,OAAOC,aAAa;YAEjC,2BAA2B;YAC3B,IAAI3B,OAAOK,OAAO,KAAKQ,WAAW;gBAChCb,OAAOK,OAAO,GAAGS,KAAKC,GAAG,KAAKf,OAAOI,QAAQ;YAC/C,CAAC;YAEDJ,OAAOC,eAAe,IAAI;QAC5B;IACF;IAEA,OAAOD;AACT,CAAC"}
package/dist/manager.d.ts CHANGED
@@ -1,3 +1,12 @@
1
1
  import type { CreateRestManagerOptions, RestManager } from './types.js';
2
+ export declare const DISCORD_API_VERSION = 10;
3
+ export declare const DISCORD_API_URL = "https://discord.com/api";
4
+ export declare const AUDIT_LOG_REASON_HEADER = "x-audit-log-reason";
5
+ export declare const RATE_LIMIT_REMAINING_HEADER = "x-ratelimit-remaining";
6
+ export declare const RATE_LIMIT_RESET_AFTER_HEADER = "x-ratelimit-reset-after";
7
+ export declare const RATE_LIMIT_GLOBAL_HEADER = "x-ratelimit-global";
8
+ export declare const RATE_LIMIT_BUCKET_HEADER = "x-ratelimit-bucket";
9
+ export declare const RATE_LIMIT_LIMIT_HEADER = "x-ratelimit-limit";
10
+ export declare const RATE_LIMIT_SCOPE_HEADER = "x-ratelimit-scope";
2
11
  export declare function createRestManager(options: CreateRestManagerOptions): RestManager;
3
12
  //# sourceMappingURL=manager.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AAkDA,OAAO,KAAK,EAA4B,wBAAwB,EAAE,WAAW,EAAsB,MAAM,YAAY,CAAA;AAMrH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,WAAW,CAwnChF"}
1
+ {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AAkDA,OAAO,KAAK,EAA4B,wBAAwB,EAAE,WAAW,EAAsB,MAAM,YAAY,CAAA;AAMrH,eAAO,MAAM,mBAAmB,KAAK,CAAA;AACrC,eAAO,MAAM,eAAe,4BAA4B,CAAA;AAExD,eAAO,MAAM,uBAAuB,uBAAuB,CAAA;AAC3D,eAAO,MAAM,2BAA2B,0BAA0B,CAAA;AAClE,eAAO,MAAM,6BAA6B,4BAA4B,CAAA;AACtE,eAAO,MAAM,wBAAwB,uBAAuB,CAAA;AAC5D,eAAO,MAAM,wBAAwB,uBAAuB,CAAA;AAC5D,eAAO,MAAM,uBAAuB,sBAAsB,CAAA;AAC1D,eAAO,MAAM,uBAAuB,sBAAsB,CAAA;AAE1D,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,WAAW,CAwnChF"}
package/dist/manager.js CHANGED
@@ -6,24 +6,35 @@ import { Queue } from './queue.js';
6
6
  import { createRoutes } from './routes.js';
7
7
  // TODO: make dynamic based on package.json file
8
8
  const version = '19.0.0-alpha.1';
9
+ export const DISCORD_API_VERSION = 10;
10
+ export const DISCORD_API_URL = 'https://discord.com/api';
11
+ export const AUDIT_LOG_REASON_HEADER = 'x-audit-log-reason';
12
+ export const RATE_LIMIT_REMAINING_HEADER = 'x-ratelimit-remaining';
13
+ export const RATE_LIMIT_RESET_AFTER_HEADER = 'x-ratelimit-reset-after';
14
+ export const RATE_LIMIT_GLOBAL_HEADER = 'x-ratelimit-global';
15
+ export const RATE_LIMIT_BUCKET_HEADER = 'x-ratelimit-bucket';
16
+ export const RATE_LIMIT_LIMIT_HEADER = 'x-ratelimit-limit';
17
+ export const RATE_LIMIT_SCOPE_HEADER = 'x-ratelimit-scope';
9
18
  export function createRestManager(options) {
10
19
  const applicationId = options.applicationId ? BigInt(options.applicationId) : options.token ? getBotIdFromToken(options.token) : undefined;
11
20
  if (!applicationId) {
12
21
  throw new Error('`applicationId` was not provided and was not able to extract the id from the bots token. Please explicitly pass `applicationId` to the rest manager.');
13
22
  }
23
+ const baseUrl = options.proxy?.baseUrl ?? DISCORD_API_URL;
14
24
  const rest = {
15
- token: options.token,
16
25
  applicationId,
17
- version: options.version ?? 10,
18
- baseUrl: options.proxy?.baseUrl ?? 'https://discord.com/api',
19
- maxRetryCount: Infinity,
26
+ authorization: options.proxy?.authorization,
27
+ baseUrl,
28
+ deleteQueueDelay: 60000,
20
29
  globallyRateLimited: false,
30
+ invalidBucket: createInvalidRequestBucket({}),
31
+ isProxied: !baseUrl.startsWith(DISCORD_API_URL),
32
+ maxRetryCount: Infinity,
21
33
  processingRateLimitedPaths: false,
22
- deleteQueueDelay: 60000,
23
34
  queues: new Map(),
24
35
  rateLimitedPaths: new Map(),
25
- invalidBucket: createInvalidRequestBucket({}),
26
- authorization: options.proxy?.authorization,
36
+ token: options.token,
37
+ version: options.version ?? DISCORD_API_VERSION,
27
38
  routes: createRoutes(),
28
39
  checkRateLimits (url) {
29
40
  const ratelimited = rest.rateLimitedPaths.get(url);
@@ -72,7 +83,7 @@ export function createRestManager(options) {
72
83
  if (options?.unauthorized !== false) headers.authorization = `Bot ${rest.token}`;
73
84
  // IF A REASON IS PROVIDED ENCODE IT IN HEADERS
74
85
  if (options?.reason !== undefined) {
75
- headers['x-audit-log-reason'] = encodeURIComponent(options?.reason);
86
+ headers[AUDIT_LOG_REASON_HEADER] = encodeURIComponent(options?.reason);
76
87
  }
77
88
  let body;
78
89
  // TODO: check if we need to add specific check for GET method
@@ -138,13 +149,13 @@ export function createRestManager(options) {
138
149
  /** Processes the rate limit headers and determines if it needs to be rate limited and returns the bucket id if available */ processHeaders (url, headers) {
139
150
  let rateLimited = false;
140
151
  // GET ALL NECESSARY HEADERS
141
- const remaining = headers.get('x-ratelimit-remaining');
142
- const retryAfter = headers.get('x-ratelimit-reset-after');
152
+ const remaining = headers.get(RATE_LIMIT_REMAINING_HEADER);
153
+ const retryAfter = headers.get(RATE_LIMIT_RESET_AFTER_HEADER);
143
154
  const reset = Date.now() + Number(retryAfter) * 1000;
144
- const global = headers.get('x-ratelimit-global');
155
+ const global = headers.get(RATE_LIMIT_GLOBAL_HEADER);
145
156
  // undefined override null needed for typings
146
- const bucketId = headers.get('x-ratelimit-bucket') ?? undefined;
147
- const limit = headers.get('x-ratelimit-limit');
157
+ const bucketId = headers.get(RATE_LIMIT_BUCKET_HEADER) ?? undefined;
158
+ const limit = headers.get(RATE_LIMIT_LIMIT_HEADER);
148
159
  rest.queues.get(url)?.handleCompletedRequest({
149
160
  remaining: remaining ? Number(remaining) : undefined,
150
161
  interval: retryAfter ? Number(retryAfter) * 1000 : undefined,
@@ -237,8 +248,8 @@ export function createRestManager(options) {
237
248
  }
238
249
  options.retryCount += 1;
239
250
  // Rate limited, add back to queue
240
- rest.invalidBucket.handleCompletedRequest(response.status, response.headers.get('X-RateLimit-Scope') === 'shared');
241
- const resetAfter = response.headers.get('x-ratelimit-reset-after');
251
+ rest.invalidBucket.handleCompletedRequest(response.status, response.headers.get(RATE_LIMIT_SCOPE_HEADER) === 'shared');
252
+ const resetAfter = response.headers.get(RATE_LIMIT_RESET_AFTER_HEADER);
242
253
  if (resetAfter) await delay(Number(resetAfter) * 1000);
243
254
  // process the response to prevent mem leak
244
255
  await response.arrayBuffer();
@@ -273,14 +284,6 @@ export function createRestManager(options) {
273
284
  return parts.join('/');
274
285
  },
275
286
  async processRequest (request) {
276
- const route = request.route.substring(request.route.indexOf('api/'));
277
- const parts = route.split('/');
278
- // Remove the api/
279
- parts.shift();
280
- // Removes the /v#/
281
- if (parts[0]?.startsWith('v')) parts.shift();
282
- // Set the full url to discord api in case it was recieved in a proxy rest
283
- request.route = `${rest.baseUrl}/v${rest.version}/${parts.join('/')}`;
284
287
  const url = rest.simplifyUrl(request.route, request.method);
285
288
  if (request.runThroughQueue === false) {
286
289
  await rest.sendRequest(request);
@@ -302,8 +305,13 @@ export function createRestManager(options) {
302
305
  }
303
306
  },
304
307
  async makeRequest (method, route, options) {
305
- if (!rest.baseUrl.startsWith('https://discord.com') && route[0] === '/') {
306
- const result = await fetch(`${rest.baseUrl}${route}`, rest.createRequestBody(method, options));
308
+ if (rest.isProxied) {
309
+ if (rest.authorization !== undefined) {
310
+ options ??= {};
311
+ options.headers ??= {};
312
+ options.headers.authorization = rest.authorization;
313
+ }
314
+ const result = await fetch(`${rest.baseUrl}/v${rest.version}${route}`, rest.createRequestBody(method, options));
307
315
  if (!result.ok) {
308
316
  const err = await result.json().catch(()=>{});
309
317
  // Legacy Handling to not break old code or when body is missing