@discordjs/rest 3.0.0-dev.1786752260-c9855614c → 3.0.0-dev.1786968995-99af6d8be

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.
package/dist/index.d.mts CHANGED
@@ -122,14 +122,16 @@ interface RESTOptions {
122
122
  */
123
123
  offset: GetRateLimitOffsetFunction | number;
124
124
  /**
125
- * Determines how rate limiting and pre-emptive throttling should be handled.
126
- * When an array of strings, each element is treated as a prefix for the request route
127
- * (e.g. `/channels` to match any route starting with `/channels` such as `/channels/:id/messages`)
128
- * for which to throw {@link RateLimitError}s. All other request routes will be queued normally
125
+ * The default policy determining how rate limiting and pre-emptive throttling should be handled.
129
126
  *
130
- * @defaultValue `null`
127
+ * Pass `true` to throw a {@link RateLimitError} on every rate limit, `false` to wait every
128
+ * rate limit out, or a filter to decide per rate limit.
129
+ *
130
+ * This can be overridden per request via the {@link RequestData.rejectOnRateLimit | rejectOnRateLimit} request option.
131
+ *
132
+ * @defaultValue `false`
131
133
  */
132
- rejectOnRateLimit: RateLimitQueueFilter | string[] | null;
134
+ rejectOnRateLimit: RateLimitQueueFilter | boolean;
133
135
  /**
134
136
  * The number of retries for errors with the 500 code, or errors
135
137
  * that timeout
@@ -343,6 +345,32 @@ interface RequestData {
343
345
  * Reason to show in the audit logs
344
346
  */
345
347
  reason?: string | undefined;
348
+ /**
349
+ * Determines how a rate limit encountered while making this request should be handled.
350
+ *
351
+ * Pass `true` to throw a {@link RateLimitError} rather than wait, `false` to wait it out, or
352
+ * a filter to decide based on rate limit data. Takes precedence over {@link RESTOptions.rejectOnRateLimit}, so
353
+ * `false` opts this request out of an instance-wide policy. Leave it unset to inherit.
354
+ *
355
+ * @example
356
+ * ```ts
357
+ * // Fail rather than wait, no matter the rate limit
358
+ * await rest.get(Routes.channel(channelId), { rejectOnRateLimit: true });
359
+ *
360
+ * // Give up rather than wait out a sublimit, which may be several minutes long
361
+ * await rest.patch(Routes.channel(channelId), {
362
+ * body: { name },
363
+ * rejectOnRateLimit: (rateLimitData) => rateLimitData.sublimitTimeout > 0,
364
+ * });
365
+ *
366
+ * // Spend at most 10 seconds waiting on rate limits
367
+ * const deadline = Date.now() + 10_000;
368
+ * await rest.get(Routes.channel(channelId), {
369
+ * rejectOnRateLimit: (rateLimitData) => Date.now() + rateLimitData.retryAfter > deadline,
370
+ * });
371
+ * ```
372
+ */
373
+ rejectOnRateLimit?: RateLimitQueueFilter | boolean | undefined;
346
374
  /**
347
375
  * The signal to abort the queue entry or the REST call, where applicable
348
376
  */
@@ -380,7 +408,7 @@ interface InternalRequest extends RequestData {
380
408
  fullRoute: RouteLike;
381
409
  method: RequestMethod;
382
410
  }
383
- interface HandlerRequestData extends Pick<InternalRequest, 'body' | 'files' | 'signal'> {
411
+ interface HandlerRequestData extends Pick<InternalRequest, 'body' | 'files' | 'rejectOnRateLimit' | 'signal'> {
384
412
  auth: boolean | string;
385
413
  }
386
414
  /**
@@ -413,7 +441,7 @@ declare const DefaultRestOptions: {
413
441
  readonly invalidRequestWarningInterval: 0;
414
442
  readonly globalRequestsPerSecond: 50;
415
443
  readonly offset: 50;
416
- readonly rejectOnRateLimit: null;
444
+ readonly rejectOnRateLimit: false;
417
445
  readonly retries: 3;
418
446
  readonly retryBackoff: 0;
419
447
  readonly timeout: 15000;
package/dist/index.d.ts CHANGED
@@ -122,14 +122,16 @@ interface RESTOptions {
122
122
  */
123
123
  offset: GetRateLimitOffsetFunction | number;
124
124
  /**
125
- * Determines how rate limiting and pre-emptive throttling should be handled.
126
- * When an array of strings, each element is treated as a prefix for the request route
127
- * (e.g. `/channels` to match any route starting with `/channels` such as `/channels/:id/messages`)
128
- * for which to throw {@link RateLimitError}s. All other request routes will be queued normally
125
+ * The default policy determining how rate limiting and pre-emptive throttling should be handled.
129
126
  *
130
- * @defaultValue `null`
127
+ * Pass `true` to throw a {@link RateLimitError} on every rate limit, `false` to wait every
128
+ * rate limit out, or a filter to decide per rate limit.
129
+ *
130
+ * This can be overridden per request via the {@link RequestData.rejectOnRateLimit | rejectOnRateLimit} request option.
131
+ *
132
+ * @defaultValue `false`
131
133
  */
132
- rejectOnRateLimit: RateLimitQueueFilter | string[] | null;
134
+ rejectOnRateLimit: RateLimitQueueFilter | boolean;
133
135
  /**
134
136
  * The number of retries for errors with the 500 code, or errors
135
137
  * that timeout
@@ -343,6 +345,32 @@ interface RequestData {
343
345
  * Reason to show in the audit logs
344
346
  */
345
347
  reason?: string | undefined;
348
+ /**
349
+ * Determines how a rate limit encountered while making this request should be handled.
350
+ *
351
+ * Pass `true` to throw a {@link RateLimitError} rather than wait, `false` to wait it out, or
352
+ * a filter to decide based on rate limit data. Takes precedence over {@link RESTOptions.rejectOnRateLimit}, so
353
+ * `false` opts this request out of an instance-wide policy. Leave it unset to inherit.
354
+ *
355
+ * @example
356
+ * ```ts
357
+ * // Fail rather than wait, no matter the rate limit
358
+ * await rest.get(Routes.channel(channelId), { rejectOnRateLimit: true });
359
+ *
360
+ * // Give up rather than wait out a sublimit, which may be several minutes long
361
+ * await rest.patch(Routes.channel(channelId), {
362
+ * body: { name },
363
+ * rejectOnRateLimit: (rateLimitData) => rateLimitData.sublimitTimeout > 0,
364
+ * });
365
+ *
366
+ * // Spend at most 10 seconds waiting on rate limits
367
+ * const deadline = Date.now() + 10_000;
368
+ * await rest.get(Routes.channel(channelId), {
369
+ * rejectOnRateLimit: (rateLimitData) => Date.now() + rateLimitData.retryAfter > deadline,
370
+ * });
371
+ * ```
372
+ */
373
+ rejectOnRateLimit?: RateLimitQueueFilter | boolean | undefined;
346
374
  /**
347
375
  * The signal to abort the queue entry or the REST call, where applicable
348
376
  */
@@ -380,7 +408,7 @@ interface InternalRequest extends RequestData {
380
408
  fullRoute: RouteLike;
381
409
  method: RequestMethod;
382
410
  }
383
- interface HandlerRequestData extends Pick<InternalRequest, 'body' | 'files' | 'signal'> {
411
+ interface HandlerRequestData extends Pick<InternalRequest, 'body' | 'files' | 'rejectOnRateLimit' | 'signal'> {
384
412
  auth: boolean | string;
385
413
  }
386
414
  /**
@@ -413,7 +441,7 @@ declare const DefaultRestOptions: {
413
441
  readonly invalidRequestWarningInterval: 0;
414
442
  readonly globalRequestsPerSecond: 50;
415
443
  readonly offset: 50;
416
- readonly rejectOnRateLimit: null;
444
+ readonly rejectOnRateLimit: false;
417
445
  readonly retries: 3;
418
446
  readonly retryBackoff: 0;
419
447
  readonly timeout: 15000;
package/dist/index.js CHANGED
@@ -144,7 +144,7 @@ var import_v102 = require("discord-api-types/v10");
144
144
  // src/lib/utils/constants.ts
145
145
  var import_util = require("@discordjs/util");
146
146
  var import_v10 = require("discord-api-types/v10");
147
- var DefaultUserAgent = `DiscordBot (https://discord.js.org, 3.0.0-dev.1786752260-c9855614c)`;
147
+ var DefaultUserAgent = `DiscordBot (https://discord.js.org, 3.0.0-dev.1786968995-99af6d8be)`;
148
148
  var DefaultUserAgentAppendix = (0, import_util.getUserAgentAppendix)();
149
149
  var DefaultRestOptions = {
150
150
  agent: null,
@@ -155,7 +155,7 @@ var DefaultRestOptions = {
155
155
  invalidRequestWarningInterval: 0,
156
156
  globalRequestsPerSecond: 50,
157
157
  offset: 50,
158
- rejectOnRateLimit: null,
158
+ rejectOnRateLimit: false,
159
159
  retries: 3,
160
160
  retryBackoff: 0,
161
161
  timeout: 15e3,
@@ -675,11 +675,10 @@ function shouldRetry(error) {
675
675
  return "code" in error && error.code === "ECONNRESET" || error.message.includes("ECONNRESET");
676
676
  }
677
677
  __name(shouldRetry, "shouldRetry");
678
- async function onRateLimit(manager, rateLimitData) {
679
- const { options } = manager;
680
- if (!options.rejectOnRateLimit) return;
681
- const shouldThrow = typeof options.rejectOnRateLimit === "function" ? await options.rejectOnRateLimit(rateLimitData) : options.rejectOnRateLimit.some((route) => rateLimitData.route.startsWith(route.toLowerCase()));
682
- if (shouldThrow) {
678
+ async function onRateLimit(manager, rateLimitData, requestData) {
679
+ const policy = requestData.rejectOnRateLimit ?? manager.options.rejectOnRateLimit;
680
+ if (!policy) return;
681
+ if (policy === true || await policy(rateLimitData)) {
683
682
  throw new RateLimitError(rateLimitData);
684
683
  }
685
684
  }
@@ -908,19 +907,23 @@ var BurstHandler = class {
908
907
  } else if (status === 429) {
909
908
  const isGlobal = res.headers.has("X-RateLimit-Global");
910
909
  const scope = res.headers.get("X-RateLimit-Scope") ?? "user";
911
- await onRateLimit(this.manager, {
912
- global: isGlobal,
913
- method,
914
- url,
915
- route: routeId.bucketRoute,
916
- majorParameter: this.majorParameter,
917
- hash: this.hash,
918
- limit: Number.POSITIVE_INFINITY,
919
- timeToReset: retryAfter,
920
- retryAfter,
921
- sublimitTimeout: 0,
922
- scope
923
- });
910
+ await onRateLimit(
911
+ this.manager,
912
+ {
913
+ global: isGlobal,
914
+ method,
915
+ url,
916
+ route: routeId.bucketRoute,
917
+ majorParameter: this.majorParameter,
918
+ hash: this.hash,
919
+ limit: Number.POSITIVE_INFINITY,
920
+ timeToReset: retryAfter,
921
+ retryAfter,
922
+ sublimitTimeout: 0,
923
+ scope
924
+ },
925
+ requestData
926
+ );
924
927
  this.debug(
925
928
  [
926
929
  "Encountered unexpected 429 rate limit",
@@ -1122,7 +1125,7 @@ var SequentialHandler = class {
1122
1125
  scope: "user"
1123
1126
  };
1124
1127
  this.manager.emit("rateLimited" /* RateLimited */, rateLimitData);
1125
- await onRateLimit(this.manager, rateLimitData);
1128
+ await onRateLimit(this.manager, rateLimitData, requestData);
1126
1129
  if (isGlobal) {
1127
1130
  this.debug(`Global rate limit hit, blocking all requests for ${timeout}ms`);
1128
1131
  } else {
@@ -1193,19 +1196,23 @@ var SequentialHandler = class {
1193
1196
  limit2 = this.limit;
1194
1197
  timeout = this.getTimeToReset(routeId);
1195
1198
  }
1196
- await onRateLimit(this.manager, {
1197
- global: isGlobal,
1198
- method,
1199
- url,
1200
- route: routeId.bucketRoute,
1201
- majorParameter: this.majorParameter,
1202
- hash: this.hash,
1203
- limit: limit2,
1204
- timeToReset: timeout,
1205
- retryAfter,
1206
- sublimitTimeout: sublimitTimeout ?? 0,
1207
- scope
1208
- });
1199
+ await onRateLimit(
1200
+ this.manager,
1201
+ {
1202
+ global: isGlobal,
1203
+ method,
1204
+ url,
1205
+ route: routeId.bucketRoute,
1206
+ majorParameter: this.majorParameter,
1207
+ hash: this.hash,
1208
+ limit: limit2,
1209
+ timeToReset: timeout,
1210
+ retryAfter,
1211
+ sublimitTimeout: sublimitTimeout ?? 0,
1212
+ scope
1213
+ },
1214
+ requestData
1215
+ );
1209
1216
  this.debug(
1210
1217
  [
1211
1218
  "Encountered unexpected 429 rate limit",
@@ -1426,6 +1433,7 @@ var REST = class _REST extends import_async_event_emitter.AsyncEventEmitter {
1426
1433
  body: request2.body,
1427
1434
  files: request2.files,
1428
1435
  auth,
1436
+ rejectOnRateLimit: request2.rejectOnRateLimit,
1429
1437
  signal: request2.signal
1430
1438
  });
1431
1439
  }
@@ -1570,7 +1578,7 @@ var REST = class _REST extends import_async_event_emitter.AsyncEventEmitter {
1570
1578
  };
1571
1579
 
1572
1580
  // src/shared.ts
1573
- var version = "3.0.0-dev.1786752260-c9855614c";
1581
+ var version = "3.0.0-dev.1786968995-99af6d8be";
1574
1582
 
1575
1583
  // src/index.ts
1576
1584
  setDefaultStrategy((0, import_util2.shouldUseGlobalFetchAndWebSocket)() ? fetch : makeRequest);