Skip to content

Commit f6677c0

Browse files
authored
Updated TypeScript docs (#1144)
1 parent 6779f3b commit f6677c0

File tree

1 file changed

+21
-60
lines changed

1 file changed

+21
-60
lines changed

docs/typescript.asciidoc

Lines changed: 21 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[[typescript]]
2+
23
== TypeScript support
34

45
The client offers a first-class support for TypeScript, since it ships the type
@@ -7,76 +8,36 @@ definitions for every exposed API.
78
NOTE: If you are using TypeScript you will be required to use _snake_case_ style
89
to define the API parameters instead of _camelCase_.
910

10-
Other than the types for the surface API, the client offers the types for every
11-
request method, via the `RequestParams`, if you need the types for a search
12-
request for instance, you can access them via `RequestParams.Search`.
13-
Every API that supports a body, accepts a
14-
https://www.typescriptlang.org/docs/handbook/generics.html[generics] which
15-
represents the type of the request body, if you don't configure anything, it
16-
will default to `RequestBody`. +
17-
`RequestBody`, along with `RequestNDBody` and `ResponseBody` are defined inside the client, and it looks like this:
11+
By default event API uses https://www.typescriptlang.org/docs/handbook/generics.html[generics] to specify the requets and response bodies and the `meta.context`. Currently we can't provide those definitions, but we are working to improve this situation.
12+
13+
You can't fid a partial definition of the request types by importing `RequestParams`, which it is used by default in the client and accepts a body (when needed) as a generic to provide a better specification.
14+
15+
The body defaults to `RequestBody` and `RequestNDBody`, which are defined as follows:
16+
1817
[source,ts]
1918
----
20-
type RequestBody<T = Record<string, any>> = T | string | Buffer | ReadableStream
21-
type RequestNDBody<T = Record<string, any>[]> = T | string[] | Buffer | ReadableStream
22-
type ResponseBody<T = Record<string, any>> = T | string | boolean | ReadableStream
19+
type RequestBody<T = Record<string, any>> = T | string | Buffer | ReadableStream
20+
type RequestNDBody<T = Record<string, any>[]> = T | string | string[] | Buffer | ReadableStream
2321
----
2422

25-
For example:
23+
You can specify the response and request body in each API as follows:
2624

2725
[source,ts]
2826
----
29-
import { RequestParams } from '@elastic/elasticsearch'
30-
31-
interface SearchBody {
32-
query: {
33-
match: { foo: string }
34-
}
35-
}
36-
37-
const searchParams: RequestParams.Search<SearchBody> = {
38-
index: 'test',
39-
body: {
40-
query: {
41-
match: { foo: 'bar' }
42-
}
43-
}
44-
}
45-
46-
// This is valid as well
47-
const searchParams: RequestParams.Search = {
27+
const response = await client.search<ResponseBody, RequestBody, Context>({
4828
index: 'test',
4929
body: {
5030
query: {
5131
match: { foo: 'bar' }
5232
}
5333
}
54-
}
55-
----
34+
})
5635
57-
You can find the type definiton of a response in `ApiResponse`, which accepts a
58-
generics as well if you want to specify the body type, otherwise it defaults to
59-
`BodyType`.
60-
61-
[source,ts]
36+
console.log(response.body)
6237
----
63-
interface SearchResponse<T> {
64-
hits: {
65-
hits: Array<{
66-
_source: T;
67-
}>
68-
}
69-
}
7038

71-
// Define the interface of the source object
72-
interface Source {
73-
foo: string
74-
}
39+
You don't have to specify all the generics, but the order must be respected.
7540

76-
client.search(searchParams)
77-
.then((response: ApiResponse<SearchResponse<Source>>) => console.log(response))
78-
.catch((err: Error) => {})
79-
----
8041

8142
=== A complete example
8243

@@ -146,7 +107,7 @@ interface Source {
146107
147108
async function run () {
148109
// All of the examples below are valid code, by default,
149-
// the request body will be `RequestBody` and response will be `ResponseBody`.
110+
// the request body will be `RequestBody` and response will be `Record<string, any>`.
150111
const response = await client.search({
151112
index: 'test',
152113
body: {
@@ -158,20 +119,20 @@ async function run () {
158119
// body here is `ResponseBody`
159120
console.log(response.body)
160121
161-
// The first generic is the request body
162-
const response = await client.search<SearchBody>({
122+
// The first generic is the response body
123+
const response = await client.search<SearchResponse<Source>>({
163124
index: 'test',
164-
// Here the body must follow the `SearchBody` interface
125+
// Here the body must follow the `RequestBody` interface
165126
body: {
166127
query: {
167128
match: { foo: 'bar' }
168129
}
169130
}
170131
})
171-
// body here is `ResponseBody`
132+
// body here is `SearchResponse<Source>`
172133
console.log(response.body)
173134
174-
const response = await client.search<SearchBody, SearchResponse<Source>>({
135+
const response = await client.search<SearchResponse<Source>, SearchBody>({
175136
index: 'test',
176137
// Here the body must follow the `SearchBody` interface
177138
body: {
@@ -180,7 +141,7 @@ async function run () {
180141
}
181142
}
182143
})
183-
// Now you can have full type definition of the response
144+
// body here is `SearchResponse<Source>`
184145
console.log(response.body)
185146
}
186147

0 commit comments

Comments
 (0)