From 06039a077b9dcf4b23a44457bf7f47722318f7d5 Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Thu, 21 May 2020 16:37:55 +0200 Subject: [PATCH] fix(twitch:oauth): handle another exception message (#177) --- src/twitch/authorization.ts | 18 +++++++++++++++++- src/twitch/getTwitchGame.ts | 12 +++++------- src/twitch/getTwitchLiveStreams.ts | 12 +++++------- src/utils.ts | 5 +++++ 4 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 src/utils.ts diff --git a/src/twitch/authorization.ts b/src/twitch/authorization.ts index bd789d1..e832ef5 100644 --- a/src/twitch/authorization.ts +++ b/src/twitch/authorization.ts @@ -1,6 +1,7 @@ -import axios from 'axios'; +import axios, { AxiosError } from 'axios'; import * as qs from 'qs'; import { getTwitchApiKey, readFromSyncStorage, writeToSyncStorage } from '..'; +import { isAxiosError } from '../utils'; const oAuthUrl = 'https://id.twitch.tv/oauth2'; @@ -82,3 +83,18 @@ export async function askTwitchAccessToken(force: boolean = false) { return accessToken; } + +export function isTwitchApiOAuthError(error: Error | AxiosError) { + if (isAxiosError(error)) { + if ( + error.response.status === 401 && + ['Invalid OAuth token', 'Client ID and OAuth token do not match'].includes(error.response.data.message) + ) { + return true; + } + + return false; + } + + return false; +} diff --git a/src/twitch/getTwitchGame.ts b/src/twitch/getTwitchGame.ts index 3fdd1da..b331d5a 100644 --- a/src/twitch/getTwitchGame.ts +++ b/src/twitch/getTwitchGame.ts @@ -1,5 +1,6 @@ import axios from 'axios'; import { askTwitchAccessToken, getTwitchAccessToken, getTwitchApiKey, registerTwitchAccessToken } from '.'; +import { isTwitchApiOAuthError } from './authorization'; export interface Game { id: string; @@ -20,14 +21,11 @@ const sendRequest = async (id: string): Promise => { try { response = await axios.get(`https://api.twitch.tv/helix/games`, config); } catch (error) { - if (error.response) { - if (error.response.status === 401 && error.response.data.message === 'Invalid OAuth token') { - registerTwitchAccessToken(await askTwitchAccessToken(true)); - return sendRequest(id); - } - } else { - throw error; + if (isTwitchApiOAuthError(error)) { + registerTwitchAccessToken(await askTwitchAccessToken(true)); + return sendRequest(id); } + throw error; } return Promise.resolve(response.data.data[0] || null); diff --git a/src/twitch/getTwitchLiveStreams.ts b/src/twitch/getTwitchLiveStreams.ts index b070fcb..d32bb90 100644 --- a/src/twitch/getTwitchLiveStreams.ts +++ b/src/twitch/getTwitchLiveStreams.ts @@ -7,6 +7,7 @@ import { askTwitchAccessToken, registerTwitchAccessToken, } from '.'; +import { isTwitchApiOAuthError } from './authorization'; import { Game } from './getTwitchGame'; export interface Stream { @@ -38,14 +39,11 @@ export const getTwitchLiveStreams = (usersId: number[]): Promise => { try { response = await axios.get(url, config); } catch (error) { - if (error.response) { - if (error.response.status === 401 && error.response.data.message === 'Invalid OAuth token') { - registerTwitchAccessToken(await askTwitchAccessToken(true)); - return getTwitchLiveStreams(usersId).then(resolve); - } - } else { - throw error; + if (isTwitchApiOAuthError(error)) { + registerTwitchAccessToken(await askTwitchAccessToken(true)); + return getTwitchLiveStreams(usersId).then(resolve); } + throw error; } const streams: Stream[] = response.data.data; diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..2a0a2f7 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,5 @@ +import { AxiosError } from 'axios'; + +export function isAxiosError(error: Error | AxiosError): error is AxiosError { + return 'response' in error; +}