1- import { AccountInfo , InteractionRequiredAuthError } from "@azure/msal-browser" ;
2- import { loginRequest , msalInstance } from "@/lib/msalConfig" ;
3- import { acquireGraphAccessToken } from "./msalHelper" ;
1+ import { InteractionRequiredAuthError } from "@azure/msal-browser" ;
2+ import { msalInstance } from "@/lib/msalConfig" ;
3+ import { acquireGraphAccessToken , handleSignIn } from "./msalHelper" ;
44
55export interface ODataResponse < T > {
6- "@odata.context" : string ,
7- value : T [ ]
6+ "@odata.context" : string ;
7+ value : T [ ] ;
88}
99
1010export async function fetcher ( ...args : Parameters < typeof fetch > ) {
11-
1211 // fake delay
1312 // await new Promise(resolve => setTimeout(resolve, 5000));
1413
@@ -24,24 +23,25 @@ export async function fetcher(...args: Parameters<typeof fetch>) {
2423
2524 const response = await fetch ( ...args ) . catch ( ( error ) => {
2625 if ( error instanceof InteractionRequiredAuthError ) {
27- msalInstance . acquireTokenRedirect ( {
28- ...loginRequest ,
29- account : msalInstance . getActiveAccount ( ) as AccountInfo ,
30- } ) ;
26+ msalInstance . clearCache ( ) ;
27+ handleSignIn ( ) ;
3128 }
3229 } ) ;
3330
3431 if ( ! response ) {
35- throw new Error ( ' No response from fetch' ) ;
32+ throw new Error ( " No response from fetch" ) ;
3633 }
3734
3835 const contentType = response . headers . get ( "Content-Type" ) ;
3936
4037 if ( contentType ?. includes ( "application/json" ) ) {
4138 const data = await response . json ( ) ;
4239 // Handle @odata .nextLink for pagination
43- if ( data [ '@odata.nextLink' ] && typeof data [ '@odata.nextLink' ] === 'string' ) {
44- const nextPageData = await fetcher ( data [ '@odata.nextLink' ] ) ;
40+ if (
41+ data [ "@odata.nextLink" ] &&
42+ typeof data [ "@odata.nextLink" ] === "string"
43+ ) {
44+ const nextPageData = await fetcher ( data [ "@odata.nextLink" ] ) ;
4545 data . value = [ ...( data . value || [ ] ) , ...( nextPageData . value || [ ] ) ] ;
4646 }
4747 return data ;
@@ -50,4 +50,4 @@ export async function fetcher(...args: Parameters<typeof fetch>) {
5050 } else {
5151 throw new Error ( `Unsupported content type: ${ contentType } ` ) ;
5252 }
53- }
53+ }
0 commit comments