Ghost/ghost/mw-api-version-mismatch/lib/mw-api-version-mismatch.js
Naz 4873303658 Added handling for version missmatch as a midleware
refs https://github.com/TryGhost/Toolbox/issues/292

- This middleware is meant to deal with version missmatch erros and call a service that does all the business logic. Having this handling in a separate module allows for thisngs to be loosely coupled
2022-04-21 15:57:43 +08:00

18 lines
614 B
JavaScript

const versionMismatchHandler = (APIVersionCompatibilityService) => {
return async (err, req, res, next) => {
if (err && err.errorType === 'RequestNotAcceptableError') {
if (err.code === 'UPDATE_CLIENT') {
await APIVersionCompatibilityService.handleMismatch({
acceptVersion: req.headers['accept-version'],
contentVersion: `v${res.locals.safeVersion}`,
userAgent: req.headers['user-agent']
});
}
}
next(err, req, res);
};
};
module.exports = versionMismatchHandler;