Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow origin and catalog endpoints in config #12

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ import { resolveConfig } from './config.js';
* @param {Config} config
*/
async function fetchProductCS(sku, config) {
const { catalogEndpoint = 'https://catalog-service.adobe.io/graphql' } = config;
const query = getProductQueryCS({ sku });

const resp = await fetch(`https://catalog-service.adobe.io/graphql?query=${encodeURIComponent(query)}`, {
const resp = await fetch(`${catalogEndpoint}?query=${encodeURIComponent(query)}`, {
headers: {
origin: 'https://api.adobecommerce.live',
origin: config.origin ?? 'https://api.adobecommerce.live',
'x-api-key': config.apiKey,
'Magento-Environment-Id': config.magentoEnvironmentId,
'Magento-Website-Code': config.magentoWebsiteCode,
'Magento-Store-View-Code': config.magentoStoreViewCode,
'Magento-Store-Code': config.magentoStoreCode,
},
});
if (!resp.ok) {
console.warn('failed to fetch product: ', resp.status, resp.statusText);
try {
console.info('body: ', await resp.text());
} catch { /* noop */ }
throw errorWithResponse(resp.status, 'failed to fetch product');
}

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

const resp = await fetch(`${config.coreEndpoint}?query=${encodeURIComponent(query)}`, {
headers: {
origin: 'https://api.adobecommerce.live',
origin: config.origin ?? 'https://api.adobecommerce.live',
'x-api-key': config.apiKey,
'Magento-Environment-Id': config.magentoEnvironmentId,
'Magento-Website-Code': config.magentoWebsiteCode,
'Magento-Store-View-Code': config.magentoStoreViewCode,
'Magento-Store-Code': config.magentoStoreCode,
},
});
if (!resp.ok) {
console.warn('failed to fetch product: ', resp.status, resp.statusText);
try {
console.info('body: ', await resp.text());
} catch { /* noop */ }
throw errorWithResponse(resp.status, 'failed to fetch product');
}

Expand All @@ -99,19 +107,24 @@ async function lookupProductSKU(urlkey, config) {
const query = getProductSKUQuery({ urlkey });
const resp = await fetch(`${config.coreEndpoint}?query=${encodeURIComponent(query)}`, {
headers: {
origin: 'https://api.adobecommerce.live',
origin: config.origin ?? 'https://api.adobecommerce.live',
'x-api-key': config.apiKey,
'Magento-Environment-Id': config.magentoEnvironmentId,
'Magento-Website-Code': config.magentoWebsiteCode,
'Magento-Store-View-Code': config.magentoStoreViewCode,
'Magento-Store-Code': config.magentoStoreCode,
},
});
if (!resp.ok) {
console.warn('failed to fetch product sku: ', resp.status, resp.statusText);
try {
console.info('body: ', await resp.text());
} catch { /* noop */ }
throw errorWithResponse(resp.status, 'failed to fetch product sku');
}

const json = await resp.json();
console.log('json: ', JSON.stringify(json, undefined, 2));
try {
const [product] = json.data.products.items;
if (!product) {
Expand Down Expand Up @@ -193,6 +206,7 @@ export default {
try {
const overrides = Object.fromEntries(ctx.url.searchParams.entries());
const config = await resolveConfig(ctx, tenant, overrides);
console.debug('resolved config: ', JSON.stringify(config, undefined, 2));
if (!config) {
return errorResponse(404, 'config not found');
}
Expand Down
6 changes: 3 additions & 3 deletions src/templates/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ export default (product) => {
<title>${metaTitle || name}</title>
<meta property="description" content="${metaDescription || description}">
<meta property="og:title" content="${metaTitle || name}">
<meta property="og:image" content="${images[0].url}">
<meta property="og:image:secure_url" content="${images[0].url}">
<meta property="og:image" content="${images[0]?.url}">
<meta property="og:image:secure_url" content="${images[0]?.url}">
<meta property="og:type" content="og:product">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="${metaTitle || name}">
<meta name="twitter:image" content="${images[0].url}">
<meta name="twitter:image" content="${images[0]?.url}">
<meta name="keywords" content="${metaKeyword}">
<meta name="sku" content="${sku}">
<meta name="urlKey" content="${urlKey}">
Expand Down
2 changes: 1 addition & 1 deletion src/templates/json-ld.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default (product) => {
prices,
} = product;

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

return JSON.stringify(pruneUndefined({
Expand Down
2 changes: 2 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import type { ExecutionContext, KVNamespace } from "@cloudflare/workers-types/ex
declare global {
export interface Config {
pageType: 'product' | string;
origin?: string;
apiKey: string;
magentoEnvironmentId: string;
magentoWebsiteCode: string;
magentoStoreViewCode: string;
magentoStoreCode: string;
coreEndpoint: string;
catalogEndpoint?: string;
params: Record<string, string>;
}

Expand Down