@@ -32,11 +32,7 @@ export class ApiClient {
3232 this . errorHandler = handler
3333 }
3434
35- async request (
36- url : string ,
37- options : RequestOptions = { } ,
38- parseJson : boolean = true
39- ) {
35+ async request ( url : string , options : RequestOptions = { } , parseJson : boolean = true ) {
4036 url = this . config ?. baseUrl ? joinURL ( this . config ?. baseUrl , url ) : url
4137 options . headers = { ...this . config ?. headers , ...options . headers }
4238 if (
@@ -59,7 +55,10 @@ export class ApiClient {
5955 logger . log ( "[request] url:" , url )
6056 logger . log ( "[request] options:" , options )
6157
62- const response = await this . $api . fetch ( url , options as RequestInit & { body ?: BodyInit | null ; aws ?: Record < string , unknown > } )
58+ const response = await this . $api . fetch (
59+ url ,
60+ options as RequestInit & { body ?: BodyInit | null ; aws ?: Record < string , unknown > } ,
61+ )
6362
6463 logger . log ( "[request] response:" , response )
6564
@@ -70,8 +69,39 @@ export class ApiClient {
7069 return
7170 }
7271 if ( response . status === 403 ) {
73- if ( this . errorHandler ) {
74- await this . errorHandler . handle403 ( )
72+ try {
73+ const cloned = response . clone ( )
74+ let codeText = ""
75+ try {
76+ const data = ( await cloned . json ( ) ) as Record < string , unknown >
77+ let code : string | undefined = ( data ?. code as string | undefined ) || ( data ?. Code as string | undefined )
78+ if ( data && typeof data === "object" && "error" in data ) {
79+ const errObj = ( data as { error ?: unknown } ) . error
80+ if ( errObj && typeof errObj === "object" ) {
81+ const e = errObj as { code ?: unknown }
82+ if ( typeof e . code === "string" ) code = code ?? e . code
83+ }
84+ }
85+ codeText = typeof code === "string" ? code : ""
86+ } catch {
87+ codeText = ""
88+ }
89+
90+ const normalizedCode = codeText . toLowerCase ( )
91+ const isUnauthorizedAccess = normalizedCode === "unauthorizedaccess"
92+ const isInvalidAccessKey = normalizedCode === "invalidaccesskeyid"
93+
94+ if ( this . errorHandler ) {
95+ if ( isUnauthorizedAccess || isInvalidAccessKey ) {
96+ await this . errorHandler . handle401 ( )
97+ } else {
98+ await this . errorHandler . handle403 ( )
99+ }
100+ }
101+ } catch {
102+ if ( this . errorHandler ) {
103+ await this . errorHandler . handle403 ( )
104+ }
75105 }
76106 return
77107 }
@@ -81,11 +111,7 @@ export class ApiClient {
81111 throw new Error ( errorMsg )
82112 }
83113
84- if (
85- response . status === 204 ||
86- response . headers . get ( "content-length" ) === "0" ||
87- ! response . body
88- ) {
114+ if ( response . status === 204 || response . headers . get ( "content-length" ) === "0" || ! response . body ) {
89115 return null
90116 }
91117
@@ -101,11 +127,7 @@ export class ApiClient {
101127 }
102128
103129 async * streamRequest ( url : string , options : RequestOptions = { } ) {
104- const response = ( await this . request (
105- url ,
106- { method : "GET" , ...options } ,
107- false
108- ) ) as Response | null
130+ const response = ( await this . request ( url , { method : "GET" , ...options } , false ) ) as Response | null
109131
110132 if ( ! response ?. body ) {
111133 throw new Error ( "No response body" )
0 commit comments