-
Notifications
You must be signed in to change notification settings - Fork 722
Added Request/Response types definitions #970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
ed96a9d
fd70174
0892ecb
9380adc
511db34
14d6401
9f04d26
36cb500
08b2827
e34aa55
d53b68c
b083db6
e82cfe3
728254b
911e883
5c6131d
1673020
41529a4
acc9346
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // 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 | ||
|
|
||
| export interface Search<T = any> { | ||
| took: number; | ||
| timed_out: boolean; | ||
| _scroll_id?: string; | ||
| _shards: Shards; | ||
| hits: { | ||
| total: { | ||
| value: number; | ||
| relation: string; | ||
| }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type is only correct when the server is running Elasticsearch 7.0 and above, and only when the request did not set the property
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now, this type definitions will land only for
How would you solve this? The easiest solutions that come to my mind is to use a union, in the same way I did in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that would create extra work for the 80% case. If possible, I would suggest making the request object into a parameter on the type definition. I've made an example here for how I would write these types: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, just for general awareness, I'd recommend looking at a Anyway, what looks simpler to me is just using two separate overloads: // Don't export this one, because it is likely to be removed when the legacy interface is removed
interface BasicSearchResponse {
/* all stable fields */
}
export interface SearchResponse extends BasicSearchResponse {
hits: {
total: {
value: number,
relation: "gte" | "eq" // @delvedor I suggest using an explicit string literal union here instead of just a `string`
}
}
}
// legacy, thus the interface name is verbose:
export interface SearchResponseWithTotalHintsAsInt extends BasicSearchResponse {
hits: { total: number }
}
// don't export it for the same reason as BasicSearchResponse
interface BasicSearchParams { /* all fields except for rest_total_hits_as_int */ }
export interface SearchParams extends BasicSearchParams {
rest_total_hits_as_int?: false;
}
export interface SearchParamsWithTotalHintsAsInt extends BasicSearchParams {
rest_total_hits_as_int: true;
}
export class Client {
search(params: SearchParams): Promise<SearchResponse>;
// @deprecated: please use other overload
search(params: SearchParamsWithTotalHintsAsInt): Promise<SearchResponseWithTotalHintsAsInt>;
} |
||
| max_score: number; | ||
| hits: Array<{ | ||
| _index: string; | ||
| _type: string; | ||
| _id: string; | ||
| _score: number; | ||
| _source: T; | ||
| _version?: number; | ||
| _explanation?: Explanation; | ||
| fields?: any; | ||
| highlight?: any; | ||
| inner_hits?: any; | ||
| matched_queries?: string[]; | ||
| sort?: string[]; | ||
| }>; | ||
| }; | ||
| aggregations?: any; | ||
| } | ||
|
|
||
| export interface MSearch<T = any> { | ||
| responses?: Array<Search<T>>; | ||
| } | ||
|
|
||
| export interface Shards { | ||
| total: number; | ||
| successful: number; | ||
| failed: number; | ||
| skipped: number; | ||
| } | ||
|
|
||
| export interface Explanation { | ||
| value: number; | ||
| description: string; | ||
| details: Explanation[]; | ||
| } | ||
|
|
||
| export interface Create { | ||
| _shards: Shards; | ||
| _index: string; | ||
| _type: string; | ||
| _id: string; | ||
| _version: number; | ||
| _seq_no: number; | ||
| _primary_term: number; | ||
| result: string; | ||
| } | ||
|
|
||
| export interface Index extends Create {} | ||
|
|
||
| export interface Delete { | ||
| _shards: Shards; | ||
| _index: string; | ||
| _type: string; | ||
| _id: string; | ||
| _version: number; | ||
| _seq_no: number; | ||
| _primary_term: number; | ||
| result: string; | ||
| } | ||
|
|
||
| export interface Update { | ||
| _shards: Shards; | ||
| _index: string; | ||
| _type: string; | ||
| _id: string; | ||
| _version: number; | ||
| result: string; | ||
| } | ||
|
|
||
| export interface Get<T = any> { | ||
| _index: string; | ||
| _type: string; | ||
| _id: string; | ||
| _version: number; | ||
| _seq_no: number; | ||
| _primary_term: number; | ||
| found: boolean; | ||
| _source: T | ||
| } | ||
|
|
||
|
|
||
| export interface Bulk { | ||
| took: number; | ||
| errors: boolean; | ||
| items: Array<BulkItem>; | ||
| } | ||
|
|
||
| type BulkItem = | ||
| | { index: BulkIndex } | ||
| | { create: BulkCreate } | ||
| | { update: BulkUpdate } | ||
| | { delete: BulkDelete } | ||
|
|
||
| interface BulkIndex extends Index { | ||
| status: number; | ||
| } | ||
|
|
||
| interface BulkCreate extends Create { | ||
| status: number; | ||
| } | ||
|
|
||
| interface BulkUpdate extends Update { | ||
| status: number; | ||
| } | ||
|
|
||
| interface BulkDelete extends Delete { | ||
| status: number; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.