@dynatrace-sdk/http-client 1.0.8 → 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,14 @@
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
+
5
13
  ## 1.0.8
6
14
 
7
15
  ### 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,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
- body,
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
- constructor(response) {
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
@@ -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.8">v1.0.8</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,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
- body,
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
- constructor(response) {
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynatrace-sdk/http-client",
3
- "version": "1.0.8",
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": {
@@ -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;