All files / postcardotp-backend/src/utils successResponse.js

100% Statements 4/4
33.33% Branches 1/3
100% Functions 1/1
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                1x             6x                   6x     1x  
/**
 * Utility function to format success responses in a consistent manner.
 * @param {Object} res - The Express response object.
 * @param {Object} data - The response data to be returned.
 * @param {string} message - Optional message to include in the response.
 * @param {number} statusCode - HTTP status code (default: 200).
 * @param {Object} meta - Optional metadata to include (e.g., pagination details).
 */
const successResponse = (
  res,
  data,
  message = "Request was successful",
  statusCode = 200,
  meta = {},
) => {
  const response = {
    success: true,
    message,
    data,
    statusCode,
    meta,
    timestamp: new Date().toISOString(),
  };
 
  // Send the response directly
  return res.status(statusCode).json(response);
};
 
module.exports = successResponse;