@dynatrace-sdk/http-client 1.0.7 → 1.0.9

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 CHANGED
@@ -2,6 +2,23 @@
2
2
 
3
3
  @dynatrace-sdk/http-client
4
4
 
5
+ ## 1.0.9
6
+
7
+ ### Patch Changes
8
+
9
+ - Expose typeguards for http-client errors.
10
+ - Allow to read http-client`s body multiple times.
11
+ - Additional `details` field in `httpClientResponseErrorSerializer`.
12
+
13
+ ## 1.0.8
14
+
15
+ ### Patch Changes
16
+
17
+ - Add optional `traceId` to `SerializedError`.
18
+ - Updated dependencies
19
+
20
+ - @dynatrace-sdk/error-handlers@1.2.0
21
+
5
22
  ## 1.0.7
6
23
 
7
24
  ### Patch Changes
package/cjs/index.js CHANGED
@@ -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
 
@@ -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,7 +462,9 @@ async function parseResponse(response) {
438
462
  return {
439
463
  message: getMessageFromObject(jsonBody.error) || getMessageFromObject(jsonBody) || consumedBody.substring(0, 100),
440
464
  body: jsonBody,
441
- errorRef: jsonBody.error?.details?.errorRef
465
+ errorCode: jsonBody.error?.code,
466
+ errorRef: jsonBody.error?.details?.errorRef,
467
+ traceId: jsonBody.error?.details?.traceId
442
468
  };
443
469
  } else {
444
470
  return {
@@ -456,19 +482,36 @@ async function parseResponse(response) {
456
482
  var httpClientResponseErrorSerializer = async (error) => {
457
483
  if (error instanceof HttpClientResponseError) {
458
484
  const status = error.response.status;
459
- const { message, body, errorRef } = await parseResponse(error.response);
485
+ const { message, body, errorRef, traceId, errorCode } = await parseResponse(error.response);
460
486
  const optionalErrorRef = errorRef ? { errorRef } : {};
487
+ const optionalTraceId = traceId ? { traceId } : {};
488
+ const stringifiedBody = JSON.stringify(body);
489
+ const bodySample = stringifiedBody.length > 100 ? `${stringifiedBody.substring(0, 100)}\u2026` : stringifiedBody;
461
490
  return {
462
491
  name: error.name,
463
492
  status,
464
493
  message,
465
494
  stack: error.stack,
466
495
  type: import_error_handlers.ErrorType.HTTP,
467
- body,
468
- ...optionalErrorRef
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
+ },
505
+ ...optionalErrorRef,
506
+ ...optionalTraceId
469
507
  };
470
508
  }
471
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
+ };
472
515
  var errorSerializerAdded = false;
473
516
  function addHttpClientResponseErrorSerializer() {
474
517
  if (!errorSerializerAdded && (0, import_error_handlers.isGlobalErrorSerializerSupported)()) {
@@ -481,10 +524,12 @@ function addHttpClientResponseErrorSerializer() {
481
524
  var HttpClientResponseError = class extends BaseError {
482
525
  name = "ResponseError";
483
526
  response;
484
- constructor(response) {
527
+ requestMethod;
528
+ constructor(response, data) {
485
529
  addHttpClientResponseErrorSerializer();
486
530
  super(`HTTP Error ${response.status}: ${response.statusText}`);
487
531
  this.response = new HttpClientResponse(response);
532
+ this.requestMethod = data?.requestMethod;
488
533
  }
489
534
  };
490
535
 
@@ -512,7 +557,7 @@ var HttpClient = class {
512
557
  if (ok) {
513
558
  return new HttpClientResponse(response);
514
559
  } else {
515
- throw new HttpClientResponseError(response);
560
+ throw new HttpClientResponseError(response, { requestMethod: options.method });
516
561
  }
517
562
  }
518
563
  };
package/docs/DOCS.md CHANGED
@@ -17,7 +17,7 @@ import NpmLogo from '@site/static/img/npm-logo.png';
17
17
  </a>
18
18
  </div>
19
19
  <div class="col" style={{textAlign: 'right'}}>
20
- <a href="https://www.npmjs.com/package/@dynatrace-sdk/http-client/v/1.0.7">v1.0.7</a>
20
+ <a href="https://www.npmjs.com/package/@dynatrace-sdk/http-client/v/1.0.9">v1.0.9</a>
21
21
  </div>
22
22
  </div>
23
23
 
@@ -375,15 +375,17 @@ Represents a situation when the response status is considered invalid by the sta
375
375
 
376
376
  #### Constructors
377
377
 
378
- ##### new PlatformHttpClientResponseError(response)
378
+ ##### new PlatformHttpClientResponseError(response,data?)
379
379
 
380
380
  <div class="padding-left--md">
381
381
 
382
382
  #### Parameters
383
383
 
384
- | Name | Type |
385
- | ----------------------------- | -------- |
386
- | response<sup>\*required</sup> | Response |
384
+ | Name | Type |
385
+ | ----------------------------- | ----------------------------------------------------------------------------- |
386
+ | response<sup>\*required</sup> | Response |
387
+ | data | Object |
388
+ | data.requestMethod | <a href="https://developer.mozilla.org/en-US/docs/Glossary/String">string</a> |
387
389
 
388
390
  </div>
389
391
 
@@ -394,6 +396,7 @@ Represents a situation when the response status is considered invalid by the sta
394
396
  | cause | <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any">any</a> | The error root cause. |
395
397
  | message<sup>\*required</sup> | <a href="https://developer.mozilla.org/en-US/docs/Glossary/String">string</a> | |
396
398
  | name<sup>\*required</sup> | "ResponseError" | The error name. |
399
+ | requestMethod | <a href="https://developer.mozilla.org/en-US/docs/Glossary/String">string</a> | |
397
400
  | response<sup>\*required</sup> | <a href="#httpclientresponse">HttpClientResponse</a> | The response. |
398
401
  | stack | <a href="https://developer.mozilla.org/en-US/docs/Glossary/String">string</a> | |
399
402
  | prepareStackTrace | Object | |
@@ -825,3 +828,81 @@ Represents a plain text field. No filename and content type is set.
825
828
  </div>
826
829
 
827
830
  </div>
831
+
832
+ ## Functions
833
+
834
+ ### isHttpClientAbortError
835
+
836
+ <div class="padding-bottom--md">
837
+ <strong>isHttpClientAbortError(e): </strong>
838
+
839
+ <div class="padding-left--md">
840
+
841
+ </div>
842
+
843
+ #### Parameters
844
+
845
+ | Name | Type |
846
+ | ---------------------- | ---------------------------------------------------------------------------------------- |
847
+ | e<sup>\*required</sup> | <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any">any</a> |
848
+
849
+ <!-- no returns -->
850
+
851
+ </div>
852
+
853
+ ### isHttpClientNetworkError
854
+
855
+ <div class="padding-bottom--md">
856
+ <strong>isHttpClientNetworkError(e): </strong>
857
+
858
+ <div class="padding-left--md">
859
+
860
+ </div>
861
+
862
+ #### Parameters
863
+
864
+ | Name | Type |
865
+ | ---------------------- | ---------------------------------------------------------------------------------------- |
866
+ | e<sup>\*required</sup> | <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any">any</a> |
867
+
868
+ <!-- no returns -->
869
+
870
+ </div>
871
+
872
+ ### isHttpClientRequestError
873
+
874
+ <div class="padding-bottom--md">
875
+ <strong>isHttpClientRequestError(e): </strong>
876
+
877
+ <div class="padding-left--md">
878
+
879
+ </div>
880
+
881
+ #### Parameters
882
+
883
+ | Name | Type |
884
+ | ---------------------- | ---------------------------------------------------------------------------------------- |
885
+ | e<sup>\*required</sup> | <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any">any</a> |
886
+
887
+ <!-- no returns -->
888
+
889
+ </div>
890
+
891
+ ### isHttpClientResponseError
892
+
893
+ <div class="padding-bottom--md">
894
+ <strong>isHttpClientResponseError(e): </strong>
895
+
896
+ <div class="padding-left--md">
897
+
898
+ </div>
899
+
900
+ #### Parameters
901
+
902
+ | Name | Type |
903
+ | ---------------------- | ---------------------------------------------------------------------------------------- |
904
+ | e<sup>\*required</sup> | <a href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any">any</a> |
905
+
906
+ <!-- no returns -->
907
+
908
+ </div>
package/esm/index.js CHANGED
@@ -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
 
@@ -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,7 +426,9 @@ async function parseResponse(response) {
406
426
  return {
407
427
  message: getMessageFromObject(jsonBody.error) || getMessageFromObject(jsonBody) || consumedBody.substring(0, 100),
408
428
  body: jsonBody,
409
- errorRef: jsonBody.error?.details?.errorRef
429
+ errorCode: jsonBody.error?.code,
430
+ errorRef: jsonBody.error?.details?.errorRef,
431
+ traceId: jsonBody.error?.details?.traceId
410
432
  };
411
433
  } else {
412
434
  return {
@@ -424,19 +446,36 @@ async function parseResponse(response) {
424
446
  var httpClientResponseErrorSerializer = async (error) => {
425
447
  if (error instanceof HttpClientResponseError) {
426
448
  const status = error.response.status;
427
- const { message, body, errorRef } = await parseResponse(error.response);
449
+ const { message, body, errorRef, traceId, errorCode } = await parseResponse(error.response);
428
450
  const optionalErrorRef = errorRef ? { errorRef } : {};
451
+ const optionalTraceId = traceId ? { traceId } : {};
452
+ const stringifiedBody = JSON.stringify(body);
453
+ const bodySample = stringifiedBody.length > 100 ? `${stringifiedBody.substring(0, 100)}\u2026` : stringifiedBody;
429
454
  return {
430
455
  name: error.name,
431
456
  status,
432
457
  message,
433
458
  stack: error.stack,
434
459
  type: ErrorType.HTTP,
435
- body,
436
- ...optionalErrorRef
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
+ },
469
+ ...optionalErrorRef,
470
+ ...optionalTraceId
437
471
  };
438
472
  }
439
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
+ };
440
479
  var errorSerializerAdded = false;
441
480
  function addHttpClientResponseErrorSerializer() {
442
481
  if (!errorSerializerAdded && isGlobalErrorSerializerSupported()) {
@@ -449,10 +488,12 @@ function addHttpClientResponseErrorSerializer() {
449
488
  var HttpClientResponseError = class extends BaseError {
450
489
  name = "ResponseError";
451
490
  response;
452
- constructor(response) {
491
+ requestMethod;
492
+ constructor(response, data) {
453
493
  addHttpClientResponseErrorSerializer();
454
494
  super(`HTTP Error ${response.status}: ${response.statusText}`);
455
495
  this.response = new HttpClientResponse(response);
496
+ this.requestMethod = data?.requestMethod;
456
497
  }
457
498
  };
458
499
 
@@ -480,7 +521,7 @@ var HttpClient = class {
480
521
  if (ok) {
481
522
  return new HttpClientResponse(response);
482
523
  } else {
483
- throw new HttpClientResponseError(response);
524
+ throw new HttpClientResponseError(response, { requestMethod: options.method });
484
525
  }
485
526
  }
486
527
  };
@@ -497,5 +538,9 @@ export {
497
538
  HttpClientResponse as PlatformHttpClientResponse,
498
539
  HttpClientResponseError as PlatformHttpClientResponseError,
499
540
  UnsupportedOperationError as PlatformUnsupportedOperationError,
500
- httpClient
541
+ httpClient,
542
+ isHttpClientAbortError,
543
+ isHttpClientNetworkError,
544
+ isHttpClientRequestError,
545
+ isHttpClientResponseError
501
546
  };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@dynatrace-sdk/http-client",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "An environment agnostic (browser/deno vs nodejs) abstraction layer for making HTTP calls.",
5
5
  "license": "Apache-2.0",
6
6
  "dependencies": {
7
- "@dynatrace-sdk/error-handlers": "^1.1.0"
7
+ "@dynatrace-sdk/error-handlers": "^1.2.0"
8
8
  },
9
9
  "main": "./cjs/index.js",
10
10
  "module": "./esm/index.js",
@@ -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
- constructor(response: Response);
7
+ readonly requestMethod?: string;
8
+ constructor(response: Response, data?: {
9
+ requestMethod?: string;
10
+ });
8
11
  }
@@ -12,3 +12,4 @@ export interface HttpClientAbortError extends BaseError {
12
12
  */
13
13
  readonly cause?: any;
14
14
  }
15
+ export declare function isHttpClientAbortError(e: any): e is HttpClientAbortError;
@@ -12,3 +12,4 @@ export interface HttpClientNetworkError extends BaseError {
12
12
  */
13
13
  readonly cause?: any;
14
14
  }
15
+ export declare function isHttpClientNetworkError(e: any): e is HttpClientNetworkError;
@@ -12,3 +12,4 @@ export interface HttpClientRequestError extends BaseError {
12
12
  */
13
13
  readonly cause?: any;
14
14
  }
15
+ export declare function isHttpClientRequestError(e: any): e is HttpClientRequestError;
@@ -17,3 +17,4 @@ export interface HttpClientResponseError extends BaseError {
17
17
  */
18
18
  readonly response: HttpClientResponse;
19
19
  }
20
+ export declare function isHttpClientResponseError(e: any): e is HttpClientResponseError;