mirror of
https://github.com/discordeno/discordeno.git
synced 2026-05-28 14:30:11 +00:00
* fix: check new types idea * fix: type errors * fix: new style * fix: more cleanup * fix: more cleanup * fix: cleanup audit logs * fix: cleanup stickers * fix: cleanup integrations * fix: more cleanup * fix: organize into 1 place * fix: few errors * fix: some broken import fixes * fix: quite a lot of fixes across the board * fix: more fixes for broken imports * fix: more fixes for broken imports * fix: handler imports * fix: all remaining import errors * fix: more errors needing fixes * fix: clearing up transformers * fix: few moer types * fix: more cleanup of extra types * fix: fmt * fix: cleanup discordeno file * Nuke Base Types (#2102) * fix: cleanup snake stuff * convert camelCase to snake_case (#2103) * fix: add camelize * fix: finalize remaining errors * fix: imports in test Co-authored-by: LTS20050703 <87189679+lts20050703@users.noreply.github.com>
70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
import { RestManager } from "../bot.ts";
|
|
import { API_VERSION, BASE_URL, IMAGE_BASE_URL } from "../util/constants.ts";
|
|
|
|
export async function runMethod<T = any>(
|
|
rest: RestManager,
|
|
method: "get" | "post" | "put" | "delete" | "patch",
|
|
url: string,
|
|
body?: unknown,
|
|
retryCount = 0,
|
|
bucketId?: string,
|
|
): Promise<T> {
|
|
rest.debug(
|
|
`[REST - RequestCreate] Method: ${method} | URL: ${url} | Retry Count: ${retryCount} | Bucket ID: ${bucketId} | Body: ${
|
|
JSON.stringify(
|
|
body,
|
|
)
|
|
}`,
|
|
);
|
|
|
|
const errorStack = new Error("Location:");
|
|
// @ts-ignore Breaks deno deploy. Luca said add tsignore until it's fixed
|
|
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: body ? JSON.stringify(body) : undefined,
|
|
headers: {
|
|
Authorization: rest.secretKey,
|
|
"Content-Type": "application/json",
|
|
},
|
|
method: method.toUpperCase(),
|
|
}).catch((error) => {
|
|
errorStack.message = (error as Error)?.message;
|
|
console.error(error);
|
|
throw errorStack;
|
|
});
|
|
|
|
if (!result.ok) {
|
|
errorStack.message = result.statusText as Error["message"];
|
|
console.error(`Error: ${errorStack.message}`);
|
|
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(
|
|
rest,
|
|
{
|
|
url,
|
|
method,
|
|
reject: (error: unknown) => {
|
|
errorStack.message = (error as Error)?.message;
|
|
reject(errorStack);
|
|
},
|
|
respond: (data: { status: number; body?: string }) =>
|
|
resolve(data.status !== 204 ? JSON.parse(data.body ?? "{}") : (undefined as unknown as T)),
|
|
},
|
|
{
|
|
bucketId,
|
|
body: body as Record<string, unknown> | undefined,
|
|
retryCount,
|
|
},
|
|
);
|
|
});
|
|
}
|