2015-06-27 18:42:10 +03:00
|
|
|
// # Get Helper
|
|
|
|
// Usage: `{{#get "posts" limit="5"}}`, `{{#get "tags" limit="all"}}`
|
|
|
|
// Fetches data from the API
|
|
|
|
var _ = require('lodash'),
|
|
|
|
hbs = require('express-hbs'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
errors = require('../errors'),
|
|
|
|
api = require('../api'),
|
2015-10-22 13:12:21 +03:00
|
|
|
jsonpath = require('jsonpath'),
|
2015-11-03 22:17:24 +03:00
|
|
|
labs = require('../utils/labs'),
|
2015-06-27 18:42:10 +03:00
|
|
|
resources,
|
2015-10-22 13:12:21 +03:00
|
|
|
pathAliases,
|
2015-06-27 18:42:10 +03:00
|
|
|
get;
|
|
|
|
|
|
|
|
// Endpoints that the helper is able to access
|
|
|
|
resources = ['posts', 'tags', 'users'];
|
|
|
|
|
2015-10-22 13:12:21 +03:00
|
|
|
// Short forms of paths which we should understand
|
|
|
|
pathAliases = {
|
|
|
|
'post.tags': 'post.tags[*].slug',
|
|
|
|
'post.author': 'post.author.slug'
|
|
|
|
};
|
|
|
|
|
2015-06-27 18:42:10 +03:00
|
|
|
/**
|
|
|
|
* ## Is Browse
|
|
|
|
* Is this a Browse request or a Read request?
|
|
|
|
* @param {Object} context
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function isBrowse(context, options) {
|
|
|
|
var browse = true;
|
|
|
|
|
|
|
|
if (options.id || options.slug) {
|
|
|
|
browse = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return browse;
|
|
|
|
}
|
|
|
|
|
2015-10-22 13:12:21 +03:00
|
|
|
/**
|
|
|
|
* ## 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;
|
|
|
|
}
|
|
|
|
|
2015-06-27 18:42:10 +03:00
|
|
|
/**
|
|
|
|
* ## Parse Options
|
|
|
|
* Ensure options passed in make sense
|
|
|
|
*
|
|
|
|
* @param {Object} data
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function parseOptions(data, options) {
|
2015-10-22 13:12:21 +03:00
|
|
|
if (_.isString(options.filter)) {
|
|
|
|
options.filter = resolvePaths(data, options.filter);
|
|
|
|
}
|
|
|
|
|
2015-06-27 18:42:10 +03:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Get
|
|
|
|
* @param {Object} context
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
get = function get(context, options) {
|
|
|
|
options = options || {};
|
|
|
|
options.hash = options.hash || {};
|
|
|
|
options.data = options.data || {};
|
|
|
|
|
|
|
|
var self = this,
|
|
|
|
data = hbs.handlebars.createFrame(options.data),
|
|
|
|
apiOptions = _.omit(options.hash, 'context'),
|
|
|
|
apiMethod;
|
|
|
|
|
|
|
|
if (!options.fn) {
|
|
|
|
data.error = 'Get helper must be called as a block';
|
|
|
|
errors.logWarn(data.error);
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.contains(resources, context)) {
|
|
|
|
data.error = 'Invalid resource given to get helper';
|
|
|
|
errors.logWarn(data.error);
|
|
|
|
return Promise.resolve(options.inverse(self, {data: data}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if this is a read or browse
|
|
|
|
apiMethod = isBrowse(context, apiOptions) ? api[context].browse : api[context].read;
|
|
|
|
// Parse the options we're going to pass to the API
|
|
|
|
apiOptions = parseOptions(this, apiOptions);
|
|
|
|
|
|
|
|
return apiMethod(apiOptions).then(function success(result) {
|
2015-10-23 04:02:26 +03:00
|
|
|
var blockParams;
|
|
|
|
|
|
|
|
// If no result is found, call the inverse or `{{else}}` function
|
2015-06-27 18:42:10 +03:00
|
|
|
if (_.isEmpty(result[context])) {
|
|
|
|
return options.inverse(self, {data: data});
|
|
|
|
}
|
|
|
|
|
2015-10-23 04:02:26 +03:00
|
|
|
// block params allows the theme developer to name the data using something like
|
2015-12-14 16:19:38 +03:00
|
|
|
// `{{#get "posts" as |result pageInfo|}}`
|
2015-10-23 04:02:26 +03:00
|
|
|
blockParams = [result[context]];
|
|
|
|
if (result.meta && result.meta.pagination) {
|
2015-12-14 16:19:38 +03:00
|
|
|
result.pagination = result.meta.pagination;
|
2015-10-23 04:02:26 +03:00
|
|
|
blockParams.push(result.meta.pagination);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the main template function
|
2015-06-27 18:42:10 +03:00
|
|
|
return options.fn(result, {
|
|
|
|
data: data,
|
2015-10-23 04:02:26 +03:00
|
|
|
blockParams: blockParams
|
2015-06-27 18:42:10 +03:00
|
|
|
});
|
|
|
|
}).catch(function error(err) {
|
|
|
|
data.error = err.message;
|
|
|
|
return options.inverse(self, {data: data});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-11-03 22:17:24 +03:00
|
|
|
module.exports = function getWithLabs(context, options) {
|
|
|
|
var self = this,
|
|
|
|
errorMessages = [
|
|
|
|
'The {{get}} helper is not available.',
|
|
|
|
'Public API access must be enabled if you wish to use the {{get}} helper.',
|
|
|
|
'See http://support.ghost.org/public-api-beta'
|
|
|
|
];
|
|
|
|
|
2015-12-03 18:50:52 +03:00
|
|
|
if (labs.isSet('publicAPI') === true) {
|
|
|
|
// get helper is active
|
|
|
|
return get.call(self, context, options);
|
|
|
|
} else {
|
|
|
|
errors.logError.apply(this, errorMessages);
|
|
|
|
return Promise.resolve(function noGetHelper() {
|
|
|
|
return '<script>console.error("' + errorMessages.join(' ') + '");</script>';
|
|
|
|
});
|
|
|
|
}
|
2015-11-03 22:17:24 +03:00
|
|
|
};
|