@sap-ux/backend-proxy-middleware 0.6.9 → 0.6.12

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.
@@ -9,6 +9,7 @@
9
9
  "error": {
10
10
  "emptyUsername": "Username can not be empty.",
11
11
  "emptyPassword": "Password can not be empty.",
12
- "sslProxy": "You are trying to connect to a server with a self signed certificate. Please check (https://help.sap.com/viewer/17d50220bcd848aa854c9c182d65b699/Latest/en-US/4b318bede7eb4021a8be385c46c74045.html) for guidance."
12
+ "sslProxy": "You are trying to connect to a server with a self signed certificate. Please check (https://help.sap.com/viewer/17d50220bcd848aa854c9c182d65b699/Latest/en-US/4b318bede7eb4021a8be385c46c74045.html) for guidance.",
13
+ "noCodeError": "Error {{-error}} thrown for request {{-request}}"
13
14
  }
14
15
  }
@@ -3,6 +3,7 @@ import type { ServerOptions } from 'http-proxy';
3
3
  import type { RequestHandler, Options } from 'http-proxy-middleware';
4
4
  import type { ClientRequest, IncomingMessage, ServerResponse } from 'http';
5
5
  import type { Logger } from '@sap-ux/logger';
6
+ import { ToolsLogger } from '@sap-ux/logger';
6
7
  import type { BackendConfig, DestinationBackendConfig } from './types';
7
8
  import type { BackendSystem } from '@sap-ux/store';
8
9
  import type { Url } from 'url';
@@ -27,20 +28,22 @@ export declare const ProxyEventHandlers: {
27
28
  * @param _res (not used)
28
29
  */
29
30
  onProxyRes(proxyRes: IncomingMessage, _req?: IncomingMessage | undefined, _res?: ServerResponse | undefined): void;
30
- /**
31
- * Specifically handling errors due to unsigned certificates.
32
- *
33
- * @param err the error thrown when proxying the request or processing the response
34
- * @param req request causing the error
35
- * @param _res (not used)
36
- * @param _target (not used)
37
- */
38
- onError(err: Error & {
39
- code?: string;
40
- }, req: IncomingMessage & {
41
- next?: Function;
42
- }, _res?: ServerResponse | undefined, _target?: string | Partial<Url> | undefined): void;
43
31
  };
32
+ /**
33
+ * Specifically handling errors due to unsigned certificates and empty errors.
34
+ *
35
+ * @param err the error thrown when proxying the request or processing the response
36
+ * @param req request causing the error
37
+ * @param logger logger instance
38
+ * @param _res (not used)
39
+ * @param _target (not used)
40
+ */
41
+ export declare function proxyErrorHandler(err: Error & {
42
+ code?: string;
43
+ }, req: IncomingMessage & {
44
+ next?: Function;
45
+ originalUrl?: string;
46
+ }, logger: ToolsLogger, _res?: ServerResponse, _target?: string | Partial<Url>): void;
44
47
  /**
45
48
  * Collection of path rewrite functions.
46
49
  */
@@ -101,7 +104,7 @@ export declare function enhanceConfigForSystem(proxyOptions: Options & {
101
104
  * @param logger optional logger instance
102
105
  * @returns options for the http-proxy-middleware
103
106
  */
104
- export declare function generateProxyMiddlewareOptions(backend: BackendConfig, options?: Options, logger?: Logger): Promise<Options>;
107
+ export declare function generateProxyMiddlewareOptions(backend: BackendConfig, options?: Options, logger?: ToolsLogger): Promise<Options>;
105
108
  /**
106
109
  * Generate an instance of the proxy middleware based on the input.
107
110
  *
@@ -110,5 +113,5 @@ export declare function generateProxyMiddlewareOptions(backend: BackendConfig, o
110
113
  * @param logger optional logger instance
111
114
  * @returns an instance of http-proxy-middleware
112
115
  */
113
- export declare function createProxy(backend: BackendConfig, options?: Options, logger?: Logger): Promise<RequestHandler>;
116
+ export declare function createProxy(backend: BackendConfig, options?: Options, logger?: ToolsLogger): Promise<RequestHandler>;
114
117
  //# sourceMappingURL=proxy.d.ts.map
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.createProxy = exports.generateProxyMiddlewareOptions = exports.enhanceConfigForSystem = exports.enhanceConfigsForDestination = exports.initI18n = exports.PathRewriters = exports.ProxyEventHandlers = void 0;
15
+ exports.createProxy = exports.generateProxyMiddlewareOptions = exports.enhanceConfigForSystem = exports.enhanceConfigsForDestination = exports.initI18n = exports.PathRewriters = exports.proxyErrorHandler = exports.ProxyEventHandlers = void 0;
16
16
  const https_proxy_agent_1 = require("https-proxy-agent");
17
17
  const http_proxy_middleware_1 = require("http-proxy-middleware");
18
18
  const i18next_1 = __importDefault(require("i18next"));
@@ -56,33 +56,39 @@ exports.ProxyEventHandlers = {
56
56
  header[i] = cookie;
57
57
  }
58
58
  }
59
- },
60
- /**
61
- * Specifically handling errors due to unsigned certificates.
62
- *
63
- * @param err the error thrown when proxying the request or processing the response
64
- * @param req request causing the error
65
- * @param _res (not used)
66
- * @param _target (not used)
67
- */
68
- onError(err, req, _res, _target) {
69
- if (err) {
70
- let error;
71
- if (err.code === 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY') {
72
- error = new Error(i18next_1.default.t('error.sslProxy'));
73
- }
74
- else {
75
- error = err;
76
- }
77
- if (typeof req.next === 'function') {
78
- req.next(error);
79
- }
80
- else {
81
- throw error;
82
- }
83
- }
84
59
  }
