0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00
ghost/core/server/services/api-version-compatibility/legacy-api-path-match.js
Hannah Wolfe b911c66bb6
Fixed legacyApiPathMatch not working with subdirs
- The recently refactored path matching code forgot to take into account that originalUrl can include the subdir
- Added more permutations to tests and ensured that all tests pass
- This means we don't have to worry about what sort of path we pass to the function, it'll figure out the version and api info
2022-05-10 13:57:06 +01:00

23 lines
524 B
JavaScript

const pathMatch = require('path-match')();
module.exports = (url) => {
let basePath = 'ghost/api';
let apiRouteMatcher = '/:version(v2|v3|v4|canary)?/:api(admin|content)/*';
let urlToMatch = url;
if (url.includes(basePath)) {
urlToMatch = url.split(basePath)[1];
}
if (!urlToMatch.endsWith('/')) {
urlToMatch += '/';
}
let {version, api} = pathMatch(apiRouteMatcher)(urlToMatch);
if (version === [null]) {
version = null;
}
return {version, api};
};