Skip to content

Commit

Permalink
Add demo for server middleware extensibility
Browse files Browse the repository at this point in the history
As per RFC 0005: SAP/ui5-tooling#151
  • Loading branch information
RandomByte committed Jun 25, 2019
1 parent b855d08 commit 05c31ee
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
29 changes: 29 additions & 0 deletions middleware/ponyTranslator.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
};
15 changes: 15 additions & 0 deletions ui5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 05c31ee

Please sign in to comment.