-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.js
More file actions
56 lines (52 loc) · 1.31 KB
/
Copy pathresponse.js
File metadata and controls
56 lines (52 loc) · 1.31 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* API response utility functions
*/
/**
* Create a success response object
* @param {*} data - The data to include in the response
* @param {Object} meta - Optional metadata for the response
* @returns {Object} Success response object
*/
const successResponse = (data, meta = {}) => {
return {
success: true,
data,
meta
};
};
/**
* Create an error response object
* @param {string} message - Error message
* @param {string} code - Error code
* @param {number} statusCode - HTTP status code
* @returns {Object} Error response object
*/
const errorResponse = (message, code = 'INTERNAL_ERROR', statusCode = 500) => {
const error = new Error(message);
error.code = code;
error.statusCode = statusCode;
return error;
};
/**
* Create a paginated response
* @param {Array} data - The data array to paginate
* @param {number} page - Current page number
* @param {number} pageSize - Number of items per page
* @param {number} total - Total number of items
* @returns {Object} Paginated response object
*/
const paginatedResponse = (data, page, pageSize, total) => {
return successResponse(data, {
pagination: {
page,
pageSize,
total,
totalPages: Math.ceil(total / pageSize)
}
});
};
module.exports = {
successResponse,
errorResponse,
paginatedResponse
};