UNPKG

outers

Version:

outers - a all in one package for your day to day use

127 lines 7.09 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _JSONResponser_StatusCode, _JSONResponser_Title, _JSONResponser_status, _JSONResponser_Message, _JSONResponser_response, _JSONResponser_contentType, _JSONResponser_CookieData; Object.defineProperty(exports, "__esModule", { value: true }); const Response_Constant_1 = require("../../Config/Constant/Response.Constant"); // Import JPG_REGEX // Global JSON Responser Class For Create Response to Client /** * Represents a JSON response object that can be sent using Express. */ class JSONResponser { /** * Creates a new instance of the JSONResponser class. * @param response The Express response object. * @param StatusCode The HTTP status code to be set. * @param contentType The content type of the response. * @param Title The title of the response (optional). * @param Message The message of the response (optional). * @param CookieData An optional array of objects representing cookie data to be set. */ constructor(response, StatusCode, contentType, Title, status, Message, CookieData) { /** * The HTTP status code to be set. */ _JSONResponser_StatusCode.set(this, void 0); /** * The title of the response (e.g., "Success" or "Error"). */ _JSONResponser_Title.set(this, void 0); /** * The Status of Request Data */ _JSONResponser_status.set(this, void 0); /** * The message of the response (e.g., "The request was successful."). */ _JSONResponser_Message.set(this, void 0); /** * The Express response object. */ _JSONResponser_response.set(this, void 0); /** * The content type of the response. */ _JSONResponser_contentType.set(this, void 0); /** * An optional array of objects representing cookie data to be set. */ _JSONResponser_CookieData.set(this, void 0); __classPrivateFieldSet(this, _JSONResponser_StatusCode, StatusCode, "f"); __classPrivateFieldSet(this, _JSONResponser_Title, Title, "f"); __classPrivateFieldSet(this, _JSONResponser_Message, Message, "f"); __classPrivateFieldSet(this, _JSONResponser_status, status, "f"); __classPrivateFieldSet(this, _JSONResponser_response, response, "f"); __classPrivateFieldSet(this, _JSONResponser_contentType, contentType, "f"); __classPrivateFieldSet(this, _JSONResponser_CookieData, CookieData, "f"); this.setContentType(); } /** * Sends a JSON response. * * @param Data - The data to be included in the response. * @param Title - The title of the response (optional). * @param Message - The message of the response (optional). * @param StatusCode - The status code of the response (optional). * @param CookieData - The cookie data to be set in the response (optional) e.g., [{ name: "cookieName", value: "cookieValue", options: { maxAge: 900000, httpOnly: true } }] */ Send(Data, Message, Title, StatusCode, CookieData) { var _a, _b, _c; // If CookieData is provided, set the cookies. Otherwise, do nothing. if (__classPrivateFieldGet(this, _JSONResponser_CookieData, "f")) { __classPrivateFieldGet(this, _JSONResponser_CookieData, "f").forEach(({ name, value, options }) => { __classPrivateFieldGet(this, _JSONResponser_response, "f").cookie(name, value, options); }); } // If the Title, Message, or StatusCode parameters are provided, update the corresponding properties. if (Title) { __classPrivateFieldSet(this, _JSONResponser_Title, Title, "f"); // Update the Title property. If the Title property } if (Message) { __classPrivateFieldSet(this, _JSONResponser_Message, Message, "f"); // Update the Message property. } if (StatusCode) { __classPrivateFieldSet(this, _JSONResponser_StatusCode, StatusCode, "f"); // Update the StatusCode property. } // if CookieData is provided, set the cookies. Otherwise, do nothing. if (CookieData) { CookieData.forEach(({ name, value, options }) => { __classPrivateFieldGet(this, _JSONResponser_response, "f").cookie(name, value, options); }); } // Send the JSON response. __classPrivateFieldGet(this, _JSONResponser_response, "f").status(__classPrivateFieldGet(this, _JSONResponser_StatusCode, "f")).json({ status: (_a = __classPrivateFieldGet(this, _JSONResponser_status, "f")) !== null && _a !== void 0 ? _a : undefined, statusCode: __classPrivateFieldGet(this, _JSONResponser_StatusCode, "f"), Title: (_b = __classPrivateFieldGet(this, _JSONResponser_Title, "f")) !== null && _b !== void 0 ? _b : undefined, message: (_c = __classPrivateFieldGet(this, _JSONResponser_Message, "f")) !== null && _c !== void 0 ? _c : undefined, data: Data, }); } /** * Sets the content type of the response. * @private */ setContentType() { if (__classPrivateFieldGet(this, _JSONResponser_contentType, "f")) { for (const Type of Response_Constant_1.MIME_Types) { if (Type.toLowerCase().includes(__classPrivateFieldGet(this, _JSONResponser_contentType, "f").toLowerCase())) { __classPrivateFieldGet(this, _JSONResponser_response, "f").setHeader("Content-Type", Type); break; } } } } } _JSONResponser_StatusCode = new WeakMap(), _JSONResponser_Title = new WeakMap(), _JSONResponser_status = new WeakMap(), _JSONResponser_Message = new WeakMap(), _JSONResponser_response = new WeakMap(), _JSONResponser_contentType = new WeakMap(), _JSONResponser_CookieData = new WeakMap(); exports.default = JSONResponser; //# sourceMappingURL=JSON-Response.class.js.map