Ghost/core/frontend/services/routing/helpers/entry-lookup.js
Naz Gargol df7e64fafa
Extracted frontend folder (#10780)
refs #10790

- Moved /core/apps into core/frontend
- Moved /core/server/helpers to /core/frontend/helpers along with /core/server/services/themes
- Changed helper location in overrides
- Moved /core/server/services/routing to /core/frontend/services
- Moved /core/server/services/url to /core/frontend/services
- Moved /core/server/data/meta to /core/frontend/meta
- Moved /core/server/services/rss to /core/frontend/services
- Moved /core/server/data/xml to /core/frontend/services
2019-06-19 11:30:28 +02:00

72 lines
2.1 KiB
JavaScript

const _ = require('lodash');
const Promise = require('bluebird');
const url = require('url');
const debug = require('ghost-ignition').debug('services:routing:helpers:entry-lookup');
const routeMatch = require('path-match')();
const config = require('../../../../server/config');
/**
* @description Query API for a single entry/resource.
* @param {String} postUrl
* @param {Object} routerOptions
* @param {Objec†} locals
* @returns {*}
*/
function entryLookup(postUrl, routerOptions, locals) {
debug(postUrl);
const api = require('../../../../server/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;
}
let options = {
/**
* @deprecated: `author`, will be removed in Ghost 3.0
* @TODO: Remove "author" when we drop v0.1
*/
include: 'author,authors,tags'
};
if (config.get('enableDeveloperExperiments')) {
options.context = {member: locals.member};
}
return (api[routerOptions.query.controller] || api[routerOptions.query.resource])
.read(_.extend(_.pick(params, 'slug', 'id'), options))
.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;