@dynatrace-sdk/http-client 1.0.8 → 1.0.10
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/CHANGELOG.md +14 -0
- package/cjs/index.js +50 -8
- package/docs/DOCS.md +104 -21
- package/esm/index.js +50 -8
- package/package.json +1 -1
- package/types/packages/http-client/src/index.d.ts +1 -1
- package/types/packages/http-client/src/lib/platform/http-client-response-error.d.ts +4 -1
- package/types/packages/http-client/src/lib/types/http-client-abort-error.d.ts +1 -0
- package/types/packages/http-client/src/lib/types/http-client-network-error.d.ts +1 -0
- package/types/packages/http-client/src/lib/types/http-client-request-error.d.ts +1 -0
- package/types/packages/http-client/src/lib/types/http-client-response-error.d.ts +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
@dynatrace-sdk/http-client
|
|
4
4
|
|
|
5
|
+
## 1.0.10
|
|
6
|
+
|
|
7
|
+
### Patch Changes
|
|
8
|
+
|
|
9
|
+
- Fix sending files in a non-browser environment.
|
|
10
|
+
|
|
11
|
+
## 1.0.9
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Expose typeguards for http-client errors.
|
|
16
|
+
- Allow to read http-client`s body multiple times.
|
|
17
|
+
- Additional `details` field in `httpClientResponseErrorSerializer`.
|
|
18
|
+
|
|
5
19
|
## 1.0.8
|
|
6
20
|
|
|
7
21
|
### Patch Changes
|
package/cjs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright 2023 Dynatrace LLC
|
|
3
|
+
* Copyright 2023-2024 Dynatrace LLC
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
6
6
|
* You may obtain a copy of the License at
|
|
@@ -46,10 +46,34 @@ __export(src_exports, {
|
|
|
46
46
|
PlatformHttpClientResponse: () => HttpClientResponse,
|
|
47
47
|
PlatformHttpClientResponseError: () => HttpClientResponseError,
|
|
48
48
|
PlatformUnsupportedOperationError: () => UnsupportedOperationError,
|
|
49
|
-
httpClient: () => httpClient
|
|
49
|
+
httpClient: () => httpClient,
|
|
50
|
+
isHttpClientAbortError: () => isHttpClientAbortError,
|
|
51
|
+
isHttpClientNetworkError: () => isHttpClientNetworkError,
|
|
52
|
+
isHttpClientRequestError: () => isHttpClientRequestError,
|
|
53
|
+
isHttpClientResponseError: () => isHttpClientResponseError
|
|
50
54
|
});
|
|
51
55
|
module.exports = __toCommonJS(src_exports);
|
|
52
56
|
|
|
57
|
+
// packages/http-client/src/lib/types/http-client-request-error.ts
|
|
58
|
+
function isHttpClientRequestError(e) {
|
|
59
|
+
return e?.name === "RequestError";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// packages/http-client/src/lib/types/http-client-abort-error.ts
|
|
63
|
+
function isHttpClientAbortError(e) {
|
|
64
|
+
return e?.name === "AbortError";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// packages/http-client/src/lib/types/http-client-response-error.ts
|
|
68
|
+
function isHttpClientResponseError(e) {
|
|
69
|
+
return e?.name === "ResponseError";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// packages/http-client/src/lib/types/http-client-network-error.ts
|
|
73
|
+
function isHttpClientNetworkError(e) {
|
|
74
|
+
return e?.name === "NetworkError";
|
|
75
|
+
}
|
|
76
|
+
|
|
53
77
|
// packages/http-client/src/lib/platform/abort-controller.ts
|
|
54
78
|
var AbortController = globalThis.AbortController;
|
|
55
79
|
|
|
@@ -160,7 +184,7 @@ async function encodeFormDataRequestBody(body) {
|
|
|
160
184
|
);
|
|
161
185
|
break;
|
|
162
186
|
case "binary": {
|
|
163
|
-
if (field.value instanceof File) {
|
|
187
|
+
if (File && field.value instanceof File) {
|
|
164
188
|
const file = field.value;
|
|
165
189
|
formData.append(
|
|
166
190
|
field.name,
|
|
@@ -410,7 +434,7 @@ var HttpClientResponse = class {
|
|
|
410
434
|
return headers;
|
|
411
435
|
}
|
|
412
436
|
body(responseBodyType = "json") {
|
|
413
|
-
return decodeResponseBody(this.response, responseBodyType);
|
|
437
|
+
return decodeResponseBody(this.response.clone(), responseBodyType);
|
|
414
438
|
}
|
|
415
439
|
};
|
|
416
440
|
|
|
@@ -438,6 +462,7 @@ async function parseResponse(response) {
|
|
|
438
462
|
return {
|
|
439
463
|
message: getMessageFromObject(jsonBody.error) || getMessageFromObject(jsonBody) || consumedBody.substring(0, 100),
|
|
440
464
|
body: jsonBody,
|
|
465
|
+
errorCode: jsonBody.error?.code,
|
|
441
466
|
errorRef: jsonBody.error?.details?.errorRef,
|
|
442
467
|
traceId: jsonBody.error?.details?.traceId
|
|
443
468
|
};
|
|
@@ -457,21 +482,36 @@ async function parseResponse(response) {
|
|
|
457
482
|
var httpClientResponseErrorSerializer = async (error) => {
|
|
458
483
|
if (error instanceof HttpClientResponseError) {
|
|
459
484
|
const status = error.response.status;
|
|
460
|
-
const { message, body, errorRef, traceId } = await parseResponse(error.response);
|
|
485
|
+
const { message, body, errorRef, traceId, errorCode } = await parseResponse(error.response);
|
|
461
486
|
const optionalErrorRef = errorRef ? { errorRef } : {};
|
|
462
487
|
const optionalTraceId = traceId ? { traceId } : {};
|
|
488
|
+
const stringifiedBody = JSON.stringify(body);
|
|
489
|
+
const bodySample = stringifiedBody.length > 100 ? `${stringifiedBody.substring(0, 100)}\u2026` : stringifiedBody;
|
|
463
490
|
return {
|
|
464
491
|
name: error.name,
|
|
465
492
|
status,
|
|
466
493
|
message,
|
|
467
494
|
stack: error.stack,
|
|
468
495
|
type: import_error_handlers.ErrorType.HTTP,
|
|
469
|
-
|
|
496
|
+
details: {
|
|
497
|
+
url: error.response.url,
|
|
498
|
+
method: error.requestMethod,
|
|
499
|
+
errorCode,
|
|
500
|
+
responseContentType: getHeaderValue(error.response, "content-type"),
|
|
501
|
+
responseContentLength: getHeaderValue(error.response, "content-length"),
|
|
502
|
+
responseDtSource: getHeaderValue(error.response, "dynatrace-response-source"),
|
|
503
|
+
responseBodySample: bodySample
|
|
504
|
+
},
|
|
470
505
|
...optionalErrorRef,
|
|
471
506
|
...optionalTraceId
|
|
472
507
|
};
|
|
473
508
|
}
|
|
474
509
|
};
|
|
510
|
+
var getHeaderValue = (response, header) => {
|
|
511
|
+
const { headers = {} } = response;
|
|
512
|
+
const headerKey = Object.keys(headers).find((key) => key.toLowerCase() === header.toLowerCase());
|
|
513
|
+
return headerKey ? headers[headerKey] : void 0;
|
|
514
|
+
};
|
|
475
515
|
var errorSerializerAdded = false;
|
|
476
516
|
function addHttpClientResponseErrorSerializer() {
|
|
477
517
|
if (!errorSerializerAdded && (0, import_error_handlers.isGlobalErrorSerializerSupported)()) {
|
|
@@ -484,10 +524,12 @@ function addHttpClientResponseErrorSerializer() {
|
|
|
484
524
|
var HttpClientResponseError = class extends BaseError {
|
|
485
525
|
name = "ResponseError";
|
|
486
526
|
response;
|
|
487
|
-
|
|
527
|
+
requestMethod;
|
|
528
|
+
constructor(response, data) {
|
|
488
529
|
addHttpClientResponseErrorSerializer();
|
|
489
530
|
super(`HTTP Error ${response.status}: ${response.statusText}`);
|
|
490
531
|
this.response = new HttpClientResponse(response);
|
|
532
|
+
this.requestMethod = data?.requestMethod;
|
|
491
533
|
}
|
|
492
534
|
};
|
|
493
535
|
|
|
@@ -515,7 +557,7 @@ var HttpClient = class {
|
|
|
515
557
|
if (ok) {
|
|
516
558
|
return new HttpClientResponse(response);
|
|
517
559
|
} else {
|
|
518
|
-
throw new HttpClientResponseError(response);
|
|
560
|
+
throw new HttpClientResponseError(response, { requestMethod: options.method });
|
|
519
561
|
}
|
|
520
562
|
}
|
|
521
563
|
};
|
package/docs/DOCS.md
CHANGED
|
@@ -7,7 +7,7 @@ description: An environment agnostic (browser/deno vs nodejs) abstraction layer
|
|
|
7
7
|
|
|
8
8
|
An environment agnostic (browser/deno vs nodejs) abstraction layer for making HTTP calls.
|
|
9
9
|
|
|
10
|
-
import NpmLogo from
|
|
10
|
+
import NpmLogo from "@site/static/img/npm-logo.png";
|
|
11
11
|
|
|
12
12
|
<div class="row margin-bottom--md">
|
|
13
13
|
<div class="col">
|
|
@@ -16,8 +16,10 @@ import NpmLogo from '@site/static/img/npm-logo.png';
|
|
|
16
16
|
<img className="no-zoom" width="20px" src={NpmLogo} />
|
|
17
17
|
</a>
|
|
18
18
|
</div>
|
|
19
|
-
<div class="col" style={{textAlign:
|
|
20
|
-
<a href="https://www.npmjs.com/package/@dynatrace-sdk/http-client/v/1.0.
|
|
19
|
+
<div class="col" style={{ textAlign: "right" }}>
|
|
20
|
+
<a href="https://www.npmjs.com/package/@dynatrace-sdk/http-client/v/1.0.10">
|
|
21
|
+
v1.0.10
|
|
22
|
+
</a>
|
|
21
23
|
</div>
|
|
22
24
|
</div>
|
|
23
25
|
|
|
@@ -88,7 +90,7 @@ Returns the binary data in a form that conforms to the specified type.
|
|
|
88
90
|
| ------------------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
89
91
|
| type<sup>\*required</sup> | T | <p>The type of the data. Can be <code>text</code>, <code>json</code>, <code>array-buffer</code>, <code>blob</code>, <code>readable-stream</code>, <code>buffer</code> or <code>stream</code>.</p> |
|
|
90
92
|
|
|
91
|
-
|
|
93
|
+
{/* no returns */}
|
|
92
94
|
|
|
93
95
|
</div>
|
|
94
96
|
|
|
@@ -107,7 +109,7 @@ Returns the binary data in a form that conforms to the specified type.
|
|
|
107
109
|
| ------------------------- | --------------------------------------------------------------------------------------------- |
|
|
108
110
|
| data<sup>\*required</sup> | ArrayBuffer | Blob | ReadableStream<Uint8Array> | Buffer | Readable |
|
|
109
111
|
|
|
110
|
-
|
|
112
|
+
{/* no returns */}
|
|
111
113
|
|
|
112
114
|
</div>
|
|
113
115
|
|
|
@@ -126,7 +128,7 @@ Returns the binary data in a form that conforms to the specified type.
|
|
|
126
128
|
| ------------------------- | ---------------------------------------------------------------------------------------- |
|
|
127
129
|
| json<sup>\*required</sup> | <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any">any</a> |
|
|
128
130
|
|
|
129
|
-
|
|
131
|
+
{/* no returns */}
|
|
130
132
|
|
|
131
133
|
</div>
|
|
132
134
|
|
|
@@ -145,7 +147,7 @@ Returns the binary data in a form that conforms to the specified type.
|
|
|
145
147
|
| ------------------------- | ----------------------------------------------------------------------------- |
|
|
146
148
|
| text<sup>\*required</sup> | <a href="https://developer.mozilla.org/en-US/docs/Glossary/String">string</a> |
|
|
147
149
|
|
|
148
|
-
|
|
150
|
+
{/* no returns */}
|
|
149
151
|
|
|
150
152
|
</div>
|
|
151
153
|
|
|
@@ -194,7 +196,7 @@ Currently, only the requests to the Platform Services and the Serverless Functio
|
|
|
194
196
|
|
|
195
197
|
<div class="padding-left--md">
|
|
196
198
|
|
|
197
|
-
|
|
199
|
+
{/* no parameters */}
|
|
198
200
|
|
|
199
201
|
</div>
|
|
200
202
|
|
|
@@ -226,7 +228,7 @@ when one of the following situations happen:
|
|
|
226
228
|
|
|
227
229
|
#### Returns
|
|
228
230
|
|
|
229
|
-
Promise<HttpClientResponse> A promise that resolves with the response object or rejects with an error.
|
|
231
|
+
Promise&lt;HttpClientResponse&gt; A promise that resolves with the response object or rejects with an error.
|
|
230
232
|
|
|
231
233
|
</div>
|
|
232
234
|
|
|
@@ -363,7 +365,7 @@ Returns the response body in a form that conforms to the specified response body
|
|
|
363
365
|
| ------------------------------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
364
366
|
| responseBodyType<sup>\*required</sup> | T | <p>The type of the response body. Can be <code>text</code>, <code>json</code>, <code>binary</code>, <code>form-data</code>, <code>array-buffer</code>, <code>blob</code>, <code>readable-stream</code>, <code>buffer</code> or <code>stream</code>. The default response body type is <code>json</code>.</p> |
|
|
365
367
|
|
|
366
|
-
|
|
368
|
+
{/* no returns */}
|
|
367
369
|
|
|
368
370
|
</div>
|
|
369
371
|
|
|
@@ -375,15 +377,17 @@ Represents a situation when the response status is considered invalid by the sta
|
|
|
375
377
|
|
|
376
378
|
#### Constructors
|
|
377
379
|
|
|
378
|
-
##### new PlatformHttpClientResponseError(response)
|
|
380
|
+
##### new PlatformHttpClientResponseError(response,data?)
|
|
379
381
|
|
|
380
382
|
<div class="padding-left--md">
|
|
381
383
|
|
|
382
384
|
#### Parameters
|
|
383
385
|
|
|
384
|
-
| Name | Type
|
|
385
|
-
| ----------------------------- |
|
|
386
|
-
| response<sup>\*required</sup> | Response
|
|
386
|
+
| Name | Type |
|
|
387
|
+
| ----------------------------- | ----------------------------------------------------------------------------- |
|
|
388
|
+
| response<sup>\*required</sup> | Response |
|
|
389
|
+
| data | Object |
|
|
390
|
+
| data.requestMethod | <a href="https://developer.mozilla.org/en-US/docs/Glossary/String">string</a> |
|
|
387
391
|
|
|
388
392
|
</div>
|
|
389
393
|
|
|
@@ -394,6 +398,7 @@ Represents a situation when the response status is considered invalid by the sta
|
|
|
394
398
|
| cause | <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any">any</a> | The error root cause. |
|
|
395
399
|
| message<sup>\*required</sup> | <a href="https://developer.mozilla.org/en-US/docs/Glossary/String">string</a> | |
|
|
396
400
|
| name<sup>\*required</sup> | "ResponseError" | The error name. |
|
|
401
|
+
| requestMethod | <a href="https://developer.mozilla.org/en-US/docs/Glossary/String">string</a> | |
|
|
397
402
|
| response<sup>\*required</sup> | <a href="#httpclientresponse">HttpClientResponse</a> | The response. |
|
|
398
403
|
| stack | <a href="https://developer.mozilla.org/en-US/docs/Glossary/String">string</a> | |
|
|
399
404
|
| prepareStackTrace | Object | |
|
|
@@ -456,9 +461,9 @@ Aborts a request before it has completed.
|
|
|
456
461
|
|
|
457
462
|
</div>
|
|
458
463
|
|
|
459
|
-
|
|
464
|
+
{/* no parameters */}
|
|
460
465
|
|
|
461
|
-
|
|
466
|
+
{/* no returns */}
|
|
462
467
|
|
|
463
468
|
</div>
|
|
464
469
|
|
|
@@ -487,9 +492,9 @@ Represents a signal object that allows to communicate with the request and abort
|
|
|
487
492
|
|
|
488
493
|
</div>
|
|
489
494
|
|
|
490
|
-
|
|
495
|
+
{/* no parameters */}
|
|
491
496
|
|
|
492
|
-
|
|
497
|
+
{/* no returns */}
|
|
493
498
|
|
|
494
499
|
</div>
|
|
495
500
|
|
|
@@ -531,7 +536,7 @@ Returns the binary data in a form that conforms to the specified type.
|
|
|
531
536
|
| ------------------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
532
537
|
| type<sup>\*required</sup> | T | <p>The type of the data. Can be <code>text</code>, <code>json</code>, <code>array-buffer</code>, <code>blob</code>, <code>readable-stream</code>, <code>buffer</code> or <code>stream</code>.</p> |
|
|
533
538
|
|
|
534
|
-
|
|
539
|
+
{/* no returns */}
|
|
535
540
|
|
|
536
541
|
</div>
|
|
537
542
|
|
|
@@ -621,7 +626,7 @@ when one of the following situations happen:
|
|
|
621
626
|
|
|
622
627
|
#### Returns
|
|
623
628
|
|
|
624
|
-
Promise<HttpClientResponse> A promise that resolves with the response object or rejects with an error.
|
|
629
|
+
Promise&lt;HttpClientResponse&gt; A promise that resolves with the response object or rejects with an error.
|
|
625
630
|
|
|
626
631
|
</div>
|
|
627
632
|
|
|
@@ -721,7 +726,7 @@ Returns the response body in a form that conforms to the specified response body
|
|
|
721
726
|
| ---------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
722
727
|
| responseBodyType | T | <p>The type of the response body. Can be <code>text</code>, <code>json</code>, <code>binary</code>, <code>form-data</code>, <code>array-buffer</code>, <code>blob</code>, <code>readable-stream</code>, <code>buffer</code> or <code>stream</code>. The default response body type is <code>json</code>.</p> |
|
|
723
728
|
|
|
724
|
-
|
|
729
|
+
{/* no returns */}
|
|
725
730
|
|
|
726
731
|
</div>
|
|
727
732
|
|
|
@@ -825,3 +830,81 @@ Represents a plain text field. No filename and content type is set.
|
|
|
825
830
|
</div>
|
|
826
831
|
|
|
827
832
|
</div>
|
|
833
|
+
|
|
834
|
+
## Functions
|
|
835
|
+
|
|
836
|
+
### isHttpClientAbortError
|
|
837
|
+
|
|
838
|
+
<div class="padding-bottom--md">
|
|
839
|
+
<strong>isHttpClientAbortError(e): </strong>
|
|
840
|
+
|
|
841
|
+
<div class="padding-left--md">
|
|
842
|
+
|
|
843
|
+
</div>
|
|
844
|
+
|
|
845
|
+
#### Parameters
|
|
846
|
+
|
|
847
|
+
| Name | Type |
|
|
848
|
+
| ---------------------- | ---------------------------------------------------------------------------------------- |
|
|
849
|
+
| e<sup>\*required</sup> | <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any">any</a> |
|
|
850
|
+
|
|
851
|
+
{/* no returns */}
|
|
852
|
+
|
|
853
|
+
</div>
|
|
854
|
+
|
|
855
|
+
### isHttpClientNetworkError
|
|
856
|
+
|
|
857
|
+
<div class="padding-bottom--md">
|
|
858
|
+
<strong>isHttpClientNetworkError(e): </strong>
|
|
859
|
+
|
|
860
|
+
<div class="padding-left--md">
|
|
861
|
+
|
|
862
|
+
</div>
|
|
863
|
+
|
|
864
|
+
#### Parameters
|
|
865
|
+
|
|
866
|
+
| Name | Type |
|
|
867
|
+
| ---------------------- | ---------------------------------------------------------------------------------------- |
|
|
868
|
+
| e<sup>\*required</sup> | <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any">any</a> |
|
|
869
|
+
|
|
870
|
+
{/* no returns */}
|
|
871
|
+
|
|
872
|
+
</div>
|
|
873
|
+
|
|
874
|
+
### isHttpClientRequestError
|
|
875
|
+
|
|
876
|
+
<div class="padding-bottom--md">
|
|
877
|
+
<strong>isHttpClientRequestError(e): </strong>
|
|
878
|
+
|
|
879
|
+
<div class="padding-left--md">
|
|
880
|
+
|
|
881
|
+
</div>
|
|
882
|
+
|
|
883
|
+
#### Parameters
|
|
884
|
+
|
|
885
|
+
| Name | Type |
|
|
886
|
+
| ---------------------- | ---------------------------------------------------------------------------------------- |
|
|
887
|
+
| e<sup>\*required</sup> | <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any">any</a> |
|
|
888
|
+
|
|
889
|
+
{/* no returns */}
|
|
890
|
+
|
|
891
|
+
</div>
|
|
892
|
+
|
|
893
|
+
### isHttpClientResponseError
|
|
894
|
+
|
|
895
|
+
<div class="padding-bottom--md">
|
|
896
|
+
<strong>isHttpClientResponseError(e): </strong>
|
|
897
|
+
|
|
898
|
+
<div class="padding-left--md">
|
|
899
|
+
|
|
900
|
+
</div>
|
|
901
|
+
|
|
902
|
+
#### Parameters
|
|
903
|
+
|
|
904
|
+
| Name | Type |
|
|
905
|
+
| ---------------------- | ---------------------------------------------------------------------------------------- |
|
|
906
|
+
| e<sup>\*required</sup> | <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any">any</a> |
|
|
907
|
+
|
|
908
|
+
{/* no returns */}
|
|
909
|
+
|
|
910
|
+
</div>
|
package/esm/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright 2023 Dynatrace LLC
|
|
3
|
+
* Copyright 2023-2024 Dynatrace LLC
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
6
6
|
* You may obtain a copy of the License at
|
|
@@ -14,6 +14,26 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
+
// packages/http-client/src/lib/types/http-client-request-error.ts
|
|
18
|
+
function isHttpClientRequestError(e) {
|
|
19
|
+
return e?.name === "RequestError";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// packages/http-client/src/lib/types/http-client-abort-error.ts
|
|
23
|
+
function isHttpClientAbortError(e) {
|
|
24
|
+
return e?.name === "AbortError";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// packages/http-client/src/lib/types/http-client-response-error.ts
|
|
28
|
+
function isHttpClientResponseError(e) {
|
|
29
|
+
return e?.name === "ResponseError";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// packages/http-client/src/lib/types/http-client-network-error.ts
|
|
33
|
+
function isHttpClientNetworkError(e) {
|
|
34
|
+
return e?.name === "NetworkError";
|
|
35
|
+
}
|
|
36
|
+
|
|
17
37
|
// packages/http-client/src/lib/platform/abort-controller.ts
|
|
18
38
|
var AbortController = globalThis.AbortController;
|
|
19
39
|
|
|
@@ -124,7 +144,7 @@ async function encodeFormDataRequestBody(body) {
|
|
|
124
144
|
);
|
|
125
145
|
break;
|
|
126
146
|
case "binary": {
|
|
127
|
-
if (field.value instanceof File) {
|
|
147
|
+
if (File && field.value instanceof File) {
|
|
128
148
|
const file = field.value;
|
|
129
149
|
formData.append(
|
|
130
150
|
field.name,
|
|
@@ -374,7 +394,7 @@ var HttpClientResponse = class {
|
|
|
374
394
|
return headers;
|
|
375
395
|
}
|
|
376
396
|
body(responseBodyType = "json") {
|
|
377
|
-
return decodeResponseBody(this.response, responseBodyType);
|
|
397
|
+
return decodeResponseBody(this.response.clone(), responseBodyType);
|
|
378
398
|
}
|
|
379
399
|
};
|
|
380
400
|
|
|
@@ -406,6 +426,7 @@ async function parseResponse(response) {
|
|
|
406
426
|
return {
|
|
407
427
|
message: getMessageFromObject(jsonBody.error) || getMessageFromObject(jsonBody) || consumedBody.substring(0, 100),
|
|
408
428
|
body: jsonBody,
|
|
429
|
+
errorCode: jsonBody.error?.code,
|
|
409
430
|
errorRef: jsonBody.error?.details?.errorRef,
|
|
410
431
|
traceId: jsonBody.error?.details?.traceId
|
|
411
432
|
};
|
|
@@ -425,21 +446,36 @@ async function parseResponse(response) {
|
|
|
425
446
|
var httpClientResponseErrorSerializer = async (error) => {
|
|
426
447
|
if (error instanceof HttpClientResponseError) {
|
|
427
448
|
const status = error.response.status;
|
|
428
|
-
const { message, body, errorRef, traceId } = await parseResponse(error.response);
|
|
449
|
+
const { message, body, errorRef, traceId, errorCode } = await parseResponse(error.response);
|
|
429
450
|
const optionalErrorRef = errorRef ? { errorRef } : {};
|
|
430
451
|
const optionalTraceId = traceId ? { traceId } : {};
|
|
452
|
+
const stringifiedBody = JSON.stringify(body);
|
|
453
|
+
const bodySample = stringifiedBody.length > 100 ? `${stringifiedBody.substring(0, 100)}\u2026` : stringifiedBody;
|
|
431
454
|
return {
|
|
432
455
|
name: error.name,
|
|
433
456
|
status,
|
|
434
457
|
message,
|
|
435
458
|
stack: error.stack,
|
|
436
459
|
type: ErrorType.HTTP,
|
|
437
|
-
|
|
460
|
+
details: {
|
|
461
|
+
url: error.response.url,
|
|
462
|
+
method: error.requestMethod,
|
|
463
|
+
errorCode,
|
|
464
|
+
responseContentType: getHeaderValue(error.response, "content-type"),
|
|
465
|
+
responseContentLength: getHeaderValue(error.response, "content-length"),
|
|
466
|
+
responseDtSource: getHeaderValue(error.response, "dynatrace-response-source"),
|
|
467
|
+
responseBodySample: bodySample
|
|
468
|
+
},
|
|
438
469
|
...optionalErrorRef,
|
|
439
470
|
...optionalTraceId
|
|
440
471
|
};
|
|
441
472
|
}
|
|
442
473
|
};
|
|
474
|
+
var getHeaderValue = (response, header) => {
|
|
475
|
+
const { headers = {} } = response;
|
|
476
|
+
const headerKey = Object.keys(headers).find((key) => key.toLowerCase() === header.toLowerCase());
|
|
477
|
+
return headerKey ? headers[headerKey] : void 0;
|
|
478
|
+
};
|
|
443
479
|
var errorSerializerAdded = false;
|
|
444
480
|
function addHttpClientResponseErrorSerializer() {
|
|
445
481
|
if (!errorSerializerAdded && isGlobalErrorSerializerSupported()) {
|
|
@@ -452,10 +488,12 @@ function addHttpClientResponseErrorSerializer() {
|
|
|
452
488
|
var HttpClientResponseError = class extends BaseError {
|
|
453
489
|
name = "ResponseError";
|
|
454
490
|
response;
|
|
455
|
-
|
|
491
|
+
requestMethod;
|
|
492
|
+
constructor(response, data) {
|
|
456
493
|
addHttpClientResponseErrorSerializer();
|
|
457
494
|
super(`HTTP Error ${response.status}: ${response.statusText}`);
|
|
458
495
|
this.response = new HttpClientResponse(response);
|
|
496
|
+
this.requestMethod = data?.requestMethod;
|
|
459
497
|
}
|
|
460
498
|
};
|
|
461
499
|
|
|
@@ -483,7 +521,7 @@ var HttpClient = class {
|
|
|
483
521
|
if (ok) {
|
|
484
522
|
return new HttpClientResponse(response);
|
|
485
523
|
} else {
|
|
486
|
-
throw new HttpClientResponseError(response);
|
|
524
|
+
throw new HttpClientResponseError(response, { requestMethod: options.method });
|
|
487
525
|
}
|
|
488
526
|
}
|
|
489
527
|
};
|
|
@@ -500,5 +538,9 @@ export {
|
|
|
500
538
|
HttpClientResponse as PlatformHttpClientResponse,
|
|
501
539
|
HttpClientResponseError as PlatformHttpClientResponseError,
|
|
502
540
|
UnsupportedOperationError as PlatformUnsupportedOperationError,
|
|
503
|
-
httpClient
|
|
541
|
+
httpClient,
|
|
542
|
+
isHttpClientAbortError,
|
|
543
|
+
isHttpClientNetworkError,
|
|
544
|
+
isHttpClientRequestError,
|
|
545
|
+
isHttpClientResponseError
|
|
504
546
|
};
|
package/package.json
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { AbortController, AbortSignal, BaseError, Binary, BinaryFormDataRequestField, BinaryFormDataResponseField, DataTypeError, DataTypes, FormDataRequestBody, FormDataRequestField, FormDataResponseBody, FormDataResponseField, Headers, HttpClient, HttpClientAbortError, HttpClientNetworkError, HttpClientRequestError, HttpClientRequestOptions, HttpClientResponse, HttpClientResponseError, JsonFormDataRequestField, RequestBodyTypes, ResponseBodyTypes, StatusValidator, TextFormDataRequestField, TextFormDataResponseField, UnsupportedOperationError, } from './lib/types';
|
|
1
|
+
export { AbortController, AbortSignal, BaseError, Binary, BinaryFormDataRequestField, BinaryFormDataResponseField, DataTypeError, DataTypes, FormDataRequestBody, FormDataRequestField, FormDataResponseBody, FormDataResponseField, Headers, HttpClient, HttpClientAbortError, HttpClientNetworkError, HttpClientRequestError, HttpClientRequestOptions, HttpClientResponse, HttpClientResponseError, JsonFormDataRequestField, RequestBodyTypes, ResponseBodyTypes, StatusValidator, TextFormDataRequestField, TextFormDataResponseField, UnsupportedOperationError, isHttpClientResponseError, isHttpClientAbortError, isHttpClientNetworkError, isHttpClientRequestError, } from './lib/types';
|
|
2
2
|
export { AbortController as PlatformAbortController, AbortSignal as PlatformAbortSignal, BaseError as PlatformBaseError, Binary as PlatformBinary, DataTypeError as PlatformDataTypeError, HttpClient as PlatformHttpClient, HttpClientAbortError as PlatformHttpClientAbortError, HttpClientRequestError as PlatformHttpClientRequestError, HttpClientResponse as PlatformHttpClientResponse, HttpClientResponseError as PlatformHttpClientResponseError, UnsupportedOperationError as PlatformUnsupportedOperationError, httpClient, } from './lib/platform';
|
|
@@ -4,5 +4,8 @@ import { BaseError } from './base-error';
|
|
|
4
4
|
export declare class HttpClientResponseError extends BaseError implements types.HttpClientResponseError {
|
|
5
5
|
readonly name = "ResponseError";
|
|
6
6
|
readonly response: HttpClientResponse;
|
|
7
|
-
|
|
7
|
+
readonly requestMethod?: string;
|
|
8
|
+
constructor(response: Response, data?: {
|
|
9
|
+
requestMethod?: string;
|
|
10
|
+
});
|
|
8
11
|
}
|