Skip to content

Commit 8fbaf01

Browse files
committed
fix(fetch): handle pagination for simple search request
1 parent 161b9a1 commit 8fbaf01

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/methods/fetch.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import { search } from '@ecomplus/client'
33
export default (self, isSimpleSearch, axiosConfig) => {
44
// mount axios req options for complex or simpĺe search
55
const reqOptions = {
6-
url: '/items.json'
6+
url: '/items.json',
7+
axiosConfig
78
}
9+
810
if (isSimpleSearch === true) {
911
// https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-uri-request.html
1012
const { query } = self.dsl
13+
reqOptions.url += '?q='
1114
if (query && query.bool && Array.isArray(query.bool.filter)) {
1215
// parse query filters to string
1316
let queryString = ''
@@ -19,11 +22,17 @@ export default (self, isSimpleSearch, axiosConfig) => {
1922
if (condition) {
2023
const field = Object.keys(condition)[0]
2124
const value = condition[field]
22-
queryString += `${field}:${(Array.isArray(value) ? `("${value.join('" OR "')}")` : value)}`
25+
queryString += `${field}:${(Array.isArray(value) ? `("${value.join('" "')}")` : value)}`
2326
}
2427
})
25-
reqOptions.url += `?q=${encodeURIComponent(queryString)}`
28+
reqOptions.url += encodeURIComponent(queryString)
2629
}
30+
// handle pagination
31+
;['from', 'size'].forEach(field => {
32+
if (self.dsl[field]) {
33+
reqOptions.url += `&${field}=${self.dsl[field]}`
34+
}
35+
})
2736
} else {
2837
reqOptions.method = 'post'
2938
reqOptions.data = self.dsl
@@ -32,9 +41,6 @@ export default (self, isSimpleSearch, axiosConfig) => {
3241
reqOptions.axiosConfig = isSimpleSearch
3342
}
3443
}
35-
if (axiosConfig) {
36-
reqOptions.axiosConfig = axiosConfig
37-
}
3844

3945
// request Search API and return promise
4046
return search(reqOptions).then(({ data }) => {

0 commit comments

Comments
 (0)