Unknown parameters handling (#761)
This commit is contained in:
committed by
GitHub
parent
7acd2e3b07
commit
23cfe11e44
@ -1,5 +1,8 @@
|
||||
'use strict'
|
||||
|
||||
/* eslint camelcase: 0 */
|
||||
/* eslint no-unused-vars: 0 */
|
||||
|
||||
function buildSearchTemplate (opts) {
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const { makeRequest, ConfigurationError, result } = opts
|
||||
@ -22,6 +25,39 @@ function buildSearchTemplate (opts) {
|
||||
* @param {boolean} rest_total_hits_as_int - Indicates whether hits.total should be rendered as an integer or an object in the rest search response
|
||||
* @param {object} body - The search definition template and its params
|
||||
*/
|
||||
|
||||
const acceptedQuerystring = [
|
||||
'ignore_unavailable',
|
||||
'ignore_throttled',
|
||||
'allow_no_indices',
|
||||
'expand_wildcards',
|
||||
'preference',
|
||||
'routing',
|
||||
'scroll',
|
||||
'search_type',
|
||||
'explain',
|
||||
'profile',
|
||||
'typed_keys',
|
||||
'rest_total_hits_as_int',
|
||||
'pretty',
|
||||
'human',
|
||||
'error_trace',
|
||||
'source',
|
||||
'filter_path'
|
||||
]
|
||||
|
||||
const snakeCase = {
|
||||
ignoreUnavailable: 'ignore_unavailable',
|
||||
ignoreThrottled: 'ignore_throttled',
|
||||
allowNoIndices: 'allow_no_indices',
|
||||
expandWildcards: 'expand_wildcards',
|
||||
searchType: 'search_type',
|
||||
typedKeys: 'typed_keys',
|
||||
restTotalHitsAsInt: 'rest_total_hits_as_int',
|
||||
errorTrace: 'error_trace',
|
||||
filterPath: 'filter_path'
|
||||
}
|
||||
|
||||
return function searchTemplate (params, options, callback) {
|
||||
options = options || {}
|
||||
if (typeof options === 'function') {
|
||||
@ -58,74 +94,22 @@ function buildSearchTemplate (opts) {
|
||||
)
|
||||
}
|
||||
|
||||
// build querystring object
|
||||
const querystring = {}
|
||||
const keys = Object.keys(params)
|
||||
const acceptedQuerystring = [
|
||||
'ignore_unavailable',
|
||||
'ignore_throttled',
|
||||
'allow_no_indices',
|
||||
'expand_wildcards',
|
||||
'preference',
|
||||
'routing',
|
||||
'scroll',
|
||||
'search_type',
|
||||
'explain',
|
||||
'profile',
|
||||
'typed_keys',
|
||||
'rest_total_hits_as_int',
|
||||
'pretty',
|
||||
'human',
|
||||
'error_trace',
|
||||
'source',
|
||||
'filter_path'
|
||||
]
|
||||
const acceptedQuerystringCamelCased = [
|
||||
'ignoreUnavailable',
|
||||
'ignoreThrottled',
|
||||
'allowNoIndices',
|
||||
'expandWildcards',
|
||||
'preference',
|
||||
'routing',
|
||||
'scroll',
|
||||
'searchType',
|
||||
'explain',
|
||||
'profile',
|
||||
'typedKeys',
|
||||
'restTotalHitsAsInt',
|
||||
'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 = params.body == null ? 'GET' : 'POST'
|
||||
}
|
||||
|
||||
// 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, type } = params
|
||||
var querystring = semicopy(params, ['method', 'body', 'index', 'type'])
|
||||
|
||||
if (method == null) {
|
||||
method = body == null ? 'GET' : 'POST'
|
||||
}
|
||||
|
||||
var ignore = options.ignore || null
|
||||
if (typeof ignore === 'number') {
|
||||
ignore = [ignore]
|
||||
@ -133,10 +117,10 @@ function buildSearchTemplate (opts) {
|
||||
|
||||
var path = ''
|
||||
|
||||
if ((params['index']) != null && (params['type']) != null) {
|
||||
path = '/' + encodeURIComponent(params['index']) + '/' + encodeURIComponent(params['type']) + '/' + '_search' + '/' + 'template'
|
||||
} else if ((params['index']) != null) {
|
||||
path = '/' + encodeURIComponent(params['index']) + '/' + '_search' + '/' + 'template'
|
||||
if ((index) != null && (type) != null) {
|
||||
path = '/' + encodeURIComponent(index) + '/' + encodeURIComponent(type) + '/' + '_search' + '/' + 'template'
|
||||
} else if ((index) != null) {
|
||||
path = '/' + encodeURIComponent(index) + '/' + '_search' + '/' + 'template'
|
||||
} else {
|
||||
path = '/' + '_search' + '/' + 'template'
|
||||
}
|
||||
@ -145,7 +129,7 @@ function buildSearchTemplate (opts) {
|
||||
const request = {
|
||||
method,
|
||||
path,
|
||||
body: params.body || '',
|
||||
body: body || '',
|
||||
querystring
|
||||
}
|
||||
|
||||
@ -154,10 +138,27 @@ function buildSearchTemplate (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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user