callback is useless

This commit is contained in:
Skillz4Killz
2021-04-09 13:11:12 +00:00
committed by GitHub
parent b4d7eaf247
commit cddf40e299

View File

@@ -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<T = any>(
url: string,
body?: unknown,
retryCount = 0,
bucketId?: string | null,
bucketId?: string | null
): Promise<T | undefined> {
rest.eventHandlers.debug?.("requestCreate", {
method,
@@ -35,7 +34,7 @@ export function runMethod<T = any>(
.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<T = any>(
// 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,
});
);
});
}