2015-06-27 18:42:10 +03:00
|
|
|
// # Get Helper
|
|
|
|
// Usage: `{{#get "posts" limit="5"}}`, `{{#get "tags" limit="all"}}`
|
|
|
|
// Fetches data from the API
|
2017-04-04 19:07:35 +03:00
|
|
|
var proxy = require('./proxy'),
|
|
|
|
_ = require('lodash'),
|
2017-03-23 22:00:58 +03:00
|
|
|
Promise = require('bluebird'),
|
|
|
|
jsonpath = require('jsonpath'),
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
logging = proxy.logging,
|
|
|
|
i18n = proxy.i18n,
|
|
|
|
createFrame = proxy.hbs.handlebars.createFrame,
|
|
|
|
|
|
|
|
api = proxy.api,
|
|
|
|
labs = proxy.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
|
2017-03-23 22:00:58 +03:00
|
|
|
resources = ['posts', 'tags', 'users'];
|
2015-06-27 18:42:10 +03:00
|
|
|
|
2015-10-22 13:12:21 +03:00
|
|
|
// Short forms of paths which we should understand
|
2017-03-23 22:00:58 +03:00
|
|
|
pathAliases = {
|
2015-10-22 13:12:21 +03:00
|
|
|
'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?
|
2016-02-21 21:48:44 +03:00
|
|
|
* @param {Object} resource
|
2015-06-27 18:42:10 +03:00
|
|
|
* @param {Object} options
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2016-02-21 21:48:44 +03:00
|
|
|
function isBrowse(resource, options) {
|
2015-06-27 18:42:10 +03:00
|
|
|
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
|
2016-02-21 21:48:44 +03:00
|
|
|
* @param {Object} resource
|
2015-06-27 18:42:10 +03:00
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2016-02-21 21:48:44 +03:00
|
|
|
get = function get(resource, options) {
|
2015-06-27 18:42:10 +03:00
|
|
|
options = options || {};
|
|
|
|
options.hash = options.hash || {};
|
|
|
|
options.data = options.data || {};
|
|
|
|
|
|
|
|
var self = this,
|
2017-04-04 19:07:35 +03:00
|
|
|
data = createFrame(options.data),
|
2016-02-21 21:48:44 +03:00
|
|
|
apiOptions = options.hash,
|
2015-06-27 18:42:10 +03:00
|
|
|
apiMethod;
|
|
|
|
|
|
|
|
if (!options.fn) {
|
2017-08-27 19:00:32 +03:00
|
|
|
data.error = i18n.t('warnings.helpers.get.mustBeCalledAsBlock', {helperName: 'get'});
|
2016-10-04 18:33:43 +03:00
|
|
|
logging.warn(data.error);
|
2015-06-27 18:42:10 +03:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2016-06-11 21:23:27 +03:00
|
|
|
if (!_.includes(resources, resource)) {
|
2015-11-12 15:29:45 +03:00
|
|
|
data.error = i18n.t('warnings.helpers.get.invalidResource');
|
2016-10-04 18:33:43 +03:00
|
|
|
logging.warn(data.error);
|
2015-06-27 18:42:10 +03:00
|
|
|
return Promise.resolve(options.inverse(self, {data: data}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if this is a read or browse
|
2016-02-21 21:48:44 +03:00
|
|
|
apiMethod = isBrowse(resource, apiOptions) ? api[resource].browse : api[resource].read;
|
2015-06-27 18:42:10 +03:00
|
|
|
// 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;
|
|
|
|
|
|
|
|
// 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|}}`
|
2016-02-21 21:48:44 +03:00
|
|
|
blockParams = [result[resource]];
|
2015-10-23 04:02:26 +03:00
|
|
|
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) {
|
2017-08-27 19:00:32 +03:00
|
|
|
logging.error(err);
|
2015-06-27 18:42:10 +03:00
|
|
|
data.error = err.message;
|
|
|
|
return options.inverse(self, {data: data});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-03-23 22:00:58 +03:00
|
|
|
module.exports = function getLabsWrapper() {
|
|
|
|
var self = this,
|
|
|
|
args = arguments;
|
|
|
|
|
|
|
|
return labs.enabledHelper({
|
2017-04-03 19:42:50 +03:00
|
|
|
flagKey: 'publicAPI',
|
2017-03-23 22:00:58 +03:00
|
|
|
flagName: 'Public API',
|
|
|
|
helperName: 'get',
|
2017-06-08 22:34:20 +03:00
|
|
|
helpUrl: 'https://help.ghost.org/hc/en-us/articles/115000301672-Public-API-Beta',
|
2017-03-23 22:00:58 +03:00
|
|
|
async: true
|
|
|
|
}, function executeHelper() {
|
|
|
|
return get.apply(self, args);
|
|
|
|
});
|
2015-11-03 22:17:24 +03:00
|
|
|
};
|