-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
26 lines (20 loc) · 788 Bytes
/
app.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
// config/responseConfig.js
import { basicConfig } from './config/basic.js';
import { responseConfig } from './config/config.js';
const customResponsesMiddleware = (configs = []) => (req, res, next) => {
// Start with the default response configuration
let mergedConfig = { ...basicConfig, ...responseConfig };
// Merge all provided configurations
configs.forEach((config) => {
mergedConfig = { ...mergedConfig, ...config };
});
// Attach response methods for each key in mergedConfig
Object.keys(mergedConfig).forEach((key) => {
res[key] = (overrides = {}) => {
const responseConfig = { ...mergedConfig[key], ...overrides };
res.status(responseConfig.code).json(responseConfig);
};
});
next();
};
export default customResponsesMiddleware;