Ghost/core/server/services/routing/helpers/entry-lookup.js
Katharina Irrgang 6b758bda79
Refactored routing config for multiple api versions (#10333)
refs #10124

- one clean v0.1 and v2 config file for routing!
- solves one underlying bug reported in #10124
- the alias handling was just a hotfix to support v2 for the site
- but it was hard to read, ugly
- now we have two clean configs
- we'll see how useful it is
- need to do proper manual testing on Monday
2019-01-04 21:59:39 +01:00

56 lines
1.7 KiB
JavaScript

const _ = require('lodash'),
Promise = require('bluebird'),
url = require('url'),
debug = require('ghost-ignition').debug('services:routing:helpers:entry-lookup'),
routeMatch = require('path-match')();
function entryLookup(postUrl, routerOptions, locals) {
debug(postUrl);
const api = require('../../../api')[locals.apiVersion];
const targetPath = url.parse(postUrl).path;
const permalinks = routerOptions.permalinks;
let isEditURL = false;
// CASE: e.g. /:slug/ -> { slug: 'value' }
const matchFunc = routeMatch(permalinks);
const params = matchFunc(targetPath);
debug(targetPath);
debug(params);
debug(permalinks);
// CASE 1: no matches, resolve
// CASE 2: params can be empty e.g. permalink is /featured/:options(edit)?/ and path is /featured/
if (params === false || !Object.keys(params).length) {
return Promise.resolve();
}
// CASE: redirect if url contains `/edit/` at the end
if (params.options && params.options.toLowerCase() === 'edit') {
isEditURL = true;
}
/**
* Query database to find entry.
* @deprecated: `author`, will be removed in Ghost 3.0
*/
return api[routerOptions.query.controller]
.read(_.extend(_.pick(params, 'slug', 'id'), {include: 'author,authors,tags'}))
.then(function then(result) {
const entry = result[routerOptions.query.resource][0];
if (!entry) {
return Promise.resolve();
}
return {
entry: entry,
isEditURL: isEditURL,
isUnknownOption: isEditURL ? false : !!params.options
};
});
}
module.exports = entryLookup;