mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 11:33:30 +01:00
feat: REST#raw (#7929)
This commit is contained in:
@@ -5,4 +5,4 @@ export * from './lib/errors/RateLimitError';
|
|||||||
export * from './lib/RequestManager';
|
export * from './lib/RequestManager';
|
||||||
export * from './lib/REST';
|
export * from './lib/REST';
|
||||||
export * from './lib/utils/constants';
|
export * from './lib/utils/constants';
|
||||||
export { makeURLSearchParams } from './lib/utils/utils';
|
export { makeURLSearchParams, parseResponse } from './lib/utils/utils';
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
import type { HashData } from './RequestManager';
|
import type { HashData } from './RequestManager';
|
||||||
import type { IHandler } from './handlers/IHandler';
|
import type { IHandler } from './handlers/IHandler';
|
||||||
import { DefaultRestOptions, RESTEvents } from './utils/constants';
|
import { DefaultRestOptions, RESTEvents } from './utils/constants';
|
||||||
|
import { parseResponse } from './utils/utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Options to be passed when creating the REST instance
|
* Options to be passed when creating the REST instance
|
||||||
@@ -314,7 +315,16 @@ export class REST extends EventEmitter {
|
|||||||
* Runs a request from the api
|
* Runs a request from the api
|
||||||
* @param options Request options
|
* @param options Request options
|
||||||
*/
|
*/
|
||||||
public request(options: InternalRequest) {
|
public async request(options: InternalRequest) {
|
||||||
|
const response = await this.raw(options);
|
||||||
|
return parseResponse(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs a request from the API, yielding the raw Response object
|
||||||
|
* @param options Request options
|
||||||
|
*/
|
||||||
|
public raw(options: InternalRequest) {
|
||||||
return this.requestManager.queueRequest(options);
|
return this.requestManager.queueRequest(options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -294,7 +294,7 @@ export class RequestManager extends EventEmitter {
|
|||||||
* @param request All the information needed to make a request
|
* @param request All the information needed to make a request
|
||||||
* @returns The response from the api request
|
* @returns The response from the api request
|
||||||
*/
|
*/
|
||||||
public async queueRequest(request: InternalRequest): Promise<unknown> {
|
public async queueRequest(request: InternalRequest): Promise<Dispatcher.ResponseData> {
|
||||||
// Generalize the endpoint to its route data
|
// Generalize the endpoint to its route data
|
||||||
const routeId = RequestManager.generateRouteData(request.fullRoute, request.method);
|
const routeId = RequestManager.generateRouteData(request.fullRoute, request.method);
|
||||||
// Get the bucket hash for the generic route, or point to a global route otherwise
|
// Get the bucket hash for the generic route, or point to a global route otherwise
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import type { Dispatcher } from 'undici';
|
||||||
import type { RequestOptions } from '../REST';
|
import type { RequestOptions } from '../REST';
|
||||||
import type { HandlerRequestData, RouteData } from '../RequestManager';
|
import type { HandlerRequestData, RouteData } from '../RequestManager';
|
||||||
|
|
||||||
@@ -7,7 +8,7 @@ export interface IHandler {
|
|||||||
url: string,
|
url: string,
|
||||||
options: RequestOptions,
|
options: RequestOptions,
|
||||||
requestData: HandlerRequestData,
|
requestData: HandlerRequestData,
|
||||||
) => Promise<unknown>;
|
) => Promise<Dispatcher.ResponseData>;
|
||||||
// eslint-disable-next-line @typescript-eslint/method-signature-style -- This is meant to be a getter returning a bool
|
// eslint-disable-next-line @typescript-eslint/method-signature-style -- This is meant to be a getter returning a bool
|
||||||
get inactive(): boolean;
|
get inactive(): boolean;
|
||||||
readonly id: string;
|
readonly id: string;
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ export class SequentialHandler implements IHandler {
|
|||||||
url: string,
|
url: string,
|
||||||
options: RequestOptions,
|
options: RequestOptions,
|
||||||
requestData: HandlerRequestData,
|
requestData: HandlerRequestData,
|
||||||
): Promise<unknown> {
|
): Promise<Dispatcher.ResponseData> {
|
||||||
let queue = this.#asyncQueue;
|
let queue = this.#asyncQueue;
|
||||||
let queueType = QueueType.Standard;
|
let queueType = QueueType.Standard;
|
||||||
// Separate sublimited requests when already sublimited
|
// Separate sublimited requests when already sublimited
|
||||||
@@ -228,7 +228,7 @@ export class SequentialHandler implements IHandler {
|
|||||||
options: RequestOptions,
|
options: RequestOptions,
|
||||||
requestData: HandlerRequestData,
|
requestData: HandlerRequestData,
|
||||||
retries = 0,
|
retries = 0,
|
||||||
): Promise<unknown> {
|
): Promise<Dispatcher.ResponseData> {
|
||||||
/*
|
/*
|
||||||
* After calculations have been done, pre-emptively stop further requests
|
* After calculations have been done, pre-emptively stop further requests
|
||||||
* Potentially loop until this task can run if e.g. the global rate limit is hit twice
|
* Potentially loop until this task can run if e.g. the global rate limit is hit twice
|
||||||
@@ -392,7 +392,7 @@ export class SequentialHandler implements IHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (status >= 200 && status < 300) {
|
if (status >= 200 && status < 300) {
|
||||||
return parseResponse(res);
|
return res;
|
||||||
} else if (status === 429) {
|
} else if (status === 429) {
|
||||||
// A rate limit was hit - this may happen if the route isn't associated with an official bucket hash yet, or when first globally rate limited
|
// A rate limit was hit - this may happen if the route isn't associated with an official bucket hash yet, or when first globally rate limited
|
||||||
const isGlobal = this.globalLimited;
|
const isGlobal = this.globalLimited;
|
||||||
@@ -474,7 +474,7 @@ export class SequentialHandler implements IHandler {
|
|||||||
// throw the API error
|
// throw the API error
|
||||||
throw new DiscordAPIError(data, 'code' in data ? data.code : data.error, status, method, url, requestData);
|
throw new DiscordAPIError(data, 'code' in data ? data.code : data.error, status, method, url, requestData);
|
||||||
}
|
}
|
||||||
return null;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user