|
8 | 8 | 'use strict' |
9 | 9 |
|
10 | 10 | import MeiliSearchTimeOutError from './errors/meilisearch-timeout-error' |
| 11 | +import MeiliSearchError from './errors/meilisearch-error' |
11 | 12 | import MeiliAxiosWrapper from './meili-axios-wrapper' |
12 | 13 | import * as Types from './types' |
13 | | -import { sleep } from './utils' |
| 14 | +import { sleep, joinIfArray, createArrayIfString } from './utils' |
14 | 15 |
|
15 | 16 | class Index<T> extends MeiliAxiosWrapper implements Types.IndexInterface<T> { |
16 | 17 | uid: string |
@@ -73,64 +74,65 @@ class Index<T> extends MeiliAxiosWrapper implements Types.IndexInterface<T> { |
73 | 74 | * @method search |
74 | 75 | */ |
75 | 76 | async search<P extends Types.SearchParams<T>>( |
76 | | - query: string, |
77 | | - options?: P |
| 77 | + query?: string, |
| 78 | + options?: P, |
| 79 | + method: 'POST' | 'GET' = 'POST' |
78 | 80 | ): Promise<Types.SearchResponse<T, P>> { |
79 | 81 | const url = `/indexes/${this.uid}/search` |
80 | | - |
81 | | - const params: Types.SearchRequest = { |
| 82 | + let params: Types.SearchRequest = { |
82 | 83 | q: query, |
| 84 | + offset: options?.offset, |
| 85 | + limit: options?.limit, |
| 86 | + cropLength: options?.cropLength, |
| 87 | + filters: options?.filters, |
| 88 | + matches: options?.matches, |
83 | 89 | } |
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, |
108 | 104 | } |
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, |
127 | 126 | } |
| 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 | + ) |
128 | 135 | } |
129 | | - |
130 | | - return await this.get(url, { |
131 | | - params, |
132 | | - cancelToken: this.cancelTokenSource.token, |
133 | | - }) |
134 | 136 | } |
135 | 137 |
|
136 | 138 | /// |
|
0 commit comments