Express/MongoDB backend with JWT auth, RBAC, S3 uploads, notifications, and scheduled jobs.
43 lines
953 B
JavaScript
43 lines
953 B
JavaScript
// /utils/apiResponse.js
|
|
|
|
const successResponse = (res, statusCode = 200, message = 'Operation successful', data = null) => {
|
|
const response = {
|
|
success: true,
|
|
message
|
|
};
|
|
if (data !== null) {
|
|
response.data = data;
|
|
}
|
|
return res.status(statusCode).json(response);
|
|
};
|
|
|
|
const listResponse = (res, statusCode = 200, data = [], meta = {}) => {
|
|
return res.status(statusCode).json({
|
|
success: true,
|
|
data,
|
|
meta: {
|
|
totalCount: meta.totalCount || 0,
|
|
totalPages: meta.totalPages || 0,
|
|
currentPage: meta.currentPage || 1,
|
|
limit: meta.limit || 20
|
|
}
|
|
});
|
|
};
|
|
|
|
const errorResponse = (res, statusCode = 500, code = 'INTERNAL_SERVER_ERROR', message = 'An error occurred', details = {}) => {
|
|
return res.status(statusCode).json({
|
|
success: false,
|
|
error: {
|
|
code,
|
|
message,
|
|
details: details || {}
|
|
}
|
|
});
|
|
};
|
|
|
|
module.exports = {
|
|
successResponse,
|
|
listResponse,
|
|
errorResponse
|
|
};
|