Files
elasticsearch-js/scripts/generate_js_api/templates/action.tmpl
2013-10-03 10:05:58 -07:00

85 lines
2.3 KiB
Cheetah

var _ = require('<%= path2lib %>toolbelt')
, paramHelper = require('<%= path2lib %>param_helper');<%
if (_.keys(enumOptions).length) {
%>
<% _.each(enumOptions, function(options, name) {
%>
var <%= name %>Options = <%= stringify(options) %>;<%
});
}
%>
<%= notes ? notes + '\n' : ''
%>
/**
* Perform an elasticsearch [<%= name %>](<%= docUrl %>) request
*
* @for Client
* @method <%= name %>
* @param {Object} params - An object with parameters used to carry out this action<% _.each(params, function(param, paramName) { %>
* @param {<%= paramType(param.type) %>} <%= paramWithDefault('params.' + paramName, param.default) %><% if (param.description) { %> - <%= param.description %><% } %><%
})
%>
*/
function do<%= _.studlyCase(name) %>(params, callback) {
params = params || {};
var request = {<%
if (~name.indexOf('exists')) {%>
ignore: _.union([404], params.ignore)<%
} else {%>
ignore: params.ignore<%
}
if (body) { if (_.contains(['bulk', 'msearch'], name)) {%>,
body: paramHelper.bulkBody(params.body, this.client.serializer) || null<%
} else { %>,
body: params.body || null<%
} }%>
}
, url = {}
, query = {}
, responseOpts = {};
<%
if (methods.length > 1) { %>
if (params.method = _.toLowerString(params.method)) {
if (<%= _.map(methods, function (method) { return 'params.method === ' + stringify(method) }).join(' || ') %>) {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of <%= methods.join(', ') %>');
}
} else {<%
if (_.contains(methods, 'get')) {
var nonGet = _.find(methods, function (m) {return m !== 'get'; });%>
request.method = params.body ? <%= stringify(nonGet) %> : 'get';<%
} else {%>
request.method = <%= stringify(methods[0]) %>;<%
}%>
}<%
} else {%>
request.method = <%= stringify(methods[0]) %>;<%
}
%>
// find the url's params
<%= writeParams(2, urlParts, 'url.') %>
// build the url
<%= writeUrls(2, urls, urlParts, params) %>
// build the query string
<%= writeParams(2, params, 'query.') %>
request.url = request.url + _.makeQueryString(query);
var reqPromise = this.client.request(request);
if (callback) {
reqPromise.then(_.bind(callback, null, null), callback);
}
return reqPromise;
}
module.exports = do<%= _.studlyCase(name) %>;