Files
elasticsearch-js/scripts/generate/js_api/templates/action.tmpl

75 lines
2.0 KiB
Cheetah

var _ = require('<%= path2lib %>utils');
var errors = require('<%= path2lib %>errors');
var q = require('q');<%
if (_.keys(enumOptions).length) {
%>
<% _.each(enumOptions, function(options, name) {
%>
var <%= name %>Options = <%= stringify(options) %>;<%
});
}
%>
/**
* 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, cb) {
if (typeof params === 'function') {
cb = params;
params = {};
} else {
params = params || {};
cb = typeof cb === 'function' ? cb : _.noop;
}
var request = {
<%= writeRequestObjectBody(4, name, body, methods) %>
};
var parts = {};
var query = {};
var responseOpts = {};
<%
if (methods.length > 1) { %>
// figure out the method
if (params.method = _.toUpperString(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]) %>;<%
}%>
}<%
}
%>
// find the paths's params
<%= writeParams(2, urlParts, 'parts.') %>
// build the path
<%= writeUrls(2, urls, urlParts, params) %>
// build the query string
<%= writeParams(2, params, 'query.') %>
request.path = request.path + _.makeQueryString(query);
<%= returnStatement(2, name) %>
}
module.exports = do<%= _.studlyCase(name) %>;