Skip to content

Commit 741fab2

Browse files
authored
Merge pull request #12 from adobe-rnd/urlkey-search
fix: allow origin and catalog endpoints in config
2 parents 7804dfa + b2a0df7 commit 741fab2

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,23 @@ import { resolveConfig } from './config.js';
2323
* @param {Config} config
2424
*/
2525
async function fetchProductCS(sku, config) {
26+
const { catalogEndpoint = 'https://catalog-service.adobe.io/graphql' } = config;
2627
const query = getProductQueryCS({ sku });
27-
28-
const resp = await fetch(`https://catalog-service.adobe.io/graphql?query=${encodeURIComponent(query)}`, {
28+
const resp = await fetch(`${catalogEndpoint}?query=${encodeURIComponent(query)}`, {
2929
headers: {
30-
origin: 'https://api.adobecommerce.live',
30+
origin: config.origin ?? 'https://api.adobecommerce.live',
3131
'x-api-key': config.apiKey,
3232
'Magento-Environment-Id': config.magentoEnvironmentId,
3333
'Magento-Website-Code': config.magentoWebsiteCode,
3434
'Magento-Store-View-Code': config.magentoStoreViewCode,
35+
'Magento-Store-Code': config.magentoStoreCode,
3536
},
3637
});
3738
if (!resp.ok) {
3839
console.warn('failed to fetch product: ', resp.status, resp.statusText);
40+
try {
41+
console.info('body: ', await resp.text());
42+
} catch { /* noop */ }
3943
throw errorWithResponse(resp.status, 'failed to fetch product');
4044
}
4145

@@ -66,15 +70,19 @@ async function fetchProductCore(opt, config) {
6670

6771
const resp = await fetch(`${config.coreEndpoint}?query=${encodeURIComponent(query)}`, {
6872
headers: {
69-
origin: 'https://api.adobecommerce.live',
73+
origin: config.origin ?? 'https://api.adobecommerce.live',
7074
'x-api-key': config.apiKey,
7175
'Magento-Environment-Id': config.magentoEnvironmentId,
7276
'Magento-Website-Code': config.magentoWebsiteCode,
7377
'Magento-Store-View-Code': config.magentoStoreViewCode,
78+
'Magento-Store-Code': config.magentoStoreCode,
7479
},
7580
});
7681
if (!resp.ok) {
7782
console.warn('failed to fetch product: ', resp.status, resp.statusText);
83+
try {
84+
console.info('body: ', await resp.text());
85+
} catch { /* noop */ }
7886
throw errorWithResponse(resp.status, 'failed to fetch product');
7987
}
8088

@@ -99,19 +107,24 @@ async function lookupProductSKU(urlkey, config) {
99107
const query = getProductSKUQuery({ urlkey });
100108
const resp = await fetch(`${config.coreEndpoint}?query=${encodeURIComponent(query)}`, {
101109
headers: {
102-
origin: 'https://api.adobecommerce.live',
110+
origin: config.origin ?? 'https://api.adobecommerce.live',
103111
'x-api-key': config.apiKey,
104112
'Magento-Environment-Id': config.magentoEnvironmentId,
105113
'Magento-Website-Code': config.magentoWebsiteCode,
106114
'Magento-Store-View-Code': config.magentoStoreViewCode,
115+
'Magento-Store-Code': config.magentoStoreCode,
107116
},
108117
});
109118
if (!resp.ok) {
110119
console.warn('failed to fetch product sku: ', resp.status, resp.statusText);
120+
try {
121+
console.info('body: ', await resp.text());
122+
} catch { /* noop */ }
111123
throw errorWithResponse(resp.status, 'failed to fetch product sku');
112124
}
113125

114126
const json = await resp.json();
127+
console.log('json: ', JSON.stringify(json, undefined, 2));
115128
try {
116129
const [product] = json.data.products.items;
117130
if (!product) {
@@ -193,6 +206,7 @@ export default {
193206
try {
194207
const overrides = Object.fromEntries(ctx.url.searchParams.entries());
195208
const config = await resolveConfig(ctx, tenant, overrides);
209+
console.debug('resolved config: ', JSON.stringify(config, undefined, 2));
196210
if (!config) {
197211
return errorResponse(404, 'config not found');
198212
}

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)