13
13
import { errorResponse , errorWithResponse , ffetch } from '../utils/http.js' ;
14
14
import getProductQuery , { adapter as productAdapter } from './queries/cs-product.js' ;
15
15
import getVariantsQuery , { adapter as variantsAdapter } from './queries/cs-variants.js' ;
16
- import getProductSKUQuery from './queries/core-product-sku.js' ;
16
+ import getProductSKUQueryCore from './queries/core-product-sku.js' ;
17
+ import getProductSKUQueryCS from './queries/cs-product-sku.js' ;
17
18
import htmlTemplateFromContext from '../templates/html/index.js' ;
18
19
19
20
/**
@@ -22,7 +23,11 @@ import htmlTemplateFromContext from '../templates/html/index.js';
22
23
*/
23
24
async function fetchProduct ( sku , config ) {
24
25
const { catalogEndpoint = 'https://catalog-service.adobe.io/graphql' } = config ;
25
- const query = getProductQuery ( { sku, imageRoles : config . imageRoles } ) ;
26
+ const query = getProductQuery ( {
27
+ sku,
28
+ imageRoles : config . imageRoles ,
29
+ linkTypes : config . linkTypes ,
30
+ } ) ;
26
31
console . debug ( query ) ;
27
32
28
33
const resp = await ffetch ( `${ catalogEndpoint } ?query=${ encodeURIComponent ( query ) } &view=${ config . storeViewCode } ` , {
@@ -111,8 +116,53 @@ async function fetchVariants(sku, config) {
111
116
* @param {string } urlkey
112
117
* @param {Config } config
113
118
*/
114
- async function lookupProductSKU ( urlkey , config ) {
115
- const query = getProductSKUQuery ( { urlkey } ) ;
119
+ async function lookupProductSKUCS ( urlkey , config ) {
120
+ const { catalogEndpoint = 'https://catalog-service.adobe.io/graphql' } = config ;
121
+ const query = getProductSKUQueryCS ( { urlkey } ) ;
122
+ console . debug ( query ) ;
123
+
124
+ const resp = await ffetch ( `${ catalogEndpoint } ?query=${ encodeURIComponent ( query ) } ` , {
125
+ headers : {
126
+ origin : config . origin ?? 'https://api.adobecommerce.live' ,
127
+ 'x-api-key' : config . apiKey ,
128
+ 'Magento-Environment-Id' : config . magentoEnvironmentId ,
129
+ 'Magento-Website-Code' : config . magentoWebsiteCode ,
130
+ 'Magento-Store-View-Code' : config . storeViewCode ,
131
+ 'Magento-Store-Code' : config . storeCode ,
132
+ ...config . headers ,
133
+ } ,
134
+ // don't disable cache, since it's unlikely to change
135
+ } ) ;
136
+ if ( ! resp . ok ) {
137
+ console . warn ( 'failed to fetch product sku (cs): ' , resp . status , resp . statusText ) ;
138
+ try {
139
+ console . info ( 'body: ' , await resp . text ( ) ) ;
140
+ } catch { /* noop */ }
141
+ throw errorWithResponse ( resp . status , 'failed to fetch product sku (cs)' ) ;
142
+ }
143
+
144
+ try {
145
+ const json = await resp . json ( ) ;
146
+ const [ product ] = json ?. data ?. productSearch . items ?? [ ] ;
147
+ if ( ! product ?. product ?. sku ) {
148
+ throw errorWithResponse ( 404 , 'could not find product sku (cs)' , json . errors ) ;
149
+ }
150
+ return product . product . sku ;
151
+ } catch ( e ) {
152
+ console . error ( 'failed to parse product sku (cs): ' , e ) ;
153
+ if ( e . response ) {
154
+ throw errorWithResponse ( e . response . status , e . message ) ;
155
+ }
156
+ throw errorWithResponse ( 500 , 'failed to parse product sku response (cs)' ) ;
157
+ }
158
+ }
159
+
160
+ /**
161
+ * @param {string } urlkey
162
+ * @param {Config } config
163
+ */
164
+ async function lookupProductSKUCore ( urlkey , config ) {
165
+ const query = getProductSKUQueryCore ( { urlkey } ) ;
116
166
if ( ! config . coreEndpoint ) {
117
167
throw errorResponse ( 400 , 'missing coreEndpoint' ) ;
118
168
}
@@ -127,27 +177,38 @@ async function lookupProductSKU(urlkey, config) {
127
177
// don't disable cache, since it's unlikely to change
128
178
} ) ;
129
179
if ( ! resp . ok ) {
130
- console . warn ( 'failed to fetch product sku: ' , resp . status , resp . statusText ) ;
180
+ console . warn ( 'failed to fetch product sku (core) : ' , resp . status , resp . statusText ) ;
131
181
try {
132
182
console . info ( 'body: ' , await resp . text ( ) ) ;
133
183
} catch { /* noop */ }
134
- throw errorWithResponse ( resp . status , 'failed to fetch product sku' ) ;
184
+ throw errorWithResponse ( resp . status , 'failed to fetch product sku (core) ' ) ;
135
185
}
136
186
137
187
try {
138
188
const json = await resp . json ( ) ;
139
189
const [ product ] = json ?. data ?. products ?. items ?? [ ] ;
140
190
if ( ! product ?. sku ) {
141
- throw errorWithResponse ( 404 , 'could not find product sku' , json . errors ) ;
191
+ throw errorWithResponse ( 404 , 'could not find product sku (core) ' , json . errors ) ;
142
192
}
143
193
return product . sku ;
144
194
} catch ( e ) {
145
- console . error ( 'failed to parse product sku: ' , e ) ;
195
+ console . error ( 'failed to parse product sku (core) : ' , e ) ;
146
196
if ( e . response ) {
147
197
throw errorWithResponse ( e . response . status , e . message ) ;
148
198
}
149
- throw errorWithResponse ( 500 , 'failed to parse product sku response' ) ;
199
+ throw errorWithResponse ( 500 , 'failed to parse product sku response (core)' ) ;
200
+ }
201
+ }
202
+
203
+ /**
204
+ * @param {string } urlkey
205
+ * @param {Config } config
206
+ */
207
+ function lookupProductSKU ( urlkey , config ) {
208
+ if ( config . liveSearchEnabled ) {
209
+ return lookupProductSKUCS ( urlkey , config ) ;
150
210
}
211
+ return lookupProductSKUCore ( urlkey , config ) ;
151
212
}
152
213
153
214
/**
0 commit comments