Files
elasticsearch-js/test/types/client.test-d.ts
Tomas Della Vedova 6c82a4967e Refactored type definitions (#1119)
* Updated types generation script

* Refactored api method definitions

* Updated test
- Removed old test code
- Added tsd dev dependency
- Rewritten test with tsd

* Removed unused dependencies

* Fixed definition

* Updated test

* Updated docs

* Improved events type definitions

* Updated test

* Minor fixes in the type definitons

* More type test

* Improved Transport type definitions

* Updated test

* Addressed comments

* Code generation

* Use RequestBody, Response and Context everywhere, also default Context to unknown

* Updated test

* body -> hasBody

* Fixed conflicts

* Updated code generation

* Improved request body type definition

* Updated code generation

* Use BodyType for both request and reponses generics
- Use extends for defining the RequestBody generic to force the user
  following the same shape.
- BodyType and NDBodyType now accepts a generics to allow injecting
  more specific types in the future

* API generation

* Updated test

* Updated docs

* Use BodyType also in ReponseError

* Removed useless client generics

* Renamed generics and types
- prefixed all generics with a T
- BodyType => RequestBody
- NDBodyType => RequestNDBody
- Added ResponseBody

* Updated test

* Updated docs

* Test ResponseBody as well

* Simplify overloads

* API generation

* Updated test

* Updated error types
2020-03-23 11:38:18 +01:00

114 lines
2.7 KiB
TypeScript

// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
import { expectType } from 'tsd'
import { Client, ApiError, ApiResponse, RequestEvent, ResurrectEvent } from '../../'
import { TransportRequestCallback } from '../..//lib/Transport';
const client = 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)
})
// Test all overloads
// Callbacks style
{
const result = client.info((err, result) => {
expectType<ApiError>(err)
expectType<ApiResponse>(result)
})
expectType<TransportRequestCallback>(result)
}
{
const result = client.info({ pretty: true }, (err, result) => {
expectType<ApiError>(err)
expectType<ApiResponse>(result)
})
expectType<TransportRequestCallback>(result)
}
{
const result = client.info({ pretty: true }, { ignore: [404] }, (err, result) => {
expectType<ApiError>(err)
expectType<ApiResponse>(result)
})
expectType<TransportRequestCallback>(result)
}
// Promise style
{
const promise = client.info()
expectType<Promise<ApiResponse>>(promise)
promise
.then(result => expectType<ApiResponse>(result))
.catch((err: ApiError) => expectType<ApiError>(err))
}
{
const promise = client.info({ pretty: true })
expectType<Promise<ApiResponse>>(promise)
promise
.then(result => expectType<ApiResponse>(result))
.catch((err: ApiError) => expectType<ApiError>(err))
}
{
const promise = client.info({ pretty: true }, { ignore: [404] })
expectType<Promise<ApiResponse>>(promise)
promise
.then(result => expectType<ApiResponse>(result))
.catch((err: ApiError) => expectType<ApiError>(err))
}
// Promise style with async await
{
const promise = client.info()
expectType<Promise<ApiResponse>>(promise)
try {
expectType<ApiResponse>(await promise)
} catch (err) {
expectType<any>(err)
}
}
{
const promise = client.info({ pretty: true })
expectType<Promise<ApiResponse>>(promise)
try {
expectType<ApiResponse>(await promise)
} catch (err) {
expectType<any>(err)
}
}
{
const promise = client.info({ pretty: true }, { ignore: [404] })
expectType<Promise<ApiResponse>>(promise)
try {
expectType<ApiResponse>(await promise)
} catch (err) {
expectType<any>(err)
}
}