diff --git a/packages/api/handlers/login-callback.ts b/packages/api/handlers/login-callback.ts index 8a5db34..59c9876 100644 --- a/packages/api/handlers/login-callback.ts +++ b/packages/api/handlers/login-callback.ts @@ -17,7 +17,13 @@ import { userAgent, } from '../utils/api-tools'; import { Bounce } from '../utils/bounce'; -import { apiPublicURI, botClientID, botClientSecret, uiPublicURI } from '../utils/config'; +import { + apiPublicURI, + botClientID, + botClientSecret, + discordAPIBase, + uiPublicURI, +} from '../utils/config'; import { Sessions } from '../utils/kv'; const AuthErrorResponse = (extra?: string) => @@ -72,7 +78,7 @@ export const LoginCallback = resolveFailures( code, }; - const tokenFetch = await fetch('https://discord.com/api/v8/oauth2/token', { + const tokenFetch = await fetch(discordAPIBase + '/oauth2/token', { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', diff --git a/packages/api/handlers/revoke-session.ts b/packages/api/handlers/revoke-session.ts index c6c0552..57593cf 100644 --- a/packages/api/handlers/revoke-session.ts +++ b/packages/api/handlers/revoke-session.ts @@ -1,6 +1,6 @@ import { SessionData } from '@roleypoly/types'; import { formData, respond, userAgent, withSession } from '../utils/api-tools'; -import { botClientID, botClientSecret } from '../utils/config'; +import { botClientID, botClientSecret, discordAPIBase } from '../utils/config'; import { Sessions } from '../utils/kv'; export const RevokeSession = withSession( @@ -11,7 +11,7 @@ export const RevokeSession = withSession( client_secret: botClientSecret, }; - await fetch('https://discord.com/api/v8/oauth2/token/revoke', { + await fetch(discordAPIBase + '/oauth2/token/revoke', { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', diff --git a/packages/api/utils/api-tools.ts b/packages/api/utils/api-tools.ts index f6ab639..a9de84a 100644 --- a/packages/api/utils/api-tools.ts +++ b/packages/api/utils/api-tools.ts @@ -5,7 +5,7 @@ import { import { SessionData, UserGuildPermissions } from '@roleypoly/types'; import KSUID from 'ksuid'; import { Handler } from '../router'; -import { allowedCallbackHosts, apiPublicURI, rootUsers } from './config'; +import { allowedCallbackHosts, apiPublicURI, discordAPIBase, rootUsers } from './config'; import { Sessions, WrappedKVNamespace } from './kv'; export const formData = (obj: Record): string => { @@ -84,7 +84,7 @@ export const discordFetch = async ( authType: AuthType = AuthType.Bearer, init?: RequestInit ): Promise => { - const response = await fetch('https://discord.com/api/v8' + url, { + const response = await fetch(discordAPIBase + url, { ...(init || {}), headers: { ...(init?.headers || {}), diff --git a/packages/api/utils/config.ts b/packages/api/utils/config.ts index 935bebd..5194504 100644 --- a/packages/api/utils/config.ts +++ b/packages/api/utils/config.ts @@ -13,3 +13,5 @@ export const apiPublicURI = safeURI(env('API_PUBLIC_URI')); export const rootUsers = list(env('ROOT_USERS')); export const allowedCallbackHosts = list(env('ALLOWED_CALLBACK_HOSTS')); export const importSharedKey = env('BOT_IMPORT_TOKEN'); + +export const discordAPIBase = 'https://discordapp.com/api/v9';