-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (29 loc) · 884 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Send response with status and additional data.
* @param {object} obj - Object containing response, status, and additional data.
* @param {object} obj.res - Express response object.
* @param {number} obj.status - HTTP status code.
* @param {object} obj.data - Additional data to send in the response.
*/
function sendResponse(obj) {
const { res, status, data } = obj;
if (!res || !status) {
throw new Error("Response object and status are required");
}
if (typeof data !== 'object' ||
Array.isArray(data) || data == null) {
throw new TypeError("Data must be an object");
}
try {
if (data) {
return res.status(status).json(data);
} else {
return res.sendStatus(status);
}
} catch (error) {
return res.sendStatus(500);
}
}
module.exports = {
sendResponse
}