/
get.js
163 lines (139 loc) · 4.51 KB
/
get.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// # 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'),
jsonpath = require('jsonpath'),
labs = require('../utils/labs'),
i18n = require('../i18n'),
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 = hbs.handlebars.createFrame(options.data),
apiOptions = options.hash,
apiMethod;
if (!options.fn) {
data.error = i18n.t('warnings.helpers.get.mustBeCalledAsBlock');
errors.logWarn(data.error);
return Promise.resolve();
}
if (!_.contains(resources, resource)) {
data.error = i18n.t('warnings.helpers.get.invalidResource');
errors.logWarn(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;
// If no result is found, call the inverse or `{{else}}` function
if (_.isEmpty(result[resource])) {
return options.inverse(self, {data: data});
}
// 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 getWithLabs(resource, options) {
var self = this,
errorMessages = [
i18n.t('warnings.helpers.get.helperNotAvailable'),
i18n.t('warnings.helpers.get.apiMustBeEnabled'),
i18n.t('warnings.helpers.get.seeLink', {url: 'http://support.ghost.org/public-api-beta'})
];
if (labs.isSet('publicAPI') === true) {
// get helper is active
return get.call(self, resource, options);
} else {
errors.logError.apply(this, errorMessages);
return Promise.resolve(function noGetHelper() {
return '<script>console.error("' + errorMessages.join(' ') + '");</script>';
});
}
};