mirror of
https://github.com/discordeno/discordeno.git
synced 2026-09-17 08:47:22 +00:00
fix: stickers tests
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
import { RestManager } from "./restManager.ts";
|
||||
import { FileContent } from "../types/discordeno.ts";
|
||||
import { USER_AGENT } from "../util/constants.ts";
|
||||
import { RequestMethod, RestPayload, RestRequest } from "./rest.ts";
|
||||
import { RequestMethod } from "./rest.ts";
|
||||
|
||||
/** Creates the request body and headers that are necessary to send a request. Will handle different types of methods and everything necessary for discord. */
|
||||
// export function createRequestBody(rest: RestManager, queuedRequest: { request: RestRequest; payload: RestPayload }) {
|
||||
export function createRequestBody(rest: RestManager, options: CreateRequestBodyOptions) {
|
||||
const headers: Record<string, string> = {
|
||||
"user-agent": USER_AGENT,
|
||||
@@ -38,15 +37,28 @@ export function createRequestBody(rest: RestManager, options: CreateRequestBodyO
|
||||
|
||||
const form = new FormData();
|
||||
|
||||
for (let i = 0; i < (options.body.file as FileContent[]).length; i++) {
|
||||
form.append(
|
||||
`file${i}`,
|
||||
(options.body.file as FileContent[])[i].blob,
|
||||
(options.body.file as FileContent[])[i].name,
|
||||
);
|
||||
// WHEN CREATING A STICKER, DISCORD WANTS FORM DATA ONLY
|
||||
if (options.url?.endsWith("/stickers") && options.method === "POST") {
|
||||
form.append(
|
||||
`file`,
|
||||
(options.body.file as FileContent[])[0].blob,
|
||||
(options.body.file as FileContent[])[0].name,
|
||||
);
|
||||
form.append(`name`, options.body.name as string);
|
||||
form.append(`description`, options.body.description as string);
|
||||
form.append(`tags`, options.body.tags as string);
|
||||
} else {
|
||||
for (let i = 0; i < (options.body.file as FileContent[]).length; i++) {
|
||||
form.append(
|
||||
`file${i}`,
|
||||
(options.body.file as FileContent[])[i].blob,
|
||||
(options.body.file as FileContent[])[i].name,
|
||||
);
|
||||
}
|
||||
|
||||
form.append("payload_json", JSON.stringify({ ...options.body, file: undefined }));
|
||||
}
|
||||
|
||||
form.append("payload_json", JSON.stringify({ ...options.body, file: undefined }));
|
||||
options.body.file = form;
|
||||
} else if (options.body && !["GET", "DELETE"].includes(options.method)) {
|
||||
headers["Content-Type"] = "application/json";
|
||||
@@ -64,4 +76,5 @@ export interface CreateRequestBodyOptions {
|
||||
method: RequestMethod;
|
||||
body?: Record<string, unknown>;
|
||||
unauthorized?: boolean;
|
||||
url?: string;
|
||||
}
|
||||
|
||||
@@ -73,6 +73,7 @@ export async function processGlobalQueue(rest: RestManager) {
|
||||
payload: rest.createRequestBody(rest, {
|
||||
method: request.request.method,
|
||||
body: request.payload.body,
|
||||
url: request.urlToUse,
|
||||
}),
|
||||
})
|
||||
// Should be handled in sendRequest, this catch just prevents bots from dying
|
||||
|
||||
+18
-1
@@ -1,4 +1,6 @@
|
||||
import { FileContent } from "../mod.ts";
|
||||
import { API_VERSION, BASE_URL, baseEndpoints } from "../util/constants.ts";
|
||||
import { encode } from "../util/urlToBase64.ts";
|
||||
import { RequestMethod, RestRequestRejection, RestRequestResponse } from "./rest.ts";
|
||||
import { RestManager } from "./restManager.ts";
|
||||
|
||||
@@ -6,7 +8,7 @@ export async function runMethod<T = any>(
|
||||
rest: RestManager,
|
||||
method: RequestMethod,
|
||||
route: string,
|
||||
body?: unknown,
|
||||
body?: any,
|
||||
options?: {
|
||||
retryCount?: number;
|
||||
bucketId?: string;
|
||||
@@ -29,6 +31,21 @@ export async function runMethod<T = any>(
|
||||
|
||||
// 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] === "/") {
|
||||
// Special handling for sending blobs across http to proxy
|
||||
if (body?.file) {
|
||||
if (!Array.isArray(body.file)) {
|
||||
body.file = [body.file];
|
||||
}
|
||||
// convert blobs to string before sending to proxy
|
||||
body.file = await Promise.all(
|
||||
body.file.map(async (f: FileContent) => {
|
||||
const url = encode(await (f.blob).arrayBuffer());
|
||||
|
||||
return { name: f.name, blob: `data:${f.blob.type};base64,${url}` };
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const result = await fetch(`${baseEndpoints.BASE_URL}${route}`, {
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
headers: {
|
||||
|
||||
Reference in New Issue
Block a user