Unknown parameters handling (#761)
This commit is contained in:
committed by
GitHub
parent
7acd2e3b07
commit
23cfe11e44
@ -95,42 +95,22 @@ function generate (spec, common) {
|
||||
|
||||
${genUrlValidation(paths, api)}
|
||||
|
||||
// build querystring object
|
||||
const querystring = {}
|
||||
const keys = Object.keys(params)
|
||||
const acceptedQuerystring = [
|
||||
${genAcceptedQuerystring()}
|
||||
]
|
||||
const acceptedQuerystringCamelCased = [
|
||||
${genAcceptedQuerystringCamelCased()}
|
||||
]
|
||||
|
||||
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) {
|
||||
${generatePickMethod(methods)}
|
||||
}
|
||||
|
||||
// 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 { ${genQueryBlacklist(false)} } = params
|
||||
var querystring = semicopy(params, [${genQueryBlacklist()}])
|
||||
|
||||
if (method == null) {
|
||||
${generatePickMethod(methods)}
|
||||
}
|
||||
|
||||
var ignore = options.ignore || null
|
||||
if (typeof ignore === 'number') {
|
||||
ignore = [ignore]
|
||||
@ -153,20 +133,49 @@ function generate (spec, common) {
|
||||
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
|
||||
}
|
||||
}
|
||||
`.trim() // always call trim to avoid newlines
|
||||
|
||||
const fn = dedent`
|
||||
'use strict'
|
||||
|
||||
/* eslint camelcase: 0 */
|
||||
/* eslint no-unused-vars: 0 */
|
||||
|
||||
function build${name[0].toUpperCase() + name.slice(1)} (opts) {
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const { makeRequest, ConfigurationError, result } = opts
|
||||
${generateDocumentation(spec[api], api)}
|
||||
|
||||
const acceptedQuerystring = [
|
||||
${acceptedQuerystring.map(q => `'${q}'`).join(',\n')}
|
||||
]
|
||||
|
||||
const snakeCase = {
|
||||
${genSnakeCaseMap()}
|
||||
}
|
||||
|
||||
return ${code}
|
||||
}
|
||||
|
||||
@ -229,23 +238,40 @@ function generate (spec, common) {
|
||||
}
|
||||
}
|
||||
|
||||
function genAcceptedQuerystring () {
|
||||
return acceptedQuerystring
|
||||
.map(q => `'${q}'`)
|
||||
.join(',\n ')
|
||||
function genSnakeCaseMap () {
|
||||
const toCamelCase = str => {
|
||||
return str[0] === '_'
|
||||
? '_' + str.slice(1).replace(/_([a-z])/g, k => k[1].toUpperCase())
|
||||
: str.replace(/_([a-z])/g, k => k[1].toUpperCase())
|
||||
}
|
||||
|
||||
return acceptedQuerystring.reduce((acc, val, index) => {
|
||||
if (toCamelCase(val) !== val) {
|
||||
acc += `${toCamelCase(val)}: '${val}'`
|
||||
if (index !== acceptedQuerystring.length - 1) {
|
||||
acc += ',\n'
|
||||
}
|
||||
}
|
||||
return acc
|
||||
}, '')
|
||||
}
|
||||
|
||||
function genAcceptedQuerystringCamelCased () {
|
||||
return acceptedQuerystring
|
||||
.map(q => {
|
||||
// if the key starts with `_` we should not camelify the first occurence
|
||||
// eg: _source_include => _sourceInclude
|
||||
return q[0] === '_'
|
||||
? '_' + q.slice(1).replace(/_([a-z])/g, k => k[1].toUpperCase())
|
||||
: q.replace(/_([a-z])/g, k => k[1].toUpperCase())
|
||||
function genQueryBlacklist (addQuotes = true) {
|
||||
const toCamelCase = str => {
|
||||
return str[0] === '_'
|
||||
? '_' + str.slice(1).replace(/_([a-z])/g, k => k[1].toUpperCase())
|
||||
: str.replace(/_([a-z])/g, k => k[1].toUpperCase())
|
||||
}
|
||||
|
||||
const blacklist = ['method', 'body']
|
||||
if (typeof parts === 'object' && parts !== null) {
|
||||
Object.keys(parts).forEach(p => {
|
||||
const camelStr = toCamelCase(p)
|
||||
if (camelStr !== p) blacklist.push(`${camelStr}`)
|
||||
blacklist.push(`${p}`)
|
||||
})
|
||||
.map(q => `'${q}'`)
|
||||
.join(',\n ')
|
||||
}
|
||||
return addQuotes ? blacklist.map(q => `'${q}'`) : blacklist
|
||||
}
|
||||
|
||||
function buildPath () {
|
||||
@ -258,8 +284,8 @@ function generate (spec, common) {
|
||||
const genAccessKey = str => {
|
||||
const camelStr = toCamelCase(str)
|
||||
return camelStr === str
|
||||
? `params['${str}']`
|
||||
: `params['${str}'] || params['${camelStr}']`
|
||||
? str
|
||||
: `${str} || ${camelStr}`
|
||||
}
|
||||
|
||||
const genCheck = path => {
|
||||
@ -336,7 +362,7 @@ function generatePickMethod (methods) {
|
||||
const bodyMethod = getBodyMethod(methods)
|
||||
const noBodyMethod = getNoBodyMethod(methods)
|
||||
if (bodyMethod && noBodyMethod) {
|
||||
return `method = params.body == null ? '${noBodyMethod}' : '${bodyMethod}'`
|
||||
return `method = body == null ? '${noBodyMethod}' : '${bodyMethod}'`
|
||||
} else if (bodyMethod) {
|
||||
return `
|
||||
method = '${bodyMethod}'
|
||||
@ -351,12 +377,12 @@ function generatePickMethod (methods) {
|
||||
function genBody (api, methods, body) {
|
||||
const bodyMethod = getBodyMethod(methods)
|
||||
if (ndjsonApi.indexOf(api) > -1) {
|
||||
return 'bulkBody: params.body,'
|
||||
return 'bulkBody: body,'
|
||||
}
|
||||
if (body === null && bodyMethod) {
|
||||
return `body: '',`
|
||||
} else if (bodyMethod) {
|
||||
return `body: params.body || '',`
|
||||
return `body: body || '',`
|
||||
} else {
|
||||
return 'body: null,'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user