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

Commit

Permalink
Merge branch 'master' of github.com:DivanteLtd/vue-storefront-api int…
Browse files Browse the repository at this point in the history
…o HEAD
  • Loading branch information
tkostuch committed Apr 9, 2020
2 parents db11e15 + 093d393 commit 42df865
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 58 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix MSI default stock id value
- Add outputFormatter to response from cache - @gibkigonzo (#428)


## [1.11.1] - 2020.03.17

### Added
- Add save address on place order - @lucasqm (#394)
- Add ElasticSearch client support for HTTP authentication - @cewald (#397)
- Add error handling for catalog and add header 'X-VS-Cache-Tags' to response - @gibkigonzo

### Fixed
- Add fallback for `sourcePriceInclTax` and `finalPriceInclTax` in `magento1` platform - @cewald (#398)
- Add default ES index to config and update `getStockList` - @gibkigonzo (#405)
- Makes elastic-stock extension compatible with both ES5 and ES7. Allows for stock fetch of configurable children that is set as "Not Visible Individually" - @didkan (#410)


## [1.11.0] - 2019.12.20

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-storefront-api",
"version": "1.11.0",
"version": "1.11.1",
"private": true,
"description": "vue-storefront API and data services",
"main": "dist",
Expand Down
106 changes: 49 additions & 57 deletions src/api/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { sha3_224 } from 'js-sha3'
import AttributeService from './attribute/service'
import bodybuilder from 'bodybuilder'
import { elasticsearch, SearchQuery } from 'storefront-query-builder'
import { apiError } from '../lib/util';

function _cacheStorageHandler (config, result, hash, tags) {
if (config.server.useOutputCache && cache) {
Expand Down Expand Up @@ -109,64 +110,55 @@ export default ({config, db}) => async function (req, res, body) {
body: requestBody,
json: true,
auth: auth
}, (_err, _res, _resBody) => { // TODO: add caching layer to speed up SSR? How to invalidate products (checksum on the response BEFORE processing it)
if (_resBody && _resBody.hits && _resBody.hits.hits) { // we're signing up all objects returned to the client to be able to validate them when (for example order)
const factory = new ProcessorFactory(config)
const tagsArray = []
if (config.server.useOutputCache && cache) {
const tagPrefix = entityType[0].toUpperCase() // first letter of entity name: P, T, A ...
tagsArray.push(entityType)
_resBody.hits.hits.map(item => {
if (item._source.id) { // has common identifier
tagsArray.push(`${tagPrefix}${item._source.id}`)
}
})
}

let resultProcessor = factory.getAdapter(entityType, indexName, req, res)

if (!resultProcessor) { resultProcessor = factory.getAdapter('default', indexName, req, res) } // get the default processor
if (entityType === 'product') {
resultProcessor.process(_resBody.hits.hits, groupId).then(async (result) => {
_resBody.hits.hits = result
if (config.get('varnish.enabled')) {
// Add tags to cache, so we can display them in response headers then
_cacheStorageHandler(config, {
..._resBody,
tags: tagsArray
}, reqHash, tagsArray)
} else {
_cacheStorageHandler(config, _resBody, reqHash, tagsArray)
}
if (_resBody.aggregations && config.entities.attribute.loadByAttributeMetadata) {
const attributeListParam = AttributeService.transformAggsToAttributeListParam(_resBody.aggregations)
// find attribute list
const attributeList = await AttributeService.list(attributeListParam, config, indexName)
_resBody.attribute_metadata = attributeList.map(AttributeService.transformToMetadata)
}
res.json(_outputFormatter(_resBody, responseFormat));
}).catch((err) => {
console.error(err)
})
} else {
resultProcessor.process(_resBody.hits.hits).then((result) => {
_resBody.hits.hits = result
if (config.get('varnish.enabled')) {
// Add tags to cache, so we can display them in response headers then
_cacheStorageHandler(config, {
..._resBody,
tags: tagsArray
}, reqHash, tagsArray)
} else {
_cacheStorageHandler(config, _resBody, reqHash, tagsArray)
}
res.json(_outputFormatter(_resBody, responseFormat));
}).catch((err) => {
console.error(err)
})
}, async (_err, _res, _resBody) => { // TODO: add caching layer to speed up SSR? How to invalidate products (checksum on the response BEFORE processing it)
if (_err || _resBody.error) {
apiError(res, _err || _resBody.error);
return
}
try {
if (_resBody && _resBody.hits && _resBody.hits.hits) { // we're signing up all objects returned to the client to be able to validate them when (for example order)
const factory = new ProcessorFactory(config)
const tagsArray = []
if (config.server.useOutputCache && cache) {
const tagPrefix = entityType[0].toUpperCase() // first letter of entity name: P, T, A ...
tagsArray.push(entityType)
_resBody.hits.hits.map(item => {
if (item._source.id) { // has common identifier
tagsArray.push(`${tagPrefix}${item._source.id}`)
}
})
const cacheTags = tagsArray.join(' ')
res.setHeader('X-VS-Cache-Tags', cacheTags)
}

let resultProcessor = factory.getAdapter(entityType, indexName, req, res)

if (!resultProcessor) { resultProcessor = factory.getAdapter('default', indexName, req, res) } // get the default processor

const productGroupId = entityType === 'product' ? groupId : undefined
const result = await resultProcessor.process(_resBody.hits.hits, productGroupId)
_resBody.hits.hits = result
if (entityType === 'product' && _resBody.aggregations && config.entities.attribute.loadByAttributeMetadata) {
const attributeListParam = AttributeService.transformAggsToAttributeListParam(_resBody.aggregations)
// find attribute list
const attributeList = await AttributeService.list(attributeListParam, config, indexName)
_resBody.attribute_metadata = attributeList.map(AttributeService.transformToMetadata)
}
if (config.get('varnish.enabled')) {
// Add tags to cache, so we can display them in response headers then
_cacheStorageHandler(config, {
..._resBody,
tags: tagsArray
}, reqHash, tagsArray)
} else {
_cacheStorageHandler(config, _resBody, reqHash, tagsArray)
}
res.json(_outputFormatter(_resBody, responseFormat));
} else { // no cache storage if no results from Elastic
res.json(_resBody);
}
} else { // no cache storage if no results from Elastic
res.json(_resBody);
} catch (err) {
apiError(res, err);
}
});
}
Expand Down

0 comments on commit 42df865

Please sign in to comment.