Skip to content

Instantly share code, notes, and snippets.

@wridgeu
Last active August 11, 2022 16:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
const ui5Proxy = require("@sap-ux/ui5-proxy-middleware").ui5Proxy;
/**
* @typedef {Object} [configuration] configuration
* @property {string} [frameworkVersion] framework version to use for proxy requests → "1.71.33"
* @property {string} [dependencyUrl] url to the CDN from which we want our resources to be loaded → where do we proxy our requests to
* @property {string[]} [excludePaths] array of strings that should be excluded and thus loaded "normally" → not be proxied
*/
/**
* Custom UI5 Server middleware to proxy UI5 resources.
*
* We want to differentiate between a third-party library and actual, to be proxied, UI5 resources so that
* when requesting the third-party resource we won't get an error (404) as it won't exist on any official
* UI5 CDN to which we proxy other "/resource" requests.
*
* @param {object} parameters Parameters
* @param {object} parameters.resources Resource collections
* @param {module:@ui5/fs.AbstractReader} parameters.resources.all Reader or Collection to read resources of the
* root project and its dependencies
* @param {module:@ui5/fs.AbstractReader} parameters.resources.rootProject Reader or Collection to read resources of
* the project the server is started in
* @param {module:@ui5/fs.AbstractReader} parameters.resources.dependencies Reader or Collection to read resources of
* the projects dependencies
* @param {object} parameters.middlewareUtil Specification version dependent interface to a
* [MiddlewareUtil]{@link module:@ui5/server.middleware.MiddlewareUtil} instance
* @param {object} parameters.options Options
* @param {string} [parameters.options.configuration] Custom server middleware configuration if given in ui5.yaml
* @returns {function} Middleware function to use
*/
module.exports = function ({ options }) {
return function (req, res, next) {
const middlewareConfig = options.configuration || {
frameworkVersion: "1.71.33", // use LTS version
// eslint-disable-next-line sap-no-hardcoded-url
dependencyUrl: "https://ui5.sap.com", // fiori env. use SAPUI5
};
// we start from root "/" and make our decisions based on the filtering below
const ui5ProxyConfig = {
path: "/",
url: middlewareConfig.dependencyUrl,
version: middlewareConfig.frameworkVersion,
};
const customFilter = (_path, _req) => {
if ((middlewareConfig.excludePaths || []).some((pattern) => _path.includes(pattern))) {
return false;
} else if (_path.includes("resources/")) {
return true;
}
// we need to make sure to not "blindly" proxy all requests
// i.e. retrieving "test/flpSandbox.html"
return false;
};
const proxyFunction = ui5Proxy(ui5ProxyConfig, {}, customFilter);
return proxyFunction(req, res, next);
};
};
# yaml-language-server: $schema=https://sap.github.io/ui5-tooling/schema/ui5.yaml.json
specVersion: "2.5"
metadata:
name: some.namespace.project
type: application
builder:
resources:
excludes:
- "/test/**"
server:
customMiddleware:
- name: fiori-tools-appreload
afterMiddleware: compression
configuration:
port: 35729
path: webapp
delay: 300
- name: custom-proxy
afterMiddleware: compression
configuration:
frameworkVersion: "1.71.33"
dependencyUrl: "https://ui5.sap.com"
excludePaths:
- "/resources/openui5"
---
# Custom middleware extension as part of your project
specVersion: "2.6"
kind: extension
type: server-middleware
metadata:
name: custom-proxy
middleware:
path: lib/middleware/customResourceProxy.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment