From 5839d07ca7d55f4ca2cd4081af90c12ee7669aa0 Mon Sep 17 00:00:00 2001 From: Skillz Date: Wed, 26 Feb 2020 22:51:00 -0500 Subject: [PATCH] IT WORKS! --- module/client.ts | 2 +- module/discord-request-manager.ts | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/module/client.ts b/module/client.ts index 203972556..94684a75f 100644 --- a/module/client.ts +++ b/module/client.ts @@ -44,7 +44,7 @@ class Client { options: FulfilledClientOptions event_handlers: Event_Handlers - protected authorization: string + authorization: string constructor(options: ClientOptions) { // Assign some defaults to the options to make them fulfilled / not annoying to use. diff --git a/module/discord-request-manager.ts b/module/discord-request-manager.ts index c0696cb4d..61b9322ae 100644 --- a/module/discord-request-manager.ts +++ b/module/discord-request-manager.ts @@ -4,18 +4,19 @@ import { RequestMethod } from "../types/fetch.ts" // type RequestBody = string | Blob | ArrayBufferView | ArrayBuffer | FormData | URLSearchParams | null | undefined export default class DiscordRequestManager { - token = this.client.token constructor(public client: Client) { this.client = client } async get(url: string, body?: unknown) { - console.log("get", url) - return this.runMethod(RequestMethod.Get, url, body) + const response = await fetch(this.resolveURL(url), { + headers: this.getDiscordHeaders(), + body: body ? JSON.stringify(body) : undefined + }) + return await response.json() } async post(url: string, body?: unknown) { - console.log("post", url) return this.runMethod(RequestMethod.Post, url, body) } @@ -24,7 +25,6 @@ export default class DiscordRequestManager { } async patch(url: string, body: unknown) { - console.log("patch", url) return this.runMethod(RequestMethod.Patch, url, body) } @@ -32,14 +32,16 @@ export default class DiscordRequestManager { return this.runMethod(RequestMethod.Put, url, body) } - protected async runMethod(method: RequestMethod, url: string, body?: unknown) { - console.log("resolve", url, this.resolveURL(url)) - const headers = this.getDiscordHeaders() - return fetch(this.resolveURL(url), { + async runMethod(method: RequestMethod, url: string, body?: unknown) { + const response = await fetch(this.resolveURL(url), { method, - headers, + headers: this.getDiscordHeaders(), body: body ? JSON.stringify(body) : undefined - }).then(response => response.json()) + }) + + const json = await response.json() + + return json } // A hook for the RouteAwareRequestManager to override URLs. @@ -50,7 +52,7 @@ export default class DiscordRequestManager { // The Record type here plays nice with Deno's `fetch.headers` expected type. getDiscordHeaders(): Record { return { - Authorization: this.token, + Authorization: this.client.authorization, "User-Agent": `Discordeno (https://github.com/skillz4killz/discordeno, 0.0.1)` } }