Skip to content

Updated new types export #1446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions api/new.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@
import {
ClientOptions,
ConnectionPool,
BaseConnectionPool,
CloudConnectionPool,
Connection,
Serializer,
Transport,
errors,
RequestEvent,
ResurrectEvent,
ApiError
ApiError,
NodeOptions,
events
} from '../index'
import Helpers from '../lib/Helpers'
import {
Expand Down Expand Up @@ -61,14 +66,14 @@ declare type extendsCallback = (options: ClientExtendsCallbackOptions) => any;
// /Extend API

declare type callbackFn<TResponse, TContext> = (err: ApiError, result: ApiResponse<TResponse, TContext>) => void;
interface NewClientTypes {
interface Client {
connectionPool: ConnectionPool
transport: Transport
serializer: Serializer
extend(method: string, fn: extendsCallback): void
extend(method: string, opts: { force: boolean }, fn: extendsCallback): void;
helpers: Helpers
child(opts?: ClientOptions): NewClientTypes
child(opts?: ClientOptions): Client
close(callback: Function): void;
close(): Promise<void>;
emit(event: string | symbol, ...args: any[]): boolean;
Expand Down Expand Up @@ -1471,4 +1476,22 @@ interface NewClientTypes {
}
}

export { NewClientTypes }
export * as estypes from './types'
export {
Client,
Transport,
ConnectionPool,
BaseConnectionPool,
CloudConnectionPool,
Connection,
Serializer,
events,
errors,
ApiError,
ApiResponse,
RequestEvent,
ResurrectEvent,
ClientOptions,
NodeOptions,
ClientExtendsCallbackOptions
};
33 changes: 30 additions & 3 deletions docs/typescript.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const response = await client.search<Source>({
The types are not 100% complete yet. Some APIs are missing (the newest ones, e.g. EQL),
and others may contain some errors, but we are continuously pushing fixes & improvements.

[discrete]
==== Request & Response types

Once you migrate to the new types, those are automatically integrated into the Elasticsearch client, you will get them out of the box.
If everything works, meaning that you won’t get compiler errors, you are good to go!
The types are already correct, and there is nothing more to do.
Expand All @@ -63,6 +66,30 @@ a `TODO` type, which accepts any object.

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

If needed you can import the request and response types.

[source,ts]
----
import { Client, estypes } from '@elastic/elasticsearch'
import type { Client as NewTypes } from '@elastic/elasticsearch/api/new'

// @ts-expect-error @elastic/elasticsearch
const client: NewTypes = new Client({
node: 'http://localhost:9200'
})

interface Source {
foo: string
}

const request: estypes.IndexRequest<Source> = {
index: 'test',
body: { foo: 'bar' }
}

await client.index(request)
----

[discrete]
===== How to migrate to the new type definitions

Expand All @@ -72,12 +99,12 @@ Following you will find a snippet that shows you how to override the default typ
[source,ts]
----
import { Client } from '@elastic/elasticsearch'
import type { NewClientTypes } from '@elastic/elasticsearch/api/new'
import type { Client as NewTypes } from '@elastic/elasticsearch/api/new'

// @ts-expect-error @elastic/elasticsearch
const client = new Client({
const client: NewTypes = new Client({
node: 'http://localhost:9200'
}) as NewClientTypes
})

interface Source {
foo: string
Expand Down
6 changes: 3 additions & 3 deletions test/types/new-types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

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

// @ts-expect-error
const client: NewClientTypes = new Client({
const client: NewTypes = new Client({
node: 'http://localhost:9200'
})

Expand Down Expand Up @@ -104,5 +104,5 @@ client.async_search.get()

// the child api should return a KibanaClient instance
const child = client.child()
expectType<NewClientTypes>(child)
expectType<NewTypes>(child)
expectNotType<Client>(child)