From 05c31ee6ad2c2693f85a918197605849f91fd81e Mon Sep 17 00:00:00 2001 From: Merlin Beutlberger Date: Tue, 25 Jun 2019 16:15:59 +0200 Subject: [PATCH] Add demo for server middleware extensibility As per RFC 0005: https://github.com/SAP/ui5-tooling/pull/151 --- middleware/ponyTranslator.js | 29 +++++++++++++++++++++++++++++ ui5.yaml | 15 +++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 middleware/ponyTranslator.js diff --git a/middleware/ponyTranslator.js b/middleware/ponyTranslator.js new file mode 100644 index 000000000..f8e6b5556 --- /dev/null +++ b/middleware/ponyTranslator.js @@ -0,0 +1,29 @@ +const rProperties = /\.properties$/; +const parseurl = require("parseurl"); + +module.exports = function({resources, options}) { + let replacement = "pony" + if (options.configuration.mood) { + replacement = `${options.configuration.mood} ${replacement}`; + } + return function (req, res, next) { + const pathname = parseurl(req).pathname; + if (!rProperties.test(pathname)) { + next(); + return; + } + resources.all.byPath(pathname).then(function(resource) { + if (!resource) { // Not found + next(); + return; + } + + resource.getBuffer().then((content) => { + const ponianizedContent = content.toString().replace(/=[^=]*$/gm, `=${replacement}`); + res.end(ponianizedContent); + }); + }).catch((err) => { + next(err); + }); + } +}; diff --git a/ui5.yaml b/ui5.yaml index 1544b5a0f..c29201163 100644 --- a/ui5.yaml +++ b/ui5.yaml @@ -2,3 +2,18 @@ specVersion: '1.0' metadata: name: openui5-sample-app type: application +server: + customMiddleware: + - name: ponyTranslator + beforeMiddleware: serveResources + mountPath: / + configuration: + mood: "🐤 happy" +--- +specVersion: '1.0' +kind: extension +type: server-middleware +metadata: + name: ponyTranslator +middleware: + path: middleware/ponyTranslator.js