mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 01:23:31 +01:00
chore: monorepo setup (#7175)
This commit is contained in:
41
packages/rest/src/lib/utils/constants.ts
Normal file
41
packages/rest/src/lib/utils/constants.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { APIVersion } from 'discord-api-types/v9';
|
||||
import type { RESTOptions } from '../REST';
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
|
||||
const Package = require('../../../package.json');
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
||||
export const DefaultUserAgent = `DiscordBot (${Package.homepage}, ${Package.version})`;
|
||||
|
||||
export const DefaultRestOptions: Required<RESTOptions> = {
|
||||
agent: {},
|
||||
api: 'https://discord.com/api',
|
||||
cdn: 'https://cdn.discordapp.com',
|
||||
headers: {},
|
||||
invalidRequestWarningInterval: 0,
|
||||
globalRequestsPerSecond: 50,
|
||||
offset: 50,
|
||||
rejectOnRateLimit: null,
|
||||
retries: 3,
|
||||
timeout: 15_000,
|
||||
userAgentAppendix: `Node.js ${process.version}`,
|
||||
version: APIVersion,
|
||||
};
|
||||
|
||||
/**
|
||||
* The events that the REST manager emits
|
||||
*/
|
||||
export const enum RESTEvents {
|
||||
Debug = 'restDebug',
|
||||
InvalidRequestWarning = 'invalidRequestWarning',
|
||||
RateLimited = 'rateLimited',
|
||||
Request = 'request',
|
||||
Response = 'response',
|
||||
}
|
||||
|
||||
export const ALLOWED_EXTENSIONS = ['webp', 'png', 'jpg', 'jpeg', 'gif'] as const;
|
||||
export const ALLOWED_STICKER_EXTENSIONS = ['png', 'json'] as const;
|
||||
export const ALLOWED_SIZES = [16, 32, 64, 128, 256, 512, 1024, 2048, 4096] as const;
|
||||
|
||||
export type ImageExtension = typeof ALLOWED_EXTENSIONS[number];
|
||||
export type StickerExtension = typeof ALLOWED_STICKER_EXTENSIONS[number];
|
||||
export type ImageSize = typeof ALLOWED_SIZES[number];
|
||||
37
packages/rest/src/lib/utils/utils.ts
Normal file
37
packages/rest/src/lib/utils/utils.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import type { RESTPatchAPIChannelJSONBody } from 'discord-api-types/v9';
|
||||
import type { Response } from 'node-fetch';
|
||||
import { RequestMethod } from '../RequestManager';
|
||||
|
||||
/**
|
||||
* Converts the response to usable data
|
||||
* @param res The node-fetch response
|
||||
*/
|
||||
export function parseResponse(res: Response): Promise<unknown> {
|
||||
if (res.headers.get('Content-Type')?.startsWith('application/json')) {
|
||||
return res.json();
|
||||
}
|
||||
|
||||
return res.buffer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a request falls under a sublimit
|
||||
* @param bucketRoute The buckets route identifier
|
||||
* @param body The options provided as JSON data
|
||||
* @param method The HTTP method that will be used to make the request
|
||||
* @returns Whether the request falls under a sublimit
|
||||
*/
|
||||
export function hasSublimit(bucketRoute: string, body?: unknown, method?: string): boolean {
|
||||
// TODO: Update for new sublimits
|
||||
// Currently known sublimits:
|
||||
// Editing channel `name` or `topic`
|
||||
if (bucketRoute === '/channels/:id') {
|
||||
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;
|
||||
const castedBody = body as RESTPatchAPIChannelJSONBody;
|
||||
return ['name', 'topic'].some((key) => Reflect.has(castedBody, key));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user