@@ -12,6 +12,7 @@ import {
12
12
createResponseError ,
13
13
ResponseError ,
14
14
} from '../../../utils/errors/response-error' ;
15
+ import { CredentialsError } from '../../../utils/errors' ;
15
16
16
17
type NodeFetch = typeof nodeFetch ;
17
18
type CreateAliasRequestBody =
@@ -127,39 +128,46 @@ class ApiService {
127
128
}
128
129
129
130
const parsedUrl = new URL ( requestUrl , this . apiEndpoint ) ;
130
- const signedRequest = await signature . sign ( {
131
- hostname : parsedUrl . hostname ,
132
- protocol : parsedUrl . protocol ,
133
- path : parsedUrl . pathname ,
134
- headers : convertFetchHeaders ( {
135
- ...fetchArgs [ 1 ] ?. headers ,
136
- host : parsedUrl . hostname ,
137
- accept : 'application/json' ,
138
- } ) ,
139
- method : fetchArgs [ 1 ] ?. method ?. toUpperCase ( ) ?? 'GET' ,
140
- query : convertURLSearchParamsToQueryBag ( parsedUrl . searchParams ) ,
141
- body : fetchArgs [ 1 ] ?. body ,
142
- } ) ;
143
- const response = await nodeFetch ( parsedUrl . href , {
144
- ...fetchArgs [ 1 ] ,
145
- headers : signedRequest . headers ,
146
- } ) ;
131
+ try {
132
+ const signedRequest = await signature . sign ( {
133
+ hostname : parsedUrl . hostname ,
134
+ protocol : parsedUrl . protocol ,
135
+ path : parsedUrl . pathname ,
136
+ headers : convertFetchHeaders ( {
137
+ ...fetchArgs [ 1 ] ?. headers ,
138
+ host : parsedUrl . hostname ,
139
+ accept : 'application/json' ,
140
+ } ) ,
141
+ method : fetchArgs [ 1 ] ?. method ?. toUpperCase ( ) ?? 'GET' ,
142
+ query : convertURLSearchParamsToQueryBag ( parsedUrl . searchParams ) ,
143
+ body : fetchArgs [ 1 ] ?. body ,
144
+ } ) ;
145
+ const response = await nodeFetch ( parsedUrl . href , {
146
+ ...fetchArgs [ 1 ] ,
147
+ headers : signedRequest . headers ,
148
+ } ) ;
149
+ if ( ! response . ok ) {
150
+ throw await createResponseError ( response ) ;
151
+ }
147
152
148
- if ( ! response . ok ) {
149
- throw await createResponseError ( response ) ;
150
- }
153
+ // OK - parse the response
154
+ if ( response . headers . get ( 'content-type' ) === 'application/json' ) {
155
+ try {
156
+ return ( await response . json ( ) ) as Promise < T > ;
157
+ } catch ( _ignoredError ) {
158
+ return { } as T ;
159
+ }
160
+ }
151
161
152
- // OK - parse the response
153
- if ( response . headers . get ( 'content-type' ) === 'application/json' ) {
154
- try {
155
- return ( await response . json ( ) ) as Promise < T > ;
156
- } catch ( _ignoredError ) {
157
- return { } as T ;
162
+ // Response from API is not OK, should never happen
163
+ throw new Error ( 'Invalid response from API' ) ;
164
+ } catch ( error : any ) {
165
+ if ( error . name === 'CredentialsProviderError' ) {
166
+ throw new CredentialsError ( ) ;
158
167
}
159
- }
160
168
161
- // Response from API is not OK, should never happen
162
- throw new Error ( 'Invalid response from API' ) ;
169
+ throw error ;
170
+ }
163
171
}
164
172
165
173
// Aliases
0 commit comments