From cddf40e2992d59f80c50132aa9cdc2f29b07199c Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Fri, 9 Apr 2021 13:11:12 +0000 Subject: [PATCH] callback is useless --- src/rest/run_method.ts | 124 ++++++----------------------------------- 1 file changed, 17 insertions(+), 107 deletions(-) diff --git a/src/rest/run_method.ts b/src/rest/run_method.ts index 06249e300..040cd824b 100644 --- a/src/rest/run_method.ts +++ b/src/rest/run_method.ts @@ -1,4 +1,3 @@ -import { Errors } from "../types/misc/errors.ts"; import { API_VERSION, BASE_URL, IMAGE_BASE_URL } from "../util/constants.ts"; import { rest } from "./rest.ts"; @@ -7,7 +6,7 @@ export function runMethod( url: string, body?: unknown, retryCount = 0, - bucketId?: string | null, + bucketId?: string | null ): Promise { rest.eventHandlers.debug?.("requestCreate", { method, @@ -35,7 +34,7 @@ export function runMethod( .then((res) => { if (res.status === 204) return undefined; - return res.json() as unknown as T; + return (res.json() as unknown) as T; }) .catch((error) => { console.error(error); @@ -45,110 +44,21 @@ export function runMethod( // No proxy so we need to handle all rate limiting and such return new Promise((resolve, reject) => { - const callback = async () => { - try { - const rateLimitResetIn = rest.checkRateLimits(url); - if (rateLimitResetIn) { - return { rateLimited: rateLimitResetIn, beforeFetch: true, bucketId }; - } - - const query = method === "get" && body - ? // deno-lint-ignore no-explicit-any - Object.entries(body as any) - .map( - ([key, value]) => - `${encodeURIComponent(key)}=${ - encodeURIComponent( - value as string | number | boolean, - ) - }`, - ) - .join("&") - : ""; - const urlToUse = method === "get" && query ? `${url}?${query}` : url; - - rest.eventHandlers.debug?.("requestFetch", { - method, - url, - body, - retryCount, - bucketId, - }); - const response = await fetch( - urlToUse, - rest.createRequestBody(body, method), - ); - rest.eventHandlers.debug?.("requestFetched", { - method, - url, - body, - retryCount, - bucketId, - response, - }); - const bucketIdFromHeaders = rest.processRequestHeaders( - url, - response.headers, - ); - await rest.handleStatusCode(response, errorStack); - - // Sometimes Discord returns an empty 204 response that can't be made to JSON. - if (response.status === 204) return resolve(undefined); - - const json = await response.json(); - if ( - json.retry_after || - json.message === "You are being rate limited." - ) { - if (retryCount > 10) { - rest.eventHandlers.error?.("globalRateLimit", { - method, - url, - body, - retryCount, - bucketId, - errorStack, - }); - throw new Error(Errors.RATE_LIMIT_RETRY_MAXED); - } - - return { - rateLimited: json.retry_after, - beforeFetch: false, - bucketId: bucketIdFromHeaders, - }; - } - - rest.eventHandlers.debug?.("requestSuccess", { - method, - url, - body, - retryCount, - bucketId, - }); - return resolve(json); - } catch (error) { - rest.eventHandlers.error?.("unknown", { - method, - url, - body, - retryCount, - bucketId, - errorStack, - }); - return reject(error); + rest.processRequest( + { + url, + method, + reject, + respond: (data: { status: number; body?: string }) => + resolve(JSON.parse(data.body || "{}")), + }, + { + bucketId, + url, + method, + body, + retryCount, } - }; - - rest.processRequest({ - url, - method, - respond: (data: { status: number; body?: string }) => - resolve(JSON.parse(data.body || "{}")), - }, { - callback, - bucketId, - url, - }); + ); }); }