Skip to content

Commit

Permalink
New type definitions (#1358)
Browse files Browse the repository at this point in the history
  • Loading branch information
delvedor committed Mar 24, 2021
1 parent f7edb80 commit f138b07
Show file tree
Hide file tree
Showing 12 changed files with 14,661 additions and 3,202 deletions.
1,418 changes: 1,418 additions & 0 deletions api/esapi.d.ts

Large diffs are not rendered by default.

761 changes: 386 additions & 375 deletions api/kibana.d.ts

Large diffs are not rendered by default.

12,802 changes: 12,802 additions & 0 deletions api/types.d.ts

Large diffs are not rendered by default.

2,569 changes: 6 additions & 2,563 deletions index.d.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/Transport.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type ApiError = errors.ConfigurationError | errors.ConnectionError |
errors.NoLivingConnectionsError | errors.ResponseError |
errors.TimeoutError | errors.RequestAbortedError

export type Context = Record<string, unknown> | null
export type Context = unknown

export interface nodeSelectorFn {
(connections: Connection[]): Connection;
Expand Down
32 changes: 1 addition & 31 deletions scripts/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const {
generate,
cloneAndCheckout,
genFactory,
generateRequestTypes,
generateDocs
} = require('./utils')

Expand All @@ -47,10 +46,7 @@ function start (opts) {
const packageFolder = join(__dirname, '..', 'api')
const apiOutputFolder = join(packageFolder, 'api')
const mainOutputFile = join(packageFolder, 'index.js')
const typeDefFile = join(__dirname, '..', 'index.d.ts')
const kibanaTypeDefFile = join(packageFolder, 'kibana.d.ts')
const docOutputFile = join(__dirname, '..', 'docs', 'reference.asciidoc')
const requestParamsOutputFile = join(packageFolder, 'requestParams.d.ts')

log.text = 'Cleaning API folder...'
rimraf.sync(join(apiOutputFolder, '*.js'))
Expand Down Expand Up @@ -85,39 +81,13 @@ function start (opts) {
writeFileSync(filePath, code, { encoding: 'utf8' })
}

writeFileSync(
requestParamsOutputFile,
generateRequestTypes(opts.branch || opts.tag, allSpec),
{ encoding: 'utf8' }
)

const { fn: factory, types, kibanaTypes } = genFactory(apiOutputFolder, [apiFolder, xPackFolder], namespaces)
const { fn: factory } = genFactory(apiOutputFolder, [apiFolder, xPackFolder], namespaces)
writeFileSync(
mainOutputFile,
factory,
{ encoding: 'utf8' }
)

let oldTypeDefString = readFileSync(typeDefFile, 'utf8')
let start = oldTypeDefString.indexOf('/* GENERATED */')
let end = oldTypeDefString.indexOf('/* /GENERATED */')
let newTypeDefString = oldTypeDefString.slice(0, start + 15) + '\n' + types + '\n ' + oldTypeDefString.slice(end)
writeFileSync(
typeDefFile,
newTypeDefString,
{ encoding: 'utf8' }
)

oldTypeDefString = readFileSync(kibanaTypeDefFile, 'utf8')
start = oldTypeDefString.indexOf('/* GENERATED */')
end = oldTypeDefString.indexOf('/* /GENERATED */')
newTypeDefString = oldTypeDefString.slice(0, start + 15) + '\n' + kibanaTypes + '\n ' + oldTypeDefString.slice(end)
writeFileSync(
kibanaTypeDefFile,
newTypeDefString,
{ encoding: 'utf8' }
)

lintFiles(log, () => {
log.text = 'Generating documentation'
writeFileSync(
Expand Down
2 changes: 0 additions & 2 deletions scripts/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
'use strict'

const generate = require('./generateApis')
const generateRequestTypes = require('./generateRequestTypes')
const cloneAndCheckout = require('./clone-es')
const genFactory = require('./generateMain')
const generateDocs = require('./generateDocs')
Expand All @@ -29,6 +28,5 @@ module.exports = {
generate,
cloneAndCheckout,
genFactory,
generateRequestTypes,
generateDocs
}
173 changes: 12 additions & 161 deletions test/types/api-response-body.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,57 +20,12 @@
import { expectType, expectError } from 'tsd'
import { Readable as ReadableStream } from 'stream';
import { TransportRequestCallback, Context } from '../../lib/Transport'
import { Client, ApiError } from '../../'
import { Client, ApiError, estypes } from '../../'

const client = new Client({
node: 'http://localhost:9200'
})

interface SearchBody {
query: {
match: { foo: string }
}
}

interface ShardsResponse {
total: number;
successful: number;
failed: number;
skipped: number;
}

interface Explanation {
value: number;
description: string;
details: Explanation[];
}

interface SearchResponse<T> {
took: number;
timed_out: boolean;
_scroll_id?: string;
_shards: ShardsResponse;
hits: {
total: number;
max_score: number;
hits: Array<{
_index: string;
_type: string;
_id: string;
_score: number;
_source: T;
_version?: number;
_explanation?: Explanation;
fields?: any;
highlight?: any;
inner_hits?: any;
matched_queries?: string[];
sort?: string[];
}>;
};
aggregations?: any;
}

interface Source {
foo: string
}
Expand All @@ -94,28 +49,13 @@ expectError(
}
})

expectType<Record<string, any>>(response.body)
expectType<Context>(response.meta.context)
}

// Define only the response body (promise style)
{
const response = await client.search<SearchResponse<Source>>({
index: 'test',
body: {
query: {
match: { foo: 'bar' }
}
}
})

expectType<SearchResponse<Source>>(response.body)
expectType<estypes.SearchResponse<unknown>>(response.body)
expectType<Context>(response.meta.context)
}

// Define response body and request body (promise style)
// Define only the source (promise style)
{
const response = await client.search<SearchResponse<Source>, SearchBody>({
const response = await client.search<Source>({
index: 'test',
body: {
query: {
Expand All @@ -124,13 +64,13 @@ expectError(
}
})

expectType<SearchResponse<Source>>(response.body)
expectType<estypes.SearchResponse<Source>>(response.body)
expectType<Context>(response.meta.context)
}

// Define response body, request body and the context (promise style)
{
const response = await client.search<SearchResponse<Source>, SearchBody, Context>({
const response = await client.search<Source, Context>({
index: 'test',
body: {
query: {
Expand All @@ -139,40 +79,7 @@ expectError(
}
})

expectType<SearchResponse<Source>>(response.body)
expectType<Context>(response.meta.context)
}

// Send request body as string (promise style)
{
const response = await client.search({
index: 'test',
body: 'hello world'
})

expectType<Record<string, any>>(response.body)
expectType<Context>(response.meta.context)
}

// Send request body as buffer (promise style)
{
const response = await client.search({
index: 'test',
body: Buffer.from('hello world')
})

expectType<Record<string, any>>(response.body)
expectType<Context>(response.meta.context)
}

// Send request body as readable stream (promise style)
{
const response = await client.search({
index: 'test',
body: new ReadableStream()
})

expectType<Record<string, any>>(response.body)
expectType<estypes.SearchResponse<Source>>(response.body)
expectType<Context>(response.meta.context)
}

Expand All @@ -187,15 +94,15 @@ expectError(
}
}, (err, response) => {
expectType<ApiError>(err)
expectType<Record<string, any>>(response.body)
expectType<estypes.SearchResponse<unknown>>(response.body)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}

// Define only the response body (callback style)
{
const result = client.search<SearchResponse<Source>>({
const result = client.search<Source>({
index: 'test',
body: {
query: {
Expand All @@ -204,32 +111,15 @@ expectError(
}
}, (err, response) => {
expectType<ApiError>(err)
expectType<SearchResponse<Source>>(response.body)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}

// Define response body and request body (callback style)
{
const result = client.search<SearchResponse<Source>, SearchBody>({
index: 'test',
body: {
query: {
match: { foo: 'bar' }
}
}
}, (err, response) => {
expectType<ApiError>(err)
expectType<SearchResponse<Source>>(response.body)
expectType<estypes.SearchResponse<Source>>(response.body)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}

// Define response body, request body and the context (callback style)
{
const result = client.search<SearchResponse<Source>, SearchBody, Context>({
const result = client.search<Source, Context>({
index: 'test',
body: {
query: {
Expand All @@ -238,46 +128,7 @@ expectError(
}
}, (err, response) => {
expectType<ApiError>(err)
expectType<SearchResponse<Source>>(response.body)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}

// Send request body as string (callback style)
{
const result = client.search({
index: 'test',
body: 'hello world'
}, (err, response) => {
expectType<ApiError>(err)
expectType<Record<string, any>>(response.body)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}

// Send request body as buffer (callback style)
{
const result = client.search({
index: 'test',
body: Buffer.from('hello world')
}, (err, response) => {
expectType<ApiError>(err)
expectType<Record<string, any>>(response.body)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}

// Send request body as readable stream (callback style)
{
const result = client.search({
index: 'test',
body: new ReadableStream()
}, (err, response) => {
expectType<ApiError>(err)
expectType<Record<string, any>>(response.body)
expectType<estypes.SearchResponse<Source>>(response.body)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
Expand Down
Loading

0 comments on commit f138b07

Please sign in to comment.