@thi.ng/server
Version:
Minimal HTTP server with declarative routing, static file serving and freely extensible via pre/post interceptors
24 lines (23 loc) • 518 B
JavaScript
import { kebab } from "@thi.ng/strings";
const cacheControl = (opts = {}) => {
const acc = [];
for (const [k, v] of Object.entries(opts)) {
switch (k) {
case "maxAge":
acc.push("max-age=" + v);
break;
case "sMaxAge":
acc.push("s-maxage=" + v);
break;
default:
if (v) acc.push(kebab(k));
}
}
const value = acc.join(", ");
return {
pre: (ctx) => (value && ctx.res.setHeader("cache-control", value), true)
};
};
export {
cacheControl
};