mirror of
https://github.com/discordeno/discordeno.git
synced 2026-09-17 16:57:22 +00:00
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
import { API_VERSION, BASE_URL, IMAGE_BASE_URL } from "../util/constants.ts";
|
|
import { rest } from "./rest.ts";
|
|
|
|
export async function runMethod<T = any>(
|
|
method: "get" | "post" | "put" | "delete" | "patch",
|
|
url: string,
|
|
body?: unknown,
|
|
retryCount = 0,
|
|
bucketId?: string
|
|
): Promise<T> {
|
|
rest.eventHandlers.debug?.("requestCreate", {
|
|
method,
|
|
url,
|
|
body,
|
|
retryCount,
|
|
bucketId,
|
|
});
|
|
|
|
const errorStack = new Error("Location:");
|
|
Error.captureStackTrace(errorStack);
|
|
|
|
// For proxies we don't need to do any of the legwork so we just forward the request
|
|
if (
|
|
!url.startsWith(`${BASE_URL}/v${API_VERSION}`) &&
|
|
!url.startsWith(IMAGE_BASE_URL)
|
|
) {
|
|
const result = await fetch(url, {
|
|
body: JSON.stringify(body || {}),
|
|
headers: {
|
|
authorization: rest.authorization,
|
|
},
|
|
method: method.toUpperCase(),
|
|
}).catch((error) => {
|
|
console.error(error);
|
|
throw errorStack;
|
|
});
|
|
|
|
return result.status !== 204 ? await result.json() : undefined;
|
|
}
|
|
|
|
// No proxy so we need to handle all rate limiting and such
|
|
return new Promise((resolve, reject) => {
|
|
rest.processRequest(
|
|
{
|
|
url,
|
|
method,
|
|
reject,
|
|
respond: (data: { status: number; body?: string }) =>
|
|
resolve(JSON.parse(data.body || "{}")),
|
|
},
|
|
{
|
|
bucketId,
|
|
body: body as Record<string, unknown> | undefined,
|
|
retryCount,
|
|
}
|
|
);
|
|
});
|
|
}
|