1- import { camelCase , castArray , chunk , find , flatMap , get , snakeCase , toString } from "lodash-es" ;
1+ import { camelCase , castArray , chunk , find , get , snakeCase , toString } from "lodash-es" ;
22
33import { allPromises , changeCase , matchString , openUrl , t } from "~/common/helpers" ;
44import { stores } from "~/common/stores" ;
@@ -11,7 +11,7 @@ class RequestError extends Error {
1111 readonly request : Request ,
1212 readonly response : Response ,
1313 ) {
14- super ( response . statusText ) ;
14+ super ( `Request failed with status code ${ response . status } : ${ request . method } ${ request . url } ` ) ;
1515
1616 if ( Error . captureStackTrace ) {
1717 Error . captureStackTrace ( this , RequestError ) ;
@@ -36,7 +36,9 @@ export async function request<T>(
3636 }
3737
3838 const request = new Request ( url , {
39- headers : [ [ "Client-ID" , process . env . TWITCH_CLIENT_ID ] ] ,
39+ headers : {
40+ "Client-ID" : process . env . TWITCH_CLIENT_ID ,
41+ } ,
4042 } ) ;
4143
4244 const accessToken = await stores . accessToken . get ( ) ;
@@ -47,20 +49,8 @@ export async function request<T>(
4749
4850 const response = await fetch ( request ) ;
4951
50- switch ( response . status ) {
51- case 204 :
52- return undefined as never ;
53-
54- case 401 : {
55- stores . accessToken . set ( null ) ;
56-
57- createNotification ( "authorize" , {
58- title : t ( "notificationTitle_accessExpired" ) ,
59- message : t ( "notificationMessage_accessExpired" ) ,
60- } ) ;
61-
62- break ;
63- }
52+ if ( response . status === 204 ) {
53+ return undefined as never ;
6454 }
6555
6656 if ( response . ok ) {
@@ -89,7 +79,7 @@ export async function getUsersByIds(userIds: string[]) {
8979 get ( await request < HelixUser > ( "users" , { id } ) , "data" ) ,
9080 ) ;
9181
92- return flatMap ( pages ) ;
82+ return pages . flat ( ) ;
9383}
9484
9585export async function getFollowedStreams ( userId : string ) {
@@ -112,6 +102,31 @@ export async function authorize() {
112102 return openUrl ( url . href , undefined , true ) ;
113103}
114104
105+ export async function validate ( ) {
106+ const accessToken = await stores . accessToken . get ( ) ;
107+
108+ if ( accessToken ) {
109+ const response = await fetch ( "https://id.twitch.tv/oauth2/validate" , {
110+ headers : {
111+ Authorization : `Bearer ${ accessToken } ` ,
112+ } ,
113+ } ) ;
114+
115+ if ( response . status === 401 ) {
116+ createNotification ( "authorize" , {
117+ title : t ( "notificationTitle_accessExpired" ) ,
118+ message : t ( "notificationMessage_accessExpired" ) ,
119+ } ) ;
120+
121+ stores . accessToken . set ( null ) ;
122+ }
123+
124+ return response . ok ;
125+ }
126+
127+ return false ;
128+ }
129+
115130export async function revoke ( ) {
116131 const token = await stores . accessToken . get ( ) ;
117132
0 commit comments