Skip to content

Commit 57fa561

Browse files
bors[bot]bidoubiwa
andauthored
Merge #1007
1007: Remove unecessary type complexity on searchParameters r=bidoubiwa a=bidoubiwa This level of type complexity was not maintainable and not exhaustive as it would require a lot of work to make it consistent around the whole codebase. It also made the usage of the library to limiting in projects that would want different degrees of severity Co-authored-by: Charlotte Vermandel <[email protected]>
2 parents d5ecd28 + defff4c commit 57fa561

File tree

3 files changed

+33
-52
lines changed

3 files changed

+33
-52
lines changed

src/lib/indexes.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
import { sleep, removeUndefinedFromObject } from './utils'
3838
import { HttpRequests } from './http-requests'
3939

40-
class Index<T> {
40+
class Index<T = Record<string, any>> {
4141
uid: string
4242
primaryKey: string | undefined
4343
httpRequest: HttpRequests
@@ -79,11 +79,11 @@ class Index<T> {
7979
* @memberof Index
8080
* @method search
8181
*/
82-
async search<P extends SearchParams<T>>(
82+
async search<T = Record<string, any>>(
8383
query?: string | null,
84-
options?: P,
84+
options?: SearchParams,
8585
config?: Partial<Request>
86-
): Promise<SearchResponse<T, P>> {
86+
): Promise<SearchResponse<T>> {
8787
const url = `indexes/${this.uid}/search`
8888

8989
return await this.httpRequest.post(
@@ -99,11 +99,11 @@ class Index<T> {
9999
* @memberof Index
100100
* @method search
101101
*/
102-
async searchGet<P extends SearchParams<T>>(
102+
async searchGet<T = Record<string, any>>(
103103
query?: string | null,
104-
options?: P,
104+
options?: SearchParams,
105105
config?: Partial<Request>
106-
): Promise<SearchResponse<T, P>> {
106+
): Promise<SearchResponse<T>> {
107107
const url = `indexes/${this.uid}/search`
108108

109109
const parseFilter = (filter?: Filter): string | undefined => {
@@ -134,7 +134,7 @@ class Index<T> {
134134
: undefined,
135135
}
136136

137-
return await this.httpRequest.get<SearchResponse<T, P>>(
137+
return await this.httpRequest.get<SearchResponse<T>>(
138138
url,
139139
removeUndefinedFromObject(getParams),
140140
config
@@ -182,7 +182,7 @@ class Index<T> {
182182
* @memberof Index
183183
* @method create
184184
*/
185-
static async create<T = any>(
185+
static async create<T = Record<string, any>>(
186186
config: Config,
187187
uid: string,
188188
options: IndexOptions = {}
@@ -278,16 +278,16 @@ class Index<T> {
278278
* @memberof Index
279279
* @method getDocuments
280280
*/
281-
async getDocuments<P extends GetDocumentsParams<T>>(
282-
options?: P
283-
): Promise<GetDocumentsResponse<T, P>> {
281+
async getDocuments<T = Record<string, any>>(
282+
options?: GetDocumentsParams<T>
283+
): Promise<GetDocumentsResponse<T>> {
284284
const url = `indexes/${this.uid}/documents`
285285
let attr
286286
if (options !== undefined && Array.isArray(options.attributesToRetrieve)) {
287287
attr = options.attributesToRetrieve.join(',')
288288
}
289289

290-
return await this.httpRequest.get<GetDocumentsResponse<T, P>>(url, {
290+
return await this.httpRequest.get<GetDocumentsResponse<T>>(url, {
291291
...options,
292292
...(attr !== undefined ? { attributesToRetrieve: attr } : {}),
293293
})

src/types/types.ts

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ export type AddDocumentParams = {
3737

3838
export type Filter = string | Array<string | string[]>
3939

40-
export type SearchParams<T> = {
40+
export type SearchParams = {
4141
offset?: number
4242
limit?: number
43-
attributesToRetrieve?: Array<Extract<keyof T, string> | '*'>
44-
attributesToCrop?: Array<Extract<keyof T, string> | '*'>
43+
attributesToRetrieve?: string[]
44+
attributesToCrop?: string[]
4545
cropLength?: number
46-
attributesToHighlight?: Array<Extract<keyof T, string> | '*'>
46+
attributesToHighlight?: string[]
4747
filter?: Filter
4848
sort?: string[]
4949
facetsDistribution?: string[]
@@ -88,26 +88,19 @@ export type _matchesInfo<T> = Partial<
8888
Record<keyof T, Array<{ start: number; length: number }>>
8989
>
9090

91-
export type Hit<T> = T & {
91+
export type document = {
92+
[field: string]: any
93+
}
94+
95+
export type Hit<T = document> = T & {
9296
_formatted?: Partial<T>
9397
_matchesInfo?: _matchesInfo<T>
9498
}
9599

96-
export type Hits<
97-
T,
98-
P extends SearchParams<T>
99-
> = P['attributesToRetrieve'] extends Array<'*'>
100-
? Array<Hit<T>>
101-
: P['attributesToRetrieve'] extends Array<infer K> // if P['attributesToRetrieve'] is an array, we use `infer K` to extract the keys in the array in place
102-
? Array<Hit<Pick<T, Exclude<keyof T, Exclude<keyof T, K>>>>> // Same extraction method as above when we have a single `attributesToRetrieve`
103-
: Array<Hit<T>> // Finally return the full type as `attributesToRetrieve` is neither a single key nor an array of keys
104-
105-
// The second generic P is used to capture the SearchParams type
106-
export type SearchResponse<T, P extends SearchParams<T>> = {
107-
// P represents the SearchParams
108-
// and by using the indexer P['attributesToRetrieve'], we're able to pick the type of `attributesToRetrieve`
109-
// and check whether the attribute is a single key present in the generic
110-
hits: Hits<T, P>
100+
export type Hits<T = document> = Array<Hit<T>>
101+
102+
export type SearchResponse<T = Record<string, any>> = {
103+
hits: Hits<T>
111104
offset: number
112105
limit: number
113106
processingTimeMs: number
@@ -125,28 +118,17 @@ export type FieldDistribution = {
125118
/*
126119
** Documents
127120
*/
128-
export type GetDocumentsParams<T> = {
121+
export type GetDocumentsParams<T = Record<string, any>> = {
129122
offset?: number
130123
limit?: number
131124
attributesToRetrieve?:
132125
| Array<Extract<keyof T, string>>
133126
| Extract<keyof T, string>
134127
}
135128

136-
export type GetDocumentsResponse<
137-
T,
138-
P extends GetDocumentsParams<T>
139-
> = P['attributesToRetrieve'] extends keyof T
140-
? Array<
141-
Document<
142-
Pick<T, Exclude<keyof T, Exclude<keyof T, P['attributesToRetrieve']>>>
143-
>
144-
>
145-
: P['attributesToRetrieve'] extends Array<infer K>
146-
? Array<Document<Pick<T, Exclude<keyof T, Exclude<keyof T, K>>>>>
147-
: Array<Document<T>>
148-
149-
export type Document<T> = T
129+
export type GetDocumentsResponse<T = Record<string, any>> = Array<Document<T>>
130+
131+
export type Document<T = Record<string, any>> = T
150132

151133
/*
152134
** Settings

tests/env/typescript-node/src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,20 @@ const client = new MeiliSearch(config)
2929
// console.log(index.something) -> ERROR
3030
})
3131

32-
const searchParams: SearchParams<Movie> = {
32+
const searchParams: SearchParams = {
3333
limit: 5,
3434
attributesToRetrieve: ['title', 'genre'],
3535
attributesToHighlight: ['title'],
3636
// test: true -> ERROR Test does not exist on type SearchParams
3737
}
3838
indexes.map((index: IndexResponse) => index.uid)
39-
const res: SearchResponse<Movie, SearchParams<Movie>> = await index.search(
39+
const res: SearchResponse<Movie> = await index.search(
4040
'avenger',
4141
searchParams
4242
)
4343

4444
// both work
45-
const { hits }: { hits: Hits<Movie, typeof searchParams> } = res
46-
// const { hits } : { hits: Hits<Movie, SearchParams<Movie>> } = res;
45+
const { hits }: { hits: Hits<Movie> } = res
4746

4847
hits.map((hit: Hit<Movie>) => {
4948
console.log(hit?.genre)

0 commit comments

Comments
 (0)