@@ -17,6 +17,7 @@ import type {
17
17
WorkflowStartedResponse ,
18
18
} from '@/types/workflow'
19
19
import { removeAccessToken } from '@/app/components/share/utils'
20
+ import { asyncRunSafe } from '@/utils'
20
21
const TIME_OUT = 100000
21
22
22
23
const ContentType = {
@@ -550,55 +551,78 @@ export const ssePost = (
550
551
}
551
552
552
553
// base request
553
- export const request = < T > ( url : string , options = { } , otherOptions ?: IOtherOptions ) => {
554
- return new Promise < T > ( ( resolve , reject ) => {
554
+ export const request = async < T > ( url : string , options = { } , otherOptions ?: IOtherOptions ) => {
555
+ try {
555
556
const otherOptionsForBaseFetch = otherOptions || { }
556
- baseFetch < T > ( url , options , otherOptionsForBaseFetch ) . then ( resolve ) . catch ( ( errResp ) => {
557
- if ( errResp ?. status === 401 ) {
558
- return refreshAccessTokenOrRelogin ( TIME_OUT ) . then ( ( ) => {
559
- baseFetch < T > ( url , options , otherOptionsForBaseFetch ) . then ( resolve ) . catch ( reject )
560
- } ) . catch ( ( ) => {
561
- const {
562
- isPublicAPI = false ,
563
- silent,
564
- } = otherOptionsForBaseFetch
565
- const bodyJson = errResp . json ( )
566
- if ( isPublicAPI ) {
567
- return bodyJson . then ( ( data : ResponseError ) => {
568
- if ( data . code === 'web_sso_auth_required' )
569
- requiredWebSSOLogin ( )
570
-
571
- if ( data . code === 'unauthorized' ) {
572
- removeAccessToken ( )
573
- globalThis . location . reload ( )
574
- }
557
+ const [ err , resp ] = await asyncRunSafe < T > ( baseFetch ( url , options , otherOptionsForBaseFetch ) )
558
+ if ( err === null )
559
+ return resp
560
+ const errResp : Response = err as any
561
+ if ( errResp . status === 401 ) {
562
+ const [ parseErr , errRespData ] = await asyncRunSafe < ResponseError > ( errResp . json ( ) )
563
+ const loginUrl = `${ globalThis . location . origin } /signin`
564
+ if ( parseErr ) {
565
+ globalThis . location . href = loginUrl
566
+ return Promise . reject ( err )
567
+ }
568
+ // special code
569
+ const { code, message } = errRespData
570
+ // webapp sso
571
+ if ( code === 'web_sso_auth_required' ) {
572
+ requiredWebSSOLogin ( )
573
+ return Promise . reject ( err )
574
+ }
575
+ if ( code === 'unauthorized_and_force_logout' ) {
576
+ localStorage . removeItem ( 'console_token' )
577
+ localStorage . removeItem ( 'refresh_token' )
578
+ globalThis . location . reload ( )
579
+ return Promise . reject ( err )
580
+ }
581
+ const {
582
+ isPublicAPI = false ,
583
+ silent,
584
+ } = otherOptionsForBaseFetch
585
+ if ( isPublicAPI && code === 'unauthorized' ) {
586
+ removeAccessToken ( )
587
+ globalThis . location . reload ( )
588
+ return Promise . reject ( err )
589
+ }
590
+ if ( code === 'init_validate_failed' && IS_CE_EDITION && ! silent ) {
591
+ Toast . notify ( { type : 'error' , message, duration : 4000 } )
592
+ return Promise . reject ( err )
593
+ }
594
+ if ( code === 'not_init_validated' && IS_CE_EDITION ) {
595
+ globalThis . location . href = `${ globalThis . location . origin } /init`
596
+ return Promise . reject ( err )
597
+ }
598
+ if ( code === 'not_setup' && IS_CE_EDITION ) {
599
+ globalThis . location . href = `${ globalThis . location . origin } /install`
600
+ return Promise . reject ( err )
601
+ }
575
602
576
- return Promise . reject ( data )
577
- } )
578
- }
579
- const loginUrl = `${ globalThis . location . origin } /signin`
580
- bodyJson . then ( ( data : ResponseError ) => {
581
- if ( data . code === 'init_validate_failed' && IS_CE_EDITION && ! silent )
582
- Toast . notify ( { type : 'error' , message : data . message , duration : 4000 } )
583
- else if ( data . code === 'not_init_validated' && IS_CE_EDITION )
584
- globalThis . location . href = `${ globalThis . location . origin } /init`
585
- else if ( data . code === 'not_setup' && IS_CE_EDITION )
586
- globalThis . location . href = `${ globalThis . location . origin } /install`
587
- else if ( location . pathname !== '/signin' || ! IS_CE_EDITION )
588
- globalThis . location . href = loginUrl
589
- else if ( ! silent )
590
- Toast . notify ( { type : 'error' , message : data . message } )
591
- } ) . catch ( ( ) => {
592
- // Handle any other errors
593
- globalThis . location . href = loginUrl
594
- } )
595
- } )
603
+ // refresh token
604
+ const [ refreshErr ] = await asyncRunSafe ( refreshAccessTokenOrRelogin ( TIME_OUT ) )
605
+ if ( refreshErr === null )
606
+ return baseFetch < T > ( url , options , otherOptionsForBaseFetch )
607
+ if ( location . pathname !== '/signin' || ! IS_CE_EDITION ) {
608
+ globalThis . location . href = loginUrl
609
+ return Promise . reject ( err )
596
610
}
597
- else {
598
- reject ( errResp )
611
+ if ( ! silent ) {
612
+ Toast . notify ( { type : 'error' , message } )
613
+ return Promise . reject ( err )
599
614
}
600
- } )
601
- } )
615
+ globalThis . location . href = loginUrl
616
+ return Promise . reject ( err )
617
+ }
618
+ else {
619
+ return Promise . reject ( err )
620
+ }
621
+ }
622
+ catch ( error ) {
623
+ console . error ( error )
624
+ return Promise . reject ( error )
625
+ }
602
626
}
603
627
604
628
// request methods
0 commit comments