From 9c53a67dd36f4615502dfc6ddb1d923cb9ee9996 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Fri, 9 Apr 2021 14:41:31 +0000 Subject: [PATCH] fix: rest method uppercase --- docs/src/migrating.md | 4 ++-- docs/src/stepbystep/createbot.md | 5 ++--- src/rest/create_request_body.ts | 9 ++++----- src/rest/process_queue.ts | 31 ++++++++++++++++--------------- src/rest/run_method.ts | 4 ++-- src/structures/guild.ts | 4 +++- src/types/util.ts | 7 +++---- src/ws/handle_discord_payload.ts | 2 +- tests/README.md | 20 +++++++++++++++----- tests/guilds/create_guild.ts | 4 ++-- tests/guilds/delete_server.ts | 13 ++++++++----- tests/mod.ts | 2 +- tests/ws/start_bot.ts | 2 +- tests/ws/ws_close.ts | 2 +- 14 files changed, 61 insertions(+), 48 deletions(-) diff --git a/docs/src/migrating.md b/docs/src/migrating.md index a3a300600..8038be1af 100644 --- a/docs/src/migrating.md +++ b/docs/src/migrating.md @@ -22,8 +22,8 @@ For the purposes of this guide, I will be using the current ## Preparations - First, create a Discordeno Bot using the - [Generator Template](https://github.com/discordeno/template) I will name - it Zodiac. + [Generator Template](https://github.com/discordeno/template) I will name it + Zodiac. - Then `git clone https://github.com/Skillz4Killz/Zodiac.git` diff --git a/docs/src/stepbystep/createbot.md b/docs/src/stepbystep/createbot.md index ed6282ffe..2523a9ad2 100644 --- a/docs/src/stepbystep/createbot.md +++ b/docs/src/stepbystep/createbot.md @@ -10,9 +10,8 @@ you go through this guide it will make a lot more sense. > If you don't have these yet please prepare them first before going forward. - First, create a Discordeno Bot using the - [Generator Template](https://github.com/discordeno/template/generate). - Give it any name you like. For the purpose of this guide we will call it, - Stargate. + [Generator Template](https://github.com/discordeno/template/generate). Give it + any name you like. For the purpose of this guide we will call it, Stargate. - Then `git clone https://github.com/Skillz4Killz/Stargate.git` Replace **Stargate** with the name you chose. diff --git a/src/rest/create_request_body.ts b/src/rest/create_request_body.ts index a5e55ede3..3751b48c8 100644 --- a/src/rest/create_request_body.ts +++ b/src/rest/create_request_body.ts @@ -16,7 +16,7 @@ export function createRequestBody(queuedRequest: QueuedRequest) { // IF A REASON IS PROVIDED ENCODE IT IN HEADERS if (queuedRequest.payload.body?.reason) { headers["X-Audit-Log-Reason"] = encodeURIComponent( - queuedRequest.payload.body.reason + queuedRequest.payload.body.reason, ); } @@ -26,11 +26,11 @@ export function createRequestBody(queuedRequest: QueuedRequest) { form.append( "file", queuedRequest.payload.body.file.blob, - queuedRequest.payload.body.file.name + queuedRequest.payload.body.file.name, ); form.append( "payload_json", - JSON.stringify({ ...queuedRequest.payload.body, file: undefined }) + JSON.stringify({ ...queuedRequest.payload.body, file: undefined }), ); queuedRequest.payload.body.file = form; } else if ( @@ -42,8 +42,7 @@ export function createRequestBody(queuedRequest: QueuedRequest) { return { headers, - body: - queuedRequest.payload.body?.file || + body: queuedRequest.payload.body?.file || JSON.stringify(queuedRequest.payload.body), method: queuedRequest.request.method.toUpperCase(), }; diff --git a/src/rest/process_queue.ts b/src/rest/process_queue.ts index ae14d3c29..24db6fe73 100644 --- a/src/rest/process_queue.ts +++ b/src/rest/process_queue.ts @@ -36,18 +36,19 @@ export async function processQueue(id: string) { // EXECUTE THE REQUEST // IF THIS IS A GET REQUEST, CHANGE THE BODY TO QUERY PARAMETERS - const query = - queuedRequest.request.method.toUpperCase() === "GET" && - queuedRequest.payload.body - ? Object.entries(queuedRequest.payload.body) - .map( - ([key, value]) => - `${encodeURIComponent(key)}=${encodeURIComponent( - value as string - )}` - ) - .join("&") - : ""; + const query = queuedRequest.request.method.toUpperCase() === "GET" && + queuedRequest.payload.body + ? Object.entries(queuedRequest.payload.body) + .map( + ([key, value]) => + `${encodeURIComponent(key)}=${ + encodeURIComponent( + value as string, + ) + }`, + ) + .join("&") + : ""; const urlToUse = queuedRequest.request.method.toUpperCase() === "GET" && query ? `${queuedRequest.request.url}?${query}` @@ -59,13 +60,13 @@ export async function processQueue(id: string) { try { const response = await fetch( urlToUse, - rest.createRequestBody(queuedRequest) + rest.createRequestBody(queuedRequest), ); rest.eventHandlers.fetched(queuedRequest.payload); const bucketIdFromHeaders = rest.processRequestHeaders( queuedRequest.request.url, - response.headers + response.headers, ); if (response.status < 200 || response.status >= 400) { @@ -123,7 +124,7 @@ export async function processQueue(id: string) { // IF IT HAS MAXED RETRIES SOMETHING SERIOUSLY WRONG. CANCEL OUT. if ( queuedRequest.payload.retryCount >= - queuedRequest.options.maxRetryCount + queuedRequest.options.maxRetryCount ) { rest.eventHandlers.retriesMaxed(queuedRequest.payload); queuedRequest.request.respond({ diff --git a/src/rest/run_method.ts b/src/rest/run_method.ts index 040cd824b..183f7e439 100644 --- a/src/rest/run_method.ts +++ b/src/rest/run_method.ts @@ -6,7 +6,7 @@ export function runMethod( url: string, body?: unknown, retryCount = 0, - bucketId?: string | null + bucketId?: string | null, ): Promise { rest.eventHandlers.debug?.("requestCreate", { method, @@ -58,7 +58,7 @@ export function runMethod( method, body, retryCount, - } + }, ); }); } diff --git a/src/structures/guild.ts b/src/structures/guild.ts index 284934f09..3a3abd484 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -163,7 +163,9 @@ export async function createGuildStruct( ), memberCount: createNewProp(memberCount), emojis: createNewProp( - new Collection((emojis || []).map((emoji) => [emoji.id ?? emoji.name, emoji])), + new Collection( + (emojis || []).map((emoji) => [emoji.id ?? emoji.name, emoji]), + ), ), voiceStates: createNewProp( new Collection( diff --git a/src/types/util.ts b/src/types/util.ts index 16cea5311..f0130fc97 100644 --- a/src/types/util.ts +++ b/src/types/util.ts @@ -72,10 +72,9 @@ type InnerCamelCaseStringArray = : ""; type CamelCaseStringArray = Parts extends - [`${infer FirstPart}`, ...infer RemainingParts] - ? Uncapitalize< - `${FirstPart}${InnerCamelCaseStringArray}` - > + [`${infer FirstPart}`, ...infer RemainingParts] ? Uncapitalize< + `${FirstPart}${InnerCamelCaseStringArray}` +> : never; type StringPartToDelimiterCase< diff --git a/src/ws/handle_discord_payload.ts b/src/ws/handle_discord_payload.ts index 548c346c3..a1ba8ddb8 100644 --- a/src/ws/handle_discord_payload.ts +++ b/src/ws/handle_discord_payload.ts @@ -4,7 +4,7 @@ import { ws } from "./ws.ts"; /** Handler for processing all dispatch payloads that should be sent/forwarded to another server/vps/process. */ export async function handleDiscordPayload( data: DiscordGatewayPayload, - shardId: number + shardId: number, ) { await fetch(ws.url, { headers: { diff --git a/tests/README.md b/tests/README.md index 8c6f43821..c0bfd46d7 100644 --- a/tests/README.md +++ b/tests/README.md @@ -2,22 +2,32 @@ Unit tests are MANDATORY! -Every time you create a new function in the library, you must also add a unit test for it. A PR should/will not be merged without a valid unit test for it. If you are unable to create a unit test, please leave a comment in your PR asking for help. +Every time you create a new function in the library, you must also add a unit +test for it. A PR should/will not be merged without a valid unit test for it. If +you are unable to create a unit test, please leave a comment in your PR asking +for help. ## Test Locally -You do not need to push to the github repo to have the CI do the tests for you. You can test them locally by doing the following: +You do not need to push to the github repo to have the CI do the tests for you. +You can test them locally by doing the following: ```shell DISCORD_TOKEN=YOUR_BOT_TOKEN_HERE deno test --no-check -A tests/mod.ts ``` -> Please note that the token you use should be for a trivial unused bot. Never use your main bot tokens for this. +> Please note that the token you use should be for a trivial unused bot. Never +> use your main bot tokens for this. ## Ordering -The order of unit tests is very important. Please do not move/change the order of the tests unless you know what you are doing. Certain tests depend on other previous tests. You may add a test but becareful where you add it. +The order of unit tests is very important. Please do not move/change the order +of the tests unless you know what you are doing. Certain tests depend on other +previous tests. You may add a test but becareful where you add it. ## Naming -Each function should have it's own separate file for it's tests. The file should be organized under it's main category which will be the `[]` portion of the tests name. For example, `[guild] create a new guild` will be found in `tests/guilds/create_guild.ts` +Each function should have it's own separate file for it's tests. The file should +be organized under it's main category which will be the `[]` portion of the +tests name. For example, `[guild] create a new guild` will be found in +`tests/guilds/create_guild.ts` diff --git a/tests/guilds/create_guild.ts b/tests/guilds/create_guild.ts index 3b46beb8f..8b0423aa0 100644 --- a/tests/guilds/create_guild.ts +++ b/tests/guilds/create_guild.ts @@ -1,6 +1,6 @@ import { cache, createGuild, delay } from "../../mod.ts"; -import { tempData, defaultTestOptions } from "../ws/start_bot.ts"; -import { assertExists, assertEquals } from "../deps.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { assertEquals, assertExists } from "../deps.ts"; Deno.test({ name: "[guild] create a new guild", diff --git a/tests/guilds/delete_server.ts b/tests/guilds/delete_server.ts index a6fc0f42f..e92fd8da7 100644 --- a/tests/guilds/delete_server.ts +++ b/tests/guilds/delete_server.ts @@ -1,19 +1,22 @@ -import { cache, deleteServer, delay } from "../../mod.ts"; -import { tempData, defaultTestOptions } from "../ws/start_bot.ts"; +import { cache, delay, deleteServer } from "../../mod.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; Deno.test({ name: "[guild] delete a guild", async fn() { - if (!tempData.guildId) + if (!tempData.guildId) { throw new Error("The guild id was not available to be deleted."); - if (!cache.guilds.has(tempData.guildId)) + } + if (!cache.guilds.has(tempData.guildId)) { throw new Error("The guild was not cached so impossible to delete."); + } await deleteServer(tempData.guildId); await delay(3000); - if (cache.guilds.has(tempData.guildId)) + if (cache.guilds.has(tempData.guildId)) { throw new Error("The guild was not able to be deleted."); + } }, ...defaultTestOptions, }); diff --git a/tests/mod.ts b/tests/mod.ts index e99fc3c18..ef2fafdd2 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -1,7 +1,7 @@ // THE ORDER OF THE IMPORTS IN THIS FILE MATTER! // DO NOT MOVE THEM UNLESS YOU KNOW WHAT YOUR DOING! -// First complete non-api reliant testing. +// First complete non-api reliant testing. // Don't waste api rate limits if a early test fails. import "./util/utils.ts"; diff --git a/tests/ws/start_bot.ts b/tests/ws/start_bot.ts index f78ebb9b8..13a8bef54 100644 --- a/tests/ws/start_bot.ts +++ b/tests/ws/start_bot.ts @@ -1,4 +1,4 @@ -import { startBot, botId } from "../../src/bot.ts"; +import { botId, startBot } from "../../src/bot.ts"; import { delay } from "../../src/util/utils.ts"; import { ws } from "../../src/ws/ws.ts"; import { assertExists } from "../deps.ts"; diff --git a/tests/ws/ws_close.ts b/tests/ws/ws_close.ts index 76be8db08..f6d274aae 100644 --- a/tests/ws/ws_close.ts +++ b/tests/ws/ws_close.ts @@ -1,4 +1,4 @@ -import { ws, delay } from "../../mod.ts"; +import { delay, ws } from "../../mod.ts"; import { defaultTestOptions } from "./start_bot.ts"; // Exit the Deno process once all tests are done.