mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-19 08:31:43 +03:00
Moved the version rewrite middleware to a module
refs https://github.com/TryGhost/Toolbox/issues/315 - There's enough logic in the middleware to become it's own module and maybe even get extracted to an external module
This commit is contained in:
parent
8e1c10d146
commit
dde9a5462a
@ -1,5 +1,3 @@
|
|||||||
/* eslint-disable max-lines */
|
|
||||||
const routeMatch = require('path-match')();
|
|
||||||
const APIVersionCompatibilityService = require('@tryghost/api-version-compatibility-service');
|
const APIVersionCompatibilityService = require('@tryghost/api-version-compatibility-service');
|
||||||
const VersionNotificationsDataService = require('@tryghost/version-notifications-data-service');
|
const VersionNotificationsDataService = require('@tryghost/version-notifications-data-service');
|
||||||
const versionMismatchHandler = require('@tryghost/mw-api-version-mismatch');
|
const versionMismatchHandler = require('@tryghost/mw-api-version-mismatch');
|
||||||
@ -50,38 +48,6 @@ module.exports.contentVersion = (req, res, next) => {
|
|||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
module.exports.versionRewrites = require('./mw-version-rewrites');
|
||||||
* If there is a version in the URL, and this is a valid API URL containing admin/content
|
|
||||||
* Rewrite the URL and add the accept-version & deprecation headers
|
|
||||||
* @param {import('express').Request} req
|
|
||||||
* @param {import('express').Response} res
|
|
||||||
* @param {import('express').NextFunction} next
|
|
||||||
*/
|
|
||||||
module.exports.versionRewrites = (req, res, next) => {
|
|
||||||
let {version} = routeMatch('/:version(v2|v3|v4|canary)/:api(admin|content)/*')(req.url);
|
|
||||||
|
|
||||||
// If we don't match a valid version, carry on
|
|
||||||
if (!version) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
|
|
||||||
const versionlessUrl = req.url.replace(`${version}/`, '');
|
|
||||||
|
|
||||||
// Always send the explicit, numeric version in headers
|
|
||||||
if (version === 'canary') {
|
|
||||||
version = 'v4';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rewrite the url
|
|
||||||
req.url = versionlessUrl;
|
|
||||||
|
|
||||||
// Add the accept-version header so our internal systems will act as if it was set on the request
|
|
||||||
req.headers['accept-version'] = req.headers['accept-version'] || `${version}.0`;
|
|
||||||
|
|
||||||
res.header('Deprecation', `version="${version}"`);
|
|
||||||
res.header('Link', `<${urlUtils.urlJoin(urlUtils.urlFor('admin', true), 'api', versionlessUrl)}>; rel="latest-version"`);
|
|
||||||
|
|
||||||
next();
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.init = init;
|
module.exports.init = init;
|
||||||
|
@ -0,0 +1,36 @@
|
|||||||
|
const routeMatch = require('path-match')();
|
||||||
|
const urlUtils = require('../../../shared/url-utils');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If there is a version in the URL, and this is a valid API URL containing admin/content
|
||||||
|
* Rewrite the URL and add the accept-version & deprecation headers
|
||||||
|
* @param {import('express').Request} req
|
||||||
|
* @param {import('express').Response} res
|
||||||
|
* @param {import('express').NextFunction} next
|
||||||
|
*/
|
||||||
|
module.exports = (req, res, next) => {
|
||||||
|
let {version} = routeMatch('/:version(v2|v3|v4|canary)/:api(admin|content)/*')(req.url);
|
||||||
|
|
||||||
|
// If we don't match a valid version, carry on
|
||||||
|
if (!version) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
const versionlessUrl = req.url.replace(`${version}/`, '');
|
||||||
|
|
||||||
|
// Always send the explicit, numeric version in headers
|
||||||
|
if (version === 'canary') {
|
||||||
|
version = 'v4';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rewrite the url
|
||||||
|
req.url = versionlessUrl;
|
||||||
|
|
||||||
|
// Add the accept-version header so our internal systems will act as if it was set on the request
|
||||||
|
req.headers['accept-version'] = req.headers['accept-version'] || `${version}.0`;
|
||||||
|
|
||||||
|
res.header('Deprecation', `version="${version}"`);
|
||||||
|
res.header('Link', `<${urlUtils.urlJoin(urlUtils.urlFor('admin', true), 'api', versionlessUrl)}>; rel="latest-version"`);
|
||||||
|
|
||||||
|
next();
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user