New type definitions (#1358)
This commit is contained in:
committed by
GitHub
parent
430de17007
commit
f86a79cb26
@ -20,57 +20,12 @@
|
||||
import { expectType, expectError } from 'tsd'
|
||||
import { Readable as ReadableStream } from 'stream';
|
||||
import { TransportRequestCallback, Context } from '../../lib/Transport'
|
||||
import { Client, ApiError } from '../../'
|
||||
import { Client, ApiError, estypes } 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
|
||||
}
|
||||
@ -94,13 +49,13 @@ expectError(
|
||||
}
|
||||
})
|
||||
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<estypes.SearchResponse<unknown>>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define only the response body (promise style)
|
||||
// Define only the source (promise style)
|
||||
{
|
||||
const response = await client.search<SearchResponse<Source>>({
|
||||
const response = await client.search<Source>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
@ -109,28 +64,13 @@ expectError(
|
||||
}
|
||||
})
|
||||
|
||||
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<estypes.SearchResponse<Source>>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define response body, request body and the context (promise style)
|
||||
{
|
||||
const response = await client.search<SearchResponse<Source>, SearchBody, Context>({
|
||||
const response = await client.search<Source, Context>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
@ -139,40 +79,7 @@ expectError(
|
||||
}
|
||||
})
|
||||
|
||||
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<estypes.SearchResponse<Source>>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
}
|
||||
|
||||
@ -187,7 +94,7 @@ expectError(
|
||||
}
|
||||
}, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<estypes.SearchResponse<unknown>>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
@ -195,7 +102,7 @@ expectError(
|
||||
|
||||
// Define only the response body (callback style)
|
||||
{
|
||||
const result = client.search<SearchResponse<Source>>({
|
||||
const result = client.search<Source>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
@ -204,24 +111,7 @@ expectError(
|
||||
}
|
||||
}, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
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<estypes.SearchResponse<Source>>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
@ -229,7 +119,7 @@ expectError(
|
||||
|
||||
// Define response body, request body and the context (callback style)
|
||||
{
|
||||
const result = client.search<SearchResponse<Source>, SearchBody, Context>({
|
||||
const result = client.search<Source, Context>({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
@ -238,46 +128,7 @@ expectError(
|
||||
}
|
||||
}, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
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<estypes.SearchResponse<Source>>(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 } from '../../'
|
||||
import { Client, ApiError, estypes } from '../../'
|
||||
|
||||
const client = new Client({
|
||||
node: 'http://localhost:9200'
|
||||
@ -29,23 +29,15 @@ const client = new Client({
|
||||
{
|
||||
const response = await client.cat.count({ index: 'test' })
|
||||
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<estypes.CatCountResponse>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define only the response body (promise style)
|
||||
// Define the context (promise style)
|
||||
{
|
||||
const response = await client.cat.count<string>({ index: 'test' })
|
||||
|
||||
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<estypes.CatCountResponse>(response.body)
|
||||
expectType<string>(response.meta.context)
|
||||
}
|
||||
|
||||
@ -53,28 +45,18 @@ const client = new Client({
|
||||
{
|
||||
const result = client.cat.count({ index: 'test' }, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<estypes.CatCountResponse>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
|
||||
// Define only the response body (callback style)
|
||||
// Define the context (callback style)
|
||||
{
|
||||
const result = client.cat.count<string>({ index: 'test' }, (err, response) => {
|
||||
expectType<ApiError>(err)
|
||||
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<estypes.CatCountResponse>(response.body)
|
||||
expectType<string>(response.meta.context)
|
||||
})
|
||||
expectType<TransportRequestCallback>(result)
|
||||
}
|
||||
|
||||
@ -669,14 +669,6 @@ expectType<Client>(
|
||||
})
|
||||
)
|
||||
|
||||
expectError<errors.ConfigurationError>(
|
||||
// @ts-expect-error
|
||||
new Client({
|
||||
node: 'http://localhost:9200',
|
||||
context: 'hello world'
|
||||
})
|
||||
)
|
||||
|
||||
/**
|
||||
* `proxy` option
|
||||
*/
|
||||
|
||||
@ -18,8 +18,8 @@
|
||||
*/
|
||||
|
||||
import { expectType } from 'tsd'
|
||||
import { Client, ApiError, ApiResponse, RequestEvent, ResurrectEvent } from '../../'
|
||||
import { TransportRequestCallback, TransportRequestPromise } from '../../lib/Transport'
|
||||
import { Client, ApiError, ApiResponse, RequestEvent, ResurrectEvent, estypes } from '../../'
|
||||
import { TransportRequestCallback, TransportRequestPromise, Context } 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>(result)
|
||||
expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(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>(result)
|
||||
expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(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>(result)
|
||||
expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(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>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
promise
|
||||
.then(result => expectType<ApiResponse>(result))
|
||||
.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>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
promise
|
||||
.then(result => expectType<ApiResponse>(result))
|
||||
.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>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
promise
|
||||
.then(result => expectType<ApiResponse>(result))
|
||||
.then(result => expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(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>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
expectType<void>(promise.abort())
|
||||
try {
|
||||
expectType<ApiResponse>(await promise)
|
||||
expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(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>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
expectType<void>(promise.abort())
|
||||
try {
|
||||
expectType<ApiResponse>(await promise)
|
||||
expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(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>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
expectType<void>(promise.abort())
|
||||
try {
|
||||
expectType<ApiResponse>(await promise)
|
||||
expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(await promise)
|
||||
} catch (err) {
|
||||
expectType<any>(err)
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
*/
|
||||
|
||||
import { expectType, expectNotType, expectError } from 'tsd'
|
||||
import { Client, RequestEvent, ResurrectEvent, ApiError, ApiResponse } from '../../'
|
||||
import { Client, RequestEvent, ResurrectEvent, ApiError, ApiResponse, estypes } from '../../'
|
||||
import { KibanaClient } from '../../api/kibana'
|
||||
import { TransportRequestPromise, Context } from '../../lib/Transport'
|
||||
|
||||
@ -50,50 +50,42 @@ client.on('resurrect', (err, meta) => {
|
||||
{
|
||||
const response = await client.cat.count({ index: 'test' })
|
||||
|
||||
expectType<Record<string, any>>(response.body)
|
||||
expectType<estypes.CatCountResponse>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define only the response body
|
||||
// Define only the context
|
||||
{
|
||||
const response = await client.cat.count<string>({ index: 'test' })
|
||||
|
||||
expectType<string>(response.body)
|
||||
expectType<Context>(response.meta.context)
|
||||
}
|
||||
|
||||
// Define response body and the context
|
||||
{
|
||||
const response = await client.cat.count<string, string>({ index: 'test' })
|
||||
|
||||
expectType<string>(response.body)
|
||||
expectType<estypes.CatCountResponse>(response.body)
|
||||
expectType<string>(response.meta.context)
|
||||
}
|
||||
|
||||
// Check API returned type and optional parameters
|
||||
{
|
||||
const promise = client.info()
|
||||
expectType<TransportRequestPromise<ApiResponse>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
promise
|
||||
.then(result => expectType<ApiResponse>(result))
|
||||
.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>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
promise
|
||||
.then(result => expectType<ApiResponse>(result))
|
||||
.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>>(promise)
|
||||
expectType<TransportRequestPromise<ApiResponse<estypes.RootNodeInfoResponse, Context>>>(promise)
|
||||
promise
|
||||
.then(result => expectType<ApiResponse>(result))
|
||||
.then(result => expectType<ApiResponse<estypes.RootNodeInfoResponse, Context>>(result))
|
||||
.catch((err: ApiError) => expectType<ApiError>(err))
|
||||
expectType<void>(promise.abort())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user