Skip to content

Commit 0d5dfbe

Browse files
committed
fix: lookup urlkey for sku, adapt product
1 parent 04f5d1a commit 0d5dfbe

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/catalog/storage/r2.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,16 @@ export default class StorageClient {
4343
throw errorWithResponse(404, 'Product not found');
4444
}
4545

46-
const productData = await object.text();
47-
return JSON.parse(productData);
46+
const productData = await object.json();
47+
productData.attributeMap = Object.fromEntries((productData.attributes ?? [])
48+
.map(({ name, value }) => [name, value]));
49+
productData.variants = productData.variants || [];
50+
productData.variants.forEach((variant) => {
51+
variant.attributeMap = Object.fromEntries((variant.attributes ?? [])
52+
.map(({ name, value }) => [name, value]));
53+
});
54+
55+
return productData;
4856
}
4957

5058
/**

src/content/helix-commerce.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ import htmlTemplateFromContext from '../templates/html/index.js';
2121
export async function handle(ctx) {
2222
const { config } = ctx;
2323
const { urlkey } = config.params;
24-
const { sku } = config.params;
24+
let { sku } = config.params;
2525

2626
if (!sku && !urlkey) {
2727
return errorResponse(404, 'missing sku or urlkey');
2828
}
2929

3030
const storageClient = new StorageClient(ctx, config);
31+
if (!sku) {
32+
sku = await storageClient.lookupSku(urlkey);
33+
if (!sku) {
34+
return errorResponse(404, 'could not find sku');
35+
}
36+
}
3137

3238
const product = await storageClient.fetchProduct(sku);
3339
const html = htmlTemplateFromContext(ctx, product, product.variants).render();

0 commit comments

Comments
 (0)