Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit 4bde19c

Browse files
author
Tomasz Kostuch
authored
Merge pull request #429 from icmaa/feature/refactor-storefront-query-builder
Update to `storefront-query-builder` version `1.0.0`
2 parents 42df865 + 3c37a3c commit 4bde19c

File tree

9 files changed

+142
-13
lines changed

9 files changed

+142
-13
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Endpoint for reset password with reset token. Only for Magento 2 - @Fifciu
1717
- Varnish Cache with autoinvalidation by Cache tags as addon - @Fifciu
1818
- Add `resetPasswordUsingResetToken` to `magento1` platform - @cewald (#415)
19+
- Update to `storefront-query-builder` version `1.0.0` - @cewald (#429)
1920
- Add `composeError` in './magento2/o2m.js' - @flancer64 (#422)
2021
- Explicit data extraction from 'Error' objects - @flancer64 (#424)
2122

config/default.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
"cms-data",
247247
"mail-service",
248248
"example-processor",
249+
"example-custom-filter",
249250
"elastic-stock"
250251
],
251252
"extensions": {
@@ -270,6 +271,9 @@
270271
"resultProcessors": {
271272
"product": "my-product-processor"
272273
}
274+
},
275+
"example-custom-filter": {
276+
"catalogFilter": [ "SampleFilter" ]
273277
}
274278
},
275279
"magento2": {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"ajv": "^6.4.0",
6161
"ajv-keywords": "^3.4.0",
6262
"body-parser": "^1.18.2",
63-
"bodybuilder": "2.2.13",
63+
"bodybuilder": "2.2.21",
6464
"commander": "^2.19.0",
6565
"compression": "^1.7.2",
6666
"config": "^1.30.0",
@@ -96,7 +96,7 @@
9696
"resource-router-middleware": "^0.6.0",
9797
"sharp": "^0.23.4",
9898
"soap": "^0.25.0",
99-
"storefront-query-builder": "^0.0.9",
99+
"storefront-query-builder": "^1.0.0",
100100
"syswide-cas": "latest",
101101
"winston": "^2.4.2"
102102
},

src/api/catalog.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import cache from '../lib/cache-instance'
66
import { sha3_224 } from 'js-sha3'
77
import AttributeService from './attribute/service'
88
import bodybuilder from 'bodybuilder'
9+
import loadCustomFilters from '../helpers/loadCustomFilters'
910
import { elasticsearch, SearchQuery } from 'storefront-query-builder'
10-
import { apiError } from '../lib/util';
11+
import { apiError } from '../lib/util'
1112

1213
function _cacheStorageHandler (config, result, hash, tags) {
1314
if (config.server.useOutputCache && cache) {
@@ -55,7 +56,8 @@ export default ({config, db}) => async function (req, res, body) {
5556
}
5657

5758
if (req.query.request_format === 'search-query') { // search query and not Elastic DSL - we need to translate it
58-
requestBody = await elasticsearch.buildQueryBodyFromSearchQuery({ config, queryChain: bodybuilder(), searchQuery: new SearchQuery(requestBody) })
59+
const customFilters = await loadCustomFilters(config)
60+
requestBody = await elasticsearch.buildQueryBodyFromSearchQuery({ config, queryChain: bodybuilder(), searchQuery: new SearchQuery(requestBody), customFilters })
5961
}
6062
if (req.query.response_format) responseFormat = req.query.response_format
6163

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { FilterInterface } from 'storefront-query-builder'
2+
3+
const filter: FilterInterface = {
4+
priority: 1,
5+
check: ({ operator, value, attribute, queryChain }) => attribute === 'custom-filter-name',
6+
filter ({ value, attribute, operator, queryChain }) {
7+
// Do you custom filter logic like: queryChain.filter('terms', attribute, value)
8+
return queryChain
9+
},
10+
mutator: (value) => typeof value !== 'object' ? { 'in': [value] } : value
11+
}
12+
13+
export default filter
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Router } from 'express'
2+
3+
module.exports = () => {
4+
let exampleFilter = Router()
5+
return exampleFilter
6+
}

src/helpers/loadCustomFilters.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import path from 'path'
2+
3+
export default async function loadModuleCustomFilters (config: Record<string, any>, type: string = 'catalog'): Promise<any> {
4+
let filters: any = {}
5+
let filterPromises: Promise<any>[] = []
6+
7+
for (const mod of config.registeredExtensions) {
8+
if (config.extensions.hasOwnProperty(mod) && config.extensions[mod].hasOwnProperty(type + 'Filter') && Array.isArray(config.extensions[mod][type + 'Filter'])) {
9+
const moduleFilter = config.extensions[mod][type + 'Filter']
10+
const dirPath = [__dirname, '../api/extensions/' + mod + '/filter/', type]
11+
for (const filterName of moduleFilter) {
12+
const filePath = path.resolve(...dirPath, filterName)
13+
filterPromises.push(
14+
import(filePath)
15+
.then(module => {
16+
filters[filterName] = module.default
17+
})
18+
.catch(e => {
19+
console.log(e)
20+
})
21+
)
22+
}
23+
}
24+
}
25+
26+
return Promise.all(filterPromises).then((e) => filters)
27+
}

yarn.lock

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,16 @@
505505
"@typescript-eslint/typescript-estree" "1.13.0"
506506
eslint-scope "^4.0.0"
507507

508+
"@typescript-eslint/experimental-utils@2.27.0":
509+
version "2.27.0"
510+
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-2.27.0.tgz#801a952c10b58e486c9a0b36cf21e2aab1e9e01a"
511+
integrity sha512-vOsYzjwJlY6E0NJRXPTeCGqjv5OHgRU1kzxHKWJVPjDYGbPgLudBXjIlc+OD1hDBZ4l1DLbOc5VjofKahsu9Jw==
512+
dependencies:
513+
"@types/json-schema" "^7.0.3"
514+
"@typescript-eslint/typescript-estree" "2.27.0"
515+
eslint-scope "^5.0.0"
516+
eslint-utils "^2.0.0"
517+
508518
"@typescript-eslint/parser@^1.7.1-alpha.17":
509519
version "1.13.0"
510520
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-1.13.0.tgz#61ac7811ea52791c47dc9fd4dd4a184fae9ac355"
@@ -514,13 +524,36 @@
514524
"@typescript-eslint/typescript-estree" "1.13.0"
515525
eslint-visitor-keys "^1.0.0"
516526

527+
"@typescript-eslint/parser@^2.26.0":
528+
version "2.27.0"
529+
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-2.27.0.tgz#d91664335b2c46584294e42eb4ff35838c427287"
530+
integrity sha512-HFUXZY+EdwrJXZo31DW4IS1ujQW3krzlRjBrFRrJcMDh0zCu107/nRfhk/uBasO8m0NVDbBF5WZKcIUMRO7vPg==
531+
dependencies:
532+
"@types/eslint-visitor-keys" "^1.0.0"
533+
"@typescript-eslint/experimental-utils" "2.27.0"
534+
"@typescript-eslint/typescript-estree" "2.27.0"
535+
eslint-visitor-keys "^1.1.0"
536+
517537
"@typescript-eslint/typescript-estree@1.13.0":
518538
version "1.13.0"
519539
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-1.13.0.tgz#8140f17d0f60c03619798f1d628b8434913dc32e"
520540
dependencies:
521541
lodash.unescape "4.0.1"
522542
semver "5.5.0"
523543

544+
"@typescript-eslint/typescript-estree@2.27.0":
545+
version "2.27.0"
546+
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-2.27.0.tgz#a288e54605412da8b81f1660b56c8b2e42966ce8"
547+
integrity sha512-t2miCCJIb/FU8yArjAvxllxbTiyNqaXJag7UOpB5DVoM3+xnjeOngtqlJkLRnMtzaRcJhe3CIR9RmL40omubhg==
548+
dependencies:
549+
debug "^4.1.1"
550+
eslint-visitor-keys "^1.1.0"
551+
glob "^7.1.6"
552+
is-glob "^4.0.1"
553+
lodash "^4.17.15"
554+
semver "^6.3.0"
555+
tsutils "^3.17.1"
556+
524557
JSONStream@^1.3.5:
525558
version "1.3.5"
526559
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0"
@@ -999,11 +1032,12 @@ body-parser@1.19.0, body-parser@^1.12.2, body-parser@^1.17.1, body-parser@^1.18.
9991032
raw-body "2.4.0"
10001033
type-is "~1.6.17"
10011034

1002-
bodybuilder@2.2.13:
1003-
version "2.2.13"
1004-
resolved "https://registry.yarnpkg.com/bodybuilder/-/bodybuilder-2.2.13.tgz#82ced1c2144bd9ddce97c5d89fa13f7e24c40f8e"
1035+
bodybuilder@2.2.21:
1036+
version "2.2.21"
1037+
resolved "https://registry.yarnpkg.com/bodybuilder/-/bodybuilder-2.2.21.tgz#1a7a5d31189cf10a6fbd87668ac8aa386ac8cc38"
1038+
integrity sha512-Ys0Cas7ri7dcv3f62HwB2Lclk2oBCv86pakuEs4liK6hhL9ikTEairZ700k4VHD7vgvr2CW2Kogh4E5Ivmkg2A==
10051039
dependencies:
1006-
lodash "^4.9.0"
1040+
lodash "^4.17.11"
10071041

10081042
boxen@^1.2.1:
10091043
version "1.3.0"
@@ -2020,16 +2054,36 @@ eslint-scope@^4.0.0, eslint-scope@^4.0.3:
20202054
esrecurse "^4.1.0"
20212055
estraverse "^4.1.1"
20222056

2057+
eslint-scope@^5.0.0:
2058+
version "5.0.0"
2059+
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.0.0.tgz#e87c8887c73e8d1ec84f1ca591645c358bfc8fb9"
2060+
integrity sha512-oYrhJW7S0bxAFDvWqzvMPRm6pcgcnWc4QnofCAqRTRfQC0JcwenzGglTtsLyIuuWFfkqDG9vz67cnttSd53djw==
2061+
dependencies:
2062+
esrecurse "^4.1.0"
2063+
estraverse "^4.1.1"
2064+
20232065
eslint-utils@^1.3.1:
20242066
version "1.4.0"
20252067
resolved "https://registry.yarnpkg.com/eslint-utils/-/eslint-utils-1.4.0.tgz#e2c3c8dba768425f897cf0f9e51fe2e241485d4c"
20262068
dependencies:
20272069
eslint-visitor-keys "^1.0.0"
20282070

2071+
eslint-utils@^2.0.0:
2072+
version "2.0.0"
2073+
resolved "https://registry.yarnpkg.com/eslint-utils/-/eslint-utils-2.0.0.tgz#7be1cc70f27a72a76cd14aa698bcabed6890e1cd"
2074+
integrity sha512-0HCPuJv+7Wv1bACm8y5/ECVfYdfsAm9xmVb7saeFlxjPYALefjhbYoCkBjPdPzGH8wWyTpAez82Fh3VKYEZ8OA==
2075+
dependencies:
2076+
eslint-visitor-keys "^1.1.0"
2077+
20292078
eslint-visitor-keys@^1.0.0:
20302079
version "1.0.0"
20312080
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#3f3180fb2e291017716acb4c9d6d5b5c34a6a81d"
20322081

2082+
eslint-visitor-keys@^1.1.0:
2083+
version "1.1.0"
2084+
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.1.0.tgz#e2a82cea84ff246ad6fb57f9bde5b46621459ec2"
2085+
integrity sha512-8y9YjtM1JBJU/A9Kc+SbaOV4y29sSWckBwMHa+FGtVj5gN/sbnKDf6xJUl+8g7FAij9LVaP8C24DUiH/f/2Z9A==
2086+
20332087
eslint@^5.0.0:
20342088
version "5.16.0"
20352089
resolved "https://registry.yarnpkg.com/eslint/-/eslint-5.16.0.tgz#a1e3ac1aae4a3fbd8296fcf8f7ab7314cbb6abea"
@@ -2623,6 +2677,18 @@ glob@^7.0.0, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3:
26232677
once "^1.3.0"
26242678
path-is-absolute "^1.0.0"
26252679

2680+
glob@^7.1.6:
2681+
version "7.1.6"
2682+
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
2683+
integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==
2684+
dependencies:
2685+
fs.realpath "^1.0.0"
2686+
inflight "^1.0.4"
2687+
inherits "2"
2688+
minimatch "^3.0.4"
2689+
once "^1.3.0"
2690+
path-is-absolute "^1.0.0"
2691+
26262692
global-dirs@^0.1.0:
26272693
version "0.1.1"
26282694
resolved "https://registry.yarnpkg.com/global-dirs/-/global-dirs-0.1.1.tgz#b319c0dd4607f353f3be9cca4c72fc148c49f445"
@@ -3132,7 +3198,7 @@ is-glob@^3.1.0:
31323198
dependencies:
31333199
is-extglob "^2.1.0"
31343200

3135-
is-glob@^4.0.0:
3201+
is-glob@^4.0.0, is-glob@^4.0.1:
31363202
version "4.0.1"
31373203
resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-4.0.1.tgz#7567dbe9f2f5e2467bc77ab83c4a29482407a5dc"
31383204
dependencies:
@@ -3990,7 +4056,7 @@ lodash.unescape@4.0.1:
39904056
version "4.0.1"
39914057
resolved "https://registry.yarnpkg.com/lodash.unescape/-/lodash.unescape-4.0.1.tgz#bf2249886ce514cda112fae9218cdc065211fc9c"
39924058

3993-
lodash@^4.0.0, lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.9.0:
4059+
lodash@^4.0.0, lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.4, lodash@^4.17.5:
39944060
version "4.17.14"
39954061
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.14.tgz#9ce487ae66c96254fe20b599f21b6816028078ba"
39964062

@@ -6105,10 +6171,12 @@ stealthy-require@^1.1.1:
61056171
version "1.1.1"
61066172
resolved "https://registry.yarnpkg.com/stealthy-require/-/stealthy-require-1.1.1.tgz#35b09875b4ff49f26a777e509b3090a3226bf24b"
61076173

6108-
"storefront-query-builder@https://github.com/DivanteLtd/storefront-query-builder.git":
6109-
version "0.0.8"
6110-
resolved "https://github.com/DivanteLtd/storefront-query-builder.git#26d17c9ae043b54725032687f116b21e83e02a77"
6174+
storefront-query-builder@^1.0.0:
6175+
version "1.0.0"
6176+
resolved "https://registry.yarnpkg.com/storefront-query-builder/-/storefront-query-builder-1.0.0.tgz#8bab0b6bd61a574dfa913cad5c2fdd17858f8b07"
6177+
integrity sha512-j0JhHOYhcfDcsBUMYWVJ0UApQDOem5zo20XikxJcxfFEcM5Et1Z16674wx3++KrWCbU5raEMgHvqEPxkDFajdw==
61116178
dependencies:
6179+
"@typescript-eslint/parser" "^2.26.0"
61126180
clone-deep "^4.0.1"
61136181

61146182
stream-events@^1.0.1, stream-events@^1.0.4:
@@ -6468,6 +6536,13 @@ tslib@^1.8.1, tslib@^1.9.0, tslib@^1.9.3:
64686536
version "1.10.0"
64696537
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"
64706538

6539+
tsutils@^3.17.1:
6540+
version "3.17.1"
6541+
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.17.1.tgz#ed719917f11ca0dee586272b2ac49e015a2dd759"
6542+
integrity sha512-kzeQ5B8H3w60nFY2g8cJIuH7JDpsALXySGtwGJ0p2LSjLgay3NdIpqq5SoOBe46bKDW2iq25irHCr8wjomUS2g==
6543+
dependencies:
6544+
tslib "^1.8.1"
6545+
64716546
tsutils@^3.7.0:
64726547
version "3.14.1"
64736548
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.14.1.tgz#f1d2b93d2a0876481f2f1f98c25ba42bbd7ee860"

0 commit comments

Comments
 (0)