fix: rest method uppercase

This commit is contained in:
Skillz4Killz
2021-04-09 14:41:31 +00:00
committed by GitHub
parent 03d01463e1
commit 9c53a67dd3
14 changed files with 61 additions and 48 deletions
+2 -2
View File
@@ -22,8 +22,8 @@ For the purposes of this guide, I will be using the current
## Preparations ## Preparations
- First, create a Discordeno Bot using the - First, create a Discordeno Bot using the
[Generator Template](https://github.com/discordeno/template) I will name [Generator Template](https://github.com/discordeno/template) I will name it
it Zodiac. Zodiac.
- Then `git clone https://github.com/Skillz4Killz/Zodiac.git` - Then `git clone https://github.com/Skillz4Killz/Zodiac.git`
+2 -3
View File
@@ -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. > If you don't have these yet please prepare them first before going forward.
- First, create a Discordeno Bot using the - First, create a Discordeno Bot using the
[Generator Template](https://github.com/discordeno/template/generate). [Generator Template](https://github.com/discordeno/template/generate). Give it
Give it any name you like. For the purpose of this guide we will call it, any name you like. For the purpose of this guide we will call it, Stargate.
Stargate.
- Then `git clone https://github.com/Skillz4Killz/Stargate.git` Replace - Then `git clone https://github.com/Skillz4Killz/Stargate.git` Replace
**Stargate** with the name you chose. **Stargate** with the name you chose.
+4 -5
View File
@@ -16,7 +16,7 @@ export function createRequestBody(queuedRequest: QueuedRequest) {
// IF A REASON IS PROVIDED ENCODE IT IN HEADERS // IF A REASON IS PROVIDED ENCODE IT IN HEADERS
if (queuedRequest.payload.body?.reason) { if (queuedRequest.payload.body?.reason) {
headers["X-Audit-Log-Reason"] = encodeURIComponent( 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( form.append(
"file", "file",
queuedRequest.payload.body.file.blob, queuedRequest.payload.body.file.blob,
queuedRequest.payload.body.file.name queuedRequest.payload.body.file.name,
); );
form.append( form.append(
"payload_json", "payload_json",
JSON.stringify({ ...queuedRequest.payload.body, file: undefined }) JSON.stringify({ ...queuedRequest.payload.body, file: undefined }),
); );
queuedRequest.payload.body.file = form; queuedRequest.payload.body.file = form;
} else if ( } else if (
@@ -42,8 +42,7 @@ export function createRequestBody(queuedRequest: QueuedRequest) {
return { return {
headers, headers,
body: body: queuedRequest.payload.body?.file ||
queuedRequest.payload.body?.file ||
JSON.stringify(queuedRequest.payload.body), JSON.stringify(queuedRequest.payload.body),
method: queuedRequest.request.method.toUpperCase(), method: queuedRequest.request.method.toUpperCase(),
}; };
+16 -15
View File
@@ -36,18 +36,19 @@ export async function processQueue(id: string) {
// EXECUTE THE REQUEST // EXECUTE THE REQUEST
// IF THIS IS A GET REQUEST, CHANGE THE BODY TO QUERY PARAMETERS // IF THIS IS A GET REQUEST, CHANGE THE BODY TO QUERY PARAMETERS
const query = const query = queuedRequest.request.method.toUpperCase() === "GET" &&
queuedRequest.request.method.toUpperCase() === "GET" && queuedRequest.payload.body
queuedRequest.payload.body ? Object.entries(queuedRequest.payload.body)
? Object.entries(queuedRequest.payload.body) .map(
.map( ([key, value]) =>
([key, value]) => `${encodeURIComponent(key)}=${
`${encodeURIComponent(key)}=${encodeURIComponent( encodeURIComponent(
value as string value as string,
)}` )
) }`,
.join("&") )
: ""; .join("&")
: "";
const urlToUse = const urlToUse =
queuedRequest.request.method.toUpperCase() === "GET" && query queuedRequest.request.method.toUpperCase() === "GET" && query
? `${queuedRequest.request.url}?${query}` ? `${queuedRequest.request.url}?${query}`
@@ -59,13 +60,13 @@ export async function processQueue(id: string) {
try { try {
const response = await fetch( const response = await fetch(
urlToUse, urlToUse,
rest.createRequestBody(queuedRequest) rest.createRequestBody(queuedRequest),
); );
rest.eventHandlers.fetched(queuedRequest.payload); rest.eventHandlers.fetched(queuedRequest.payload);
const bucketIdFromHeaders = rest.processRequestHeaders( const bucketIdFromHeaders = rest.processRequestHeaders(
queuedRequest.request.url, queuedRequest.request.url,
response.headers response.headers,
); );
if (response.status < 200 || response.status >= 400) { 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 IT HAS MAXED RETRIES SOMETHING SERIOUSLY WRONG. CANCEL OUT.
if ( if (
queuedRequest.payload.retryCount >= queuedRequest.payload.retryCount >=
queuedRequest.options.maxRetryCount queuedRequest.options.maxRetryCount
) { ) {
rest.eventHandlers.retriesMaxed(queuedRequest.payload); rest.eventHandlers.retriesMaxed(queuedRequest.payload);
queuedRequest.request.respond({ queuedRequest.request.respond({
+2 -2
View File
@@ -6,7 +6,7 @@ export function runMethod<T = any>(
url: string, url: string,
body?: unknown, body?: unknown,
retryCount = 0, retryCount = 0,
bucketId?: string | null bucketId?: string | null,
): Promise<T | undefined> { ): Promise<T | undefined> {
rest.eventHandlers.debug?.("requestCreate", { rest.eventHandlers.debug?.("requestCreate", {
method, method,
@@ -58,7 +58,7 @@ export function runMethod<T = any>(
method, method,
body, body,
retryCount, retryCount,
} },
); );
}); });
} }
+3 -1
View File
@@ -163,7 +163,9 @@ export async function createGuildStruct(
), ),
memberCount: createNewProp(memberCount), memberCount: createNewProp(memberCount),
emojis: createNewProp( emojis: createNewProp(
new Collection((emojis || []).map((emoji) => [emoji.id ?? emoji.name, emoji])), new Collection(
(emojis || []).map((emoji) => [emoji.id ?? emoji.name, emoji]),
),
), ),
voiceStates: createNewProp( voiceStates: createNewProp(
new Collection( new Collection(
+3 -4
View File
@@ -72,10 +72,9 @@ type InnerCamelCaseStringArray<Parts extends any[], PreviousPart> =
: ""; : "";
type CamelCaseStringArray<Parts extends string[]> = Parts extends type CamelCaseStringArray<Parts extends string[]> = Parts extends
[`${infer FirstPart}`, ...infer RemainingParts] [`${infer FirstPart}`, ...infer RemainingParts] ? Uncapitalize<
? Uncapitalize< `${FirstPart}${InnerCamelCaseStringArray<RemainingParts, FirstPart>}`
`${FirstPart}${InnerCamelCaseStringArray<RemainingParts, FirstPart>}` >
>
: never; : never;
type StringPartToDelimiterCase< type StringPartToDelimiterCase<
+1 -1
View File
@@ -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. */ /** Handler for processing all dispatch payloads that should be sent/forwarded to another server/vps/process. */
export async function handleDiscordPayload( export async function handleDiscordPayload(
data: DiscordGatewayPayload, data: DiscordGatewayPayload,
shardId: number shardId: number,
) { ) {
await fetch(ws.url, { await fetch(ws.url, {
headers: { headers: {
+15 -5
View File
@@ -2,22 +2,32 @@
Unit tests are MANDATORY! 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 ## 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 ```shell
DISCORD_TOKEN=YOUR_BOT_TOKEN_HERE deno test --no-check -A tests/mod.ts 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 ## 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 ## 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`
+2 -2
View File
@@ -1,6 +1,6 @@
import { cache, createGuild, delay } from "../../mod.ts"; import { cache, createGuild, delay } from "../../mod.ts";
import { tempData, defaultTestOptions } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
import { assertExists, assertEquals } from "../deps.ts"; import { assertEquals, assertExists } from "../deps.ts";
Deno.test({ Deno.test({
name: "[guild] create a new guild", name: "[guild] create a new guild",
+8 -5
View File
@@ -1,19 +1,22 @@
import { cache, deleteServer, delay } from "../../mod.ts"; import { cache, delay, deleteServer } from "../../mod.ts";
import { tempData, defaultTestOptions } from "../ws/start_bot.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts";
Deno.test({ Deno.test({
name: "[guild] delete a guild", name: "[guild] delete a guild",
async fn() { async fn() {
if (!tempData.guildId) if (!tempData.guildId) {
throw new Error("The guild id was not available to be deleted."); 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."); throw new Error("The guild was not cached so impossible to delete.");
}
await deleteServer(tempData.guildId); await deleteServer(tempData.guildId);
await delay(3000); 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."); throw new Error("The guild was not able to be deleted.");
}
}, },
...defaultTestOptions, ...defaultTestOptions,
}); });
+1 -1
View File
@@ -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 { delay } from "../../src/util/utils.ts";
import { ws } from "../../src/ws/ws.ts"; import { ws } from "../../src/ws/ws.ts";
import { assertExists } from "../deps.ts"; import { assertExists } from "../deps.ts";
+1 -1
View File
@@ -1,4 +1,4 @@
import { ws, delay } from "../../mod.ts"; import { delay, ws } from "../../mod.ts";
import { defaultTestOptions } from "./start_bot.ts"; import { defaultTestOptions } from "./start_bot.ts";
// Exit the Deno process once all tests are done. // Exit the Deno process once all tests are done.