Improve typings (#813)

The ApiResponse now accepts a generics that defaults to any, same for every API method that might need a body.
This commit is contained in:
Tomas Della Vedova
2019-04-10 11:40:25 +02:00
committed by delvedor
parent 48233503f7
commit 48c6ad15a6
7 changed files with 832 additions and 171 deletions

View File

@ -22,6 +22,7 @@
import {
Client,
ApiResponse,
RequestParams,
RequestEvent,
ResurrectEvent,
events,
@ -94,6 +95,40 @@ client.index({
.then((result: ApiResponse) => {})
.catch((err: Error) => {})
// --- Use generics ---
// Define the search parameters
interface SearchBody {
query: {
match: { foo: string }
}
}
const searchParams: RequestParams.Search<SearchBody> = {
index: 'test',
body: {
query: {
match: { foo: 'bar' }
}
}
}
// Dewfine the interface of the search response
interface SearchResponse<T> {
hits: {
hits: Array<{
_source: T;
}>
}
}
// Define the intefrace of the source object
interface Source {
foo: string
}
client.search(searchParams)
.then((response: ApiResponse<SearchResponse<Source>>) => console.log(response))
.catch((err: Error) => {})
// extend client
client.extend('namespace.method', (options: ClientExtendsCallbackOptions) => {
return function (params: any) {