Skip to content

Commit

Permalink
Fixing a regression in middleware via the wrong method on a string va…
Browse files Browse the repository at this point in the history
…riable; creating `this.final(req, res, body)` which can be used to modify the response body output of `this.hypermedia()`
  • Loading branch information
avoidwork committed Apr 24, 2019
1 parent 2ef238f commit 9f89ed3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Sort order can be specified via then `order-by` which accepts `[field ]asc|desc`
}
```

Final modifications can be made to a response body after `hypermedia()` by overriding `tenso.final(req, res, body)`.

## REST / Hypermedia
Hypermedia is a prerequisite of REST, and is best described by the [Richardson Maturity Model](http://martinfowler.com/articles/richardsonMaturityModel.html). Tensō will automagically paginate Arrays of results, or parse Entity representations for keys that imply
relationships, and create the appropriate Objects in the `link` Array, as well as the `Link` HTTP header. Object keys that match this pattern: `/_(guid|uuid|id|uri|url)$/` will be considered
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function keymaster (req, res) {
}
} else {
retsu.each(Array.from(routes.keys()), i => {
const key = i.contains(":") && i.contains("(") === false ? i.replace(/\/:(\w*)/g, "/(.*)") : i;
const key = i.includes(":") && i.includes("(") === false ? i.replace(/\/:(\w*)/g, "/(.*)") : i;

return new RegExp(`^${key}$`, "i").test(uri) ? !(result = routes.get(i)[0]) : void 0;
});
Expand Down
6 changes: 5 additions & 1 deletion lib/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ class Tenso extends Base {
return eventsource(...args);
}

final (req, res, arg) {
return arg;
}

finish (req, res) {
return this.log(this.clf(req, res, res.getHeaders()), "info");
}
Expand Down Expand Up @@ -485,7 +489,7 @@ class Tenso extends Base {

if (status !== 204 && status !== 304) {
if (body === null || typeof body.on !== "function") {
result = this.render(req, res, hypermedia(this, req, serialize(req, body, status, headers), headers), headers);
result = this.render(req, res, this.final(req, res, hypermedia(this, req, serialize(req, body, status, headers), headers)), headers);
headers["content-length"] = Buffer.byteLength(result);

if (req.method === "HEAD") {
Expand Down
48 changes: 24 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tenso",
"description": "Tensō is an HTTP/HTTP2 REST API framework",
"version": "11.0.3",
"version": "11.1.0",
"homepage": "http://avoidwork.github.io/tenso",
"author": "Jason Mulligan <[email protected]>",
"repository": {
Expand Down

0 comments on commit 9f89ed3

Please sign in to comment.