Skip to content

Commit 4ab53ee

Browse files
Updated new types export (#1446) (#1447)
* Updated new types * Updated new types * Updated test * Updated docs Co-authored-by: Tomas Della Vedova <[email protected]>
1 parent 17b3d63 commit 4ab53ee

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

api/new.d.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@
2222
import {
2323
ClientOptions,
2424
ConnectionPool,
25+
BaseConnectionPool,
26+
CloudConnectionPool,
27+
Connection,
2528
Serializer,
2629
Transport,
2730
errors,
2831
RequestEvent,
2932
ResurrectEvent,
30-
ApiError
33+
ApiError,
34+
NodeOptions,
35+
events
3136
} from '../index'
3237
import Helpers from '../lib/Helpers'
3338
import {
@@ -61,14 +66,14 @@ declare type extendsCallback = (options: ClientExtendsCallbackOptions) => any;
6166
// /Extend API
6267

6368
declare type callbackFn<TResponse, TContext> = (err: ApiError, result: ApiResponse<TResponse, TContext>) => void;
64-
interface NewClientTypes {
69+
interface Client {
6570
connectionPool: ConnectionPool
6671
transport: Transport
6772
serializer: Serializer
6873
extend(method: string, fn: extendsCallback): void
6974
extend(method: string, opts: { force: boolean }, fn: extendsCallback): void;
7075
helpers: Helpers
71-
child(opts?: ClientOptions): NewClientTypes
76+
child(opts?: ClientOptions): Client
7277
close(callback: Function): void;
7378
close(): Promise<void>;
7479
emit(event: string | symbol, ...args: any[]): boolean;
@@ -1471,4 +1476,22 @@ interface NewClientTypes {
14711476
}
14721477
}
14731478

1474-
export { NewClientTypes }
1479+
export * as estypes from './types'
1480+
export {
1481+
Client,
1482+
Transport,
1483+
ConnectionPool,
1484+
BaseConnectionPool,
1485+
CloudConnectionPool,
1486+
Connection,
1487+
Serializer,
1488+
events,
1489+
errors,
1490+
ApiError,
1491+
ApiResponse,
1492+
RequestEvent,
1493+
ResurrectEvent,
1494+
ClientOptions,
1495+
NodeOptions,
1496+
ClientExtendsCallbackOptions
1497+
};

docs/typescript.asciidoc

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const response = await client.search<Source>({
5050
The types are not 100% complete yet. Some APIs are missing (the newest ones, e.g. EQL),
5151
and others may contain some errors, but we are continuously pushing fixes & improvements.
5252

53+
[discrete]
54+
==== Request & Response types
55+
5356
Once you migrate to the new types, those are automatically integrated into the Elasticsearch client, you will get them out of the box.
5457
If everything works, meaning that you won’t get compiler errors, you are good to go!
5558
The types are already correct, and there is nothing more to do.
@@ -63,6 +66,30 @@ a `TODO` type, which accepts any object.
6366

6467
Open an issue in the client repository letting us know if you encounter any problem!
6568

69+
If needed you can import the request and response types.
70+
71+
[source,ts]
72+
----
73+
import { Client, estypes } from '@elastic/elasticsearch'
74+
import type { Client as NewTypes } from '@elastic/elasticsearch/api/new'
75+
76+
// @ts-expect-error @elastic/elasticsearch
77+
const client: NewTypes = new Client({
78+
node: 'http://localhost:9200'
79+
})
80+
81+
interface Source {
82+
foo: string
83+
}
84+
85+
const request: estypes.IndexRequest<Source> = {
86+
index: 'test',
87+
body: { foo: 'bar' }
88+
}
89+
90+
await client.index(request)
91+
----
92+
6693
[discrete]
6794
===== How to migrate to the new type definitions
6895

@@ -72,12 +99,12 @@ Following you will find a snippet that shows you how to override the default typ
7299
[source,ts]
73100
----
74101
import { Client } from '@elastic/elasticsearch'
75-
import type { NewClientTypes } from '@elastic/elasticsearch/api/new'
102+
import type { Client as NewTypes } from '@elastic/elasticsearch/api/new'
76103
77104
// @ts-expect-error @elastic/elasticsearch
78-
const client = new Client({
105+
const client: NewTypes = new Client({
79106
node: 'http://localhost:9200'
80-
}) as NewClientTypes
107+
})
81108
82109
interface Source {
83110
foo: string

test/types/new-types.test-d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
import { expectType, expectNotType, expectError } from 'tsd'
2121
import { Client, RequestEvent, ResurrectEvent, ApiError, ApiResponse, estypes } from '../../'
22-
import { NewClientTypes } from '../../api/new'
22+
import type { Client as NewTypes } from '../../api/new'
2323
import { TransportRequestPromise, Context } from '../../lib/Transport'
2424

2525
// @ts-expect-error
26-
const client: NewClientTypes = new Client({
26+
const client: NewTypes = new Client({
2727
node: 'http://localhost:9200'
2828
})
2929

@@ -104,5 +104,5 @@ client.async_search.get()
104104

105105
// the child api should return a KibanaClient instance
106106
const child = client.child()
107-
expectType<NewClientTypes>(child)
107+
expectType<NewTypes>(child)
108108
expectNotType<Client>(child)

0 commit comments

Comments
 (0)