ui5-middleware-cfdestination v3.3.11
UI5 middleware for CF destinations
: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.
Middleware for ui5-server, making destinations configured in SAP CF available for local development using the http-proxy-middleware.
Prerequisites
- Requires at least
@ui5/cli@3.0.0(to supportspecVersion: "3.0")
:warning: UI5 Tooling Compatibility All releases of this tooling extension using the major version
3require UI5 Tooling V3. Any previous releases below major version3(if available) also support older versions of the UI5 Tooling. But the usage of the latest UI5 Tooling is strongly recommended!
Install
npm install ui5-middleware-cfdestination --save-devConfiguration options (in $yourapp/ui5.yaml)
debug:<boolean>, default:false
verbose loggingport:<int>, default:5000
port to run the underlyingapprouteronxsappJson:<string>, default:"./xs-app.json"
path to the cf-style approuter configuration filexs-app.json:information_source: the regex for the destination routes must match the pattern :/[^/]*\/(.*\/)?[^/]*/, e.g."^/backend/(.*)$"or"^/index.html"destinations:<string[]>, default:[]name: <string>destination name, matching the one used in routes inxs-app.jsonurl: <string>URI to the host to "proxy" to
alternatively the destinations can also be defined in a
.envfile. They need to be in encoded in a single JSON string
e.g.xsapp_dest = [{"name": "destination-name", "url": "<some-service-url>"}]To use these destinations they need to passed to the
destinationsoption as a string"$env:<key-in-env-file>"(e.g.destinations: "$env:xsapp_dest")allowServices:<boolean>, default:false
allow BTP services to be used at runtime that are configured inxs-app.json
(requires an authenticated BTP session!)authenticationMethod:"none"||"route", default:"none"
whether to equip routes with authenticationallowLocalDir:<boolean>, default:false
allow static assets to be picked up by the includedapprouter; defaults tofalseas usually all local files/assets are supposedly served byui5-serverrewriteContent:<boolean>, default:true
enables/disables rewriting of the content by replacing the proxied url in the response body with the server urlrewriteContentTypes:<string[]>, default:["text/html", "application/json", "application/atom+xml", "application/xml"]
defines the content types which are included for rewriting the content by enabling therewriteContentoptionextensions:<string[]>, default:[]- a list of extensions to be required and injected into the local approuter instancemodule: <string>- local module path (must start with./):"./my-local-extension.js"or a module from an npm package:"@my-scope/my-package/my-extension.js"parameters: <Map<string, string>>, optional - a map of parameters given as key value pairs which will be injected into the handler function of the extension as the last (4th) argument (function(req, res, next, params), remark: handler functions with more than 3 arguments will not be called for regular extensions only for these extensions!)
disableWelcomeFile:<boolean>, default:falseexperimental disables the welcome file handling of the approuter based on thewelcomeFileproperty in thexsappJsonfileappendAuthRoute:<boolean>, default:falseexperimental iftruethe middleware adds a custom route for all HTML pages to trigger authentication:{ "source": "^/([^.]+\\.html?(?:\\?.*)?)$", "localDir": relativeSourcePath, "target": "$1", "cacheControl": "no-store", "authenticationType": "xsuaa" }enableWebSocket:<boolean>, default:falseexperimental enables support for proxying web sockets, will be also automatically detected fromxs-app.json
Usage
- Define the dependency in
$yourapp/package.json:
"devDependencies": {
// ...
"ui5-middleware-cfdestination": "*"
// ...
}- configure it in
$yourapp/ui5.yaml:
server:
customMiddleware:
- name: ui5-middleware-cfdestination
afterMiddleware: compression
configuration:
authenticationMethod: "none" # "none" || "route", default: "none"
allowServices: true # allows BTP services like SAP IoT to be used
debug: true
port: 1091
xsappJson: "xs-app.json"
destinations:
# check that the destination name (here: "backend") matches your router in xs-app.json
- name: "backend"
url: "https://services.odata.org/V4/(S(fdng4tbvlxgzpdtpfap2rqss))/TripPinServiceRW/"- put the cf routing config file
xs-app.jsonin the location of$yourappyou specified above (xsappJson) -
recommendation is to put it at the root/of your UI5 app!
How it works
The middleware wraps the @sap/approuter npm module that is used in the SAP BTP CloudFoundry environment for serving UI5 applications, including proxying the configured destinations.
During development, the approuter is started on a configurable port, running alongside the regular local ui5-server. When a call to a URL destination is detected at $webserver/destination, it is proxied to $approuter:$port via request.
Using approuter extensions
The ui5-middleware-cfdestination allows using approuter extensions. It is possible to pass parameters to handler functions of the extensions as a 4th argument of the handler function.
The configuration of the extensions in the ui5.yaml looks like that:
- name: ui5-middleware-cfdestination
afterMiddleware: compression
configuration:
extensions:
- module: ./approuter-local-ext.js
parameters:
userId: mustermann@ui5.comAnd in the approuter-local-ext.js you can consume the parameters like that:
module.exports = {
insertMiddleware: {
beforeRequestHandler: [
{
path: '/',
handler: function forwardUserInfo(req, res, next, params) {
res.setHeader('x-user-id', params?.userId || "unknown@ui5.com")
next()
}
}
]
}
}Keep in mind that the 4th parameter doesn't work for the regular approuter extensions and the handler function will not be called when having more than 3 arguments.
Misc/FAQ
q: does authentication on destinations
a: yes, works 🥳
- prerequisite: a
default-env.jsonfile at the root/of your UI5 app Bespokendefault-env.jsonfile can be obtained via theDefault Env CLI Plugin ui5.yaml > server > customMiddlware > ui5-middleware-cfdestination: setauthenticationMethodtoroute(see config example above)xs-app.json(also at the root/of your UI5 app):- set
authenticationMethodtoroute - in the route with the desired protected authentication, set
authenticationTypetoxsuaa
- set
see ./test/auth/**/* for example files for the above!
q: what's allowLocalDir for?
a: allows to protect local static assets (e.g. html files) -in addition to destinations- with approuter as well.
see test/auth/xs-app-with-localDir.json for an example!
License
This work is dual-licensed 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.
When you like this stuff, buy @vobu a beer or buy @pmuessig a coke when you see them.
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago