Cleaned up some config, error handling

This commit is contained in:
Alec LaLonde
2021-08-26 09:47:30 -06:00
committed by Alec LaLonde
parent 1989a4f734
commit 1783a34dfb
8 changed files with 16 additions and 25 deletions

View File

@@ -98,7 +98,7 @@ export const handleOAuthCallback = async (
);
res.json(createGuildResponse);
} else {
throw new Error(creationError);
throw creationError;
}
}
res.json(createGuildResponse);
@@ -169,5 +169,5 @@ const createNewGuild = async (
return successResponse;
}
}
throw new Error();
throw new Error(`Failed to create guild "${newGuildPayload.guildname}"`);
};

View File

@@ -19,7 +19,6 @@ export const saveGuildHandler = async (
try {
const { guildInformation } = input;
console.log('save guild handler', guildInformation);
await saveGuild(playerId, guildInformation as GuildInfo);
res.json({

View File

@@ -36,7 +36,7 @@ export const updateDiscordRole = async (payload: TriggerPayload<Player>) => {
if (newRank == null) return;
// hardcoded for now
// hardcoded for now to metagame discord server
const guildDiscordId = '629411177947987986';
// instantiate discord client. We'll need serverId, playerId, and roleIds

View File

@@ -3,10 +3,7 @@ import fetch from 'node-fetch';
import { URLSearchParams } from 'url';
import { CONFIG } from './config';
import {
DiscordAccessTokenResponse,
PartialAuthorizationResponse,
} from './types';
import { DiscordAccessTokenResponse } from './types';
export const tokenRequestData = {
client_id: Constants.DISCORD_BOT_CLIENT_ID,
@@ -24,7 +21,7 @@ export const exchangeCodeForAccessToken = async (
};
const discordResponse = await fetch(
'https://discord.com/api/v8/oauth2/token',
`${CONFIG.discordApiBaseUrl}/oauth2/token`,
{
method: 'POST',
body: new URLSearchParams(data),
@@ -46,17 +43,3 @@ export const exchangeCodeForAccessToken = async (
return response;
};
export const getCurrentAuthorization = async (
accessToken: string,
): Promise<PartialAuthorizationResponse> => {
const discordResponse = await fetch('https://discord.com/api/oauth2/@me', {
method: 'GET',
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
const parsedBody = await discordResponse.json();
return parsedBody as PartialAuthorizationResponse;
};

View File

@@ -10,6 +10,7 @@ interface IConfig {
discordBotToken: string;
discordBotClientSecret: string;
sourceCredLedgerBranch: string;
discordApiBaseUrl: string;
}
function parseEnv<T extends string | number>(
@@ -47,4 +48,8 @@ export const CONFIG: IConfig = {
process.env.SOURCECRED_LEDGER_BRANCH,
'staging', // Just so we dont mess up the master ledger in case people are testing locally
),
discordApiBaseUrl: parseEnv(
process.env.DISCORD_API_BASE_URL,
'https://discord.com/api/v8',
),
};

View File

@@ -29,7 +29,9 @@ export const GuildJoin: React.FC = () => {
scope: Constants.DISCORD_OAUTH_SCOPES,
});
const discordAuthURL = `https://discord.com/api/oauth2/authorize?${discordAuthParams.toString()}`;
const discordAuthURL = `${
CONFIG.discordApiBaseUrl
}/authorize?${discordAuthParams.toString()}`;
return (
<FlexContainer flex="1" justify="start" mt={5}>

View File

@@ -21,4 +21,6 @@ export const CONFIG = {
publicURL: process.env.NEXT_PUBLIC_VERCEL_URL || 'http://localhost:3000',
gaId: process.env.NEXT_PUBLIC_GA4_ID || undefined,
clarityId: process.env.NEXT_CLARITY_ID || undefined,
discordApiBaseUrl:
process.env.DISCORD_API_BASE_URL || 'https://discord.com/api/v8',
};

View File

@@ -37,7 +37,7 @@ const GuildSetupAuthCallback: React.FC = () => {
setFetching(true);
submitAuthCode();
}
}, [router, authGuild, error, fetching, setFetching]);
}, [router, authGuild, error, fetching]);
return (
<PageContainer>