Skip to content

Commit 1db476f

Browse files
Updated code generation (#1441) (#1442)
* Updated code generation * Nit Co-authored-by: Tomas Della Vedova <[email protected]>
1 parent 93231e6 commit 1db476f

File tree

3 files changed

+19
-40
lines changed

3 files changed

+19
-40
lines changed

scripts/utils/generateApis.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,6 @@ const noPathValidation = [
5959
'update'
6060
]
6161

62-
// apis that uses bulkBody property
63-
const ndjsonApi = [
64-
'bulk',
65-
'msearch',
66-
'msearch_template',
67-
'ml.find_file_structure',
68-
'monitoring.bulk',
69-
'xpack.ml.find_file_structure',
70-
'xpack.monitoring.bulk'
71-
]
72-
7362
function generateNamespace (namespace, nested, specFolder, version) {
7463
const common = require(join(specFolder, '_common.json'))
7564
let code = dedent`
@@ -251,7 +240,7 @@ function generateSingleApi (version, spec, common) {
251240
const request = {
252241
method,
253242
path,
254-
${genBody(api, methods, spec[api].body)}
243+
${genBody(api, methods, spec[api].body, spec)}
255244
querystring
256245
}
257246
@@ -378,7 +367,7 @@ function generateSingleApi (version, spec, common) {
378367
}
379368

380369
let hasStaticPath = false
381-
const sortedPaths = paths
370+
let sortedPaths = paths
382371
// some legacy API have mutliple statis paths
383372
// this filter removes them
384373
.filter(p => {
@@ -392,6 +381,9 @@ function generateSingleApi (version, spec, common) {
392381
// sort by number of parameters (desc)
393382
.sort((a, b) => Object.keys(b.parts || {}).length - Object.keys(a.parts || {}).length)
394383

384+
const allDeprecated = paths.filter(path => path.deprecated != null)
385+
if (allDeprecated.length === paths.length) sortedPaths = [paths[0]]
386+
395387
let code = ''
396388
for (let i = 0; i < sortedPaths.length; i++) {
397389
const { path, methods } = sortedPaths[i]
@@ -439,9 +431,10 @@ function generatePickMethod (methods) {
439431
}
440432
}
441433

442-
function genBody (api, methods, body) {
434+
function genBody (api, methods, body, spec) {
443435
const bodyMethod = getBodyMethod(methods)
444-
if (ndjsonApi.indexOf(api) > -1) {
436+
const { content_type } = spec[api].headers
437+
if (content_type && content_type.includes('application/x-ndjson')) {
445438
return 'bulkBody: body,'
446439
}
447440
if (body === null && bodyMethod) {
@@ -560,4 +553,3 @@ function Uppercase (str) {
560553
}
561554

562555
module.exports = generateNamespace
563-
module.exports.ndjsonApi = ndjsonApi

scripts/utils/generateMain.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,14 @@
1818
*/
1919

2020
/* eslint-disable no-template-curly-in-string */
21+
/* eslint camelcase: 0 */
2122

2223
'use strict'
2324

2425
const { readdirSync } = require('fs')
2526
const { join } = require('path')
2627
const dedent = require('dedent')
2728
const deepmerge = require('deepmerge')
28-
const { ndjsonApi } = require('./generateApis')
29-
30-
const ndjsonApiKey = ndjsonApi
31-
.map(api => {
32-
return api
33-
.replace(/\.([a-z])/g, k => k[1].toUpperCase())
34-
.replace(/_([a-z])/g, k => k[1].toUpperCase())
35-
})
36-
.map(toPascalCase)
3729

3830
function genFactory (folder, specFolder, namespaces) {
3931
// get all the API files
@@ -57,7 +49,7 @@ function genFactory (folder, specFolder, namespaces) {
5749
const spec = readSpec(specFolder, file.slice(0, -5))
5850
const isHead = isHeadMethod(spec, file.slice(0, -5))
5951
const body = hasBody(spec, file.slice(0, -5))
60-
const methods = acc === null ? buildMethodDefinition({ kibana: false }, val, name, body, isHead) : null
52+
const methods = acc === null ? buildMethodDefinition({ kibana: false }, val, name, body, isHead, spec) : null
6153
const obj = {}
6254
if (methods) {
6355
for (const m of methods) {
@@ -89,7 +81,7 @@ function genFactory (folder, specFolder, namespaces) {
8981
const spec = readSpec(specFolder, file.slice(0, -5))
9082
const isHead = isHeadMethod(spec, file.slice(0, -5))
9183
const body = hasBody(spec, file.slice(0, -5))
92-
const methods = acc === null ? buildMethodDefinition({ kibana: true }, val, name, body, isHead) : null
84+
const methods = acc === null ? buildMethodDefinition({ kibana: true }, val, name, body, isHead, spec) : null
9385
const obj = {}
9486
if (methods) {
9587
for (const m of methods) {
@@ -225,11 +217,12 @@ function toPascalCase (str) {
225217
return str[0].toUpperCase() + str.slice(1)
226218
}
227219

228-
function buildMethodDefinition (opts, api, name, hasBody, isHead) {
220+
function buildMethodDefinition (opts, api, name, hasBody, isHead, spec) {
229221
const Name = toPascalCase(name)
230-
const bodyType = ndjsonApiKey.includes(Name) ? 'RequestNDBody' : 'RequestBody'
222+
const { content_type } = spec[Object.keys(spec)[0]].headers
223+
const bodyType = content_type && content_type.includes('application/x-ndjson') ? 'RequestNDBody' : 'RequestBody'
231224
const responseType = isHead ? 'boolean' : 'Record<string, any>'
232-
const defaultBodyType = ndjsonApiKey.includes(Name) ? 'Record<string, any>[]' : 'Record<string, any>'
225+
const defaultBodyType = content_type && content_type.includes('application/x-ndjson') ? 'Record<string, any>[]' : 'Record<string, any>'
233226

234227
if (opts.kibana) {
235228
if (hasBody) {

scripts/utils/generateRequestTypes.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,11 @@
1717
* under the License.
1818
*/
1919

20+
/* eslint camelcase: 0 */
21+
2022
'use strict'
2123

2224
const deprecatedParameters = require('./patch.json')
23-
const { ndjsonApi } = require('./generateApis')
24-
25-
const ndjsonApiKey = ndjsonApi
26-
.map(api => {
27-
return api
28-
.replace(/\.([a-z])/g, k => k[1].toUpperCase())
29-
.replace(/_([a-z])/g, k => k[1].toUpperCase())
30-
})
31-
.map(toPascalCase)
3225

3326
function generate (version, api) {
3427
const release = version.charAt(0)
@@ -122,7 +115,8 @@ export interface Generic {
122115
return `${e.key}${optional}: ${getType(e.value.type, e.value.options)};`
123116
}
124117

125-
const bodyGeneric = ndjsonApiKey.includes(toPascalCase(name)) ? 'RequestNDBody' : 'RequestBody'
118+
const { content_type } = spec[api].headers
119+
const bodyGeneric = content_type && content_type.includes('application/x-ndjson') ? 'RequestNDBody' : 'RequestBody'
126120

127121
const code = `
128122
export interface ${toPascalCase(name)}${body ? `<T = ${bodyGeneric}>` : ''} extends Generic {

0 commit comments

Comments
 (0)