chore: run format

This commit is contained in:
Vlad Frangu
2025-10-05 16:13:56 +03:00
parent 2a712d4909
commit 8dc1692d87
189 changed files with 3172 additions and 916 deletions

View File

@@ -93,8 +93,14 @@ describe('makeURLSearchParams', () => {
describe('option normalization functions', () => {
describe('rate limit offset', () => {
const func: GetRateLimitOffsetFunction = (route) => {
if (route === '/negative') return -150;
if (route === '/high') return 150;
if (route === '/negative') {
return -150;
}
if (route === '/high') {
return 150;
}
return 50;
};
@@ -116,11 +122,26 @@ describe('option normalization functions', () => {
content: 'yo',
};
const func: GetRetryBackoffFunction = (_route, statusCode, retryCount) => {
if (statusCode === null) return 0;
if (statusCode === 502) return 50;
if (retryCount === 0) return 0;
if (retryCount === 1) return 150;
if (retryCount === 2) return 500;
if (statusCode === null) {
return 0;
}
if (statusCode === 502) {
return 50;
}
if (retryCount === 0) {
return 0;
}
if (retryCount === 1) {
return 150;
}
if (retryCount === 2) {
return 500;
}
return null;
};
@@ -161,8 +182,14 @@ describe('option normalization functions', () => {
return 1_000;
}
if (route === '/negative') return -150;
if (route === '/high') return 150;
if (route === '/negative') {
return -150;
}
if (route === '/high') {
return 150;
}
return 50;
};

View File

@@ -104,7 +104,9 @@ export class REST extends AsyncEventEmitter<RestEvents> {
// Begin sweeping hash based on lifetimes
this.hashes.sweep((val, key) => {
// `-1` indicates a global hash
if (val.lastAccess === -1) return false;
if (val.lastAccess === -1) {
return false;
}
// Check if lifetime has been exceeded
const shouldSweep = Math.floor(currentDate - val.lastAccess) > this.options.hashLifetime;

View File

@@ -91,7 +91,9 @@ export class BurstHandler implements IHandler {
// Amount of time in milliseconds until we should retry if rate limited (globally or otherwise)
const offset = normalizeRateLimitOffset(this.manager.options.offset, routeId.bucketRoute);
if (retry) retryAfter = Number(retry) * 1_000 + offset;
if (retry) {
retryAfter = Number(retry) * 1_000 + offset;
}
// Count the invalid requests
if (status === 401 || status === 403 || status === 429) {

View File

@@ -298,7 +298,9 @@ export class SequentialHandler implements IHandler {
this.reset = reset ? Number(reset) * 1_000 + Date.now() + offset : Date.now();
// Amount of time in milliseconds until we should retry if rate limited (globally or otherwise)
if (retry) retryAfter = Number(retry) * 1_000 + offset;
if (retry) {
retryAfter = Number(retry) * 1_000 + offset;
}
// Handle buckets via the hash header retroactively
if (hash && hash !== this.hash) {

View File

@@ -73,15 +73,21 @@ export async function makeNetworkRequest(
// If the user signal was aborted, abort the controller, else abort the local signal.
// The reason why we don't re-use the user's signal, is because users may use the same signal for multiple
// requests, and we do not want to cause unexpected side-effects.
if (requestData.signal.aborted) controller.abort();
else requestData.signal.addEventListener('abort', () => controller.abort());
if (requestData.signal.aborted) {
controller.abort();
} else {
requestData.signal.addEventListener('abort', () => controller.abort());
}
}
let res: ResponseLike;
try {
res = await manager.options.makeRequest(url, { ...options, signal: controller.signal });
} catch (error: unknown) {
if (!(error instanceof Error)) throw error;
if (!(error instanceof Error)) {
throw error;
}
// Retry the specified number of times if needed
if (shouldRetry(error) && retries !== manager.options.retries) {
const backoff = normalizeRetryBackoff(

View File

@@ -20,13 +20,19 @@ function serializeSearchParam(value: unknown): string | null {
case 'boolean':
return value.toString();
case 'object':
if (value === null) return null;
if (value === null) {
return null;
}
if (value instanceof Date) {
return Number.isNaN(value.getTime()) ? null : value.toISOString();
}
// eslint-disable-next-line @typescript-eslint/no-base-to-string
if (typeof value.toString === 'function' && value.toString !== Object.prototype.toString) return value.toString();
if (typeof value.toString === 'function' && value.toString !== Object.prototype.toString) {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
return value.toString();
}
return null;
default:
return null;
@@ -42,11 +48,15 @@ function serializeSearchParam(value: unknown): string | null {
*/
export function makeURLSearchParams<OptionsType extends object>(options?: Readonly<OptionsType>) {
const params = new URLSearchParams();
if (!options) return params;
if (!options) {
return params;
}
for (const [key, value] of Object.entries(options)) {
const serialized = serializeSearchParam(value);
if (serialized !== null) params.append(key, serialized);
if (serialized !== null) {
params.append(key, serialized);
}
}
return params;
@@ -78,9 +88,15 @@ export function hasSublimit(bucketRoute: string, body?: unknown, method?: string
// Currently known sublimits:
// Editing channel `name` or `topic`
if (bucketRoute === '/channels/:id') {
if (typeof body !== 'object' || body === null) return false;
if (typeof body !== 'object' || body === null) {
return false;
}
// This should never be a POST body, but just in case
if (method !== RequestMethod.Patch) return false;
if (method !== RequestMethod.Patch) {
return false;
}
const castedBody = body as RESTPatchAPIChannelJSONBody;
return ['name', 'topic'].some((key) => Reflect.has(castedBody, key));
}
@@ -97,7 +113,10 @@ export function hasSublimit(bucketRoute: string, body?: unknown, method?: string
*/
export function shouldRetry(error: Error | NodeJS.ErrnoException) {
// Retry for possible timed out requests
if (error.name === 'AbortError') return true;
if (error.name === 'AbortError') {
return true;
}
// Downlevel ECONNRESET to retry as it may be recoverable
return ('code' in error && error.code === 'ECONNRESET') || error.message.includes('ECONNRESET');
}
@@ -109,7 +128,9 @@ export function shouldRetry(error: Error | NodeJS.ErrnoException) {
*/
export async function onRateLimit(manager: REST, rateLimitData: RateLimitData) {
const { options } = manager;
if (!options.rejectOnRateLimit) return;
if (!options.rejectOnRateLimit) {
return;
}
const shouldThrow =
typeof options.rejectOnRateLimit === 'function'