Skip to content

Commit d70fdad

Browse files
committed
Updated code generation (#1441)
* Updated code generation * Nit
1 parent 540d9e6 commit d70fdad

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
@@ -60,17 +60,6 @@ const noPathValidation = [
6060
'update'
6161
]
6262

63-
// apis that uses bulkBody property
64-
const ndjsonApi = [
65-
'bulk',
66-
'msearch',
67-
'msearch_template',
68-
'ml.find_file_structure',
69-
'monitoring.bulk',
70-
'xpack.ml.find_file_structure',
71-
'xpack.monitoring.bulk'
72-
]
73-
7463
function generateNamespace (namespace, nested, folders, version) {
7564
const common = require(join(folders.apiFolder, '_common.json'))
7665
let code = dedent`
@@ -262,7 +251,7 @@ function generateSingleApi (version, spec, common) {
262251
const request = {
263252
method,
264253
path,
265-
${genBody(api, methods, spec[api].body)}
254+
${genBody(api, methods, spec[api].body, spec)}
266255
querystring
267256
}
268257
@@ -389,7 +378,7 @@ function generateSingleApi (version, spec, common) {
389378
}
390379

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

395+
const allDeprecated = paths.filter(path => path.deprecated != null)
396+
if (allDeprecated.length === paths.length) sortedPaths = [paths[0]]
397+
406398
let code = ''
407399
for (let i = 0; i < sortedPaths.length; i++) {
408400
const { path, methods } = sortedPaths[i]
@@ -450,9 +442,10 @@ function generatePickMethod (methods) {
450442
}
451443
}
452444

453-
function genBody (api, methods, body) {
445+
function genBody (api, methods, body, spec) {
454446
const bodyMethod = getBodyMethod(methods)
455-
if (ndjsonApi.indexOf(api) > -1) {
447+
const { content_type } = spec[api].headers
448+
if (content_type && content_type.includes('application/x-ndjson')) {
456449
return 'bulkBody: body,'
457450
}
458451
if (body === null && bodyMethod) {
@@ -571,4 +564,3 @@ function Uppercase (str) {
571564
}
572565

573566
module.exports = generateNamespace
574-
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, paths, namespaces) {
3931
// get all the API files
@@ -58,7 +50,7 @@ function genFactory (folder, paths, namespaces) {
5850
const spec = readSpec(paths, file.slice(0, -5))
5951
const isHead = isHeadMethod(spec, file.slice(0, -5))
6052
const body = hasBody(spec, file.slice(0, -5))
61-
const methods = acc === null ? buildMethodDefinition({ kibana: false }, val, name, body, isHead) : null
53+
const methods = acc === null ? buildMethodDefinition({ kibana: false }, val, name, body, isHead, spec) : null
6254
const obj = {}
6355
if (methods) {
6456
for (const m of methods) {
@@ -90,7 +82,7 @@ function genFactory (folder, paths, namespaces) {
9082
const spec = readSpec(paths, file.slice(0, -5))
9183
const isHead = isHeadMethod(spec, file.slice(0, -5))
9284
const body = hasBody(spec, file.slice(0, -5))
93-
const methods = acc === null ? buildMethodDefinition({ kibana: true }, val, name, body, isHead) : null
85+
const methods = acc === null ? buildMethodDefinition({ kibana: true }, val, name, body, isHead, spec) : null
9486
const obj = {}
9587
if (methods) {
9688
for (const m of methods) {
@@ -226,11 +218,12 @@ function toPascalCase (str) {
226218
return str[0].toUpperCase() + str.slice(1)
227219
}
228220

229-
function buildMethodDefinition (opts, api, name, hasBody, isHead) {
221+
function buildMethodDefinition (opts, api, name, hasBody, isHead, spec) {
230222
const Name = toPascalCase(name)
231-
const bodyType = ndjsonApiKey.includes(Name) ? 'RequestNDBody' : 'RequestBody'
223+
const { content_type } = spec[Object.keys(spec)[0]].headers
224+
const bodyType = content_type && content_type.includes('application/x-ndjson') ? 'RequestNDBody' : 'RequestBody'
232225
const responseType = isHead ? 'boolean' : 'Record<string, any>'
233-
const defaultBodyType = ndjsonApiKey.includes(Name) ? 'Record<string, any>[]' : 'Record<string, any>'
226+
const defaultBodyType = content_type && content_type.includes('application/x-ndjson') ? 'Record<string, any>[]' : 'Record<string, any>'
234227

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

scripts/utils/generateRequestTypes.js

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

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

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

3427
function generate (version, api) {
3528
const release = semver.valid(version) ? semver.major(version) : version
@@ -123,7 +116,8 @@ export interface Generic {
123116
return `${e.key}${optional}: ${getType(e.value.type, e.value.options)};`
124117
}
125118

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

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

0 commit comments

Comments
 (0)