Skip to content

Commit

Permalink
Simplifying the text/html renderer when the template is empty, upda…
Browse files Browse the repository at this point in the history
…ting middleware export syntax, updating `/uuid` headers
  • Loading branch information
avoidwork committed Oct 15, 2018
1 parent 6cc94c5 commit 0611727
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
14 changes: 7 additions & 7 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,12 @@ function zuul (req, res, next) {
}

module.exports = {
asyncFlag: asyncFlag,
bypass: bypass,
guard: guard,
parse: parse,
payload: payload,
rate: rate,
asyncFlag,
bypass,
guard,
parse,
payload,
rate,
"static": statik,
zuul: zuul
zuul
};
7 changes: 3 additions & 4 deletions lib/renderers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ renderers.set("text/csv", (arg, req, headers) => {
return csv.encode(obj);
});

renderers.set("text/html", (arg, req, headers, tpl) => {
renderers.set("text/html", (arg, req, headers, tpl = "") => {
const protocol = req.headers["x-forwarded-proto"] ? req.headers["x-forwarded-proto"] + ":" : req.parsed.protocol;

return (tpl || "")
.replace(/\{\{title\}\}/g, req.server.config.title)
return tpl.length > 0 ? tpl.replace(/\{\{title\}\}/g, req.server.config.title)
.replace("{{url}}", req.parsed.href.replace(req.parsed.protocol, protocol))
.replace("{{headers}}", Object.keys(headers).sort().map(i => `<tr><td>${i}</td><td>${sanitize(headers[i])}</td></tr>`).join("\n"))
.replace("{{formats}}", `<option value=''></option>${(Array.from(renderers.keys()).filter(i => i.indexOf("html") === -1).map(i => `<option value='${i.trim()}'>${i.replace(/^.*\//, "").toUpperCase()}</option>`).join("\n"))}`)
Expand All @@ -51,7 +50,7 @@ renderers.set("text/html", (arg, req, headers, tpl) => {
.replace("{{allow}}", headers.allow)
.replace("{{methods}}", utility.explode((headers.allow || "").replace("GET, HEAD, OPTIONS", "")).filter(i => i !== "").map(i => `<option value='${i.trim()}'>$i.trim()}</option>`).join("\n"))
.replace("{{csrf}}", headers["x-csrf-token"] || "")
.replace("class=\"headers", req.server.config.renderHeaders === false ? "class=\"headers dr-hidden" : "class=\"headers");
.replace("class=\"headers", req.server.config.renderHeaders === false ? "class=\"headers dr-hidden" : "class=\"headers") : "";
});

renderers.set("text/plain", arg => arg.toString());
Expand Down
2 changes: 1 addition & 1 deletion test/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports.get = {
lastName: "Mulligan"
},
"/uuid": function (req, res) {
res.send(uuid());
res.send(uuid(), 200, {"cache-control": "no-cache, must-revalidate"});
}
};

Expand Down

0 comments on commit 0611727

Please sign in to comment.