refactor(rest): move user agent to constants

This commit is contained in:
ayntee
2020-12-26 19:36:08 +04:00
parent 8092128a0f
commit 64884439db
2 changed files with 17 additions and 12 deletions
+4 -5
View File
@@ -1,6 +1,6 @@
import { authorization, eventHandlers } from "../bot.ts";
import { Errors, HttpResponseCode, RequestMethods } from "../types/types.ts";
import { baseEndpoints, discordAPIURLS } from "../util/constants.ts";
import { API_VERSION, baseEndpoints, BASE_URL, IMAGE_BASE_URL, USER_AGENT } from "../util/constants.ts";
import { delay } from "../util/utils.ts";
const pathQueues: { [key: string]: QueuedRequest[] } = {};
@@ -137,8 +137,7 @@ export const RequestManager = {
function createRequestBody(body: any, method: RequestMethods) {
const headers: { [key: string]: string } = {
Authorization: authorization,
"User-Agent":
`DiscordBot (https://github.com/skillz4killz/discordeno, v10)`,
"User-Agent": USER_AGENT,
};
if (method === "get") body = undefined;
@@ -199,8 +198,8 @@ async function runMethod(
// For proxies we don't need to do any of the legwork so we just forward the request
if (
!url.startsWith(discordAPIURLS.BASE_URL) &&
!url.startsWith(discordAPIURLS.CDN_URL)
!url.startsWith(`${BASE_URL}/v${API_VERSION}`) &&
!url.startsWith(IMAGE_BASE_URL)
) {
return fetch(url, { method, body: body ? JSON.stringify(body) : undefined })
.then((res) => res.json())
+13 -7
View File
@@ -1,13 +1,19 @@
// These will never be modified and remain constants
export const discordAPIURLS = {
BASE_URL: `https://discord.com/api/v8`,
CDN_URL: "https://cdn.discordapp.com",
};
/** https://discord.com/developers/docs/reference#api-reference-base-url */
export const BASE_URL = "https://discord.com/api";
/** https://discord.com/developers/docs/reference#api-versioning-api-versions */
export const API_VERSION = 8;
/** https://discord.com/developers/docs/reference#user-agent */
export const USER_AGENT = "DiscordBot (https://github.com/discordeno/discordeno, v10)";
/** https://discord.com/developers/docs/reference#image-formatting-image-base-url */
export const IMAGE_BASE_URL = "https://cdn.discordapp.com/";
// This can be modified by big brain bots and use a proxy
export const baseEndpoints = {
BASE_URL: discordAPIURLS.BASE_URL,
CDN_URL: discordAPIURLS.CDN_URL,
BASE_URL: `${BASE_URL}/v${API_VERSION}`,
CDN_URL: IMAGE_BASE_URL,
};
const GUILDS_BASE = (id: string) => `${baseEndpoints.BASE_URL}/guilds/${id}`;