Tidy up, handle some errors when talking to bot

This commit is contained in:
Marshall Polaris 2022-09-16 00:15:41 -07:00
parent 0161d7fb75
commit c9750515ca

View File

@ -3,14 +3,18 @@ import { generateNewApiKey } from '../api/api-key'
const TWITCH_BOT_PUBLIC_URL = 'https://king-prawn-app-5btyw.ondigitalocean.app' // TODO: Add this to env config appropriately const TWITCH_BOT_PUBLIC_URL = 'https://king-prawn-app-5btyw.ondigitalocean.app' // TODO: Add this to env config appropriately
function postToBot(url: string, body: any): Promise<Response> { async function postToBot(url: string, body: unknown) {
return fetch(url, { const result = await fetch(url, {
method: 'POST', method: 'POST',
headers: { headers: { 'Content-Type': 'application/json' },
'Content-Type': 'application/json',
},
body: JSON.stringify(body), body: JSON.stringify(body),
}) })
const json = await result.json()
if (!result.ok) {
throw new Error(json.message)
} else {
return json
}
} }
export async function initLinkTwitchAccount( export async function initLinkTwitchAccount(
@ -22,14 +26,10 @@ export async function initLinkTwitchAccount(
apiKey: manifoldUserAPIKey, apiKey: manifoldUserAPIKey,
redirectURL: window.location.href, redirectURL: window.location.href,
}) })
const responseData = await response.json()
if (!response.ok) {
throw new Error(responseData.message)
}
const responseFetch = fetch( const responseFetch = fetch(
`${TWITCH_BOT_PUBLIC_URL}/api/linkResult?userID=${manifoldUserID}` `${TWITCH_BOT_PUBLIC_URL}/api/linkResult?userID=${manifoldUserID}`
) )
return [responseData.twitchAuthURL, responseFetch.then((r) => r.json())] return [response.twitchAuthURL, responseFetch.then((r) => r.json())]
} }
export async function linkTwitchAccountRedirect( export async function linkTwitchAccountRedirect(
@ -51,18 +51,14 @@ export async function updateBotEnabledForUser(
if (botEnabled) { if (botEnabled) {
return postToBot(`${TWITCH_BOT_PUBLIC_URL}/registerchanneltwitch`, { return postToBot(`${TWITCH_BOT_PUBLIC_URL}/registerchanneltwitch`, {
apiKey: privateUser.apiKey, apiKey: privateUser.apiKey,
}).then((r) => {
if (!r.success) throw new Error(r.message)
}) })
.then((r) => r.json())
.then((r) => {
if (!r.success) throw new Error(r.message)
})
} else { } else {
return postToBot(`${TWITCH_BOT_PUBLIC_URL}/unregisterchanneltwitch`, { return postToBot(`${TWITCH_BOT_PUBLIC_URL}/unregisterchanneltwitch`, {
apiKey: privateUser.apiKey, apiKey: privateUser.apiKey,
}).then((r) => {
if (!r.success) throw new Error(r.message)
}) })
.then((r) => r.json())
.then((r) => {
if (!r.success) throw new Error(r.message)
})
} }
} }