Slight refactor to the api module, so it will simply extend the client like it did

This commit is contained in:
Spencer Alger
2013-10-23 14:49:00 -07:00
parent 35209ea61a
commit c14d37aa42
39 changed files with 25589 additions and 3627 deletions

View File

@ -0,0 +1,63 @@
module.exports = {
'cluster.node_hot_threads': [
'/_cluster/nodes/hotthreads',
'/_cluster/nodes/hot_threads',
'/_nodes/hot_threads',
'/_cluster/nodes/{node_id}/hotthreads',
'/_cluster/nodes/{node_id}/hot_threads',
'/_nodes/{node_id}/hot_threads'
],
'cluster.node_info': [
'/_cluster/nodes',
'/_nodes/settings',
'/_nodes/os',
'/_nodes/process',
'/_nodes/jvm',
'/_nodes/thread_pool',
'/_nodes/network',
'/_nodes/transport',
'/_nodes/http',
'/_nodes/plugin',
'/_cluster/nodes/{node_id}',
'/_nodes/{node_id}/settings',
'/_nodes/{node_id}/os',
'/_nodes/{node_id}/process',
'/_nodes/{node_id}/jvm',
'/_nodes/{node_id}/thread_pool',
'/_nodes/{node_id}/network',
'/_nodes/{node_id}/transport',
'/_nodes/{node_id}/http',
'/_nodes/{node_id}/plugin'
],
'cluster.node_shutdown': [
'/_cluster/nodes/_shutdown'
],
'cluster.node_stats': [
'/_cluster/nodes/stats',
'/_nodes/stats/{metric_family}',
'/_nodes/stats/indices/{metric}/{fields}',
'/_cluster/nodes/{node_id}/stats',
'/_nodes/{node_id}/stats/{metric_family}',
'/_nodes/{node_id}/stats/indices/{metric}/{fields}'
],
'get': [
'/{index}/{type}/{id}/_source'
],
'indices.delete_mapping': [
'/{index}/{type}/_mapping'
],
'indices.stats': [
'_stats/{metric_family}',
'/_stats/indexing',
'/_stats/indexing/{indexing_types}',
'/_stats/search/{search_groups}',
'/_stats/fielddata/{fields}',
'/{index}/_stats/{metric_family}',
'/{index}/_stats/indexing',
'/{index}/_stats/search/{search_groups}',
'/{index}/_stats/fielddata/{fields}'
],
'search': [
'/_search'
]
};

View File

