Skip to content

Commit a6c0020

Browse files
committed
Add type for meilisearch error
1 parent d75ba5a commit a6c0020

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src/types.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import MeiliAxiosWrapper from './meili-axios-wrapper'
1717
import MeiliSearch from './meilisearch'
1818
import MeiliSearchApiError from './errors/meilisearch-api-error'
1919
import MeiliSearchTimeOutError from './errors/meilisearch-timeout-error'
20+
import MeiliSearchError from './errors/meilisearch-error'
2021
export { Index }
2122
export { MeiliSearchApiError }
23+
export { MeiliSearchError }
2224
export { MeiliSearchTimeOutError }
2325

2426
export interface Config {
@@ -73,7 +75,21 @@ export interface SearchParams<T> {
7375
}
7476

7577
export interface SearchRequest {
76-
q: string
78+
q?: string
79+
offset?: number
80+
limit?: number
81+
cropLength?: number
82+
attributesToRetrieve?: string[]
83+
attributesToCrop?: string[]
84+
attributesToHighlight?: string[]
85+
facetFilters?: string | FacetFilter | FacetFilter[]
86+
facetsDistribution?: string[]
87+
filters?: string
88+
matches?: boolean
89+
}
90+
91+
export interface GetSearchRequest {
92+
q?: string
7793
offset?: number
7894
limit?: number
7995
attributesToRetrieve?: string
@@ -88,16 +104,23 @@ export interface SearchRequest {
88104

89105
export type Hit<T> = T & { _formatted?: T }
90106

107+
// The second generic P is used to capture the SearchParams type
91108
export interface SearchResponse<T, P extends SearchParams<T>> {
92-
hits: P['attributesToRetrieve'] extends keyof T
109+
// P represents the SearchParams
110+
// and by using the indexer P['attributesToRetrieve'], we're able to pick the type of `attributesToRetrieve`
111+
// and check whether the attribute is a single key present in the generic
112+
hits: P['attributesToRetrieve'] extends keyof T // `attributesToRetrieve` contains one single key
93113
? Array<
114+
// So we return an array of
94115
Hit<
116+
// hits
117+
// We exclude the `attributesToRetrieve` first from the generic, and then we exclude what has been returned to make sure we only Pick the `attributesToRetrieve` on the generic T
95118
Pick<T, Exclude<keyof T, Exclude<keyof T, P['attributesToRetrieve']>>>
96119
>
97120
>
98-
: P['attributesToRetrieve'] extends Array<infer K>
99-
? Array<Hit<Pick<T, Exclude<keyof T, Exclude<keyof T, K>>>>>
100-
: Array<Hit<T>>
121+
: P['attributesToRetrieve'] extends Array<infer K> // Otherwise if P['attributesToRetrieve'] is an array, we use `infer K` to extract the keys in the array in place
122+
? Array<Hit<Pick<T, Exclude<keyof T, Exclude<keyof T, K>>>>> // Same extraction method as above when we have a single `attributesToRetrieve`
123+
: Array<Hit<T>> // Finally return the full type as `attributesToRetrieve` is neither a single key nor an array of keys
101124
offset: number
102125
limit: number
103126
processingTimeMs: number
@@ -249,7 +272,8 @@ export interface IndexInterface<T = any> extends MeiliAxiosWrapperInterface {
249272
getAllUpdateStatus: () => Promise<Update[]>
250273
search: <P extends SearchParams<T>>(
251274
query: string,
252-
options?: P
275+
options?: P,
276+
method?: 'POST' | 'GET'
253277
) => Promise<SearchResponse<T, P>>
254278
show: () => Promise<IndexResponse>
255279
updateIndex: (indexData: IndexOptions) => Promise<IndexResponse>

0 commit comments

Comments
 (0)