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'
44import { adjustBackendProxyUrl } from '../lib/elastic'
55import cache from '../lib/cache-instance'
66import { sha3_224 } from 'js-sha3'
@@ -10,9 +10,9 @@ import loadCustomFilters from '../helpers/loadCustomFilters'
1010import { elasticsearch , SearchQuery } from 'storefront-query-builder'
1111import { 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