Files
discordeno/rest/runMethod.ts
ITOH b00504ce07 perf(plugins/helpers,rest,site,template)!: uppercase rest methods (#2252)
* perf(plugins/helpers,rest,site,template)!: uppercase rest methods
There is no need for us to pass the methods in lower case and then use the `toUpperCase()` method everywhere. Therefore this PR changes every lowercase method to be uppercase (eg. `"get"` => `"GET"`).

* template is old version
2022-05-25 20:45:51 +02:00

77 lines
2.4 KiB
TypeScript

import { RestManager } from "../bot.ts";
import { API_VERSION, BASE_URL, baseEndpoints, IMAGE_BASE_URL } from "../util/constants.ts";
import { RestRequestRejection, RestRequestResponse } from "./rest.ts";
export async function runMethod<T = any>(
rest: RestManager,
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH",
route: string,
body?: unknown,
options?: {
retryCount?: number;
bucketId?: string;
headers?: Record<string, string>;
},
): Promise<T> {
rest.debug(
`[REST - RequestCreate] Method: ${method} | URL: ${route} | Retry Count: ${
options?.retryCount ?? 0
} | Bucket ID: ${options?.bucketId} | Body: ${
JSON.stringify(
body,
)
}`,
);
const errorStack = new Error("Location:");
// @ts-ignore Breaks deno deploy. Luca said add ts-ignore 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 (!baseEndpoints.BASE_URL.startsWith(BASE_URL) && route[0] === "/") {
const result = await fetch(`${baseEndpoints.BASE_URL}${route}`, {
body: body ? JSON.stringify(body) : undefined,
headers: {
Authorization: rest.secretKey,
"Content-Type": "application/json",
},
method,
}).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: route[0] === "/" ? `${BASE_URL}/v${API_VERSION}${route}` : route,
method,
reject: (data: RestRequestRejection) => {
const restError = rest.convertRestError(errorStack, data);
reject(restError);
},
respond: (data: RestRequestResponse) =>
resolve(data.status !== 204 ? JSON.parse(data.body ?? "{}") : (undefined as unknown as T)),
},
{
bucketId: options?.bucketId,
body: body as Record<string, unknown> | undefined,
retryCount: options?.retryCount ?? 0,
headers: options?.headers,
},
);
});
}