From dfe449c253b617e8f92c720a2f71135aa1601a65 Mon Sep 17 00:00:00 2001 From: DD Date: Tue, 17 May 2022 17:31:19 +0300 Subject: [PATCH] feat: REST#raw (#7929) --- packages/rest/src/index.ts | 2 +- packages/rest/src/lib/REST.ts | 12 +++++++++++- packages/rest/src/lib/RequestManager.ts | 2 +- packages/rest/src/lib/handlers/IHandler.ts | 3 ++- packages/rest/src/lib/handlers/SequentialHandler.ts | 8 ++++---- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/rest/src/index.ts b/packages/rest/src/index.ts index 1522bd259ce2..eee7ee0c1791 100644 --- a/packages/rest/src/index.ts +++ b/packages/rest/src/index.ts @@ -5,4 +5,4 @@ export * from './lib/errors/RateLimitError'; export * from './lib/RequestManager'; export * from './lib/REST'; export * from './lib/utils/constants'; -export { makeURLSearchParams } from './lib/utils/utils'; +export { makeURLSearchParams, parseResponse } from './lib/utils/utils'; diff --git a/packages/rest/src/lib/REST.ts b/packages/rest/src/lib/REST.ts index 2d75112777f8..cf763843b2e7 100644 --- a/packages/rest/src/lib/REST.ts +++ b/packages/rest/src/lib/REST.ts @@ -13,6 +13,7 @@ import { import type { HashData } from './RequestManager'; import type { IHandler } from './handlers/IHandler'; import { DefaultRestOptions, RESTEvents } from './utils/constants'; +import { parseResponse } from './utils/utils'; /** * Options to be passed when creating the REST instance @@ -314,7 +315,16 @@ export class REST extends EventEmitter { * Runs a request from the api * @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); } } diff --git a/packages/rest/src/lib/RequestManager.ts b/packages/rest/src/lib/RequestManager.ts index ccfc9ce8d3a8..7d0bd1da6d3b 100644 --- a/packages/rest/src/lib/RequestManager.ts +++ b/packages/rest/src/lib/RequestManager.ts @@ -294,7 +294,7 @@ export class RequestManager extends EventEmitter { * @param request All the information needed to make a request * @returns The response from the api request */ - public async queueRequest(request: InternalRequest): Promise { + public async queueRequest(request: InternalRequest): Promise { // Generalize the endpoint to its route data const routeId = RequestManager.generateRouteData(request.fullRoute, request.method); // Get the bucket hash for the generic route, or point to a global route otherwise diff --git a/packages/rest/src/lib/handlers/IHandler.ts b/packages/rest/src/lib/handlers/IHandler.ts index 811bf497643b..840dcccbba8b 100644 --- a/packages/rest/src/lib/handlers/IHandler.ts +++ b/packages/rest/src/lib/handlers/IHandler.ts @@ -1,3 +1,4 @@ +import type { Dispatcher } from 'undici'; import type { RequestOptions } from '../REST'; import type { HandlerRequestData, RouteData } from '../RequestManager'; @@ -7,7 +8,7 @@ export interface IHandler { url: string, options: RequestOptions, requestData: HandlerRequestData, - ) => Promise; + ) => Promise; // eslint-disable-next-line @typescript-eslint/method-signature-style -- This is meant to be a getter returning a bool get inactive(): boolean; readonly id: string; diff --git a/packages/rest/src/lib/handlers/SequentialHandler.ts b/packages/rest/src/lib/handlers/SequentialHandler.ts index 90abd1915dc0..f6de02fe6b93 100644 --- a/packages/rest/src/lib/handlers/SequentialHandler.ts +++ b/packages/rest/src/lib/handlers/SequentialHandler.ts @@ -170,7 +170,7 @@ export class SequentialHandler implements IHandler { url: string, options: RequestOptions, requestData: HandlerRequestData, - ): Promise { + ): Promise { let queue = this.#asyncQueue; let queueType = QueueType.Standard; // Separate sublimited requests when already sublimited @@ -228,7 +228,7 @@ export class SequentialHandler implements IHandler { options: RequestOptions, requestData: HandlerRequestData, retries = 0, - ): Promise { + ): Promise { /* * 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 @@ -392,7 +392,7 @@ export class SequentialHandler implements IHandler { } if (status >= 200 && status < 300) { - return parseResponse(res); + return res; } 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 const isGlobal = this.globalLimited; @@ -474,7 +474,7 @@ export class SequentialHandler implements IHandler { // throw the API error throw new DiscordAPIError(data, 'code' in data ? data.code : data.error, status, method, url, requestData); } - return null; + return res; } } }