20
20
import { expectType , expectError } from 'tsd'
21
21
import { Readable as ReadableStream } from 'stream' ;
22
22
import { TransportRequestCallback , Context } from '../../lib/Transport'
23
- import { Client , ApiError } from '../../'
23
+ import { Client , ApiError , estypes } from '../../'
24
24
25
25
const client = new Client ( {
26
26
node : 'http://localhost:9200'
27
27
} )
28
28
29
- interface SearchBody {
30
- query : {
31
- match : { foo : string }
32
- }
33
- }
34
-
35
- interface ShardsResponse {
36
- total : number ;
37
- successful : number ;
38
- failed : number ;
39
- skipped : number ;
40
- }
41
-
42
- interface Explanation {
43
- value : number ;
44
- description : string ;
45
- details : Explanation [ ] ;
46
- }
47
-
48
- interface SearchResponse < T > {
49
- took : number ;
50
- timed_out : boolean ;
51
- _scroll_id ?: string ;
52
- _shards : ShardsResponse ;
53
- hits : {
54
- total : number ;
55
- max_score : number ;
56
- hits : Array < {
57
- _index : string ;
58
- _type : string ;
59
- _id : string ;
60
- _score : number ;
61
- _source : T ;
62
- _version ?: number ;
63
- _explanation ?: Explanation ;
64
- fields ?: any ;
65
- highlight ?: any ;
66
- inner_hits ?: any ;
67
- matched_queries ?: string [ ] ;
68
- sort ?: string [ ] ;
69
- } > ;
70
- } ;
71
- aggregations ?: any ;
72
- }
73
-
74
29
interface Source {
75
30
foo : string
76
31
}
@@ -94,28 +49,13 @@ expectError(
94
49
}
95
50
} )
96
51
97
- expectType < Record < string , any > > ( response . body )
98
- expectType < Context > ( response . meta . context )
99
- }
100
-
101
- // Define only the response body (promise style)
102
- {
103
- const response = await client . search < SearchResponse < Source > > ( {
104
- index : 'test' ,
105
- body : {
106
- query : {
107
- match : { foo : 'bar' }
108
- }
109
- }
110
- } )
111
-
112
- expectType < SearchResponse < Source > > ( response . body )
52
+ expectType < estypes . SearchResponse < unknown > > ( response . body )
113
53
expectType < Context > ( response . meta . context )
114
54
}
115
55
116
- // Define response body and request body (promise style)
56
+ // Define only the source (promise style)
117
57
{
118
- const response = await client . search < SearchResponse < Source > , SearchBody > ( {
58
+ const response = await client . search < Source > ( {
119
59
index : 'test' ,
120
60
body : {
121
61
query : {
@@ -124,13 +64,13 @@ expectError(
124
64
}
125
65
} )
126
66
127
- expectType < SearchResponse < Source > > ( response . body )
67
+ expectType < estypes . SearchResponse < Source > > ( response . body )
128
68
expectType < Context > ( response . meta . context )
129
69
}
130
70
131
71
// Define response body, request body and the context (promise style)
132
72
{
133
- const response = await client . search < SearchResponse < Source > , SearchBody , Context > ( {
73
+ const response = await client . search < Source , Context > ( {
134
74
index : 'test' ,
135
75
body : {
136
76
query : {
@@ -139,40 +79,7 @@ expectError(
139
79
}
140
80
} )
141
81
142
- expectType < SearchResponse < Source > > ( response . body )
143
- expectType < Context > ( response . meta . context )
144
- }
145
-
146
- // Send request body as string (promise style)
147
- {
148
- const response = await client . search ( {
149
- index : 'test' ,
150
- body : 'hello world'
151
- } )
152
-
153
- expectType < Record < string , any > > ( response . body )
154
- expectType < Context > ( response . meta . context )
155
- }
156
-
157
- // Send request body as buffer (promise style)
158
- {
159
- const response = await client . search ( {
160
- index : 'test' ,
161
- body : Buffer . from ( 'hello world' )
162
- } )
163
-
164
- expectType < Record < string , any > > ( response . body )
165
- expectType < Context > ( response . meta . context )
166
- }
167
-
168
- // Send request body as readable stream (promise style)
169
- {
170
- const response = await client . search ( {
171
- index : 'test' ,
172
- body : new ReadableStream ( )
173
- } )
174
-
175
- expectType < Record < string , any > > ( response . body )
82
+ expectType < estypes . SearchResponse < Source > > ( response . body )
176
83
expectType < Context > ( response . meta . context )
177
84
}
178
85
@@ -187,15 +94,15 @@ expectError(
187
94
}
188
95
} , ( err , response ) => {
189
96
expectType < ApiError > ( err )
190
- expectType < Record < string , any > > ( response . body )
97
+ expectType < estypes . SearchResponse < unknown > > ( response . body )
191
98
expectType < Context > ( response . meta . context )
192
99
} )
193
100
expectType < TransportRequestCallback > ( result )
194
101
}
195
102
196
103
// Define only the response body (callback style)
197
104
{
198
- const result = client . search < SearchResponse < Source > > ( {
105
+ const result = client . search < Source > ( {
199
106
index : 'test' ,
200
107
body : {
201
108
query : {
@@ -204,32 +111,15 @@ expectError(
204
111
}
205
112
} , ( err , response ) => {
206
113
expectType < ApiError > ( err )
207
- expectType < SearchResponse < Source > > ( response . body )
208
- expectType < Context > ( response . meta . context )
209
- } )
210
- expectType < TransportRequestCallback > ( result )
211
- }
212
-
213
- // Define response body and request body (callback style)
214
- {
215
- const result = client . search < SearchResponse < Source > , SearchBody > ( {
216
- index : 'test' ,
217
- body : {
218
- query : {
219
- match : { foo : 'bar' }
220
- }
221
- }
222
- } , ( err , response ) => {
223
- expectType < ApiError > ( err )
224
- expectType < SearchResponse < Source > > ( response . body )
114
+ expectType < estypes . SearchResponse < Source > > ( response . body )
225
115
expectType < Context > ( response . meta . context )
226
116
} )
227
117
expectType < TransportRequestCallback > ( result )
228
118
}
229
119
230
120
// Define response body, request body and the context (callback style)
231
121
{
232
- const result = client . search < SearchResponse < Source > , SearchBody , Context > ( {
122
+ const result = client . search < Source , Context > ( {
233
123
index : 'test' ,
234
124
body : {
235
125
query : {
@@ -238,46 +128,7 @@ expectError(
238
128
}
239
129
} , ( err , response ) => {
240
130
expectType < ApiError > ( err )
241
- expectType < SearchResponse < Source > > ( response . body )
242
- expectType < Context > ( response . meta . context )
243
- } )
244
- expectType < TransportRequestCallback > ( result )
245
- }
246
-
247
- // Send request body as string (callback style)
248
- {
249
- const result = client . search ( {
250
- index : 'test' ,
251
- body : 'hello world'
252
- } , ( err , response ) => {
253
- expectType < ApiError > ( err )
254
- expectType < Record < string , any > > ( response . body )
255
- expectType < Context > ( response . meta . context )
256
- } )
257
- expectType < TransportRequestCallback > ( result )
258
- }
259
-
260
- // Send request body as buffer (callback style)
261
- {
262
- const result = client . search ( {
263
- index : 'test' ,
264
- body : Buffer . from ( 'hello world' )
265
- } , ( err , response ) => {
266
- expectType < ApiError > ( err )
267
- expectType < Record < string , any > > ( response . body )
268
- expectType < Context > ( response . meta . context )
269
- } )
270
- expectType < TransportRequestCallback > ( result )
271
- }
272
-
273
- // Send request body as readable stream (callback style)
274
- {
275
- const result = client . search ( {
276
- index : 'test' ,
277
- body : new ReadableStream ( )
278
- } , ( err , response ) => {
279
- expectType < ApiError > ( err )
280
- expectType < Record < string , any > > ( response . body )
131
+ expectType < estypes . SearchResponse < Source > > ( response . body )
281
132
expectType < Context > ( response . meta . context )
282
133
} )
283
134
expectType < TransportRequestCallback > ( result )
0 commit comments