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
This commit is contained in:
committed by
GitHub
parent
a80f510a9a
commit
6c82a4967e
@ -13,7 +13,14 @@ request for instance, you can access them via `RequestParams.Search`.
|
||||
Every API that supports a body, accepts a
|
||||
https://www.typescriptlang.org/docs/handbook/generics.html[generics] which
|
||||
represents the type of the request body, if you don't configure anything, it
|
||||
will default to `any`.
|
||||
will default to `RequestBody`. +
|
||||
`RequestBody`, along with `RequestNDBody` and `ResponseBody` are defined inside the client, and it looks like this:
|
||||
[source,ts]
|
||||
----
|
||||
type RequestBody<T = Record<string, any>> = T | string | Buffer | ReadableStream
|
||||
type RequestNDBody<T = Record<string, any>[]> = T | string[] | Buffer | ReadableStream
|
||||
type ResponseBody<T = Record<string, any>> = T | string | boolean | ReadableStream
|
||||
----
|
||||
|
||||
For example:
|
||||
|
||||
@ -49,7 +56,7 @@ const searchParams: RequestParams.Search = {
|
||||
|
||||
You can find the type definiton of a response in `ApiResponse`, which accepts a
|
||||
generics as well if you want to specify the body type, otherwise it defaults to
|
||||
`any`.
|
||||
`BodyType`.
|
||||
|
||||
[source,ts]
|
||||
----
|
||||
@ -137,19 +144,43 @@ interface Source {
|
||||
foo: string
|
||||
}
|
||||
|
||||
async function run (): Promise<void> {
|
||||
// Define the search parameters
|
||||
const searchParams: RequestParams.Search<SearchBody> = {
|
||||
async function run () {
|
||||
// All of the examples below are valid code, by default,
|
||||
// the request body will be `RequestBody` and response will be `ResponseBody`.
|
||||
const response = await client.search({
|
||||
index: 'test',
|
||||
body: {
|
||||
query: {
|
||||
match: { foo: 'bar' }
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
// body here is `ResponseBody`
|
||||
console.log(response.body)
|
||||
|
||||
// Craft the final type definition
|
||||
const response: ApiResponse<SearchResponse<Source>> = await client.search(searchParams)
|
||||
// The first generic is the request body
|
||||
const response = await client.search<SearchBody>({
|
||||
index: 'test',
|
||||
// Here the body must follow the `SearchBody` interface
|
||||
body: {
|
||||
query: {
|
||||
match: { foo: 'bar' }
|
||||
}
|
||||
}
|
||||
})
|
||||
// body here is `ResponseBody`
|
||||
console.log(response.body)
|
||||
|
||||
const response = await client.search<SearchBody, SearchResponse<Source>>({
|
||||
index: 'test',
|
||||
// Here the body must follow the `SearchBody` interface
|
||||
body: {
|
||||
query: {
|
||||
match: { foo: 'bar' }
|
||||
}
|
||||
}
|
||||
})
|
||||
// Now you can have full type definition of the response
|
||||
console.log(response.body)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user