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

Commit 44a753e

Browse files
author
tkostuch
committed
Merge branch 'bugfix/437' of github.com:gibkigonzo/vue-storefront-api into bugfix/437
2 parents 5c431e5 + 3b1b3e2 commit 44a753e

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
- Fix MSI default stock id value
2828
- Add outputFormatter to response from cache - @gibkigonzo (#428)
2929
- disable showing stack for invalid requests - @gibkigonzo (#431)
30+
- Improve `_outputFormatter` on cache catalog-response to prevent exception - @cewald (#432)
3031
- use ts for compiling o2m modules - @gibkigonzo (#437)
3132

32-
3333
## [1.11.1] - 2020.03.17
3434

3535
### Added

src/api/catalog.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import jwt from 'jwt-simple';
2-
import request from 'request';
3-
import ProcessorFactory from '../processor/factory';
1+
import jwt from 'jwt-simple'
2+
import request from 'request'
3+
import ProcessorFactory from '../processor/factory'
44
import { adjustBackendProxyUrl } from '../lib/elastic'
55
import cache from '../lib/cache-instance'
66
import { sha3_224 } from 'js-sha3'
@@ -10,9 +10,9 @@ import loadCustomFilters from '../helpers/loadCustomFilters'
1010
import { elasticsearch, SearchQuery } from 'storefront-query-builder'
1111
import { apiError } from '../lib/util'
1212

13-
function _cacheStorageHandler (config, result, hash, tags) {
13+
async function _cacheStorageHandler (config, result, hash, tags) {
1414
if (config.server.useOutputCache && cache) {
15-
cache.set(
15+
return cache.set(
1616
'api:' + hash,
1717
result,
1818
tags
@@ -61,12 +61,12 @@ export default ({config, db}) => async function (req, res, body) {
6161
}
6262
if (req.query.response_format) responseFormat = req.query.response_format
6363

64-
const urlSegments = req.url.split('/');
64+
const urlSegments = req.url.split('/')
6565

6666
let indexName = ''
6767
let entityType = ''
6868
if (urlSegments.length < 2) { throw new Error('No index name given in the URL. Please do use following URL format: /api/catalog/<index_name>/<entity_type>_search') } else {
69-
indexName = urlSegments[1];
69+
indexName = urlSegments[1]
7070

7171
if (urlSegments.length > 2) { entityType = urlSegments[2] }
7272

@@ -94,14 +94,14 @@ export default ({config, db}) => async function (req, res, body) {
9494
delete requestBody.groupToken
9595
delete requestBody.groupId
9696

97-
let auth = null;
97+
let auth = null
9898

9999
// Only pass auth if configured
100100
if (config.elasticsearch.user || config.elasticsearch.password) {
101101
auth = {
102102
user: config.elasticsearch.user,
103103
pass: config.elasticsearch.password
104-
};
104+
}
105105
}
106106
const s = Date.now()
107107
const reqHash = sha3_224(`${JSON.stringify(requestBody)}${req.url}`)
@@ -114,7 +114,7 @@ export default ({config, db}) => async function (req, res, body) {
114114
auth: auth
115115
}, async (_err, _res, _resBody) => { // TODO: add caching layer to speed up SSR? How to invalidate products (checksum on the response BEFORE processing it)
116116
if (_err || _resBody.error) {
117-
apiError(res, _err || _resBody.error);
117+
apiError(res, _err || _resBody.error)
118118
return
119119
}
120120
try {
@@ -146,6 +146,9 @@ export default ({config, db}) => async function (req, res, body) {
146146
const attributeList = await AttributeService.list(attributeListParam, config, indexName)
147147
_resBody.attribute_metadata = attributeList.map(AttributeService.transformToMetadata)
148148
}
149+
150+
_resBody = _outputFormatter(_resBody, responseFormat)
151+
149152
if (config.get('varnish.enabled')) {
150153
// Add tags to cache, so we can display them in response headers then
151154
_cacheStorageHandler(config, {
@@ -155,14 +158,13 @@ export default ({config, db}) => async function (req, res, body) {
155158
} else {
156159
_cacheStorageHandler(config, _resBody, reqHash, tagsArray)
157160
}
158-
res.json(_outputFormatter(_resBody, responseFormat));
159-
} else { // no cache storage if no results from Elastic
160-
res.json(_resBody);
161161
}
162+
163+
res.json(_resBody)
162164
} catch (err) {
163-
apiError(res, err);
165+
apiError(res, err)
164166
}
165-
});
167+
})
166168
}
167169

168170
if (config.server.useOutputCache && cache) {
@@ -176,7 +178,7 @@ export default ({config, db}) => async function (req, res, body) {
176178
res.setHeader('X-VS-Cache-Tag', tagsHeader)
177179
delete output.tags
178180
}
179-
res.json(_outputFormatter(output, responseFormat));
181+
res.json(output)
180182
console.log(`cache hit [${req.url}], cached request: ${Date.now() - s}ms`)
181183
} else {
182184
res.setHeader('X-VS-Cache', 'Miss')

0 commit comments

Comments
 (0)