Skip to content

Commit 689cc98

Browse files
committed
Search method can only accept array of string on bigamous fields
1 parent b80c928 commit 689cc98

File tree

5 files changed

+34
-80
lines changed

5 files changed

+34
-80
lines changed

src/index.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
'use strict'
99

10-
import MeiliSearchTimeOutError from './errors/meilisearch-timeout-error'
1110
import MeiliSearchError from './errors/meilisearch-error'
11+
import MeiliSearchTimeOutError from './errors/meilisearch-timeout-error'
1212
import MeiliAxiosWrapper from './meili-axios-wrapper'
1313
import * as Types from './types'
14-
import { sleep, joinIfArray, createArrayIfString } from './utils'
14+
import { sleep } from './utils'
1515

1616
class Index<T> extends MeiliAxiosWrapper implements Types.IndexInterface<T> {
1717
uid: string
@@ -79,51 +79,44 @@ class Index<T> extends MeiliAxiosWrapper implements Types.IndexInterface<T> {
7979
method: 'POST' | 'GET' = 'POST'
8080
): Promise<Types.SearchResponse<T, P>> {
8181
const url = `/indexes/${this.uid}/search`
82-
let params: Types.SearchRequest = {
82+
const params: Types.SearchRequest = {
8383
q: query,
8484
offset: options?.offset,
8585
limit: options?.limit,
8686
cropLength: options?.cropLength,
8787
filters: options?.filters,
8888
matches: options?.matches,
89+
facetFilters: options?.facetFilters,
90+
facetsDistribution: options?.facetsDistribution,
91+
attributesToRetrieve: options?.attributesToRetrieve,
92+
attributesToCrop: options?.attributesToCrop,
93+
attributesToHighlight: options?.attributesToHighlight,
8994
}
9095
if (method.toUpperCase() === 'POST') {
91-
params = {
92-
...params,
93-
facetFilters: options?.facetFilters,
94-
facetsDistribution: options?.facetsDistribution,
95-
attributesToRetrieve: options?.attributesToRetrieve
96-
? createArrayIfString(options.attributesToRetrieve)
97-
: undefined,
98-
attributesToCrop: options?.attributesToCrop
99-
? createArrayIfString(options.attributesToCrop)
100-
: undefined,
101-
attributesToHighlight: options?.attributesToHighlight
102-
? createArrayIfString(options.attributesToHighlight)
103-
: undefined,
104-
}
10596
return await this.post(url, params, {
10697
cancelToken: this.cancelTokenSource.token,
10798
})
10899
} else if (method.toUpperCase() === 'GET') {
109100
const getParams: Types.GetSearchRequest = {
110101
...params,
111-
facetFilters: options?.facetFilters
112-
? JSON.stringify(options.facetFilters)
113-
: undefined,
102+
facetFilters:
103+
Array.isArray(options?.facetFilters) && options?.facetFilters
104+
? JSON.stringify(options.facetFilters)
105+
: undefined,
114106
facetsDistribution: options?.facetsDistribution
115107
? JSON.stringify(options.facetsDistribution)
116108
: undefined,
117109
attributesToRetrieve: options?.attributesToRetrieve
118-
? joinIfArray(options.attributesToRetrieve)
110+
? options.attributesToRetrieve.join(',')
119111
: undefined,
120112
attributesToCrop: options?.attributesToCrop
121-
? joinIfArray(options.attributesToCrop)
113+
? options.attributesToCrop.join(',')
122114
: undefined,
123115
attributesToHighlight: options?.attributesToHighlight
124-
? joinIfArray(options.attributesToHighlight)
116+
? options.attributesToHighlight.join(',')
125117
: undefined,
126118
}
119+
127120
return await this.get(url, {
128121
params: getParams,
129122
cancelToken: this.cancelTokenSource.token,

src/types.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,12 @@ export type FacetFilter = Array<string | string[]>
5858
export interface SearchParams<T> {
5959
offset?: number
6060
limit?: number
61-
attributesToRetrieve?:
62-
| Array<Extract<keyof T, string>>
63-
| Extract<keyof T, string>
64-
attributesToCrop?:
65-
| Array<Extract<keyof T, string> | '*'>
66-
| (Extract<keyof T, string> | '*')
61+
attributesToRetrieve?: Array<Extract<keyof T, string> | '*'>
62+
attributesToCrop?: Array<Extract<keyof T, string> | '*'>
6763
cropLength?: number
68-
attributesToHighlight?:
69-
| Array<Extract<keyof T, string> | '*'>
70-
| (Extract<keyof T, string> | '*')
64+
attributesToHighlight?: Array<Extract<keyof T, string> | '*'>
7165
filters?: string
72-
facetFilters?: string | FacetFilter | FacetFilter[]
66+
facetFilters?: FacetFilter | FacetFilter[]
7367
facetsDistribution?: string[]
7468
matches?: boolean
7569
}
@@ -109,16 +103,7 @@ export interface SearchResponse<T, P extends SearchParams<T>> {
109103
// P represents the SearchParams
110104
// and by using the indexer P['attributesToRetrieve'], we're able to pick the type of `attributesToRetrieve`
111105
// 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
113-
? Array<
114-
// So we return an array of
115-
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
118-
Pick<T, Exclude<keyof T, Exclude<keyof T, P['attributesToRetrieve']>>>
119-
>
120-
>
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
106+
hits: 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
122107
? Array<Hit<Pick<T, Exclude<keyof T, Exclude<keyof T, K>>>>> // Same extraction method as above when we have a single `attributesToRetrieve`
123108
: Array<Hit<T>> // Finally return the full type as `attributesToRetrieve` is neither a single key nor an array of keys
124109
offset: number

src/utils.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,4 @@ async function sleep(ms: number): Promise<void> {
22
return await new Promise((resolve) => setTimeout(resolve, ms))
33
}
44

5-
function joinIfArray(params: string | string[]): string {
6-
if (Array.isArray(params)) return params.join(',')
7-
else return params
8-
}
9-
10-
function createArrayIfString(params: string | string[]): string[] {
11-
if (Array.isArray(params)) return params
12-
else return [params]
13-
}
14-
15-
export { sleep, joinIfArray, createArrayIfString }
5+
export { sleep }

tests/get_search_tests.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ describe.each([
160160
'prince',
161161
{
162162
filters: 'title = "Le Petit Prince"',
163-
attributesToCrop: '*',
163+
attributesToCrop: ['*'],
164164
cropLength: 5,
165165
matches: true,
166166
},
@@ -189,9 +189,9 @@ describe.each([
189189
limit: 5,
190190
offset: 0,
191191
attributesToRetrieve: ['id', 'title'],
192-
attributesToCrop: '*',
192+
attributesToCrop: ['*'],
193193
cropLength: 6,
194-
attributesToHighlight: '*',
194+
attributesToHighlight: ['*'],
195195
filters: 'title = "Le Petit Prince"',
196196
matches: true,
197197
},
@@ -233,10 +233,10 @@ describe.each([
233233
{
234234
limit: 5,
235235
offset: 0,
236-
attributesToRetrieve: '*',
237-
attributesToCrop: '*',
236+
attributesToRetrieve: ['*'],
237+
attributesToCrop: ['*'],
238238
cropLength: 6,
239-
attributesToHighlight: '*',
239+
attributesToHighlight: ['*'],
240240
filters: 'title = "Le Petit Prince"',
241241
matches: true,
242242
},
@@ -383,20 +383,6 @@ describe.each([
383383
})
384384
})
385385

386-
test(`${permission} key: Search with one string facetFilters `, async () => {
387-
await expect(
388-
client.getIndex(index.uid).search(
389-
'a',
390-
{
391-
facetFilters: 'genre:romance',
392-
},
393-
'GET'
394-
)
395-
).rejects.toThrowError(
396-
`error processing facet filter: unexpected token "genre:romance", expected Array`
397-
)
398-
})
399-
400386
test(`${permission} key: Try to Search on deleted index and fail`, async () => {
401387
await masterClient.getIndex(index.uid).deleteIndex()
402388
await expect(

tests/search_tests.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe.each([
154154
.getIndex(index.uid)
155155
.search('prince', {
156156
filters: 'title = "Le Petit Prince"',
157-
attributesToCrop: '*',
157+
attributesToCrop: ['*'],
158158
cropLength: 5,
159159
matches: true,
160160
})
@@ -179,9 +179,9 @@ describe.each([
179179
limit: 5,
180180
offset: 0,
181181
attributesToRetrieve: ['id', 'title'],
182-
attributesToCrop: '*',
182+
attributesToCrop: ['*'],
183183
cropLength: 6,
184-
attributesToHighlight: '*',
184+
attributesToHighlight: ['*'],
185185
filters: 'title = "Le Petit Prince"',
186186
matches: true,
187187
})
@@ -219,10 +219,10 @@ describe.each([
219219
.search('prince', {
220220
limit: 5,
221221
offset: 0,
222-
attributesToRetrieve: '*',
223-
attributesToCrop: '*',
222+
attributesToRetrieve: ['*'],
223+
attributesToCrop: ['*'],
224224
cropLength: 6,
225-
attributesToHighlight: '*',
225+
attributesToHighlight: ['*'],
226226
filters: 'title = "Le Petit Prince"',
227227
matches: true,
228228
})

0 commit comments

Comments
 (0)