IT WORKS!

This commit is contained in:
Skillz
2020-02-26 22:51:00 -05:00
parent 8104dd19bd
commit 5839d07ca7
2 changed files with 15 additions and 13 deletions

View File

@@ -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.

View File

@@ -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<string, string> {
return {
Authorization: this.token,
Authorization: this.client.authorization,
"User-Agent": `Discordeno (https://github.com/skillz4killz/discordeno, 0.0.1)`
}
}