Skip to content

Commit f138b07

Browse files
committed
New type definitions (#1358)
1 parent f7edb80 commit f138b07

File tree

12 files changed

+14661
-3202
lines changed

12 files changed

+14661
-3202
lines changed

api/esapi.d.ts

Lines changed: 1418 additions & 0 deletions
Large diffs are not rendered by default.

api/kibana.d.ts

Lines changed: 386 additions & 375 deletions
Large diffs are not rendered by default.

api/types.d.ts

Lines changed: 12802 additions & 0 deletions
Large diffs are not rendered by default.

index.d.ts

Lines changed: 6 additions & 2563 deletions
Large diffs are not rendered by default.

lib/Transport.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export type ApiError = errors.ConfigurationError | errors.ConnectionError |
2828
errors.NoLivingConnectionsError | errors.ResponseError |
2929
errors.TimeoutError | errors.RequestAbortedError
3030

31-
export type Context = Record<string, unknown> | null
31+
export type Context = unknown
3232

3333
export interface nodeSelectorFn {
3434
(connections: Connection[]): Connection;

scripts/generate.js

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ const {
3030
generate,
3131
cloneAndCheckout,
3232
genFactory,
33-
generateRequestTypes,
3433
generateDocs
3534
} = require('./utils')
3635

@@ -47,10 +46,7 @@ function start (opts) {
4746
const packageFolder = join(__dirname, '..', 'api')
4847
const apiOutputFolder = join(packageFolder, 'api')
4948
const mainOutputFile = join(packageFolder, 'index.js')
50-
const typeDefFile = join(__dirname, '..', 'index.d.ts')
51-
const kibanaTypeDefFile = join(packageFolder, 'kibana.d.ts')
5249
const docOutputFile = join(__dirname, '..', 'docs', 'reference.asciidoc')
53-
const requestParamsOutputFile = join(packageFolder, 'requestParams.d.ts')
5450

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

88-
writeFileSync(
89-
requestParamsOutputFile,
90-
generateRequestTypes(opts.branch || opts.tag, allSpec),
91-
{ encoding: 'utf8' }
92-
)
93-
94-
const { fn: factory, types, kibanaTypes } = genFactory(apiOutputFolder, [apiFolder, xPackFolder], namespaces)
84+
const { fn: factory } = genFactory(apiOutputFolder, [apiFolder, xPackFolder], namespaces)
9585
writeFileSync(
9686
mainOutputFile,
9787
factory,
9888
{ encoding: 'utf8' }
9989
)
10090

101-
let oldTypeDefString = readFileSync(typeDefFile, 'utf8')
102-
let start = oldTypeDefString.indexOf('/* GENERATED */')
103-
let end = oldTypeDefString.indexOf('/* /GENERATED */')
104-
let newTypeDefString = oldTypeDefString.slice(0, start + 15) + '\n' + types + '\n ' + oldTypeDefString.slice(end)
105-
writeFileSync(
106-
typeDefFile,
107-
newTypeDefString,
108-
{ encoding: 'utf8' }
109-
)
110-
111-
oldTypeDefString = readFileSync(kibanaTypeDefFile, 'utf8')
112-
start = oldTypeDefString.indexOf('/* GENERATED */')
113-
end = oldTypeDefString.indexOf('/* /GENERATED */')
114-
newTypeDefString = oldTypeDefString.slice(0, start + 15) + '\n' + kibanaTypes + '\n ' + oldTypeDefString.slice(end)
115-
writeFileSync(
116-
kibanaTypeDefFile,
117-
newTypeDefString,
118-
{ encoding: 'utf8' }
119-
)
120-
12191
lintFiles(log, () => {
12292
log.text = 'Generating documentation'
12393
writeFileSync(

scripts/utils/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
'use strict'
2121

2222
const generate = require('./generateApis')
23-
const generateRequestTypes = require('./generateRequestTypes')
2423
const cloneAndCheckout = require('./clone-es')
2524
const genFactory = require('./generateMain')
2625
const generateDocs = require('./generateDocs')
@@ -29,6 +28,5 @@ module.exports = {
2928
generate,
3029
cloneAndCheckout,
3130
genFactory,
32-
generateRequestTypes,
3331
generateDocs
3432
}

test/types/api-response-body.test-d.ts

Lines changed: 12 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -20,57 +20,12 @@
2020
import { expectType, expectError } from 'tsd'
2121
import { Readable as ReadableStream } from 'stream';
2222
import { TransportRequestCallback, Context } from '../../lib/Transport'
23-
import { Client, ApiError } from '../../'
23+
import { Client, ApiError, estypes } from '../../'
2424

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

29-
interface SearchBody {
30-
query: {
31-
match: { foo: string }
32-
}
33-
}
34-
35-
interface ShardsResponse {
36-
total: number;
37-
successful: number;
38-
failed: number;
39-
skipped: number;
40-
}
41-
42-
interface Explanation {
43-
value: number;
44-
description: string;
45-
details: Explanation[];
46-
}
47-
48-
interface SearchResponse<T> {
49-
took: number;
50-
timed_out: boolean;
51-
_scroll_id?: string;
52-
_shards: ShardsResponse;
53-
hits: {
54-
total: number;
55-
max_score: number;
56-
hits: Array<{
57-
_index: string;
58-
_type: string;
59-
_id: string;
60-
_score: number;
61-
_source: T;
62-
_version?: number;
63-
_explanation?: Explanation;
64-
fields?: any;
65-
highlight?: any;
66-
inner_hits?: any;
67-
matched_queries?: string[];
68-
sort?: string[];
69-
}>;
70-
};
71-
aggregations?: any;
72-
}
73-
7429
interface Source {
7530
foo: string
7631
}
@@ -94,28 +49,13 @@ expectError(
9449
}
9550
})
9651

97-
expectType<Record<string, any>>(response.body)
98-
expectType<Context>(response.meta.context)
99-
}
100-
101-
// Define only the response body (promise style)
102-
{
103-
const response = await client.search<SearchResponse<Source>>({
104-
index: 'test',
105-
body: {
106-
query: {
107-
match: { foo: 'bar' }
108-
}
109-
}
110-
})
111-
112-
expectType<SearchResponse<Source>>(response.body)
52+
expectType<estypes.SearchResponse<unknown>>(response.body)
11353
expectType<Context>(response.meta.context)
11454
}
11555

116-
// Define response body and request body (promise style)
56+
// Define only the source (promise style)
11757
{
118-
const response = await client.search<SearchResponse<Source>, SearchBody>({
58+
const response = await client.search<Source>({
11959
index: 'test',
12060
body: {
12161
query: {
@@ -124,13 +64,13 @@ expectError(
12464
}
12565
})
12666

127-
expectType<SearchResponse<Source>>(response.body)
67+
expectType<estypes.SearchResponse<Source>>(response.body)
12868
expectType<Context>(response.meta.context)
12969
}
13070

13171
// Define response body, request body and the context (promise style)
13272
{
133-
const response = await client.search<SearchResponse<Source>, SearchBody, Context>({
73+
const response = await client.search<Source, Context>({
13474
index: 'test',
13575
body: {
13676
query: {
@@ -139,40 +79,7 @@ expectError(
13979
}
14080
})
14181

142-
expectType<SearchResponse<Source>>(response.body)
143-
expectType<Context>(response.meta.context)
144-
}
145-
146-
// Send request body as string (promise style)
147-
{
148-
const response = await client.search({
149-
index: 'test',
150-
body: 'hello world'
151-
})
152-
153-
expectType<Record<string, any>>(response.body)
154-
expectType<Context>(response.meta.context)
155-
}
156-
157-
// Send request body as buffer (promise style)
158-
{
159-
const response = await client.search({
160-
index: 'test',
161-
body: Buffer.from('hello world')
162-
})
163-
164-
expectType<Record<string, any>>(response.body)
165-
expectType<Context>(response.meta.context)
166-
}
167-
168-
// Send request body as readable stream (promise style)
169-
{
170-
const response = await client.search({
171-
index: 'test',
172-
body: new ReadableStream()
173-
})
174-
175-
expectType<Record<string, any>>(response.body)
82+
expectType<estypes.SearchResponse<Source>>(response.body)
17683
expectType<Context>(response.meta.context)
17784
}
17885

@@ -187,15 +94,15 @@ expectError(
18794
}
18895
}, (err, response) => {
18996
expectType<ApiError>(err)
190-
expectType<Record<string, any>>(response.body)
97+
expectType<estypes.SearchResponse<unknown>>(response.body)
19198
expectType<Context>(response.meta.context)
19299
})
193100
expectType<TransportRequestCallback>(result)
194101
}
195102

196103
// Define only the response body (callback style)
197104
{
198-
const result = client.search<SearchResponse<Source>>({
105+
const result = client.search<Source>({
199106
index: 'test',
200107
body: {
201108
query: {
@@ -204,32 +111,15 @@ expectError(
204111
}
205112
}, (err, response) => {
206113
expectType<ApiError>(err)
207-
expectType<SearchResponse<Source>>(response.body)
208-
expectType<Context>(response.meta.context)
209-
})
210-
expectType<TransportRequestCallback>(result)
211-
}
212-
213-
// Define response body and request body (callback style)
214-
{
215-
const result = client.search<SearchResponse<Source>, SearchBody>({
216-
index: 'test',
217-
body: {
218-
query: {
219-
match: { foo: 'bar' }
220-
}
221-
}
222-
}, (err, response) => {
223-
expectType<ApiError>(err)
224-
expectType<SearchResponse<Source>>(response.body)
114+
expectType<estypes.SearchResponse<Source>>(response.body)
225115
expectType<Context>(response.meta.context)
226116
})
227117
expectType<TransportRequestCallback>(result)
228118
}
229119

230120
// Define response body, request body and the context (callback style)
231121
{
232-
const result = client.search<SearchResponse<Source>, SearchBody, Context>({
122+
const result = client.search<Source, Context>({
233123
index: 'test',
234124
body: {
235125
query: {
@@ -238,46 +128,7 @@ expectError(
238128
}
239129
}, (err, response) => {
240130
expectType<ApiError>(err)
241-
expectType<SearchResponse<Source>>(response.body)
242-
expectType<Context>(response.meta.context)
243-
})
244-
expectType<TransportRequestCallback>(result)
245-
}
246-
247-
// Send request body as string (callback style)
248-
{
249-
const result = client.search({
250-
index: 'test',
251-
body: 'hello world'
252-
}, (err, response) => {
253-
expectType<ApiError>(err)
254-
expectType<Record<string, any>>(response.body)
255-
expectType<Context>(response.meta.context)
256-
})
257-
expectType<TransportRequestCallback>(result)
258-
}
259-
260-
// Send request body as buffer (callback style)
261-
{
262-
const result = client.search({
263-
index: 'test',
264-
body: Buffer.from('hello world')
265-
}, (err, response) => {
266-
expectType<ApiError>(err)
267-
expectType<Record<string, any>>(response.body)
268-
expectType<Context>(response.meta.context)
269-
})
270-
expectType<TransportRequestCallback>(result)
271-
}
272-
273-
// Send request body as readable stream (callback style)
274-
{
275-
const result = client.search({
276-
index: 'test',
277-
body: new ReadableStream()
278-
}, (err, response) => {
279-
expectType<ApiError>(err)
280-
expectType<Record<string, any>>(response.body)
131+
expectType<estypes.SearchResponse<Source>>(response.body)
281132
expectType<Context>(response.meta.context)
282133
})
283134
expectType<TransportRequestCallback>(result)

0 commit comments

Comments
 (0)