Skip to content

Updated code generation #1441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions scripts/utils/generateApis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 => {
Expand All @@ -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]
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -558,4 +551,3 @@ function Uppercase (str) {
}

module.exports = generateNamespace
module.exports.ndjsonApi = ndjsonApi
21 changes: 7 additions & 14 deletions scripts/utils/generateMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,14 @@
*/

/* eslint-disable no-template-curly-in-string */
/* eslint camelcase: 0 */

'use strict'

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
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 4 additions & 10 deletions scripts/utils/generateRequestTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down