Skip to content

Commit d7d55e1

Browse files
authored
Updated code generation (#1441)
* Updated code generation * Nit
1 parent 4102a28 commit d7d55e1

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
@@ -57,17 +57,6 @@ const noPathValidation = [
5757
'update'
5858
]
5959

60-
// apis that uses bulkBody property
61-
const ndjsonApi = [
62-
'bulk',
63-
'msearch',
64-
'msearch_template',
65-
'ml.find_file_structure',
66-
'monitoring.bulk',
67-
'xpack.ml.find_file_structure',
68-
'xpack.monitoring.bulk'
69-
]
70-
7160
function generateNamespace (namespace, nested, specFolder, version) {
7261
const common = require(join(specFolder, '_common.json'))
7362
let code = dedent`
@@ -249,7 +238,7 @@ function generateSingleApi (version, spec, common) {
249238
const request = {
250239
method,
251240
path,
252-
${genBody(api, methods, spec[api].body)}
241+
${genBody(api, methods, spec[api].body, spec)}
253242
querystring
254243
}
255244
@@ -376,7 +365,7 @@ function generateSingleApi (version, spec, common) {
376365
}
377366

378367
let hasStaticPath = false
379-
const sortedPaths = paths
368+
let sortedPaths = paths
380369
// some legacy API have mutliple statis paths
381370
// this filter removes them
382371
.filter(p => {
@@ -390,6 +379,9 @@ function generateSingleApi (version, spec, common) {
390379
// sort by number of parameters (desc)
391380
.sort((a, b) => Object.keys(b.parts || {}).length - Object.keys(a.parts || {}).length)
392381

382+
const allDeprecated = paths.filter(path => path.deprecated != null)
383+
if (allDeprecated.length === paths.length) sortedPaths = [paths[0]]
384+
393385
let code = ''
394386
for (let i = 0; i < sortedPaths.length; i++) {
395387
const { path, methods } = sortedPaths[i]
@@ -437,9 +429,10 @@ function generatePickMethod (methods) {
437429
}
438430
}
439431

440-
function genBody (api, methods, body) {
432+
function genBody (api, methods, body, spec) {
441433
const bodyMethod = getBodyMethod(methods)
442-
if (ndjsonApi.indexOf(api) > -1) {
434+
const { content_type } = spec[api].headers
435+
if (content_type && content_type.includes('application/x-ndjson')) {
443436
return 'bulkBody: body,'
444437
}
445438
if (body === null && bodyMethod) {
@@ -558,4 +551,3 @@ function Uppercase (str) {
558551
}
559552

560553
module.exports = generateNamespace
561-
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)