Skip to content

Commit aa4a6e4

Browse files
committed
Add get and post method for search function
1 parent 87035c1 commit aa4a6e4

File tree

4 files changed

+498
-60
lines changed

4 files changed

+498
-60
lines changed

src/index.ts

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
'use strict'
99

1010
import MeiliSearchTimeOutError from './errors/meilisearch-timeout-error'
11+
import MeiliSearchError from './errors/meilisearch-error'
1112
import MeiliAxiosWrapper from './meili-axios-wrapper'
1213
import * as Types from './types'
13-
import { sleep } from './utils'
14+
import { sleep, joinIfArray, createArrayIfString } from './utils'
1415

1516
class Index<T> extends MeiliAxiosWrapper implements Types.IndexInterface<T> {
1617
uid: string
@@ -73,64 +74,65 @@ class Index<T> extends MeiliAxiosWrapper implements Types.IndexInterface<T> {
7374
* @method search
7475
*/
7576
async search<P extends Types.SearchParams<T>>(
76-
query: string,
77-
options?: P
77+
query?: string,
78+
options?: P,
79+
method: 'POST' | 'GET' = 'POST'
7880
): Promise<Types.SearchResponse<T, P>> {
7981
const url = `/indexes/${this.uid}/search`
80-
81-
const params: Types.SearchRequest = {
82+
let params: Types.SearchRequest = {
8283
q: query,
84+
offset: options?.offset,
85+
limit: options?.limit,
86+
cropLength: options?.cropLength,
87+
filters: options?.filters,
88+
matches: options?.matches,
8389
}
84-
if (options !== undefined) {
85-
if (options.offset !== undefined) {
86-
params.offset = options.offset
87-
}
88-
if (options.limit !== undefined) {
89-
params.limit = options.limit
90-
}
91-
if (options.attributesToRetrieve !== undefined) {
92-
if (Array.isArray(options.attributesToRetrieve)) {
93-
params.attributesToRetrieve = options.attributesToRetrieve.join(',')
94-
} else {
95-
params.attributesToRetrieve = options.attributesToRetrieve
96-
}
97-
}
98-
99-
if (options.attributesToCrop !== undefined) {
100-
if (Array.isArray(options.attributesToCrop)) {
101-
params.attributesToCrop = options.attributesToCrop.join(',')
102-
} else {
103-
params.attributesToCrop = options.attributesToCrop
104-
}
105-
}
106-
if (options.cropLength !== undefined) {
107-
params.cropLength = options.cropLength
90+
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,
108104
}
109-
if (options.attributesToHighlight !== undefined) {
110-
if (Array.isArray(options.attributesToHighlight)) {
111-
params.attributesToHighlight = options.attributesToHighlight.join(',')
112-
} else {
113-
params.attributesToHighlight = options.attributesToHighlight
114-
}
115-
}
116-
if (options.filters !== undefined) {
117-
params.filters = options.filters
118-
}
119-
if (options.matches !== undefined) {
120-
params.matches = options.matches
121-
}
122-
if (options.facetFilters !== undefined) {
123-
params.facetFilters = JSON.stringify(options.facetFilters)
124-
}
125-
if (options.facetsDistribution !== undefined) {
126-
params.facetsDistribution = JSON.stringify(options.facetsDistribution)
105+
return await this.post(url, params, {
106+
cancelToken: this.cancelTokenSource.token,
107+
})
108+
} else if (method.toUpperCase() === 'GET') {
109+
const getParams: Types.GetSearchRequest = {
110+
...params,
111+
facetFilters: options?.facetFilters
112+
? JSON.stringify(options.facetFilters)
113+
: undefined,
114+
facetsDistribution: options?.facetsDistribution
115+
? JSON.stringify(options.facetsDistribution)
116+
: undefined,
117+
attributesToRetrieve: options?.attributesToRetrieve
118+
? joinIfArray(options.attributesToRetrieve)
119+
: undefined,
120+
attributesToCrop: options?.attributesToCrop
121+
? joinIfArray(options.attributesToCrop)
122+
: undefined,
123+
attributesToHighlight: options?.attributesToHighlight
124+
? joinIfArray(options.attributesToHighlight)
125+
: undefined,
127126
}
127+
return await this.get(url, {
128+
params: getParams,
129+
cancelToken: this.cancelTokenSource.token,
130+
})
131+
} else {
132+
throw new MeiliSearchError(
133+
'method parameter should be either POST or GET'
134+
)
128135
}
129-
130-
return await this.get(url, {
131-
params,
132-
cancelToken: this.cancelTokenSource.token,
133-
})
134136
}
135137

136138
///

src/utils.ts

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

5-
export { sleep }
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 }

0 commit comments

Comments
 (0)