2017-08-22 12:59:01 +03:00
|
|
|
var semver = require('semver'),
|
|
|
|
errors = require('../../errors'),
|
2016-06-09 18:14:36 +03:00
|
|
|
i18n = require('../../i18n');
|
|
|
|
|
|
|
|
function checkVersionMatch(req, res, next) {
|
2017-08-22 12:59:01 +03:00
|
|
|
var clientVersion = req.get('X-Ghost-Version'),
|
|
|
|
serverVersion = res.locals.version,
|
|
|
|
constraint = '^' + clientVersion + '.0';
|
2016-06-09 18:14:36 +03:00
|
|
|
|
2017-08-22 12:59:01 +03:00
|
|
|
// no error when client is on an earlier minor version than server
|
|
|
|
// error when client is on a later minor version than server
|
|
|
|
// always error when the major version is different
|
|
|
|
|
|
|
|
if (clientVersion && !semver.satisfies(serverVersion, constraint)) {
|
2016-10-06 15:27:35 +03:00
|
|
|
return next(new errors.VersionMismatchError({
|
2017-08-22 12:59:01 +03:00
|
|
|
message: i18n.t('errors.middleware.api.versionMismatch', {clientVersion: clientVersion, serverVersion: serverVersion})
|
2016-10-06 15:27:35 +03:00
|
|
|
}));
|
2016-06-09 18:14:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = checkVersionMatch;
|