[Backport 7.x] Export a kibana restricted type definition (#1248)

Co-authored-by: Tomas Della Vedova <delvedor@users.noreply.github.com>
This commit is contained in:
github-actions[bot]
2020-07-06 11:49:49 +02:00
committed by GitHub
parent 40c47902f3
commit 615481d5ed
7 changed files with 655 additions and 16 deletions

View File

@ -36,7 +36,7 @@ function genFactory (folder, paths) {
.reverse()
.reduce((acc, val) => {
const body = hasBody(paths, file.slice(0, -3))
const methods = acc === null ? buildMethodDefinition(val, name, body) : null
const methods = acc === null ? buildMethodDefinition({ kibana: false }, val, name, body) : null
const obj = {}
if (methods) {
for (const m of methods) {
@ -53,6 +53,33 @@ function genFactory (folder, paths) {
})
.reduce((acc, val) => deepmerge(acc, val), {})
const kibanaTypes = apiFiles
.map(file => {
const name = file
.slice(0, -3)
.replace(/\.([a-z])/g, k => k[1].toUpperCase())
.replace(/_([a-z])/g, k => k[1].toUpperCase())
return file
.slice(0, -3) // remove `.js` extension
.split('.')
.reverse()
.reduce((acc, val) => {
const body = hasBody(paths, file.slice(0, -3))
const methods = acc === null ? buildMethodDefinition({ kibana: true }, val, name, body) : null
const obj = {}
if (methods) {
for (const m of methods) {
obj[m.key] = m.val
}
} else {
obj[camelify(val)] = acc
}
return obj
}, null)
})
.reduce((acc, val) => deepmerge(acc, val), {})
const apis = apiFiles
.map(file => {
// const name = format(file.slice(0, -3))
@ -97,6 +124,18 @@ function genFactory (folder, paths) {
// remove useless quotes and commas
.replace(/"/g, '')
.replace(/,$/gm, '')
const kibanaTypesStr = Object.keys(kibanaTypes)
.map(key => {
const line = ` ${key}: ${JSON.stringify(kibanaTypes[key], null, 4)}`
if (line.slice(-1) === '}') {
return line.slice(0, -1) + ' }'
}
return line
})
.join('\n')
// remove useless quotes and commas
.replace(/"/g, '')
.replace(/,$/gm, '')
const fn = dedent`
// Licensed to Elasticsearch B.V under one or more agreements.
@ -161,7 +200,7 @@ function genFactory (folder, paths) {
`
// new line at the end of file
return { fn: fn + '\n', types: typesStr }
return { fn: fn + '\n', types: typesStr, kibanaTypes: kibanaTypesStr }
}
// from snake_case to camelCase
@ -177,11 +216,23 @@ function toPascalCase (str) {
return str[0].toUpperCase() + str.slice(1)
}
function buildMethodDefinition (api, name, hasBody) {
function buildMethodDefinition (opts, api, name, hasBody) {
const Name = toPascalCase(name)
const bodyType = ndjsonApiKey.includes(Name) ? 'RequestNDBody' : 'RequestBody'
const defaultBodyType = ndjsonApiKey.includes(Name) ? 'Record<string, any>[]' : 'Record<string, any>'
if (opts.kibana) {
if (hasBody) {
return [
{ key: `${camelify(api)}<TResponse = Record<string, any>, TRequestBody extends ${bodyType} = ${defaultBodyType}, TContext = unknown>(params?: RequestParams.${Name}<TRequestBody>, options?: TransportRequestOptions)`, val: `TransportRequestPromise<ApiResponse<TResponse, TContext>>` }
]
} else {
return [
{ key: `${camelify(api)}<TResponse = Record<string, any>, TContext = unknown>(params?: RequestParams.${Name}, options?: TransportRequestOptions)`, val: `TransportRequestPromise<ApiResponse<TResponse, TContext>>` }
]
}
}
if (hasBody) {
let methods = [
{ key: `${api}<TResponse = Record<string, any>, TRequestBody extends ${bodyType} = ${defaultBodyType}, TContext = unknown>(params?: RequestParams.${Name}<TRequestBody>, options?: TransportRequestOptions)`, val: `TransportRequestPromise<ApiResponse<TResponse, TContext>>` },