85
60
  };
61
+ /**
62
+ * Specifically handling errors due to unsigned certificates and empty errors.
63
+ *
64
+ * @param err the error thrown when proxying the request or processing the response
65
+ * @param req request causing the error
66
+ * @param logger logger instance
67
+ * @param _res (not used)
68
+ * @param _target (not used)
69
+ */
70
+ function proxyErrorHandler(err, req, logger, _res, _target) {
71
+ var _a;
72
+ if (err && ((_a = err.stack) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== 'error') {
73
+ let error;
74
+ if (err.code === 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY') {
75
+ error = new Error(i18next_1.default.t('error.sslProxy'));
76
+ }
77
+ else {
78
+ error = err;
79
+ }
80
+ if (typeof req.next === 'function') {
81
+ req.next(error);
82
+ }
83
+ else {
84
+ throw error;
85
+ }
86
+ }
87
+ else {
88
+ logger.debug(i18next_1.default.t('error.noCodeError', { error: JSON.stringify(err, null, 2), request: req.originalUrl }));
89
+ }
90
+ }
91
+ exports.proxyErrorHandler = proxyErrorHandler;
86
92
  /**
87
93
  * Return the SAP API Hub key either provided as environment variable (including .env file) or from the secure store when not running in AppStudio.
88
94
  * not found or error while extracting the key.
@@ -163,7 +169,11 @@ exports.PathRewriters = {
163
169
  };
164
170
  }
165
171
  else {
166
- return undefined;
172
+ // Display request path even if it was not rewritten
173
+ return (path) => {
174
+ log.info(path);
175
+ return path;
176
+ };
167
177
  }
168
178
  }
169
179
  };
@@ -271,7 +281,9 @@ function generateProxyMiddlewareOptions(backend, options = {}, logger = new logg
271
281
  var _a;
272
282
  return __awaiter(this, void 0, void 0, function* () {
273
283
  // add required options
274
- const proxyOptions = Object.assign(Object.assign({ headers: {} }, exports.ProxyEventHandlers), options);
284
+ const proxyOptions = Object.assign(Object.assign(Object.assign({ headers: {} }, exports.ProxyEventHandlers), { onError: (err, req, res, target) => {
285
+ proxyErrorHandler(err, req, logger, res, target);
286
+ } }), options);
275
287
  proxyOptions.changeOrigin = true;
276
288
  proxyOptions.logProvider = () => logger;
277
289
  // always set the target to the url provided in yaml
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "bugs": {
10
10
  "url": "https://github.com/SAP/open-ux-tools/issues?q=is%3Aopen+is%3Aissue+label%3Abug+label%3Abackend-proxy-middleware"
11
11
  },
12
- "version": "0.6.9",
12
+ "version": "0.6.12",
13
13
  "license": "Apache-2.0",
14
14
  "author": "@SAP/ux-tools-team",
15
15
  "main": "dist/index.js",
@@ -21,10 +21,10 @@
21
21
  "!dist/**/*.map"
22
22
  ],
23
23
  "dependencies": {
24
- "@sap-ux/axios-extension": "0.8.0",
25
- "@sap-ux/btp-utils": "0.10.2",
26
- "@sap-ux/logger": "0.2.1",
27
- "@sap-ux/store": "0.3.1",
24
+ "@sap-ux/axios-extension": "0.9.0",
25
+ "@sap-ux/btp-utils": "0.10.3",
26
+ "@sap-ux/logger": "0.2.2",
27
+ "@sap-ux/store": "0.3.2",
28
28
  "chalk": "4.1.2",
29
29
  "dotenv": "16.0.0",
30
30
  "express": "4.17.2",