@verii/http-client 1.2.0-pre.1783496206.0 → 1.2.0-pre.1783496499.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +3 -0
- package/package.json +1 -1
- package/src/client.js +22 -4
package/index.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ export type HttpRawBody = Dispatcher.ResponseData['body'];
|
|
|
4
4
|
|
|
5
5
|
export type HttpCacheStore = InstanceType<typeof cacheStores.MemoryCacheStore>;
|
|
6
6
|
|
|
7
|
+
export type HttpResponseErrorMode = 'throw' | 'return';
|
|
8
|
+
|
|
7
9
|
export interface HttpClientContext {
|
|
8
10
|
traceId?: string;
|
|
9
11
|
log?: {
|
|
@@ -47,6 +49,7 @@ export interface HttpClientInitOptions {
|
|
|
47
49
|
isTest?: boolean;
|
|
48
50
|
bearerToken?: string;
|
|
49
51
|
requestTimeout?: number;
|
|
52
|
+
responseErrorMode?: HttpResponseErrorMode;
|
|
50
53
|
traceIdHeader?: string;
|
|
51
54
|
customHeaders?: Record<string, string>;
|
|
52
55
|
cache?: HttpCacheStore;
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -66,10 +66,8 @@ const initHttpClient = (options) => {
|
|
|
66
66
|
path,
|
|
67
67
|
method,
|
|
68
68
|
headers: reqHeaders,
|
|
69
|
+
...(body ? { body } : {}),
|
|
69
70
|
};
|
|
70
|
-
if (body) {
|
|
71
|
-
httpRequest.body = body;
|
|
72
|
-
}
|
|
73
71
|
const httpResponse = await agent.request(httpRequest);
|
|
74
72
|
const { statusCode, headers: resHeaders, body: rawBody } = httpResponse;
|
|
75
73
|
return {
|
|
@@ -172,6 +170,7 @@ const parseOptions = (options) => {
|
|
|
172
170
|
},
|
|
173
171
|
};
|
|
174
172
|
if (options.requestTimeout != null) {
|
|
173
|
+
clientOptions.headersTimeout = options.requestTimeout;
|
|
175
174
|
clientOptions.bodyTimeout = options.requestTimeout;
|
|
176
175
|
}
|
|
177
176
|
|
|
@@ -192,6 +191,7 @@ const buildInterceptorChain = (
|
|
|
192
191
|
clientSecret,
|
|
193
192
|
scopes,
|
|
194
193
|
audience,
|
|
194
|
+
responseErrorMode = 'throw',
|
|
195
195
|
},
|
|
196
196
|
) => {
|
|
197
197
|
const chain = [];
|
|
@@ -201,7 +201,7 @@ const buildInterceptorChain = (
|
|
|
201
201
|
);
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
-
chain.push(
|
|
204
|
+
chain.push(...buildResponseErrorInterceptors(responseErrorMode));
|
|
205
205
|
|
|
206
206
|
if (cacheStore != null) {
|
|
207
207
|
chain.push(interceptors.cache({ store: cacheStore, methods: ['GET'] }));
|
|
@@ -223,6 +223,24 @@ const buildInterceptorChain = (
|
|
|
223
223
|
return chain;
|
|
224
224
|
};
|
|
225
225
|
|
|
226
|
+
const buildResponseErrorInterceptors = (responseErrorMode) =>
|
|
227
|
+
parseResponseErrorMode(responseErrorMode) === 'throw'
|
|
228
|
+
? [interceptors.responseError()]
|
|
229
|
+
: [];
|
|
230
|
+
|
|
231
|
+
const parseResponseErrorMode = (responseErrorMode) => {
|
|
232
|
+
if (responseErrorMode == null) {
|
|
233
|
+
return 'throw';
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (responseErrorMode === 'throw' || responseErrorMode === 'return') {
|
|
237
|
+
return responseErrorMode;
|
|
238
|
+
}
|
|
239
|
+
throw new Error(
|
|
240
|
+
`HttpClient: Unsupported responseErrorMode '${responseErrorMode}'`,
|
|
241
|
+
);
|
|
242
|
+
};
|
|
243
|
+
|
|
226
244
|
const parsePrefixUrl = (prefixUrl) => {
|
|
227
245
|
const url = new URL(prefixUrl);
|
|
228
246
|
return {
|