tspace-spear
Version:
tspace-spear is a lightweight, high-performance API framework for Node.js that leverages the native HTTP server and supports uWebSockets.js (C++) for maximum speed and efficiency.
44 lines • 1.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WriteHeader = void 0;
/**
* Sets the HTTP response headers and status code before the controller method executes.
*
* This decorator calls `res.writeHead()` on the underlying Node.js response object,
* allowing you to define the response **status code** and **headers** in advance.
*
* @example
* ```ts
* class UserController {
*
* \@WriteHeader(200, { "Content-Type": "application/json" })
* async profile(ctx: T.Context) {
* return { id: 1, name: "John" };
* }
*
* }
* ```
*
* Example response:
*
* ```
* HTTP/1.1 200 OK
* Content-Type: application/json
* ```
*
* @param {number} statusCode - HTTP status code to send with the response.
* @param {OutgoingHttpHeaders} contentType - Response headers to set.
* @returns {MethodDecorator}
*/
const WriteHeader = (statusCode, contentType) => {
return (target, key, descriptor) => {
const originalMethod = descriptor.value;
descriptor.value = async function (ctx, next) {
ctx.res.writeHead(...[statusCode, contentType]);
return await originalMethod.call(this, ctx, next);
};
return descriptor;
};
};
exports.WriteHeader = WriteHeader;
//# sourceMappingURL=headers.js.map