UNPKG

@d3vtool/kazejs

Version:

A flexible Node.js web framework built with TypeScript, focusing on dependency injection, routing, middleware, and schema validation. Supports dynamic routes, global middleware, static files, and customizable error handling for scalable apps.

35 lines (34 loc) 1.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.cors = cors; const optionHeaderMap = { maxAge: "Access-Control-Max-Age", origin: "Access-Control-Allow-Origin", allowHeaders: "Access-Control-Allow-Headers", allowMethods: "Access-Control-Allow-Methods", credentials: "Access-Control-Allow-Credentials", exposeHeaders: "Access-Control-Expose-Headers", }; function cors(options) { return function (ctx, next) { Object.keys(options).map((key) => { const option = options[key]; const header = optionHeaderMap[key]; if (Array.isArray(option)) { ctx.res.setHeader(header, option.join(", ")); return; } if (key === "origin" && option instanceof Function) { const protocol = ctx.req.secure ? "https" : "http"; const reqOrigin = `${protocol}://${ctx.req.headers["host"]}`; const result = option(reqOrigin); ctx.res.setHeader(header, result); } else { if (option) ctx.res.setHeader(header, option.toString()); } }); next(); }; }