mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 19:28:17 +00:00
refactor(rest): separate functions into diff files (#685)
* shtufffff * fixe rate limit path infinite loop * refactor * ID > id * Update process_request_headers.ts * file names Co-authored-by: ITOH <72305210+itohatweb@users.noreply.github.com> Co-authored-by: ayntee <ayyantee@gmail.com>
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
/** 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(queuedRequest: QueuedRequest) {
|
||||
const headers: { [key: string]: string } = {
|
||||
Authorization: `Bot ${queuedRequest.options.token}`,
|
||||
"User-Agent": USER_AGENT,
|
||||
};
|
||||
|
||||
// GET METHODS SHOULD NOT HAVE A BODY
|
||||
if (queuedRequest.request.method === "GET") {
|
||||
queuedRequest.payload.body = undefined;
|
||||
}
|
||||
|
||||
// IF A REASON IS PROVIDED ENCODE IT IN HEADERS
|
||||
if (queuedRequest.payload.body?.reason) {
|
||||
headers["X-Audit-Log-Reason"] = encodeURIComponent(
|
||||
queuedRequest.payload.body.reason,
|
||||
);
|
||||
}
|
||||
|
||||
// IF A FILE/ATTACHMENT IS PRESENT WE NEED SPECIAL HANDLING
|
||||
if (queuedRequest.payload.body?.file) {
|
||||
const form = new FormData();
|
||||
form.append(
|
||||
"file",
|
||||
queuedRequest.payload.body.file.blob,
|
||||
queuedRequest.payload.body.file.name,
|
||||
);
|
||||
form.append(
|
||||
"payload_json",
|
||||
JSON.stringify({ ...queuedRequest.payload.body, file: undefined }),
|
||||
);
|
||||
queuedRequest.payload.body.file = form;
|
||||
} else if (
|
||||
queuedRequest.payload.body &&
|
||||
!["GET", "DELETE"].includes(queuedRequest.request.method)
|
||||
) {
|
||||
headers["Content-Type"] = "application/json";
|
||||
}
|
||||
|
||||
return {
|
||||
headers,
|
||||
body: queuedRequest.payload.body?.file ||
|
||||
JSON.stringify(queuedRequest.payload.body),
|
||||
method: queuedRequest.request.method,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user