lambda-essentials-ts 5.3.1 → 5.4.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/CHANGELOG.md CHANGED
@@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6
6
 
7
+ ## [5.4.0] - 2024-02-08
8
+
9
+ ### Added
10
+
11
+ HttpClient options now accept `clientExceptionStatusCodeMapOverride` which can be used to override the default HTTP error status code mapping. This is useful e.g. when a dependent service is not following REST-ful best practices and e.g. returns a 403 when there's an intermittent network error communicating with the authorization service
12
+
13
+ ## [5.3.2] - 2024-02-08
14
+
15
+ ### Fixed
16
+
17
+ Error details of external HTTP error responses are propagated correctly
18
+
7
19
  ## [5.3.1] - 2023-10-25
8
20
 
9
21
  ### Fixed
@@ -3,6 +3,6 @@ export declare class ClientException extends Exception {
3
3
  readonly serviceName: string;
4
4
  readonly originalStatusCode?: number;
5
5
  private static readonly statusCodeMap;
6
- constructor(serviceName: string, originalStatusCode?: number, details?: any);
6
+ constructor(serviceName: string, originalStatusCode?: number, details?: any, statusCodeMapOverride?: Record<number, number>);
7
7
  private static convertStatusCode;
8
8
  }
@@ -3,15 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ClientException = void 0;
4
4
  const exception_1 = require("./exception");
5
5
  class ClientException extends exception_1.Exception {
6
- constructor(serviceName, originalStatusCode, details) {
7
- super(`Dependent service "${serviceName}" returned error`, ClientException.convertStatusCode(originalStatusCode), details);
6
+ constructor(serviceName, originalStatusCode, details, statusCodeMapOverride) {
7
+ super(`Dependent service "${serviceName}" returned error`, ClientException.convertStatusCode(originalStatusCode, statusCodeMapOverride), details);
8
8
  this.serviceName = serviceName;
9
9
  this.originalStatusCode = originalStatusCode;
10
10
  }
11
- static convertStatusCode(originalStatusCode) {
11
+ static convertStatusCode(originalStatusCode, statusCodeMapOverride) {
12
12
  let statusCode = 503;
13
- if (originalStatusCode && this.statusCodeMap[originalStatusCode]) {
14
- statusCode = this.statusCodeMap[originalStatusCode];
13
+ const statusCodeMap = {
14
+ ...ClientException.statusCodeMap,
15
+ ...(statusCodeMapOverride || {}),
16
+ };
17
+ if (originalStatusCode && statusCodeMap[originalStatusCode]) {
18
+ statusCode = statusCodeMap[originalStatusCode];
15
19
  }
16
20
  return statusCode;
17
21
  }
@@ -17,6 +17,7 @@ export default class HttpClient {
17
17
  private readonly enableRetry;
18
18
  private readonly enableCache;
19
19
  private readonly timeout?;
20
+ private readonly clientExceptionStatusCodeMapOverride?;
20
21
  /**
21
22
  * Create a new Instance of the HttpClient
22
23
  */
@@ -100,6 +101,11 @@ export interface HttpClientOptions {
100
101
  * @link https://github.com/axios/axios/blob/main/README.md#request-config
101
102
  */
102
103
  timeout?: number;
104
+ /**
105
+ * Override the default mapping of status code when wrapping error responses returned by dependencies into ClientException.
106
+ * This is useful, when dependent services return incorrect status codes than then drive incorrect behavior upstream (e.g. 403 instead of 503)
107
+ */
108
+ clientExceptionStatusCodeMapOverride?: Record<number, number>;
103
109
  }
104
110
  /**
105
111
  * Log options object.
@@ -62,6 +62,7 @@ class HttpClient {
62
62
  this.enableCache = (_c = options === null || options === void 0 ? void 0 : options.enableCache) !== null && _c !== void 0 ? _c : false;
63
63
  this.enableRetry = (_d = options === null || options === void 0 ? void 0 : options.enableRetry) !== null && _d !== void 0 ? _d : false;
64
64
  this.timeout = options === null || options === void 0 ? void 0 : options.timeout;
65
+ this.clientExceptionStatusCodeMapOverride = options === null || options === void 0 ? void 0 : options.clientExceptionStatusCodeMapOverride;
65
66
  this.client =
66
67
  (_e = options === null || options === void 0 ? void 0 : options.client) !== null && _e !== void 0 ? _e : axios_1.default.create({
67
68
  adapter: (() => {
@@ -129,7 +130,7 @@ class HttpClient {
129
130
  const hostname = ((_a = error.config) === null || _a === void 0 ? void 0 : _a.url)
130
131
  ? new url_1.URL(error.config.url, error.config.baseURL).hostname
131
132
  : 'N/A';
132
- throw new clientException_1.ClientException(hostname, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.status, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.details);
133
+ throw new clientException_1.ClientException(hostname, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.status, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.details, this.clientExceptionStatusCodeMapOverride);
133
134
  });
134
135
  this.client.interceptors.response.use((response) => {
135
136
  if (this.logOptions.enabledLogs.includes(HttpLogType.responses)) {
@@ -168,7 +169,7 @@ class HttpClient {
168
169
  const hostname = ((_a = error.config) === null || _a === void 0 ? void 0 : _a.url)
169
170
  ? new url_1.URL(error.config.url, error.config.baseURL).hostname
170
171
  : 'N/A';
171
- throw new clientException_1.ClientException(hostname, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.status, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.details);
172
+ throw new clientException_1.ClientException(hostname, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.status, serializedAxiosError === null || serializedAxiosError === void 0 ? void 0 : serializedAxiosError.details, this.clientExceptionStatusCodeMapOverride);
172
173
  });
173
174
  }
174
175
  static extractRequestLogData(requestConfig) {
package/lib/util.js CHANGED
@@ -53,7 +53,7 @@ function serializeAxiosError(error) {
53
53
  const { status, data } = error.response;
54
54
  return {
55
55
  status: (_a = data.originalStatusCode) !== null && _a !== void 0 ? _a : status,
56
- details: data.details ? data.details : data, // Prevent wrapping of Exception
56
+ details: data.details && Object.keys(data.details).length > 0 ? data.details : data, // Prevent wrapping of Exception
57
57
  };
58
58
  }
59
59
  exports.serializeAxiosError = serializeAxiosError;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambda-essentials-ts",
3
- "version": "5.3.1",
3
+ "version": "5.4.0",
4
4
  "description": "A selection of the finest modules supporting authorization, API routing, error handling, logging and sending HTTP requests.",
5
5
  "main": "lib/index.js",
6
6
  "private": false,