Skip to content

Commit ee83a69

Browse files
committed
fix: allow origin and catalog endpoints in config
1 parent 9ccfde9 commit ee83a69

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

src/index.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,24 @@ import { resolveConfig } from './config.js';
2323
* @param {Config} config
2424
*/
2525
async function fetchProductCS(sku, config) {
26+
console.log('config: ', config);
27+
const { catalogEndpoint = 'https://catalog-service.adobe.io/graphql' } = config;
2628
const query = getProductQueryCS({ sku });
27-
28-
const resp = await fetch(`https://catalog-service.adobe.io/graphql?query=${encodeURIComponent(query)}`, {
29+
const resp = await fetch(`${catalogEndpoint}?query=${encodeURIComponent(query)}`, {
2930
headers: {
30-
origin: 'https://api.adobecommerce.live',
31+
origin: config.origin ?? 'https://api.adobecommerce.live',
3132
'x-api-key': config.apiKey,
3233
'Magento-Environment-Id': config.magentoEnvironmentId,
3334
'Magento-Website-Code': config.magentoWebsiteCode,
3435
'Magento-Store-View-Code': config.magentoStoreViewCode,
36+
'Magento-Store-Code': config.magentoStoreCode,
3537
},
3638
});
3739
if (!resp.ok) {
3840
console.warn('failed to fetch product: ', resp.status, resp.statusText);
41+
try {
42+
console.info('body: ', await resp.text());
43+
} catch { /* noop */ }
3944
throw errorWithResponse(resp.status, 'failed to fetch product');
4045
}
4146

@@ -66,15 +71,19 @@ async function fetchProductCore(opt, config) {
6671

6772
const resp = await fetch(`${config.coreEndpoint}?query=${encodeURIComponent(query)}`, {
6873
headers: {
69-
origin: 'https://api.adobecommerce.live',
74+
origin: config.origin ?? 'https://api.adobecommerce.live',
7075
'x-api-key': config.apiKey,
7176
'Magento-Environment-Id': config.magentoEnvironmentId,
7277
'Magento-Website-Code': config.magentoWebsiteCode,
7378
'Magento-Store-View-Code': config.magentoStoreViewCode,
79+
'Magento-Store-Code': config.magentoStoreCode,
7480
},
7581
});
7682
if (!resp.ok) {
7783
console.warn('failed to fetch product: ', resp.status, resp.statusText);
84+
try {
85+
console.info('body: ', await resp.text());
86+
} catch { /* noop */ }
7887
throw errorWithResponse(resp.status, 'failed to fetch product');
7988
}
8089

@@ -99,19 +108,24 @@ async function lookupProductSKU(urlkey, config) {
99108
const query = getProductSKUQuery({ urlkey });
100109
const resp = await fetch(`${config.coreEndpoint}?query=${encodeURIComponent(query)}`, {
101110
headers: {
102-
origin: 'https://api.adobecommerce.live',
111+
origin: config.origin ?? 'https://api.adobecommerce.live',
103112
'x-api-key': config.apiKey,
104113
'Magento-Environment-Id': config.magentoEnvironmentId,
105114
'Magento-Website-Code': config.magentoWebsiteCode,
106115
'Magento-Store-View-Code': config.magentoStoreViewCode,
116+
'Magento-Store-Code': config.magentoStoreCode,
107117
},
108118
});
109119
if (!resp.ok) {
110120
console.warn('failed to fetch product sku: ', resp.status, resp.statusText);
121+
try {
122+
console.info('body: ', await resp.text());
123+
} catch { /* noop */ }
111124
throw errorWithResponse(resp.status, 'failed to fetch product sku');
112125
}
113126

114127
const json = await resp.json();
128+
console.log('json: ', JSON.stringify(json, undefined, 2));
115129
try {
116130
const [product] = json.data.products.items;
117131
if (!product) {

src/templates/html.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ export default (product) => {
4747
<title>${metaTitle || name}</title>
4848
<meta property="description" content="${metaDescription || description}">
4949
<meta property="og:title" content="${metaTitle || name}">
50-
<meta property="og:image" content="${images[0].url}">
51-
<meta property="og:image:secure_url" content="${images[0].url}">
50+
<meta property="og:image" content="${images[0]?.url}">
51+
<meta property="og:image:secure_url" content="${images[0]?.url}">
5252
<meta property="og:type" content="og:product">
5353
<meta name="twitter:card" content="summary_large_image">
5454
<meta name="twitter:title" content="${metaTitle || name}">
55-
<meta name="twitter:image" content="${images[0].url}">
55+
<meta name="twitter:image" content="${images[0]?.url}">
5656
<meta name="keywords" content="${metaKeyword}">
5757
<meta name="sku" content="${sku}">
5858
<meta name="urlKey" content="${urlKey}">

src/templates/json-ld.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default (product) => {
3131
prices,
3232
} = product;
3333

34-
const image = images?.[0].url;
34+
const image = images?.[0]?.url;
3535
const brandName = attributes.find((attr) => attr.name === 'brand')?.value;
3636

3737
return JSON.stringify(pruneUndefined({

src/types.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import type { ExecutionContext, KVNamespace } from "@cloudflare/workers-types/ex
33
declare global {
44
export interface Config {
55
pageType: 'product' | string;
6+
origin?: string;
67
apiKey: string;
78
magentoEnvironmentId: string;
89
magentoWebsiteCode: string;
910
magentoStoreViewCode: string;
1011
magentoStoreCode: string;
1112
coreEndpoint: string;
13+
catalogEndpoint?: string;
1214
params: Record<string, string>;
1315
}
1416

0 commit comments

Comments
 (0)