UNPKG

restifyx.js

Version:

Advanced API endpoint handler system with automatic documentation

118 lines 3.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SendData = void 0; const logger_1 = require("../utils/logger"); class SendData { static getResponse() { throw new Error("SendData must be used within an endpoint handler"); } /** * Send a JSON response * * @param data - The data to send * @param statusCode - HTTP status code (default: 200) * @param headers - Additional headers to include */ static json(data, statusCode = 200, headers) { const res = this.getResponseFromContext(); if (headers) { Object.entries(headers).forEach(([key, value]) => { res.setHeader(key, value); }); } res.status(statusCode).json(data); } /** * Send a text response * * @param text - The text to send * @param statusCode - HTTP status code (default: 200) * @param headers - Additional headers to include */ static text(text, statusCode = 200, headers) { const res = this.getResponseFromContext(); if (headers) { Object.entries(headers).forEach(([key, value]) => { res.setHeader(key, value); }); } res.status(statusCode).send(text); } /** * Send an HTML response * * @param html - The HTML to send * @param statusCode - HTTP status code (default: 200) * @param headers - Additional headers to include */ static html(html, statusCode = 200, headers) { const res = this.getResponseFromContext(); if (headers) { Object.entries(headers).forEach(([key, value]) => { res.setHeader(key, value); }); } res.status(statusCode).send(html); } /** * Send a file as a response * * @param filePath - Path to the file * @param options - Options for res.sendFile() */ static file(filePath, options) { const res = this.getResponseFromContext(); res.sendFile(filePath, options); } /** * Redirect to another URL * * @param url - The URL to redirect to * @param statusCode - HTTP status code (default: 302) */ static redirect(url, statusCode = 302) { const res = this.getResponseFromContext(); res.redirect(statusCode, url); } /** * Send a 204 No Content response */ static noContent() { const res = this.getResponseFromContext(); res.status(204).end(); } /** * Stream data as a response * * @param stream - The stream to send * @param statusCode - HTTP status code (default: 200) * @param headers - Additional headers to include */ static stream(stream, statusCode = 200, headers) { const res = this.getResponseFromContext(); if (headers) { Object.entries(headers).forEach(([key, value]) => { res.setHeader(key, value); }); } res.status(statusCode); stream.pipe(res); } /** * Get the response object from the current execution context * * @returns The Express response object * @private */ static getResponseFromContext() { const res = global.CURRENT_RESPONSE; if (!res) { const logger = (0, logger_1.getLogger)(); logger.error("SendData used outside of an endpoint handler context"); throw new Error("SendData must be used within an endpoint handler"); } return res; } } exports.SendData = SendData; //# sourceMappingURL=SendData.js.map