From 4ab53eee4b4726fe2bf110f15f381dffa1a699ee Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 15 Apr 2021 08:44:37 +0200 Subject: [PATCH] Updated new types export (#1446) (#1447) * Updated new types * Updated new types * Updated test * Updated docs Co-authored-by: Tomas Della Vedova --- api/new.d.ts | 31 +++++++++++++++++++++++++++---- docs/typescript.asciidoc | 33 ++++++++++++++++++++++++++++++--- test/types/new-types.test-d.ts | 6 +++--- 3 files changed, 60 insertions(+), 10 deletions(-) diff --git a/api/new.d.ts b/api/new.d.ts index 74432ba99..dfcbf3efc 100644 --- a/api/new.d.ts +++ b/api/new.d.ts @@ -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 { @@ -61,14 +66,14 @@ declare type extendsCallback = (options: ClientExtendsCallbackOptions) => any; // /Extend API declare type callbackFn = (err: ApiError, result: ApiResponse) => 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; emit(event: string | symbol, ...args: any[]): boolean; @@ -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 +}; diff --git a/docs/typescript.asciidoc b/docs/typescript.asciidoc index ed3a206ad..177676a91 100644 --- a/docs/typescript.asciidoc +++ b/docs/typescript.asciidoc @@ -50,6 +50,9 @@ const response = await client.search({ 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. @@ -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 = { + index: 'test', + body: { foo: 'bar' } +} + +await client.index(request) +---- + [discrete] ===== How to migrate to the new type definitions @@ -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 diff --git a/test/types/new-types.test-d.ts b/test/types/new-types.test-d.ts index 63a9f33ed..9d35666a5 100644 --- a/test/types/new-types.test-d.ts +++ b/test/types/new-types.test-d.ts @@ -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' }) @@ -104,5 +104,5 @@ client.async_search.get() // the child api should return a KibanaClient instance const child = client.child() -expectType(child) +expectType(child) expectNotType(child) \ No newline at end of file