v1.13.4: Standardize all route responses to use response helpers
Convert ~160 raw res.json()/res.status().json() calls across 32+ files to use centralized helpers from src/utils/responses.js (ok, errorResponse, successMessage, notFound, validationError, forbidden, unauthorized, conflict). No behavior changes — response shapes are identical. Future schema changes (e.g., requestId envelope) only need to update one module. Fix error vs errorResponse signature mismatch in routes/health.js CA cert endpoint where error(res, message, statusCode) was being called with errorResponse(res, statusCode, message, extras) argument order. Files changed: middleware.js, csrf-protection.js, error-handler.js, license-manager.js, src/app.js, and 27 route files. Test suite: 755 pass / 4 pre-existing failures (services credential tests).
This commit is contained in:
@@ -3,7 +3,7 @@ const fs = require('fs');
|
||||
const { CADDY, REGEX, LIMITS } = require('../constants');
|
||||
const { ValidationError, ConflictError, NotFoundError } = require('../errors');
|
||||
const { validateURL } = require('../input-validator');
|
||||
const { ok } = require('../src/utils/responses');
|
||||
const { ok, successMessage } = require('../src/utils/responses');
|
||||
|
||||
/**
|
||||
* Sites route factory
|
||||
@@ -24,14 +24,14 @@ module.exports = function({ asyncHandler, caddy, dns, fetchT, buildDomain, addSe
|
||||
// Get Caddyfile contents
|
||||
router.get('/caddyfile', asyncHandler(async (req, res) => {
|
||||
const content = await caddy.read();
|
||||
res.json({ success: true, content });
|
||||
ok(res, { content });
|
||||
}, 'caddyfile-get'));
|
||||
|
||||
// Get current Caddy config (from admin API)
|
||||
router.get('/caddy/config', asyncHandler(async (req, res) => {
|
||||
const response = await fetchT(`${caddy.adminUrl}/config/`);
|
||||
const config = await response.json();
|
||||
res.json({ success: true, config });
|
||||
ok(res, { config });
|
||||
}, 'caddy-config'));
|
||||
|
||||
// Reload Caddy configuration via admin API
|
||||
@@ -50,7 +50,7 @@ module.exports = function({ asyncHandler, caddy, dns, fetchT, buildDomain, addSe
|
||||
throw new Error('Caddy reload failed. Check server logs for details.');
|
||||
}
|
||||
|
||||
res.json({ success: true, message: 'Caddy configuration reloaded successfully' });
|
||||
successMessage(res, 'Caddy configuration reloaded successfully');
|
||||
}, 'caddy-reload'));
|
||||
|
||||
// Get Certificate Authorities from Caddyfile
|
||||
@@ -153,7 +153,7 @@ module.exports = function({ asyncHandler, caddy, dns, fetchT, buildDomain, addSe
|
||||
throw new NotFoundError(`Site block for "" in Caddyfile`);
|
||||
}
|
||||
|
||||
res.json({ success: true, message: `Site "${domain}" removed from Caddyfile and Caddy reloaded` });
|
||||
successMessage(res, `Site "${domain}" removed from Caddyfile and Caddy reloaded`);
|
||||
}, 'site-delete'));
|
||||
|
||||
// Add a new site to Caddyfile and reload
|
||||
@@ -181,7 +181,7 @@ module.exports = function({ asyncHandler, caddy, dns, fetchT, buildDomain, addSe
|
||||
result.rolledBack ? { note: 'Caddyfile was rolled back to previous state' } : {});
|
||||
}
|
||||
|
||||
res.json({ success: true, message: `Site "${domain}" added to Caddyfile and Caddy reloaded successfully` });
|
||||
successMessage(res, `Site "${domain}" added to Caddyfile and Caddy reloaded successfully`);
|
||||
}, 'site-add'));
|
||||
|
||||
// Add external service reverse proxy to Caddyfile
|
||||
@@ -261,12 +261,11 @@ module.exports = function({ asyncHandler, caddy, dns, fetchT, buildDomain, addSe
|
||||
}
|
||||
}
|
||||
|
||||
const response = {
|
||||
success: true,
|
||||
const responseData = {
|
||||
message: `External service proxy for ${domain} -> ${externalUrl} created${shouldReload ? ' and Caddy reloaded' : ''}`
|
||||
};
|
||||
if (dnsWarning) response.warning = dnsWarning;
|
||||
res.json(response);
|
||||
if (dnsWarning) responseData.warning = dnsWarning;
|
||||
ok(res, responseData);
|
||||
}, 'site-external'));
|
||||
|
||||
return router;
|
||||
|
||||
Reference in New Issue
Block a user