2** EPITECH PROJECT, 2024
8import { Response } from "express";
9import { TokenManagement as token_management } from "./token_management";
10import { SpeakOnCorrectStatus as speak_on_correct_status, SpeakOnCorrectStatus } from "./speak_on_correct_status";
12export namespace BuildResponse {
13 // Type alias for a function that handles response and JSON
14 export type ResponseFunction = (res: Response, json: object) => void;
17 * Builds a response JSON object based on the given parameters.
19 * @param {string} title - The title of the response.
20 * @param {string} message - The message of the response.
21 * @param {any} resp - The response payload.
22 * @param {string} token - The token to validate.
23 * @param {boolean} [error=false] - Whether the response is an error response.
24 * @returns {object} The constructed JSON response object.
26 export function build_response(
31 error: boolean = false
33 // Create a response object dynamically using JavaScript native object literals
34 const json_body: { [key: string]: any } = {
35 title, // Add title property
36 message, // Add message property
37 logged_in: token_management.is_token_correct(token), // Check if the token is correct
40 // Add the response or error dynamically based on the 'error' flag
42 json_body.error = resp; // If it's an error, add the error response
44 json_body.resp = resp; // Otherwise, add the regular response
47 return json_body; // Return the dynamically constructed object
51 * Overload signatures for send_response function.
53 * When the status is a number (HTTP status code):
55 * @param {Response} res - The Express response object.
56 * @param {number} status - The HTTP status code to send.
57 * @param {object} json - The JSON response to send.
59 * When the status is a function (custom handler):
61 * @param {Response} res - The Express response object.
62 * @param {ResponseFunction} status - A function that handles the response.
63 * @param {object} json - The JSON response to send.
65 export function send_response(
69 ): void; // Signature when status is a number (HTTP status code)
71 export function send_response(
73 status: ResponseFunction,
75 ): void; // Signature when status is a function
78 * Sends a JSON response to the client with a specified HTTP status.
79 * Handles both number (status code) and function (custom handler).
81 * @param {Response} res - The Express response object.
82 * @param {number|ResponseFunction} status - The HTTP status code or custom handler function.
83 * @param {object} json - The JSON response to send.
85 export function send_response(
87 status: number | ResponseFunction,
90 if (typeof status === 'function') {
91 status(res, json); // Call the function if status is a function
93 speak_on_correct_status.send_message_on_status(res, status, json); // Otherwise, use status as the code
98 * Overload signatures for build_and_send_response function.
100 * When the status is a number (HTTP status code):
102 * @param {Response} res - The Express response object.
103 * @param {number} status - The HTTP status code to send.
104 * @param {string} title - The title of the response.
105 * @param {string} message - The message of the response.
106 * @param {any} resp - The response payload.
107 * @param {string} token - The token to validate.
108 * @param {boolean} [error=false] - Whether the response is an error response.
110 * When the status is a function (custom handler):
112 * @param {Response} res - The Express response object.
113 * @param {ResponseFunction} status - A function that handles the response.
114 * @param {string} title - The title of the response.
115 * @param {string} message - The message of the response.
116 * @param {any} resp - The response payload.
117 * @param {string} token - The token to validate.
118 * @param {boolean} [error=false] - Whether the response is an error response.
120 export function build_and_send_response(
130 export function build_and_send_response(
132 status: ResponseFunction,
141 * Builds a JSON response and sends it to the client.
142 * Handles both number (status code) and function (custom handler).
144 * @param {Response} res - The Express response object.
145 * @param {number|ResponseFunction} status - The HTTP status code or custom handler function.
146 * @param {string} title - The title of the response.
147 * @param {string} message - The message of the response.
148 * @param {any} resp - The response payload.
149 * @param {string} token - The token to validate.
150 * @param {boolean} [error=false] - Whether the response is an error response.
152 export function build_and_send_response(
154 status: number | ResponseFunction,
159 error: boolean = false
161 const json = build_response(title, message, resp, token, error);
163 if (typeof status === 'function') {
166 send_response(res, status, json);