Unknown parameters handling (#761)

This commit is contained in:
Tomas Della Vedova
2019-01-29 17:31:43 +01:00
committed by GitHub
parent 7acd2e3b07
commit 23cfe11e44
253 changed files with 12369 additions and 10836 deletions

View File

@ -1,5 +1,8 @@
'use strict'
/* eslint camelcase: 0 */
/* eslint no-unused-vars: 0 */
function buildClusterHealth (opts) {
// eslint-disable-next-line no-unused-vars
const { makeRequest, ConfigurationError, result } = opts
@ -18,6 +21,37 @@ function buildClusterHealth (opts) {
* @param {boolean} wait_for_no_initializing_shards - Whether to wait until there are no initializing shards in the cluster
* @param {enum} wait_for_status - Wait until cluster is in a specific state
*/
const acceptedQuerystring = [
'level',
'local',
'master_timeout',
'timeout',
'wait_for_active_shards',
'wait_for_nodes',
'wait_for_events',
'wait_for_no_relocating_shards',
'wait_for_no_initializing_shards',
'wait_for_status',
'pretty',
'human',
'error_trace',
'source',
'filter_path'
]
const snakeCase = {
masterTimeout: 'master_timeout',
waitForActiveShards: 'wait_for_active_shards',
waitForNodes: 'wait_for_nodes',
waitForEvents: 'wait_for_events',
waitForNoRelocatingShards: 'wait_for_no_relocating_shards',
waitForNoInitializingShards: 'wait_for_no_initializing_shards',
waitForStatus: 'wait_for_status',
errorTrace: 'error_trace',
filterPath: 'filter_path'
}
return function clusterHealth (params, options, callback) {
options = options || {}
if (typeof options === 'function') {
@ -46,70 +80,22 @@ function buildClusterHealth (opts) {
)
}
// build querystring object
const querystring = {}
const keys = Object.keys(params)
const acceptedQuerystring = [
'level',
'local',
'master_timeout',
'timeout',
'wait_for_active_shards',
'wait_for_nodes',
'wait_for_events',
'wait_for_no_relocating_shards',
'wait_for_no_initializing_shards',
'wait_for_status',
'pretty',
'human',
'error_trace',
'source',
'filter_path'
]
const acceptedQuerystringCamelCased = [
'level',
'local',
'masterTimeout',
'timeout',
'waitForActiveShards',
'waitForNodes',
'waitForEvents',
'waitForNoRelocatingShards',
'waitForNoInitializingShards',
'waitForStatus',
'pretty',
'human',
'errorTrace',
'source',
'filterPath'
]
for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i]
if (acceptedQuerystring.indexOf(key) !== -1) {
querystring[key] = params[key]
} else {
var camelIndex = acceptedQuerystringCamelCased.indexOf(key)
if (camelIndex !== -1) {
querystring[acceptedQuerystring[camelIndex]] = params[key]
}
}
}
// configure http method
var method = params.method
if (method == null) {
method = 'GET'
}
// validate headers object
if (params.headers != null && typeof params.headers !== 'object') {
if (options.headers != null && typeof options.headers !== 'object') {
return callback(
new ConfigurationError(`Headers should be an object, instead got: ${typeof params.headers}`),
new ConfigurationError(`Headers should be an object, instead got: ${typeof options.headers}`),
result
)
}
var warnings = null
var { method, body, index } = params
var querystring = semicopy(params, ['method', 'body', 'index'])
if (method == null) {
method = 'GET'
}
var ignore = options.ignore || null
if (typeof ignore === 'number') {
ignore = [ignore]
@ -117,8 +103,8 @@ function buildClusterHealth (opts) {
var path = ''
if ((params['index']) != null) {
path = '/' + '_cluster' + '/' + 'health' + '/' + encodeURIComponent(params['index'])
if ((index) != null) {
path = '/' + '_cluster' + '/' + 'health' + '/' + encodeURIComponent(index)
} else {
path = '/' + '_cluster' + '/' + 'health'
}
@ -136,10 +122,27 @@ function buildClusterHealth (opts) {
requestTimeout: options.requestTimeout || null,
maxRetries: options.maxRetries || null,
asStream: options.asStream || false,
headers: options.headers || null
headers: options.headers || null,
warnings
}
return makeRequest(request, requestOptions, callback)
function semicopy (obj, exclude) {
var target = {}
var keys = Object.keys(obj)
for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i]
if (exclude.indexOf(key) === -1) {
target[snakeCase[key] || key] = obj[key]
if (acceptedQuerystring.indexOf(snakeCase[key] || key) === -1) {
warnings = warnings || []
warnings.push('Client - Unknown parameter: "' + key + '", sending it as query parameter')
}
}
}
return target
}
}
}