Expose the new type definition along with the current one (#1440)
This commit is contained in:
committed by
GitHub
parent
052e138fbd
commit
7fdfa4834f
@ -20,12 +20,57 @@
|
||||
import { expectType, expectError } from 'tsd'
|
||||
import { Readable as ReadableStream } from 'stream';
|
||||
import { TransportRequestCallback, Context } from '../../lib/Transport'
|
||||
import { Client, ApiError, estypes } from '../../'
|
||||
import { Client, ApiError } 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
|
||||
}
|
||||
@ -49,13 +94,13 @@ expectError(
|
||||
}
|
||||
})
|
||||
|
||||
expectType<estypes.SearchResponse<unknown>>(response.body)
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define only the source (promise style)
|
||||
// Define only the response body (promise style)
|
||||
{
|
||||
const response = await client.search<Source>({
|
||||
const response = await client.search<SearchResponse<Source>>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
@ -64,13 +109,28 @@ expectError(
|
||||
}
|
||||
})
|
||||
|
||||
expectType<estypes.SearchResponse<Source>>(response.body)
|
||||
expectType<SearchResponse<Source>>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define response body and request body (promise style)
|
||||
{
|
||||
const response = await client.search<SearchResponse<Source>, SearchBody>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
match: { foo: 'bar' }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
expectType<SearchResponse<Source>>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define response body, request body and the context (promise style)
|
||||
{
|
||||
const response = await client.search<Source, Context>({
|
||||
const response = await client.search<SearchResponse<Source>, SearchBody, Context>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
@ -79,7 +139,40 @@ expectError(
|
||||
}
|
||||
})
|
||||
|
||||
expectType<estypes.SearchResponse<Source>>(response.body)
|
||||
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<Context>(response.meta.context)
|
||||
}
|
||||
|
||||
@ -94,7 +187,7 @@ expectError(
|
||||
}
|
||||
}, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<estypes.SearchResponse<unknown>>(response.body)
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
@ -102,7 +195,7 @@ expectError(
|
||||
|
||||
// Define only the response body (callback style)
|
||||
{
|
||||
const result = client.search<Source>({
|
||||
const result = client.search<SearchResponse<Source>>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
@ -111,7 +204,24 @@ expectError(
|
||||
}
|
||||
}, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<estypes.SearchResponse<Source>>(response.body)
|
||||
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<Context>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
@ -119,7 +229,7 @@ expectError(
|
||||
|
||||
// Define response body, request body and the context (callback style)
|
||||
{
|
||||
const result = client.search<Source, Context>({
|
||||
const result = client.search<SearchResponse<Source>, SearchBody, Context>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
@ -128,7 +238,46 @@ expectError(
|
||||
}
|
||||
}, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<estypes.SearchResponse<Source>>(response.body)
|
||||
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<Context>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
|
||||
@ -19,7 +19,7 @@
|
||||
|
||||
import { expectType } from 'tsd'
|
||||
import { TransportRequestCallback, Context } from '../../lib/Transport'
|
||||
import { Client, ApiError, estypes } from '../../'
|
||||
import { Client, ApiError } from '../../'
|
||||
|
||||
const client = new Client({
|
||||
node: 'http://localhost:9200'
|
||||
@ -29,15 +29,23 @@ const client = new Client({
|
||||
{
|
||||
const response = await client.cat.count({ index: 'test' })
|
||||
|
||||
expectType<estypes.CatCountResponse>(response.body)
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define the context (promise style)
|
||||
// Define only the response body (promise style)
|
||||
{
|
||||
const response = await client.cat.count<string>({ index: 'test' })
|
||||
|
||||
expectType<estypes.CatCountResponse>(response.body)
|
||||
expectType<string>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define response body and the context (promise style)
|
||||
{
|
||||
const response = await client.cat.count<string, string>({ index: 'test' })
|
||||
|
||||
expectType<string>(response.body)
|
||||
expectType<string>(response.meta.context)
|
||||
}
|
||||
|
||||
@ -45,18 +53,28 @@ const client = new Client({
|
||||
{
|
||||
const result = client.cat.count({ index: 'test' }, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<estypes.CatCountResponse>(response.body)
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
|
||||
// Define the context (callback style)
|
||||
// Define only the response body (callback style)
|
||||
{
|
||||
const result = client.cat.count<string>({ index: 'test' }, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<estypes.CatCountResponse>(response.body)
|
||||
expectType<string>(response.meta.context)
|
||||
expectType<string>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
|
||||
// Define response body and the context (callback style)
|
||||
{
|
||||
const result = client.cat.count<string, Context>({ index: 'test' }, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<string>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
|
||||
@ -18,8 +18,8 @@
|
||||
*/
|
||||
|
||||
import { expectType } from 'tsd'
|
||||
import { Client, ApiError, ApiResponse, RequestEvent, ResurrectEvent, estypes } from '../../'
|
||||
import { TransportRequestCallback, TransportRequestPromise, Context } from '../../lib/Transport'
|
||||
import { Client, ApiError, ApiResponse, RequestEvent, ResurrectEvent } from '../../'
|
||||
import { TransportRequestCallback, TransportRequestPromise } from '../../lib/Transport'
|
||||
|
||||
const client = new Client({
|
||||
node: 'http://localhost:9200'
|
||||
@ -51,7 +51,7 @@ client.on('resurrect', (err, meta) => {
|
||||
{
|
||||
const result = client.info((err, result) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(result)
|
||||
expectType<ApiResponse>(result)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
expectType<void>(result.abort())
|
||||
@ -60,7 +60,7 @@ client.on('resurrect', (err, meta) => {
|
||||
{
|
||||
const result = client.info({ pretty: true }, (err, result) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(result)
|
||||
expectType<ApiResponse>(result)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
expectType<void>(result.abort())
|
||||
@ -69,7 +69,7 @@ client.on('resurrect', (err, meta) => {
|
||||
{
|
||||
const result = client.info({ pretty: true }, { ignore: [404] }, (err, result) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(result)
|
||||
expectType<ApiResponse>(result)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
expectType<void>(result.abort())
|
||||
@ -78,27 +78,27 @@ client.on('resurrect', (err, meta) => {
|
||||
// Promise style
|
||||
{
|
||||
const promise = client.info()
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse>>(promise)
|
||||
promise
|
||||
.then(result => expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(result))
|
||||
.then(result => expectType<ApiResponse>(result))
|
||||
.catch((err: ApiError) => expectType<ApiError>(err))
|
||||
expectType<void>(promise.abort())
|
||||
}
|
||||
|
||||
{
|
||||
const promise = client.info({ pretty: true })
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse>>(promise)
|
||||
promise
|
||||
.then(result => expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(result))
|
||||
.then(result => expectType<ApiResponse>(result))
|
||||
.catch((err: ApiError) => expectType<ApiError>(err))
|
||||
expectType<void>(promise.abort())
|
||||
}
|
||||
|
||||
{
|
||||
const promise = client.info({ pretty: true }, { ignore: [404] })
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse>>(promise)
|
||||
promise
|
||||
.then(result => expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(result))
|
||||
.then(result => expectType<ApiResponse>(result))
|
||||
.catch((err: ApiError) => expectType<ApiError>(err))
|
||||
expectType<void>(promise.abort())
|
||||
}
|
||||
@ -106,10 +106,10 @@ client.on('resurrect', (err, meta) => {
|
||||
// Promise style with async await
|
||||
{
|
||||
const promise = client.info()
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse>>(promise)
|
||||
expectType<void>(promise.abort())
|
||||
try {
|
||||
expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(await promise)
|
||||
expectType<ApiResponse>(await promise)
|
||||
} catch (err) {
|
||||
expectType<any>(err)
|
||||
}
|
||||
@ -117,10 +117,10 @@ client.on('resurrect', (err, meta) => {
|
||||
|
||||
{
|
||||
const promise = client.info({ pretty: true })
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse>>(promise)
|
||||
expectType<void>(promise.abort())
|
||||
try {
|
||||
expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(await promise)
|
||||
expectType<ApiResponse>(await promise)
|
||||
} catch (err) {
|
||||
expectType<any>(err)
|
||||
}
|
||||
@ -128,10 +128,10 @@ client.on('resurrect', (err, meta) => {
|
||||
|
||||
{
|
||||
const promise = client.info({ pretty: true }, { ignore: [404] })
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse>>(promise)
|
||||
expectType<void>(promise.abort())
|
||||
try {
|
||||
expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(await promise)
|
||||
expectType<ApiResponse>(await promise)
|
||||
} catch (err) {
|
||||
expectType<any>(err)
|
||||
}
|
||||
|
||||
@ -101,4 +101,4 @@ const response = {
|
||||
expectType<string>(err.name)
|
||||
expectType<string>(err.message)
|
||||
expectType<ApiResponse>(err.meta)
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,6 +22,7 @@ import { Client, RequestEvent, ResurrectEvent, ApiError, ApiResponse, estypes }
|
||||
import { KibanaClient } from '../../api/kibana'
|
||||
import { TransportRequestPromise, Context } from '../../lib/Transport'
|
||||
|
||||
// @ts-expect-error
|
||||
const client: KibanaClient = new Client({
|
||||
node: 'http://localhost:9200'
|
||||
})
|
||||
@ -112,4 +113,4 @@ expectError(client.close(() => {}))
|
||||
// the child api should return a KibanaClient instance
|
||||
const child = client.child()
|
||||
expectType<KibanaClient>(child)
|
||||
expectNotType<Client>(child)
|
||||
expectNotType<Client>(child)
|
||||
108
test/types/new-types.test-d.ts
Normal file
108
test/types/new-types.test-d.ts
Normal file
@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Licensed to Elasticsearch B.V. under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch B.V. licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
import { expectType, expectNotType, expectError } from 'tsd'
|
||||
import { Client, RequestEvent, ResurrectEvent, ApiError, ApiResponse, estypes } from '../../'
|
||||
import { NewClientTypes } from '../../api/new'
|
||||
import { TransportRequestPromise, Context } from '../../lib/Transport'
|
||||
|
||||
// @ts-expect-error
|
||||
const client: NewClientTypes = new Client({
|
||||
node: 'http://localhost:9200'
|
||||
})
|
||||
|
||||
client.on('request', (err, meta) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<RequestEvent>(meta)
|
||||
})
|
||||
|
||||
client.on('response', (err, meta) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<RequestEvent>(meta)
|
||||
})
|
||||
|
||||
client.on('sniff', (err, meta) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<RequestEvent>(meta)
|
||||
})
|
||||
|
||||
client.on('resurrect', (err, meta) => {
|
||||
expectType<null>(err)
|
||||
expectType<ResurrectEvent>(meta)
|
||||
})
|
||||
|
||||
// No generics
|
||||
{
|
||||
const response = await client.cat.count({ index: 'test' })
|
||||
|
||||
expectType<estypes.CatCountResponse>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define only the context
|
||||
{
|
||||
const response = await client.cat.count<string>({ index: 'test' })
|
||||
|
||||
expectType<estypes.CatCountResponse>(response.body)
|
||||
expectType<string>(response.meta.context)
|
||||
}
|
||||
|
||||
// Check API returned type and optional parameters
|
||||
{
|
||||
const promise = client.info()
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
promise
|
||||
.then(result => expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(result))
|
||||
.catch((err: ApiError) => expectType<ApiError>(err))
|
||||
expectType<void>(promise.abort())
|
||||
}
|
||||
|
||||
{
|
||||
const promise = client.info({ pretty: true })
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
promise
|
||||
.then(result => expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(result))
|
||||
.catch((err: ApiError) => expectType<ApiError>(err))
|
||||
expectType<void>(promise.abort())
|
||||
}
|
||||
|
||||
{
|
||||
const promise = client.info({ pretty: true }, { ignore: [404] })
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
promise
|
||||
.then(result => expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(result))
|
||||
.catch((err: ApiError) => expectType<ApiError>(err))
|
||||
expectType<void>(promise.abort())
|
||||
}
|
||||
|
||||
// body that does not respect the RequestBody constraint
|
||||
expectError(
|
||||
client.search({
|
||||
index: 'hello',
|
||||
body: 42
|
||||
}).then(console.log)
|
||||
)
|
||||
|
||||
// @ts-expect-error
|
||||
client.async_search.get()
|
||||
|
||||
// the child api should return a KibanaClient instance
|
||||
const child = client.child()
|
||||
expectType<NewClientTypes>(child)
|
||||
expectNotType<Client>(child)
|
||||
Reference in New Issue
Block a user