Updated code generation (#1441)

* Updated code generation

* Nit
This commit is contained in:
Tomas Della Vedova
2021-04-07 15:24:45 +02:00
committed by GitHub
parent 4102a28a0b
commit d7d55e1146
3 changed files with 19 additions and 40 deletions

View File

@ -57,17 +57,6 @@ const noPathValidation = [
'update'
]
// apis that uses bulkBody property
const ndjsonApi = [
'bulk',
'msearch',
'msearch_template',
'ml.find_file_structure',
'monitoring.bulk',
'xpack.ml.find_file_structure',
'xpack.monitoring.bulk'
]
function generateNamespace (namespace, nested, specFolder, version) {
const common = require(join(specFolder, '_common.json'))
let code = dedent`
@ -249,7 +238,7 @@ function generateSingleApi (version, spec, common) {
const request = {
method,
path,
${genBody(api, methods, spec[api].body)}
${genBody(api, methods, spec[api].body, spec)}
querystring
}
@ -376,7 +365,7 @@ function generateSingleApi (version, spec, common) {
}
let hasStaticPath = false
const sortedPaths = paths
let sortedPaths = paths
// some legacy API have mutliple statis paths
// this filter removes them
.filter(p => {
@ -390,6 +379,9 @@ function generateSingleApi (version, spec, common) {
// sort by number of parameters (desc)
.sort((a, b) => Object.keys(b.parts || {}).length - Object.keys(a.parts || {}).length)
const allDeprecated = paths.filter(path => path.deprecated != null)
if (allDeprecated.length === paths.length) sortedPaths = [paths[0]]
let code = ''
for (let i = 0; i < sortedPaths.length; i++) {
const { path, methods } = sortedPaths[i]
@ -437,9 +429,10 @@ function generatePickMethod (methods) {
}
}
function genBody (api, methods, body) {
function genBody (api, methods, body, spec) {
const bodyMethod = getBodyMethod(methods)
if (ndjsonApi.indexOf(api) > -1) {
const { content_type } = spec[api].headers
if (content_type && content_type.includes('application/x-ndjson')) {
return 'bulkBody: body,'
}
if (body === null && bodyMethod) {
@ -558,4 +551,3 @@ function Uppercase (str) {
}
module.exports = generateNamespace
module.exports.ndjsonApi = ndjsonApi

View File

@ -18,6 +18,7 @@
*/
/* eslint-disable no-template-curly-in-string */
/* eslint camelcase: 0 */
'use strict'
@ -25,15 +26,6 @@ const { readdirSync } = require('fs')
const { join } = require('path')
const dedent = require('dedent')
const deepmerge = require('deepmerge')
const { ndjsonApi } = require('./generateApis')
const ndjsonApiKey = ndjsonApi
.map(api => {
return api
.replace(/\.([a-z])/g, k => k[1].toUpperCase())
.replace(/_([a-z])/g, k => k[1].toUpperCase())
})
.map(toPascalCase)
function genFactory (folder, specFolder, namespaces) {
// get all the API files
@ -57,7 +49,7 @@ function genFactory (folder, specFolder, namespaces) {
const spec = readSpec(specFolder, file.slice(0, -5))
const isHead = isHeadMethod(spec, file.slice(0, -5))
const body = hasBody(spec, file.slice(0, -5))
const methods = acc === null ? buildMethodDefinition({ kibana: false }, val, name, body, isHead) : null
const methods = acc === null ? buildMethodDefinition({ kibana: false }, val, name, body, isHead, spec) : null
const obj = {}
if (methods) {
for (const m of methods) {
@ -89,7 +81,7 @@ function genFactory (folder, specFolder, namespaces) {
const spec = readSpec(specFolder, file.slice(0, -5))
const isHead = isHeadMethod(spec, file.slice(0, -5))
const body = hasBody(spec, file.slice(0, -5))
const methods = acc === null ? buildMethodDefinition({ kibana: true }, val, name, body, isHead) : null
const methods = acc === null ? buildMethodDefinition({ kibana: true }, val, name, body, isHead, spec) : null
const obj = {}
if (methods) {
for (const m of methods) {
@ -225,11 +217,12 @@ function toPascalCase (str) {
return str[0].toUpperCase() + str.slice(1)
}
function buildMethodDefinition (opts, api, name, hasBody, isHead) {
function buildMethodDefinition (opts, api, name, hasBody, isHead, spec) {
const Name = toPascalCase(name)
const bodyType = ndjsonApiKey.includes(Name) ? 'RequestNDBody' : 'RequestBody'
const { content_type } = spec[Object.keys(spec)[0]].headers
const bodyType = content_type && content_type.includes('application/x-ndjson') ? 'RequestNDBody' : 'RequestBody'
const responseType = isHead ? 'boolean' : 'Record<string, any>'
const defaultBodyType = ndjsonApiKey.includes(Name) ? 'Record<string, any>[]' : 'Record<string, any>'
const defaultBodyType = content_type && content_type.includes('application/x-ndjson') ? 'Record<string, any>[]' : 'Record<string, any>'
if (opts.kibana) {
if (hasBody) {

View File

@ -17,18 +17,11 @@
* under the License.
*/
/* eslint camelcase: 0 */
'use strict'
const deprecatedParameters = require('./patch.json')
const { ndjsonApi } = require('./generateApis')
const ndjsonApiKey = ndjsonApi
.map(api => {
return api
.replace(/\.([a-z])/g, k => k[1].toUpperCase())
.replace(/_([a-z])/g, k => k[1].toUpperCase())
})
.map(toPascalCase)
function generate (version, api) {
const release = version.charAt(0)
@ -122,7 +115,8 @@ export interface Generic {
return `${e.key}${optional}: ${getType(e.value.type, e.value.options)};`
}
const bodyGeneric = ndjsonApiKey.includes(toPascalCase(name)) ? 'RequestNDBody' : 'RequestBody'
const { content_type } = spec[api].headers
const bodyGeneric = content_type && content_type.includes('application/x-ndjson') ? 'RequestNDBody' : 'RequestBody'
const code = `
export interface ${toPascalCase(name)}${body ? `<T = ${bodyGeneric}>` : ''} extends Generic {