mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
28 lines
1.5 KiB
JavaScript
28 lines
1.5 KiB
JavaScript
/***********************************************************************************************************************************************************************
|
|
* File Name: response.util.js
|
|
* Type of Program: Utility
|
|
* Description: Standardised HTTP response helpers.
|
|
* Wraps all responses in a consistent envelope:
|
|
* { status, message, data?, errors? }
|
|
* Author: rgrgogu
|
|
* Date Created: Oct. 6, 2025
|
|
***********************************************************************************************************************************************************************
|
|
* HOW TO USE:
|
|
* const R = require('../utils/response.util');
|
|
* return R.success(res, 'User created', user, 201);
|
|
* return R.error(res, 'Not found', 404);
|
|
***********************************************************************************************************************************************************************/
|
|
|
|
const success = (res, message = 'OK', data = null, statusCode = 200) =>
|
|
res.status(statusCode).json({ status: 'success', message, data });
|
|
|
|
const error = (res, message = 'An error occurred', statusCode = 500, errors = null) => {
|
|
const body = { status: 'error', message };
|
|
if (errors) body.errors = errors;
|
|
return res.status(statusCode).json(body);
|
|
};
|
|
|
|
const validationError = (res, errors) =>
|
|
res.status(422).json({ status: 'error', message: 'Validation failed', errors });
|
|
|
|
module.exports = { success, error, validationError }; |