Add support for a global context option (#1256)

This commit is contained in:
Tomas Della Vedova
2020-07-13 14:36:53 +02:00
committed by GitHub
parent 1a7727588e
commit 39cf023426
16 changed files with 2971 additions and 2851 deletions

View File

@ -4,7 +4,7 @@
import { expectType, expectError } from 'tsd'
import { Readable as ReadableStream } from 'stream';
import { TransportRequestCallback } from '../../lib/Transport'
import { TransportRequestCallback, Context } from '../../lib/Transport'
import { Client, ApiError } from '../../'
const client = new Client({
@ -80,7 +80,7 @@ expectError(
})
expectType<Record<string, any>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
}
// Define only the response body (promise style)
@ -95,7 +95,7 @@ expectError(
})
expectType<SearchResponse<Source>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
}
// Define response body and request body (promise style)
@ -110,12 +110,12 @@ expectError(
})
expectType<SearchResponse<Source>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
}
// Define response body, request body and the context (promise style)
{
const response = await client.search<SearchResponse<Source>, SearchBody, string>({
const response = await client.search<SearchResponse<Source>, SearchBody, Context>({
index: 'test',
body: {
query: {
@ -125,7 +125,7 @@ expectError(
})
expectType<SearchResponse<Source>>(response.body)
expectType<string>(response.meta.context)
expectType<Context>(response.meta.context)
}
// Send request body as string (promise style)
@ -136,7 +136,7 @@ expectError(
})
expectType<Record<string, any>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
}
// Send request body as buffer (promise style)
@ -147,7 +147,7 @@ expectError(
})
expectType<Record<string, any>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
}
// Send request body as readable stream (promise style)
@ -158,7 +158,7 @@ expectError(
})
expectType<Record<string, any>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
}
// No generics (callback style)
@ -173,7 +173,7 @@ expectError(
}, (err, response) => {
expectType<ApiError>(err)
expectType<Record<string, any>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}
@ -190,7 +190,7 @@ expectError(
}, (err, response) => {
expectType<ApiError>(err)
expectType<SearchResponse<Source>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}
@ -207,14 +207,14 @@ expectError(
}, (err, response) => {
expectType<ApiError>(err)
expectType<SearchResponse<Source>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}
// Define response body, request body and the context (callback style)
{
const result = client.search<SearchResponse<Source>, SearchBody, string>({
const result = client.search<SearchResponse<Source>, SearchBody, Context>({
index: 'test',
body: {
query: {
@ -224,7 +224,7 @@ expectError(
}, (err, response) => {
expectType<ApiError>(err)
expectType<SearchResponse<Source>>(response.body)
expectType<string>(response.meta.context)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}
@ -237,7 +237,7 @@ expectError(
}, (err, response) => {
expectType<ApiError>(err)
expectType<Record<string, any>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}
@ -250,7 +250,7 @@ expectError(
}, (err, response) => {
expectType<ApiError>(err)
expectType<Record<string, any>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}
@ -263,7 +263,7 @@ expectError(
}, (err, response) => {
expectType<ApiError>(err)
expectType<Record<string, any>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}
}

View File

@ -3,7 +3,7 @@
// See the LICENSE file in the project root for more information
import { expectType } from 'tsd'
import { TransportRequestCallback } from '../../lib/Transport'
import { TransportRequestCallback, Context } from '../../lib/Transport'
import { Client, ApiError } from '../../'
const client = new Client({
@ -15,7 +15,7 @@ const client = new Client({
const response = await client.cat.count({ index: 'test' })
expectType<Record<string, any>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
}
// Define only the response body (promise style)
@ -23,7 +23,7 @@ const client = new Client({
const response = await client.cat.count<string>({ index: 'test' })
expectType<string>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
}
// Define response body and the context (promise style)
@ -39,7 +39,7 @@ const client = new Client({
const result = client.cat.count({ index: 'test' }, (err, response) => {
expectType<ApiError>(err)
expectType<Record<string, any>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}
@ -49,17 +49,17 @@ const client = new Client({
const result = client.cat.count<string>({ index: 'test' }, (err, response) => {
expectType<ApiError>(err)
expectType<string>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}
// Define response body and the context (callback style)
{
const result = client.cat.count<string, string>({ index: 'test' }, (err, response) => {
const result = client.cat.count<string, Context>({ index: 'test' }, (err, response) => {
expectType<ApiError>(err)
expectType<string>(response.body)
expectType<string>(response.meta.context)
expectType<Context>(response.meta.context)
})
expectType<TransportRequestCallback>(result)
}

View File

@ -623,3 +623,20 @@ expectError<errors.ConfigurationError>(
})
)
}
/**
* `context` option
*/
expectType<Client>(
new Client({
node: 'http://localhost:9200',
context: { hello: 'world' }
})
)
expectError<errors.ConfigurationError>(
new Client({
node: 'http://localhost:9200',
context: 'hello world'
})
)

View File

@ -12,7 +12,7 @@ import {
OnDropDocument,
MsearchHelper
} from '../../lib/Helpers'
import { ApiResponse, ApiError } from '../../lib/Transport'
import { ApiResponse, ApiError, Context } from '../../lib/Transport'
const client = new Client({
node: 'http://localhost:9200'
@ -134,7 +134,7 @@ expectError(
expectAssignable<ScrollSearchResponse>(response)
expectType<Record<string, any>>(response.body)
expectType<unknown[]>(response.documents)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
}
}
}
@ -198,7 +198,7 @@ expectError(
expectAssignable<ScrollSearchResponse>(response)
expectType<SearchResponse<Source>>(response.body)
expectType<Source[]>(response.documents)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
}
}
}
@ -254,7 +254,7 @@ expectError(
}
async function test () {
const scrollSearch = client.helpers.scrollSearch<Source, SearchResponse<Source>, SearchBody, string>({
const scrollSearch = client.helpers.scrollSearch<Source, SearchResponse<Source>, SearchBody, Record<string, unknown>>({
index: 'test',
body: {
query: {
@ -267,7 +267,7 @@ expectError(
expectAssignable<ScrollSearchResponse>(response)
expectType<SearchResponse<Source>>(response.body)
expectType<Source[]>(response.documents)
expectType<string>(response.meta.context)
expectType<Record<string, unknown>>(response.meta.context)
}
}
}

View File

@ -5,7 +5,7 @@
import { expectType, expectNotType, expectError } from 'tsd'
import { Client, RequestEvent, ResurrectEvent, ApiError, ApiResponse } from '../../'
import { KibanaClient } from '../../api/kibana'
import { TransportRequestPromise } from '../../lib/Transport'
import { TransportRequestPromise, Context } from '../../lib/Transport'
const client: KibanaClient = new Client({
node: 'http://localhost:9200'
@ -36,7 +36,7 @@ client.on('resurrect', (err, meta) => {
const response = await client.cat.count({ index: 'test' })
expectType<Record<string, any>>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
}
// Define only the response body
@ -44,7 +44,7 @@ client.on('resurrect', (err, meta) => {
const response = await client.cat.count<string>({ index: 'test' })
expectType<string>(response.body)
expectType<unknown>(response.meta.context)
expectType<Context>(response.meta.context)
}
// Define response body and the context