dev-approuter 0.1.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 ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # 0.1.0 (2023-08-25)
7
+
8
+
9
+ ### Features
10
+
11
+ * **dev-approuter:** initial commit ([#801](https://github.com/ui5-community/ui5-ecosystem-showcase/issues/801)) ([324890f](https://github.com/ui5-community/ui5-ecosystem-showcase/commit/324890f9916e6a3379685e353007c1ee6e73ca4c))
package/LICENSE ADDED
@@ -0,0 +1,253 @@
1
+ /*
2
+ * ----------------------------------------------------------------------------
3
+ * "THE DERIVED BEER-WARE LICENSE" (Revision 1):
4
+ * You can do whatever you want with this stuff. When you like it, just buy
5
+ * Volker Buzek (@vobu) a beer or buy Peter Muessig (@pmuessig) a coke when
6
+ * you see one of them.
7
+ *
8
+ * Inspired by the official: https://fedoraproject.org/wiki/Licensing/Beerware
9
+ *
10
+ * "THE BEER-WARE LICENSE" (Revision 42):
11
+ * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
12
+ * can do whatever you want with this stuff. If we meet some day, and you think
13
+ * this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp
14
+ * ----------------------------------------------------------------------------
15
+ */
16
+
17
+ ---
18
+
19
+
20
+ Apache License
21
+ Version 2.0, January 2004
22
+ http://www.apache.org/licenses/
23
+
24
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
25
+
26
+ 1. Definitions.
27
+
28
+ "License" shall mean the terms and conditions for use, reproduction,
29
+ and distribution as defined by Sections 1 through 9 of this document.
30
+
31
+ "Licensor" shall mean the copyright owner or entity authorized by
32
+ the copyright owner that is granting the License.
33
+
34
+ "Legal Entity" shall mean the union of the acting entity and all
35
+ other entities that control, are controlled by, or are under common
36
+ control with that entity. For the purposes of this definition,
37
+ "control" means (i) the power, direct or indirect, to cause the
38
+ direction or management of such entity, whether by contract or
39
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
40
+ outstanding shares, or (iii) beneficial ownership of such entity.
41
+
42
+ "You" (or "Your") shall mean an individual or Legal Entity
43
+ exercising permissions granted by this License.
44
+
45
+ "Source" form shall mean the preferred form for making modifications,
46
+ including but not limited to software source code, documentation
47
+ source, and configuration files.
48
+
49
+ "Object" form shall mean any form resulting from mechanical
50
+ transformation or translation of a Source form, including but
51
+ not limited to compiled object code, generated documentation,
52
+ and conversions to other media types.
53
+
54
+ "Work" shall mean the work of authorship, whether in Source or
55
+ Object form, made available under the License, as indicated by a
56
+ copyright notice that is included in or attached to the work
57
+ (an example is provided in the Appendix below).
58
+
59
+ "Derivative Works" shall mean any work, whether in Source or Object
60
+ form, that is based on (or derived from) the Work and for which the
61
+ editorial revisions, annotations, elaborations, or other modifications
62
+ represent, as a whole, an original work of authorship. For the purposes
63
+ of this License, Derivative Works shall not include works that remain
64
+ separable from, or merely link (or bind by name) to the interfaces of,
65
+ the Work and Derivative Works thereof.
66
+
67
+ "Contribution" shall mean any work of authorship, including
68
+ the original version of the Work and any modifications or additions
69
+ to that Work or Derivative Works thereof, that is intentionally
70
+ submitted to Licensor for inclusion in the Work by the copyright owner
71
+ or by an individual or Legal Entity authorized to submit on behalf of
72
+ the copyright owner. For the purposes of this definition, "submitted"
73
+ means any form of electronic, verbal, or written communication sent
74
+ to the Licensor or its representatives, including but not limited to
75
+ communication on electronic mailing lists, source code control systems,
76
+ and issue tracking systems that are managed by, or on behalf of, the
77
+ Licensor for the purpose of discussing and improving the Work, but
78
+ excluding communication that is conspicuously marked or otherwise
79
+ designated in writing by the copyright owner as "Not a Contribution."
80
+
81
+ "Contributor" shall mean Licensor and any individual or Legal Entity
82
+ on behalf of whom a Contribution has been received by Licensor and
83
+ subsequently incorporated within the Work.
84
+
85
+ 2. Grant of Copyright License. Subject to the terms and conditions of
86
+ this License, each Contributor hereby grants to You a perpetual,
87
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
88
+ copyright license to reproduce, prepare Derivative Works of,
89
+ publicly display, publicly perform, sublicense, and distribute the
90
+ Work and such Derivative Works in Source or Object form.
91
+
92
+ 3. Grant of Patent License. Subject to the terms and conditions of
93
+ this License, each Contributor hereby grants to You a perpetual,
94
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
95
+ (except as stated in this section) patent license to make, have made,
96
+ use, offer to sell, sell, import, and otherwise transfer the Work,
97
+ where such license applies only to those patent claims licensable
98
+ by such Contributor that are necessarily infringed by their
99
+ Contribution(s) alone or by combination of their Contribution(s)
100
+ with the Work to which such Contribution(s) was submitted. If You
101
+ institute patent litigation against any entity (including a
102
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
103
+ or a Contribution incorporated within the Work constitutes direct
104
+ or contributory patent infringement, then any patent licenses
105
+ granted to You under this License for that Work shall terminate
106
+ as of the date such litigation is filed.
107
+
108
+ 4. Redistribution. You may reproduce and distribute copies of the
109
+ Work or Derivative Works thereof in any medium, with or without
110
+ modifications, and in Source or Object form, provided that You
111
+ meet the following conditions:
112
+
113
+ (a) You must give any other recipients of the Work or
114
+ Derivative Works a copy of this License; and
115
+
116
+ (b) You must cause any modified files to carry prominent notices
117
+ stating that You changed the files; and
118
+
119
+ (c) You must retain, in the Source form of any Derivative Works
120
+ that You distribute, all copyright, patent, trademark, and
121
+ attribution notices from the Source form of the Work,
122
+ excluding those notices that do not pertain to any part of
123
+ the Derivative Works; and
124
+
125
+ (d) If the Work includes a "NOTICE" text file as part of its
126
+ distribution, then any Derivative Works that You distribute must
127
+ include a readable copy of the attribution notices contained
128
+ within such NOTICE file, excluding those notices that do not
129
+ pertain to any part of the Derivative Works, in at least one
130
+ of the following places: within a NOTICE text file distributed
131
+ as part of the Derivative Works; within the Source form or
132
+ documentation, if provided along with the Derivative Works; or,
133
+ within a display generated by the Derivative Works, if and
134
+ wherever such third-party notices normally appear. The contents
135
+ of the NOTICE file are for informational purposes only and
136
+ do not modify the License. You may add Your own attribution
137
+ notices within Derivative Works that You distribute, alongside
138
+ or as an addendum to the NOTICE text from the Work, provided
139
+ that such additional attribution notices cannot be construed
140
+ as modifying the License.
141
+
142
+ You may add Your own copyright statement to Your modifications and
143
+ may provide additional or different license terms and conditions
144
+ for use, reproduction, or distribution of Your modifications, or
145
+ for any such Derivative Works as a whole, provided Your use,
146
+ reproduction, and distribution of the Work otherwise complies with
147
+ the conditions stated in this License.
148
+
149
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
150
+ any Contribution intentionally submitted for inclusion in the Work
151
+ by You to the Licensor shall be under the terms and conditions of
152
+ this License, without any additional terms or conditions.
153
+ Notwithstanding the above, nothing herein shall supersede or modify
154
+ the terms of any separate license agreement you may have executed
155
+ with Licensor regarding such Contributions.
156
+
157
+ 6. Trademarks. This License does not grant permission to use the trade
158
+ names, trademarks, service marks, or product names of the Licensor,
159
+ except as required for reasonable and customary use in describing the
160
+ origin of the Work and reproducing the content of the NOTICE file.
161
+
162
+ 7. Disclaimer of Warranty. Unless required by applicable law or
163
+ agreed to in writing, Licensor provides the Work (and each
164
+ Contributor provides its Contributions) on an "AS IS" BASIS,
165
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
166
+ implied, including, without limitation, any warranties or conditions
167
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
168
+ PARTICULAR PURPOSE. You are solely responsible for determining the
169
+ appropriateness of using or redistributing the Work and assume any
170
+ risks associated with Your exercise of permissions under this License.
171
+
172
+ 8. Limitation of Liability. In no event and under no legal theory,
173
+ whether in tort (including negligence), contract, or otherwise,
174
+ unless required by applicable law (such as deliberate and grossly
175
+ negligent acts) or agreed to in writing, shall any Contributor be
176
+ liable to You for damages, including any direct, indirect, special,
177
+ incidental, or consequential damages of any character arising as a
178
+ result of this License or out of the use or inability to use the
179
+ Work (including but not limited to damages for loss of goodwill,
180
+ work stoppage, computer failure or malfunction, or any and all
181
+ other commercial damages or losses), even if such Contributor
182
+ has been advised of the possibility of such damages.
183
+
184
+ 9. Accepting Warranty or Additional Liability. While redistributing
185
+ the Work or Derivative Works thereof, You may choose to offer,
186
+ and charge a fee for, acceptance of support, warranty, indemnity,
187
+ or other liability obligations and/or rights consistent with this
188
+ License. However, in accepting such obligations, You may act only
189
+ on Your own behalf and on Your sole responsibility, not on behalf
190
+ of any other Contributor, and only if You agree to indemnify,
191
+ defend, and hold each Contributor harmless for any liability
192
+ incurred by, or claims asserted against, such Contributor by reason
193
+ of your accepting any such warranty or additional liability.
194
+
195
+ END OF TERMS AND CONDITIONS
196
+
197
+ APPENDIX: How to apply the Apache License to your work.
198
+
199
+ To apply the Apache License to your work, attach the following
200
+ boilerplate notice, with the fields enclosed by brackets "[]"
201
+ replaced with your own identifying information. (Don't include
202
+ the brackets!) The text should be enclosed in the appropriate
203
+ comment syntax for the file format. We also recommend that a
204
+ file or class name and description of purpose be included on the
205
+ same "printed page" as the copyright notice for easier
206
+ identification within third-party archives.
207
+
208
+ Copyright [yyyy] [name of copyright owner]
209
+
210
+ Licensed under the Apache License, Version 2.0 (the "License");
211
+ you may not use this file except in compliance with the License.
212
+ You may obtain a copy of the License at
213
+
214
+ http://www.apache.org/licenses/LICENSE-2.0
215
+
216
+ Unless required by applicable law or agreed to in writing, software
217
+ distributed under the License is distributed on an "AS IS" BASIS,
218
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
219
+ See the License for the specific language governing permissions and
220
+ limitations under the License.
221
+
222
+ ------------------------------------------------------------------------------
223
+ APIs
224
+
225
+ This project may include APIs to SAP or third party products or services. The use of these APIs, products and services may be subject to additional agreements. In no event shall the application of the Apache Software License, v.2 to this project grant any rights in or to these APIs, products or services that would alter, expand, be inconsistent with, or supersede any terms of these additional agreements. “API” means application programming interfaces, as well as their respective specifications and implementing code that allows other software products to communicate with or call on SAP or third party products or services (for example, SAP Enterprise Services, BAPIs, Idocs, RFCs and ABAP calls or other user exits) and may be made available through SAP or third party products, SDKs, documentation or other media.
226
+
227
+ ------------------------------------------------------------------------------
228
+ SUBCOMPONENTS
229
+
230
+ This project includes the following subcomponents that are subject to separate license terms.
231
+ Your use of these subcomponents is subject to the separate license terms applicable to
232
+ each subcomponent.
233
+
234
+ Component: deploy.sh
235
+ Licensor: Domenic Denicola
236
+ Website: https://gist.github.com/domenic/ec8b0fc8ab45f39403dd/e445116166c79d7ac35eb38a5d348d546f3d1620
237
+ License: MIT License
238
+ <year> = 2018
239
+ <copyright holders> = Domenic Denicola
240
+
241
+ ------------------------------------------------------------------------------
242
+
243
+ The MIT License (MIT)
244
+
245
+ Copyright <YEAR> <COPYRIGHT HOLDER>
246
+
247
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
248
+
249
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
250
+
251
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
252
+
253
+ ------------------------------------------------------------------------------
package/README.md ADDED
@@ -0,0 +1,179 @@
1
+ # dev-approuter
2
+
3
+ > :wave: This is a **community project** and there is no official support for this package! Feel free to use it, open issues, contribute, and help answering questions.
4
+
5
+ The `dev-approuter` is a dev time wrapper for the [SAP Application Router](https://www.npmjs.com/package/@sap/approuter) that can serve [UI5](https://ui5.sap.com/) and [SAP CAP](https://cap.cloud.sap/docs/) apps that are added as (dev)dependencies to the approuter's `package.json`. A few key notes to begin with:
6
+ - The `dev-approuter` utilizes the [SAP Application Router's extension API](https://help.sap.com/docs/btp/sap-business-technology-platform/extension-api-of-application-router) by adding UI5 servers as extensions - providing the full [UI5 Tooling](https://sap.github.io/ui5-tooling/v3/) experience.
7
+ - A linked SAP CAP app is started on a different port - this is to mimic a deployed architecture. The corresponding destination is automatically created for you.
8
+ - In order to safely separate development configuration from productive code, the `dev-approuter` introduces the concept of the `xs-dev.json` - think of it as an extension to the [`xs-app.json`](https://help.sap.com/docs/btp/sap-business-technology-platform/routing-configuration-file).
9
+ - As the name suggests, the `dev-approuter` is for development only and not meant to be used in production.
10
+
11
+ ---
12
+
13
+ 1. [Prerequisites](#prerequisites)
14
+ 2. [Starting the `dev-approuter`](#starting-the-dev-approuter)
15
+ 3. [Adding and serving apps](#adding-and-serving-apps)
16
+ - [UI5 apps](#ui5-apps)
17
+ - [SAP CAP apps](#sap-cap-apps)
18
+ 4. [The `xs-dev.json` file](#the-xs-devjson-file)
19
+ 5. [Using the `dev-approuter` and SAP Application Router simultaneously](#using-the-dev-approuter-and-sap-approuter-simultaneously)
20
+ 6. [Extending the `dev-approuter`](#extending-the-dev-approuter)
21
+
22
+ ## Prerequisites
23
+
24
+ - [Node.js](https://nodejs.org/en) version 18 or higher.
25
+
26
+ ## Starting the `dev-approuter`
27
+
28
+ The `dev-approuter` is a wrapper for the SAP Application Router, meaning your current (productive) approuter configuration will also work with the `dev-approuter`, with the option to add dev time configuration to it (see [xs-dev.json](#the-xs-devjson-file)).
29
+
30
+ 1. Install the `dev-approuter` as a dev dependency:
31
+ ```bash
32
+ npm install dev-approuter --save-dev
33
+ ```
34
+
35
+ 2. Add the following script to the `scripts` section of your approuter's `package.json` file:
36
+ ```json
37
+ {
38
+ ...
39
+ "scripts": {
40
+ ...
41
+ "dev": "node node_modules/dev-approuter"
42
+ }
43
+ }
44
+ ```
45
+
46
+ 3. Start the `dev-approuter`:
47
+ ```bash
48
+ npm run dev
49
+ ```
50
+
51
+ 4. The `dev-approuter` starts on port 5000 by default, just like the SAP Application Router. If that port is already in use on your machine (hello Mac users :wave:), you can set another port via the `default-env.json` file:
52
+ ```json
53
+ {
54
+ "PORT": 5001,
55
+ ...
56
+ }
57
+ ```
58
+
59
+ ## Adding and serving apps
60
+
61
+ To add a UI5 or SAP CAP app to the `dev-approuter`, add it to the `devDependencies` section of your approuter's `package.json` file:
62
+
63
+ ```json
64
+ {
65
+ ...
66
+ "devDependencies": {
67
+ ...
68
+ "my-ui5-app": "path/to/ui5-app",
69
+ "my-cap-app": "path/to/cap-app"
70
+ }
71
+ }
72
+ ```
73
+
74
+ ### UI5 apps
75
+
76
+ For UI5 apps the `metadata.name` (as defined in the `ui5.yaml`) is used as the mount path by default. This can be overwritten with via a `customConfiguration.mountPath` in the `ui5.yaml`:
77
+
78
+ ```yaml
79
+ specVersion: "3.0"
80
+ metadata:
81
+ name: ui5-app # default mount path would be /ui5-app
82
+ type: application
83
+ customConfiguration:
84
+ mountPath: /my-custom-mount-path # overwrites the default mount path
85
+ ```
86
+
87
+ The above configuration result in the UI5 app being available at `http://localhost:5000/my-custom-mount-path`.
88
+
89
+ ### SAP CAP apps
90
+
91
+ SAP CAP apps are started on a separate port (4004 by default, can be overwritten via `CAP_PORT`). The `dev-approuter` automatically creates a route and destination behind the scenes, so that you (and your UI5 apps) can reach your SAP CAP services directly at their service path, but through the `dev-approuter`, e.g. `http://localhost:5000/my-cap-service`.
92
+
93
+ There is no need manually create a destination for you SAP CAP app, unless you want to overwrite the [default destination configuration](./lib/helpers.js#L81-L85). In this case, create a new destination in a `default-env.json`. The destination's name has to match the module name as declared in the approuter's `devDependencies`:
94
+
95
+ ```json
96
+ {
97
+ "CAP_PORT": 4005,
98
+ "destinations": [
99
+ {
100
+ "Name": "my-cap-app",
101
+ "Authentication": "NoAuthentication",
102
+ "ProxyType": "Internet",
103
+ "Type": "HTTP",
104
+ "URL": "http://localhost:4005",
105
+ "forwardAuthToken": true
106
+ }
107
+ ],
108
+ ...
109
+ }
110
+ ```
111
+
112
+ ## The `xs-dev.json` file
113
+
114
+ The `dev-approuter` introduces the concept of an `xs-dev.json` file, which works like a regular [`xs-app.json`](https://help.sap.com/docs/btp/sap-business-technology-platform/routing-configuration-file) file, but is used by the `dev-approuter` exclusively (meaning it's ignored by the SAP Application Router). The idea behind this concept is to safely separate dev time configuration from productive code.
115
+
116
+ The `xs-dev.json` follows the same logic and syntax as the [`xs-app.json`](https://help.sap.com/docs/btp/sap-business-technology-platform/routing-configuration-file) file, but has one additional key feature: You can add a `dependency` to a `route`, which links it to a UI5 or SAP CAP app.
117
+
118
+ Look at the following example `xs-dev.json` that defines different `authenticationType`s for different UI5 apps:
119
+
120
+ ```json
121
+ {
122
+ "welcomeFile": "index.html",
123
+ "authenticationMethod": "route",
124
+ "routes": [
125
+ {
126
+ "dependency": "my-ui5-app1",
127
+ "authenticationType": "none"
128
+ },
129
+ {
130
+ "dependency": "my-ui5-app2",
131
+ "authenticationType": "xsuaa"
132
+ }
133
+ ]
134
+ }
135
+ ```
136
+
137
+ Behind the scenes, the `dev-approuter` will resolve these "dependency routes" by adding the `source`, `target`, and `destination` properties to them. Be aware that exactly these properties might get overwritten by the `dev-approuter` in case you use them together with `dependency`.
138
+
139
+ ## Using the `dev-approuter` and SAP Application Router simultaneously
140
+
141
+ If you choose to place your `dev-approuter` in the same directory as an SAP Application Router, which you will eventually deploy, you will have to remove the `devDependencies` section of the `package.json` before deployment. This is required because the SAP Application Router will not be able to install local dev dependencies (your UI5 and SAP CAP apps) in the cloud. To achieve this, you could introduce a build step for the approuter, moving required files to a `dist/` folder and removing dev dependencies:
142
+
143
+ ```json
144
+ "build": "mkdir -p dist && jq 'del(.devDependencies)' package.json > dist/package.json && cp xs-app.json dist/xs-app.json"
145
+ ```
146
+
147
+ ## Extending the `dev-approuter`
148
+
149
+ The `dev-approuter` offers an extension point to pass middleware to the SAP Application Router, that gets started by the `dev-approuter` (behind the scenes) and has an extension point of its own (see the [documentation](https://help.sap.com/docs/btp/sap-business-technology-platform/extension-api-of-application-router) for more info). You can use this extension point by passing extensions to the `dev-approuter`'s `start()` method:
150
+
151
+ ```js
152
+ const devApprouter = require("dev-approuter/lib/devApprouter");
153
+ devApprouter.start([
154
+ {
155
+ insertMiddleware: {
156
+ first: [
157
+ {
158
+ path: "/my-ext",
159
+ handler: (req, res, next) => {
160
+ res.end("Request handled by my extension!")
161
+ }
162
+ }
163
+ ]
164
+ }
165
+ }
166
+ ]);
167
+ ```
168
+
169
+ ## Support
170
+
171
+ Please use the GitHub bug tracking system to post questions, bug reports or to create pull requests.
172
+
173
+ ## Contributing
174
+
175
+ Any type of contribution (code contributions, pull requests, issues) to this set of tooling extensions will be equally appreciated.
176
+
177
+ ## License
178
+
179
+ This work is [dual-licensed](../../LICENSE) under Apache 2.0 and the Derived Beer-ware License. The official license will be Apache 2.0 but finally you can choose between one of them if you use this work.
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ const devApprouter = require("./lib/devApprouter");
2
+ devApprouter.start();
@@ -0,0 +1,147 @@
1
+ const approuter = require("@sap/approuter");
2
+ const express = require("express");
3
+ const xsenv = require('@sap/xsenv')
4
+
5
+ const findUI5Modules = require("cds-plugin-ui5/lib/findUI5Modules");
6
+ const createPatchedRouter = require("cds-plugin-ui5/lib/createPatchedRouter");
7
+ const applyUI5Middleware = require("cds-plugin-ui5/lib/applyUI5Middleware");
8
+
9
+ const findCAPModules = require("ui5-middleware-cap/lib/findCAPModules");
10
+ const applyCAPMiddleware = require("ui5-middleware-cap/lib/applyCAPMiddleware");
11
+
12
+ const {
13
+ parseConfig,
14
+ applyDependencyConfig,
15
+ addDestination,
16
+ configureCAPRoute,
17
+ configureUI5Route
18
+ } = require("./helpers");
19
+
20
+
21
+ class DevApprouter {
22
+
23
+ constructor() {}
24
+
25
+ /**
26
+ * Starts the dev approuter.
27
+ * Extensions passed as argument are handed to the SAP Approuter without modifications.
28
+ * We suggest to check the documentation (link below) for an extensive read on how the dev approuter works.
29
+ * Here is a shorter summary:
30
+ * UI5 modules declared as (dev)dependencies are added as extensions
31
+ * using the extension API of the SAP Approuter.
32
+ * CAP modules declared as (dev)dependencies are started on a different port.
33
+ * Corresponding routes and destinations are automatically created.
34
+ * A custom `xs-dev.json` can be used to configure the dev approuter,
35
+ * so the productive configuration can be kept in the `xs-app.json`.
36
+ * @param {Object[]} [extensions] - an optional array of extensions that are handed to the SAP Approuter without modification.
37
+ * @param {Object} extensions[].insertMiddleware - an object containing the middlewares.
38
+ * @param {Object[]} extensions[].insertMiddleware.first - an array of middlewares to be inserted in the `first` slot.
39
+ * @param {String} extensions[].insertMiddleware.first[].path - a string representing the path to handle requests for.
40
+ * @param {Function} extensions[].insertMiddleware.first[].handler - a function handling `(req, res, next)`.
41
+ * @param {Object[]} extensions[].insertMiddleware.beforeRequestHandler - an array of middlewares to be inserted in the `beforeRequestHandler` slot.
42
+ * @param {String} extensions[].insertMiddleware.beforeRequestHandler[].path - a string representing the path to handle requests for.
43
+ * @param {Function} extensions[].insertMiddleware.beforeRequestHandler[].handler - a function handling `(req, res, next)`.
44
+ * @param {Object[]} extensions[].insertMiddleware.beforeErrorHandler - an array of middlewares to be inserted in the `beforeErrorHandler` slot.
45
+ * @param {String} extensions[].insertMiddleware.beforeErrorHandler[].path - a string representing the path to handle requests for.
46
+ * @param {Function} extensions[].insertMiddleware.beforeErrorHandler[].handler - a function handling `(req, res, next)`.
47
+ * @see https://github.com/ui5-community/ui5-ecosystem-showcase/tree/main/packages/dev-approuter
48
+ */
49
+ async start(extensions = []) {
50
+ // loads env from default-env.json
51
+ xsenv.loadEnv();
52
+
53
+ const config = parseConfig();
54
+ const cwd = process.cwd();
55
+
56
+ // lookup the CAP server root
57
+ let capServerConfig;
58
+ const capModules = await findCAPModules({ cwd });
59
+ if (capModules.length > 1) {
60
+ throw new Error(`Multiple CAP modules found. The package dev-approuter can only handle one CAP module as dependency.`);
61
+ } else if (capModules.length === 1) {
62
+ capServerConfig = capModules[0];
63
+ }
64
+
65
+ // find all UI5 modules from the CAP server root and dependencies from the approuter
66
+ const ui5Modules = [...(await findUI5Modules({ cwd, skipLocalApps: true }))];
67
+ if (capServerConfig) {
68
+ ui5Modules.push(...(await findUI5Modules({ cwd: capServerConfig.modulePath, skipDeps: true })))
69
+ }
70
+
71
+ // collect UI5 middlewares
72
+ const ui5Middlewares = [];
73
+ for await (const ui5Module of ui5Modules) {
74
+ const { moduleId, modulePath, mountPath } = ui5Module;
75
+
76
+ // create a patched router
77
+ const router = await createPatchedRouter();
78
+
79
+ // apply the UI5 middlewares to the router
80
+ await applyUI5Middleware(router, {
81
+ basePath: modulePath,
82
+ configPath: modulePath,
83
+ });
84
+
85
+ // mounting the router for the UI5 application to the CAP server
86
+ console.log(`Mounting ${mountPath} to UI5 app ${modulePath}`);
87
+
88
+ let middlewareMountPath
89
+ // define middlewareMountPath as `/_${mountPath}` if ui5 module is referenced as "dependency" in xs-dev.json or xs-app.json
90
+ if (config.dependencyRoutes && config.dependencyRoutes[moduleId]) {
91
+ // configure UI5 route
92
+ config.dependencyRoutes[moduleId] = configureUI5Route(moduleId, mountPath, config.dependencyRoutes[moduleId]);
93
+
94
+ middlewareMountPath = "/_" + mountPath;
95
+
96
+ // add destination for newly configured route
97
+ addDestination(moduleId, process.env.PORT, middlewareMountPath);
98
+ } else {
99
+ middlewareMountPath = mountPath;
100
+ }
101
+
102
+ // store the router for later registration
103
+ ui5Middlewares.push({
104
+ path: middlewareMountPath,
105
+ handler: router
106
+ });
107
+
108
+ }
109
+
110
+ // start CAP server on different port
111
+ if (capServerConfig) {
112
+ const { modulePath, moduleId } = capServerConfig;
113
+
114
+ // start CAP server on different port
115
+ const app = express();
116
+ const { servicesPaths } = await applyCAPMiddleware(app, { root: modulePath, cwd });
117
+ app.listen(process.env.CAP_PORT || 4004, () => {
118
+ console.log(`CAP server started at: http://localhost:${process.env.CAP_PORT || 4004}`);
119
+ });
120
+
121
+ // configure CAP route if referenced as "dependency" in xs-dev.json/xs-app.json
122
+ if (config.dependencyRoutes && config.dependencyRoutes[moduleId]) {
123
+ config.dependencyRoutes[moduleId] = configureCAPRoute(moduleId, servicesPaths, config.dependencyRoutes[moduleId]);
124
+ }
125
+
126
+ // add destination for newly configured route
127
+ addDestination(moduleId, process.env.CAP_PORT || 4004);
128
+ }
129
+
130
+ // create and start the SAP Approuter
131
+ // https://help.sap.com/docs/btp/sap-business-technology-platform/extension-api-of-application-router
132
+ approuter().start({
133
+ port: process.env.PORT || 5001,
134
+ xsappConfig: applyDependencyConfig(config),
135
+ extensions: [
136
+ {
137
+ insertMiddleware: {
138
+ first: ui5Middlewares
139
+ }
140
+ }
141
+ ].concat(extensions)
142
+ });
143
+ console.log(`Approuter started at: http://localhost:${process.env.PORT || 5001}`);
144
+ };
145
+ }
146
+
147
+ module.exports = new DevApprouter();
package/lib/helpers.js ADDED
@@ -0,0 +1,136 @@
1
+ const path = require("path");
2
+
3
+ /**
4
+ * Parses the approuter configuration from an `xs-dev.json` file.
5
+ * If that file doesn't exist, it looks for an `xs-app.json` file.
6
+ * Also scans the routes of the config file and checks for the `dependency` property
7
+ * and stores those in `config.dependencyRoutes`, which is where the dev approuter will do its magic.
8
+ * @returns {Object} the approuter configuration including all `dependencyRoutes`.
9
+ */
10
+ const parseConfig = () => {
11
+ let config;
12
+ let configFile;
13
+ let configFiles = ["xs-dev.json", "xs-app.json"];
14
+ for (const file of configFiles) {
15
+ if (fs.existsSync(path.join(process.cwd(), file))) {
16
+ config = JSON.parse(
17
+ fs.readFileSync(
18
+ path.join(process.cwd(), file),
19
+ { encoding: "utf8" }
20
+ )
21
+ );
22
+ configFile = file;
23
+ break;
24
+ }
25
+ }
26
+ config.dependencyRoutes = {};
27
+ config.routes?.forEach(route => {
28
+ if (route.dependency) {
29
+ if (config.dependencyRoutes[`${route.dependency}`]) {
30
+ throw new Error(`Duplicate dependency "${route.dependency}" found in file ${path.join(process.cwd(), configFile)}.`);
31
+ } else {
32
+ config.dependencyRoutes[`${route.dependency}`] = route;
33
+ }
34
+ }
35
+ });
36
+ return config;
37
+ };
38
+
39
+ /**
40
+ * Applies the `config.dependencyRoutes` to the `config.routes` and removes them.
41
+ * @param {Object} config - the approuter configuration including all `dependencyRoutes`.
42
+ * @returns {Object} config - the approuter configuration that can be used to start the approuter.
43
+ */
44
+ const applyDependencyConfig = (config) => {
45
+ config.routes?.forEach(route => {
46
+ if (route.dependency) {
47
+ route = config.dependencyRoutes[route.dependency];
48
+ }
49
+ });
50
+ delete config.dependencyRoutes;
51
+ return config;
52
+ };
53
+
54
+ /**
55
+ * Adds a destination to `process.env.destinations` for a given module.
56
+ * If a destination with this `name === moduleId` already exists, no new destination will be created.
57
+ * @param {String} moduleId - the id of the module that a destination is created for.
58
+ * @param {Number} port - the port of the localhost that the destination should point to.
59
+ * @param {String} mountPath - the path the module was mounted to and the destination should point to.
60
+ */
61
+ const addDestination = (moduleId, port, mountPath) => {
62
+ let destinations = [];
63
+ if (process.env.destinations) {
64
+ destinations = JSON.parse(process.env.destinations);
65
+ }
66
+
67
+ let url;
68
+ if (mountPath) {
69
+ url = `http://localhost:${process.env.PORT || 5000}${mountPath}`;
70
+ } else {
71
+ url = `http://localhost:${port}`;
72
+ }
73
+
74
+ // only add new destination if it's not already provided
75
+ const destinationAlreadyExists = destinations.some(destination => {
76
+ const lowerCaseDestination = {};
77
+ Object.keys(destination).forEach(key => {
78
+ lowerCaseDestination[key.toLowerCase()] = destination[key];
79
+ })
80
+ return lowerCaseDestination.name === moduleId
81
+ })
82
+ if (!destinationAlreadyExists) {
83
+ destinations.push({
84
+ Name: moduleId,
85
+ Authentication: "NoAuthentication",
86
+ ProxyType: "Internet",
87
+ Type: "HTTP",
88
+ URL: url
89
+ });
90
+ process.env.destinations = JSON.stringify(destinations);
91
+ }
92
+ };
93
+
94
+ /**
95
+ * Configures the route for a given CAP module.
96
+ * @param {String} moduleId - the id of the module that the route should be configured for.
97
+ * @param {String[]} servicePaths - an array of service paths that the CAP module serves.
98
+ * @param {Object} route - the route that is to be configured.
99
+ * @returns {Object} the configured route.
100
+ */
101
+ const configureCAPRoute = (moduleId, servicesPaths, route) => {
102
+ route.source = servicesPaths.map(path => { return `${path}(.*)` }).join("|");
103
+ route.destination = moduleId;
104
+ delete route.dependency;
105
+
106
+ return route;
107
+ };
108
+
109
+ /**
110
+ * Configures the route for a given UI5 module.
111
+ * @param {String} moduleId - the id of the module that the route should be configured for.
112
+ * @param {String} sourcePath - the path the approuter should handle the module at.
113
+ * @param {Object} route - the route that is to be configured.
114
+ * @returns {Object} the configured route.
115
+ */
116
+ const configureUI5Route = (moduleId, sourcePath, route) => {
117
+ if (sourcePath === "/") {
118
+ // special regex to avoid endless loop
119
+ route.source = `^(?!.*(/_${sourcePath}))`;
120
+ } else {
121
+ route.source = `^${sourcePath}(.*)$`;
122
+ route.target = "$1";
123
+ }
124
+ route.destination = moduleId;
125
+ delete route.dependency;
126
+
127
+ return route;
128
+ };
129
+
130
+ module.exports = {
131
+ parseConfig,
132
+ applyDependencyConfig,
133
+ addDestination,
134
+ configureCAPRoute,
135
+ configureUI5Route
136
+ };
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "dev-approuter",
3
+ "version": "0.1.0",
4
+ "description": "A dev time wrapper for the SAP Application Router that can serve UI5 and CAP modules added as dependencies.",
5
+ "author": "Nico Schoenteich <nicolai.schoenteich@sap.com> (https://github.com/nicoschoenteich)",
6
+ "license": "Apache-2.0",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/ui5-community/ui5-ecosystem-showcase.git",
10
+ "directory": "packages/dev-approuter"
11
+ },
12
+ "dependencies": {
13
+ "@sap/approuter": ">=14.3.0",
14
+ "@sap/xsenv": "4.0.0",
15
+ "cds-plugin-ui5": "^0.4.0",
16
+ "express": "^4.18.2",
17
+ "path": "^0.12.7",
18
+ "ui5-middleware-cap": "^3.1.0"
19
+ },
20
+ "gitHead": "4a6a4401cc13120cb5f91bba8c116dce78c3812b"
21
+ }