mirror of
https://github.com/discordeno/discordeno.git
synced 2026-09-17 08:47:22 +00:00
feat: Modified processGlobalQueue to be able to handle results in runMethod (#2101)
* feat: Modified processGlobalQueue to be able to handle results in runMethod * Apply suggestions from code review, step 1 Co-authored-by: ITOH <to@itoh.at> * Apply suggestions from code review, step 2 * Update Files * Update to resolve/reject * Fixes & deno fmt Co-authored-by: ITOH <to@itoh.at> Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>
This commit is contained in:
co-authored by
ITOH
Skillz4Killz
parent
f94b6f35bb
commit
88f7529dc4
@@ -118,18 +118,27 @@ export async function processGlobalQueue(rest: RestManager) {
|
|||||||
|
|
||||||
// If NOT rate limited remove from queue
|
// If NOT rate limited remove from queue
|
||||||
if (response.status !== 429) {
|
if (response.status !== 429) {
|
||||||
|
let json = undefined;
|
||||||
if (response.type) {
|
if (response.type) {
|
||||||
console.log(JSON.stringify(await response.json()));
|
json = JSON.stringify(await response.json());
|
||||||
|
console.log(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
request.request.reject(new Error(`[${response.status}] ${error}`));
|
request.request.reject({
|
||||||
|
ok: false,
|
||||||
|
status: response.status,
|
||||||
|
error,
|
||||||
|
body: json,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
if (request.payload.retryCount++ >= rest.maxRetryCount) {
|
if (request.payload.retryCount++ >= rest.maxRetryCount) {
|
||||||
rest.debug(`[REST - RetriesMaxed] ${JSON.stringify(request.payload)}`);
|
rest.debug(`[REST - RetriesMaxed] ${JSON.stringify(request.payload)}`);
|
||||||
// REMOVE ITEM FROM QUEUE TO PREVENT RETRY
|
// REMOVE ITEM FROM QUEUE TO PREVENT RETRY
|
||||||
request.request.reject(
|
request.request.reject({
|
||||||
new Error(`[${response.status}] The request was rate limited and it maxed out the retries limit.`),
|
ok: false,
|
||||||
);
|
status: response.status,
|
||||||
|
error: "The request was rate limited and it maxed out the retries limit.",
|
||||||
|
});
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,21 +152,29 @@ export async function processGlobalQueue(rest: RestManager) {
|
|||||||
// SOMETIMES DISCORD RETURNS AN EMPTY 204 RESPONSE THAT CAN'T BE MADE TO JSON
|
// SOMETIMES DISCORD RETURNS AN EMPTY 204 RESPONSE THAT CAN'T BE MADE TO JSON
|
||||||
if (response.status === 204) {
|
if (response.status === 204) {
|
||||||
rest.debug(`[REST - FetchSuccess] URL: ${request.urlToUse} | ${JSON.stringify(request.payload)}`);
|
rest.debug(`[REST - FetchSuccess] URL: ${request.urlToUse} | ${JSON.stringify(request.payload)}`);
|
||||||
request.request.respond({ status: 204 });
|
request.request.respond({
|
||||||
|
ok: true,
|
||||||
|
status: 204,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// CONVERT THE RESPONSE TO JSON
|
// CONVERT THE RESPONSE TO JSON
|
||||||
const json = await response.json();
|
const json = JSON.stringify(await response.json());
|
||||||
|
|
||||||
rest.debug(`[REST - fetchSuccess] ${JSON.stringify(request.payload)}`);
|
rest.debug(`[REST - fetchSuccess] ${JSON.stringify(request.payload)}`);
|
||||||
request.request.respond({
|
request.request.respond({
|
||||||
|
ok: true,
|
||||||
status: 200,
|
status: 200,
|
||||||
body: JSON.stringify(json),
|
body: json,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// SOMETHING WENT WRONG, LOG AND RESPOND WITH ERROR
|
// SOMETHING WENT WRONG, LOG AND RESPOND WITH ERROR
|
||||||
rest.debug(`[REST - fetchFailed] Payload: ${JSON.stringify(request.payload)} | Error: ${error}`);
|
rest.debug(`[REST - fetchFailed] Payload: ${JSON.stringify(request.payload)} | Error: ${error}`);
|
||||||
request.request.reject(error);
|
request.request.reject({
|
||||||
|
ok: false,
|
||||||
|
status: 599,
|
||||||
|
error: "Internal Proxy Error",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+14
-2
@@ -7,6 +7,7 @@ import { processRateLimitedPaths } from "./processRateLimitedPaths.ts";
|
|||||||
import { processRequest } from "./processRequest.ts";
|
import { processRequest } from "./processRequest.ts";
|
||||||
import { processRequestHeaders } from "./processRequestHeaders.ts";
|
import { processRequestHeaders } from "./processRequestHeaders.ts";
|
||||||
import { runMethod } from "./runMethod.ts";
|
import { runMethod } from "./runMethod.ts";
|
||||||
|
import { runProxyMethod } from "./runProxyMethod.ts";
|
||||||
import { simplifyUrl } from "./simplifyUrl.ts";
|
import { simplifyUrl } from "./simplifyUrl.ts";
|
||||||
|
|
||||||
export const rest = {
|
export const rest = {
|
||||||
@@ -49,14 +50,25 @@ export const rest = {
|
|||||||
processRequest,
|
processRequest,
|
||||||
createRequestBody,
|
createRequestBody,
|
||||||
runMethod,
|
runMethod,
|
||||||
|
runProxyMethod,
|
||||||
simplifyUrl,
|
simplifyUrl,
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface RestRequest {
|
export interface RestRequest {
|
||||||
url: string;
|
url: string;
|
||||||
method: string;
|
method: string;
|
||||||
respond: (payload: { status: number; body?: string }) => unknown;
|
respond: (payload: RestRequestResponse) => unknown;
|
||||||
reject: (error: unknown) => unknown;
|
reject: (payload: RestRequestRejection) => unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RestRequestResponse {
|
||||||
|
ok: boolean;
|
||||||
|
status: number;
|
||||||
|
body?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RestRequestRejection extends RestRequestResponse {
|
||||||
|
error: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RestPayload {
|
export interface RestPayload {
|
||||||
|
|||||||
+4
-3
@@ -1,5 +1,6 @@
|
|||||||
import { RestManager } from "../bot.ts";
|
import { RestManager } from "../bot.ts";
|
||||||
import { API_VERSION, BASE_URL, IMAGE_BASE_URL } from "../util/constants.ts";
|
import { API_VERSION, BASE_URL, IMAGE_BASE_URL } from "../util/constants.ts";
|
||||||
|
import { RestRequestRejection, RestRequestResponse } from "./rest.ts";
|
||||||
|
|
||||||
export async function runMethod<T = any>(
|
export async function runMethod<T = any>(
|
||||||
rest: RestManager,
|
rest: RestManager,
|
||||||
@@ -63,11 +64,11 @@ export async function runMethod<T = any>(
|
|||||||
{
|
{
|
||||||
url,
|
url,
|
||||||
method,
|
method,
|
||||||
reject: (error: unknown) => {
|
reject: (data: RestRequestRejection) => {
|
||||||
errorStack.message = (error as Error)?.message;
|
errorStack.message = `[${data.status}] ${data.error}`;
|
||||||
reject(errorStack);
|
reject(errorStack);
|
||||||
},
|
},
|
||||||
respond: (data: { status: number; body?: string }) =>
|
respond: (data: RestRequestResponse) =>
|
||||||
resolve(data.status !== 204 ? JSON.parse(data.body ?? "{}") : (undefined as unknown as T)),
|
resolve(data.status !== 204 ? JSON.parse(data.body ?? "{}") : (undefined as unknown as T)),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import { RestManager } from "../bot.ts";
|
||||||
|
import { RestRequestRejection, RestRequestResponse } from "./rest.ts";
|
||||||
|
|
||||||
|
export type ProxyMethodResponse<T> = Omit<RestRequestResponse | RestRequestRejection, "body"> & { body?: T };
|
||||||
|
|
||||||
|
// Left out proxy request, because it's not needed here
|
||||||
|
// this file could also be moved to a plugin.
|
||||||
|
export async function runProxyMethod<T = any>(
|
||||||
|
rest: RestManager,
|
||||||
|
method: "get" | "post" | "put" | "delete" | "patch",
|
||||||
|
url: string,
|
||||||
|
body?: unknown,
|
||||||
|
retryCount = 0,
|
||||||
|
bucketId?: string,
|
||||||
|
): Promise<ProxyMethodResponse<T>> {
|
||||||
|
rest.debug(
|
||||||
|
`[REST - RequestCreate] Method: ${method} | URL: ${url} | Retry Count: ${retryCount} | Bucket ID: ${bucketId} | Body: ${
|
||||||
|
JSON.stringify(
|
||||||
|
body,
|
||||||
|
)
|
||||||
|
}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
// No proxy so we need to handle all rate limiting and such
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
rest.processRequest(
|
||||||
|
rest,
|
||||||
|
{
|
||||||
|
url,
|
||||||
|
method,
|
||||||
|
reject: (data: RestRequestRejection) => {
|
||||||
|
const { body: b, ...r } = data;
|
||||||
|
reject({ body: data.status !== 204 ? JSON.parse(b ?? "{}") : (undefined as unknown as T), ...r });
|
||||||
|
},
|
||||||
|
respond: (data: RestRequestResponse) => {
|
||||||
|
const { body: b, ...r } = data;
|
||||||
|
resolve({ body: data.status !== 204 ? JSON.parse(b ?? "{}") : (undefined as unknown as T), ...r });
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
bucketId,
|
||||||
|
body: body as Record<string, unknown> | undefined,
|
||||||
|
retryCount,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user