Dashboard  2
Lot's of data
Loading...
Searching...
No Matches
build_response.ts
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** area-rattrapage
4** File description:
5** build_response.ts
6*/
7
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";
11
12export namespace BuildResponse {
13 // Type alias for a function that handles response and JSON
14 export type ResponseFunction = (res: Response, json: object) => void;
15
16 /**
17 * Builds a response JSON object based on the given parameters.
18 *
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.
25 */
26 export function build_response(
27 title: string,
28 message: string,
29 resp: any,
30 token: string,
31 error: boolean = false
32 ): object {
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
38 };
39
40 // Add the response or error dynamically based on the 'error' flag
41 if (error) {
42 json_body.error = resp; // If it's an error, add the error response
43 } else {
44 json_body.resp = resp; // Otherwise, add the regular response
45 }
46
47 return json_body; // Return the dynamically constructed object
48 }
49
50 /**
51 * Overload signatures for send_response function.
52 *
53 * When the status is a number (HTTP status code):
54 *
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.
58 *
59 * When the status is a function (custom handler):
60 *
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.
64 */
65 export function send_response(
66 res: Response,
67 status: number,
68 json: object
69 ): void; // Signature when status is a number (HTTP status code)
70
71 export function send_response(
72 res: Response,
73 status: ResponseFunction,
74 json: object
75 ): void; // Signature when status is a function
76
77 /**
78 * Sends a JSON response to the client with a specified HTTP status.
79 * Handles both number (status code) and function (custom handler).
80 *
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.
84 */
85 export function send_response(
86 res: Response,
87 status: number | ResponseFunction,
88 json: object
89 ): void {
90 if (typeof status === 'function') {
91 status(res, json); // Call the function if status is a function
92 } else {
93 speak_on_correct_status.send_message_on_status(res, status, json); // Otherwise, use status as the code
94 }
95 }
96
97 /**
98 * Overload signatures for build_and_send_response function.
99 *
100 * When the status is a number (HTTP status code):
101 *
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.
109 *
110 * When the status is a function (custom handler):
111 *
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.
119 */
120 export function build_and_send_response(
121 res: Response,
122 status: number,
123 title: string,
124 message: string,
125 resp: any,
126 token: string,
127 error?: boolean
128 ): void;
129
130 export function build_and_send_response(
131 res: Response,
132 status: ResponseFunction,
133 title: string,
134 message: string,
135 resp: any,
136 token: string,
137 error?: boolean
138 ): void;
139
140 /**
141 * Builds a JSON response and sends it to the client.
142 * Handles both number (status code) and function (custom handler).
143 *
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.
151 */
152 export function build_and_send_response(
153 res: Response,
154 status: number | ResponseFunction,
155 title: string,
156 message: string,
157 resp: any,
158 token: string,
159 error: boolean = false
160 ): void {
161 const json = build_response(title, message, resp, token, error);
162
163 if (typeof status === 'function') {
164 status(res, json);
165 } else {
166 send_response(res, status, json);
167 }
168 }
169};