@ -0,0 +1,128 @@
var _ = require('../../../src/lib/utils');
var asset = require('assert');
var path = require('path');
var fs = require('fs');
var mkdirp = require('mkdirp');
var outputPath = _.joinPath(__dirname, '../../../src/lib/api.js');
var templates = require('./templates');
var specs = require('./spec');
// completely delete the output directory
var clean = (function () {
function rmDirRecursive(path) {
fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + '/' + file;
if (fs.statSync(curPath).isDirectory()) { // recurse
rmDirRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
return function (path) {
try {
var stats = fs.statSync(path);
if (stats && stats.isDirectory()) {
console.log('removing', path, 'directory recursively');
rmDirRecursive(path);
} else {
console.log('removing', path);
fs.unlinkSync(path);
}
return true;
} catch (e) {
return false;
}
};
})();
exports.run = function () {
var defs = [];
var namespaces = [];
clean(outputPath);
var actions = _.map(specs, function (spec) {
spec.urls = _.map(
_.sortBy(
_.transform(spec.urls, function (note, url, i) {
var optionalVars = {};
var requiredVars = {};
var param;
var target;
var urlParamRE = /\{(\w+)\}/g;
if (url.charAt(0) !== '/') {
url = '/' + url;
}
while (match = urlParamRE.exec(url)) {
param = spec.urlParts[match[1]] || {};
target = (param.required || !param.default) ? requiredVars : optionalVars;
target[match[1]] = _.omit(param, 'required');
}
[requiredVars, optionalVars].forEach(function (vars) {
_.each(vars, function (v, name) {
vars[name] = _.omit(v, 'description');
})
})
note.push(_.omit({
fmt: url.replace(urlParamRE, '<%=$1%>'),
opt: _.size(optionalVars) ? optionalVars : null,
req: _.size(requiredVars) ? requiredVars : null,
sortOrder: _.size(requiredVars) * -1
}, function (v) { return !v; }));
}, [])
, 'sortOrder')
, function (url) {
return _.omit(url, 'sortOrder');
});
var docUrl = spec.docUrl;
var location = _.map(spec.name.split('.'), _.camelCase).join('.');
spec = _.pick(spec, [
'methods',
'params',
'urls',
'needBody',
'bulkBody',
'castNotFound'
]);
spec.params = _.transform(spec.params, function (note, param, name) {
param.name = name;
note[name] = _.pick(param, [
'type', 'default', 'options', 'required'
]);
}, {});
if (~location.indexOf('.')) {
var steps = location.split('.');
namespaces.push(steps.slice(0, -1).join('.'));
location = steps.join('.prototype.');
}
// escape method names with "special" keywords
location = location.replace(/(^|\.)(delete|default)(\.|$)/g, '[\'$2\']');
return {
spec: spec,
location: location,
docUrl: docUrl
};
});
console.log('writing', actions.length, 'api actions to', outputPath);
fs.writeFileSync(outputPath, templates.apiFile({
actions: actions,
namespaces: _.unique(namespaces.sort(), true)
}));
};
exports.run();

View File

@ -0,0 +1,48 @@
var _ = require('../../../src/lib/utils')
var docs = _.requireDir(module, '../../../es_api_spec/api');
var aliases = require('./aliases');
var castNotFoundRE = /exists/;
var usesBulkBodyRE = /^(bulk|msearch)$/;
var defs = [];
// itterate all of the found docs
Object.keys(docs).forEach(function (filename) {
Object.keys(docs[filename]).forEach(function (name) {
var def = docs[filename][name];
def.name = name;
defs.push(def);
})
});
module.exports = _.map(defs, function (def) {
var name = def.name;
var steps = name.split('.');
var spec = {
name: name,
methods: _.map(def.methods, function (m) { return m.toUpperCase(); }),
docUrl: def.documentation,
urlParts: def.url.parts,
params: def.url.params,
urls: _.difference(def.url.paths, aliases[name]),
body: def.body || null,
path2lib: _.repeat('../', steps.length + 1) + 'lib/'
};
if (def.body && def.body.requires) {
spec.needBody = true;
}
if (usesBulkBodyRE.test(name)) {
spec.bulkBody = true;
}
if (castNotFoundRE.test(name)) {
spec.castNotFound = true;
}
return spec;
});

View File

@ -0,0 +1,74 @@
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) %>;

View File

@ -0,0 +1,27 @@
var ca = require('./client_action').create;
var errors = require('./errors');
var api = module.exports = {};
api._namespaces = <%= stringify(namespaces) %>;<%
_.each(actions, function (action) {
var namespace = action.location.split('.').shift();
if (_.contains(namespaces, namespace)) {
_.pull(namespaces, namespace);
var className = _.studlyCase(namespace) + 'NS';
%>
api.<%= namespace %> = function <%= className %>(client) {
if (this instanceof <%= className %>) {
this.client = client;
} else {
return new <%= className %>(client);
}
};<%
}
%>
<%= partials.client_action(action) %><%
}); %>

View File

@ -0,0 +1,12 @@
/**
* Perform a [<%= spec.name %>](<%= docUrl %>) request
*
* @param {Object} params - An object with parameters used to carry out this action<%
_.each(spec.params, function(param, paramName) { %>
* @param {<%= paramType(param.type) %>} <%= paramWithDefault('params.' + paramName, param.default) %><%
if (param.description) {
%> - <%= param.description %><%
}
%><% }) %>
*/
api<%= (location[0] === '[' ? '' : '.') + location %> = ca(<%= stringify(spec, true) %>);

View File

@ -0,0 +1,295 @@
var _ = require('../../../../src/lib/utils')
, fs = require('fs')
, path = require('path')
, urlParamRE = /\{(\w+)\}/g;
/**
* Simple manager to take care of indentation
* @param {number} i - Width of the indentation
* @return {function} - Call this to add a new line to the output
*/
function lines(i) {
function l(line) {
if (line === '') {
// no indent on empty lines
l.lines.push('');
} else if (typeof line !== 'undefined') {
l.lines.push(_.repeat(' ', l.indent) + line);
}
return l;
}
l.lines = [];
l.indent = i || 0;
l.split = function (toSplit) {
_.each(toSplit.split(/\r?\n/), l);
return l;
};
l.in = function (line) {
l.indent += 2;
return l(line);
};
l.out = function (line) {
l.indent -= 2;
return l(line);
};
l.toString = function () {
return l.lines.join('\n');
};
return l;
}
/**
* we want strings in code to use single-quotes, so this will JSON encode vars, but then
* modify them to follow our code standards.
*
* @param {*} thing - Any thing
* @return {String} - our pretty string
*/
function stringify(thing, pretty) {
return (pretty ? JSON.stringify(thing, null, ' ') : JSON.stringify(thing))
.replace(/\'/g, '\\\'')
.replace(/\\?"/g, function (quote) {
// replace external (unescaped) double quotes with single quotes
return quote === '\\"' ? '"' : '\'';
})
// inject a space between STRING array parts
.replace(/([^\\])','/g, '$1\', \'')
// remove quotes around key names that are only made up of letters
.replace(/^( +)'([a-zA-Z_]+)':/gm, '$1$2:')
// requote "special" key names
.replace(/^( +)(default):/gm, '$1\'$2\':')
}
/**
* We'll collect the templates here
* @type {Object}
*/
var templates = {};
/**
* These keys will be available as local variables to each template
* @type {Object}
*/
var templateGlobals = {
writeParams: function (indent, params, namespace) {
var l = lines(indent);
_.each(params, function (param, name) {
if (!param.required) {
l('if (typeof params.' + name + ' !== \'undefined\') {').in();
}
l.split(templates[param.type || 'any']({
get: 'params.' + name,
set: namespace + name,
name: name
}));
if (!param.required) {
l.out();
l('}');
}
l('');
});
return l.toString();
},
writeBrowserParams: function (indent, params, namespace) {
var l = lines(indent);
_.each(params, function (param, name) {
if (!param.required) {
l('if (_.has(params, ' + stringify(name) + ')) {').in();
}
switch (param.type) {
case 'enum':
l(
namespace + name + ' = _.' +
(param.type || 'any') + 'Param(params.' + name + ', ' + stringify(param.options) +
');'
);
break;
default:
l(namespace + name + ' = _.' + (param.type || 'any') + 'Param(params.' + name + ');');
break;
}
if (!param.required) {
l.out('}');
}
l('');
});
return l.toString();
},
writeUrls: function (indent, urls, urlParams, queryStringParams) {
var l = lines(indent);
function urlVarIsRequired(varDetails) {
varDetails = typeof varDetails === 'string' ? urlParams[varDetails] : varDetails;
return varDetails && (varDetails.required || !varDetails.default);
}
// turn a url string into an object describing the url, then sort them in decending order by how many args they have
urls = _.sortBy(urls, function (url) {
var requiredVars = _.filter(_.collectMatches(url, urlParamRE), function (match) {
return urlVarIsRequired(urlParams[match[1]]);
});
return requiredVars ? requiredVars.length * -1 : 0;
});
_.each(urls, function (url, urlIndex) {
// collect the vars from the url and replace them to form the js that will build the url
var makeL = lines(), vars = [];
makeL('request.path = \'' + url.replace(urlParamRE, function (match, varName) {
var varDetails = urlParams[varName];
varDetails.name = varName;
vars.push(varDetails);
if (urlVarIsRequired(varDetails)) {
return '\' + encodeURIComponent(parts.' + varName + ') + \'';
} else {
return '\' + encodeURIComponent(parts.' + varName + ' || ' + stringify(varDetails.default) + ') + \'';
}
}) + '\';');
makeL(_.filter(_.map(vars, function (v, i) {
if (_.has(queryStringParams, v.name)) {
// delete the param so that it's not used later on in the queryString
return 'delete params.' + v.name + ';';
}
})).join(' '));
if (vars.length || urlIndex) {
var requiredVars = _.filter(vars, urlVarIsRequired);
var condition = _.map(requiredVars, function (v) {
return 'parts.' + v.name + ')';
}).join(' && ');
l((urlIndex > 0 ? 'else ' : '') + (condition ? 'if (' + condition + ') ' : '') + '{')
.in()
.split(makeL.toString())
.out('}');
if (urlIndex === urls.length - 1 && condition) {
l('else {')
.in('throw new TypeError(\'Unable to build a path with those params. Supply at least ' +
vars.join(', ') + '\');'
)
.out('}');
}
} else {
l.split(makeL.toString());
}
});
l('');
return l.toString();
},
writeRequestObjectBody: function (indent, name, body, methods) {
var parts = [], l = lines(indent);
if (~name.indexOf('exists')) {
parts.push('ignore: _.union([404], params.ignore)');
} else {
parts.push('ignore: params.ignore');
}
if (body) {
if (_.contains(['bulk', 'msearch'], name)) {
parts.push('body: this.client.config.serializer.bulkBody(params.body || null)');
} else {
parts.push('body: params.body || null');
}
}
if (methods.length === 1) {
parts.push('method: ' + stringify(methods[0]));
}
_.each(parts, function (part, i) {
l(part + (i < parts.length - 1 ? ',' : ''));
});
return l.toString();
},
stringify: stringify,
_: _,
paramType: function (type) {
switch (type && type.toLowerCase ? type.toLowerCase() : 'any') {
case 'time':
return 'Date|Number';
case 'any':
return '*';
case 'enum':
return 'String';
case 'list':
return 'String|ArrayOfStrings|Boolean';
default:
return _.ucfirst(type);
}
},
paramWithDefault: function (name, def) {
if (def) {
return '[' + name + '=' + def + ']';
} else {
return name;
}
},
returnStatement: function (indent, name) {
var l = lines(indent);
if (name.match(/(^|\.)exists/)) {
l('this.client.request(request, function (err, response) {')
.in('if (err instanceof errors.NotFound) {')
.in('cb(err, false);')
.out('} else {')
.in('cb(err, true);')
.out('}')
.out('});');
} else {
l('this.client.request(request, cb);');
}
return l.toString();
},
partials: templates
};
fs.readdirSync(path.resolve(__dirname)).forEach(function (filename) {
var name = filename.replace(/\..+$/, '');
if (name !== 'index') {
templates[name] = _.template(
fs.readFileSync(path.resolve(__dirname, filename), {
encoding: 'utf8'
}),
null,
{
imports: templateGlobals
}
);
}
});
templates.text = templates.string;
module.exports = {
apiFile: templates.api_file,
urlParamRE: urlParamRE
};