243b387063
refs #8126, #8221, #8223 ✨ New 'Proxy' for all helper requires - this is not currently enforced, but could be, much like apps - the proxy object is HUGE - changed date to use SafeString, this should have been there anyway - use the proxy for all helpers, including those in apps 😁 ✨ 🎨 Single instance of hbs for theme + for errors - we now have theme/engine instead of requiring express-hbs everywhere - only error-handler still also requires express-hbs, this is so that we can render errors without extra crud - TODO: remove the asset helper after #8126 IF it is not needed, or else remove the TODO 🎨 Cleanup visibility utils 🎨 Clean up the proxy a little bit 🚨 Unskip test as it now works! 🎨 Minor amends as per comments
158 lines
3.9 KiB
JavaScript
158 lines
3.9 KiB
JavaScript
// # Get Helper
|
|
// Usage: `{{#get "posts" limit="5"}}`, `{{#get "tags" limit="all"}}`
|
|
// Fetches data from the API
|
|
var proxy = require('./proxy'),
|
|
_ = require('lodash'),
|
|
Promise = require('bluebird'),
|
|
jsonpath = require('jsonpath'),
|
|
|
|
logging = proxy.logging,
|
|
i18n = proxy.i18n,
|
|
createFrame = proxy.hbs.handlebars.createFrame,
|
|
|
|
api = proxy.api,
|
|
labs = proxy.labs,
|
|
resources,
|
|
pathAliases,
|
|
get;
|
|
|
|
// Endpoints that the helper is able to access
|
|
resources = ['posts', 'tags', 'users'];
|
|
|
|
// Short forms of paths which we should understand
|
|
pathAliases = {
|
|
'post.tags': 'post.tags[*].slug',
|
|
'post.author': 'post.author.slug'
|
|
};
|
|
|
|
/**
|
|
* ## Is Browse
|
|
* Is this a Browse request or a Read request?
|
|
* @param {Object} resource
|
|
* @param {Object} options
|
|
* @returns {boolean}
|
|
*/
|
|
function isBrowse(resource, options) {
|
|
var browse = true;
|
|
|
|
if (options.id || options.slug) {
|
|
browse = false;
|
|
}
|
|
|
|
return browse;
|
|
}
|
|
|
|
/**
|
|
* ## Resolve Paths
|
|
* Find and resolve path strings
|
|
*
|
|
* @param {Object} data
|
|
* @param {String} value
|
|
* @returns {String}
|
|
*/
|
|
function resolvePaths(data, value) {
|
|
var regex = /\{\{(.*?)\}\}/g;
|
|
|
|
value = value.replace(regex, function (match, path) {
|
|
var result;
|
|
|
|
// Handle aliases
|
|
path = pathAliases[path] ? pathAliases[path] : path;
|
|
// Handle Handlebars .[] style arrays
|
|
path = path.replace(/\.\[/g, '[');
|
|
|
|
// Do the query, and convert from array to string
|
|
result = jsonpath.query(data, path).join(',');
|
|
|
|
return result;
|
|
});
|
|
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* ## Parse Options
|
|
* Ensure options passed in make sense
|
|
*
|
|
* @param {Object} data
|
|
* @param {Object} options
|
|
* @returns {*}
|
|
*/
|
|
function parseOptions(data, options) {
|
|
if (_.isString(options.filter)) {
|
|
options.filter = resolvePaths(data, options.filter);
|
|
}
|
|
|
|
return options;
|
|
}
|
|
|
|
/**
|
|
* ## Get
|
|
* @param {Object} resource
|
|
* @param {Object} options
|
|
* @returns {Promise}
|
|
*/
|
|
get = function get(resource, options) {
|
|
options = options || {};
|
|
options.hash = options.hash || {};
|
|
options.data = options.data || {};
|
|
|
|
var self = this,
|
|
data = createFrame(options.data),
|
|
apiOptions = options.hash,
|
|
apiMethod;
|
|
|
|
if (!options.fn) {
|
|
data.error = i18n.t('warnings.helpers.get.mustBeCalledAsBlock');
|
|
logging.warn(data.error);
|
|
return Promise.resolve();
|
|
}
|
|
|
|
if (!_.includes(resources, resource)) {
|
|
data.error = i18n.t('warnings.helpers.get.invalidResource');
|
|
logging.warn(data.error);
|
|
return Promise.resolve(options.inverse(self, {data: data}));
|
|
}
|
|
|
|
// Determine if this is a read or browse
|
|
apiMethod = isBrowse(resource, apiOptions) ? api[resource].browse : api[resource].read;
|
|
// Parse the options we're going to pass to the API
|
|
apiOptions = parseOptions(this, apiOptions);
|
|
|
|
return apiMethod(apiOptions).then(function success(result) {
|
|
var blockParams;
|
|
|
|
// block params allows the theme developer to name the data using something like
|
|
// `{{#get "posts" as |result pageInfo|}}`
|
|
blockParams = [result[resource]];
|
|
if (result.meta && result.meta.pagination) {
|
|
result.pagination = result.meta.pagination;
|
|
blockParams.push(result.meta.pagination);
|
|
}
|
|
|
|
// Call the main template function
|
|
return options.fn(result, {
|
|
data: data,
|
|
blockParams: blockParams
|
|
});
|
|
}).catch(function error(err) {
|
|
data.error = err.message;
|
|
return options.inverse(self, {data: data});
|
|
});
|
|
};
|
|
|
|
module.exports = function getLabsWrapper() {
|
|
var self = this,
|
|
args = arguments;
|
|
|
|
return labs.enabledHelper({
|
|
flagKey: 'publicAPI',
|
|
flagName: 'Public API',
|
|
helperName: 'get',
|
|
helpUrl: 'http://support.ghost.org/public-api-beta/',
|
|
async: true
|
|
}, function executeHelper() {
|
|
return get.apply(self, args);
|
|
});
|
|
};
|