Skip to content

Commit 0f54de5

Browse files
authored
fix: lookup urlkey for sku, adapt product (#77)
* fix: lookup urlkey for sku, adapt product * chore: fix tesst
1 parent 04f5d1a commit 0f54de5

File tree

3 files changed

+20
-32
lines changed

3 files changed

+20
-32
lines changed

src/catalog/storage/r2.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,15 @@ 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 ?? []).forEach((variant) => {
50+
variant.attributeMap = Object.fromEntries((variant.attributes ?? [])
51+
.map(({ name, value }) => [name, value]));
52+
});
53+
54+
return productData;
4855
}
4956

5057
/**

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();

test/catalog/storage/r2.test.js

+4-29
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('StorageClient Class Tests', () => {
7474
env: {
7575
CATALOG_BUCKET: {
7676
get: sinon.stub().resolves({
77-
text: sinon.stub().resolves(JSON.stringify({ sku: 'sku1', name: 'Test Product' })),
77+
json: sinon.stub().resolves({ sku: 'sku1', name: 'Test Product' }),
7878
}),
7979
},
8080
},
@@ -86,7 +86,9 @@ describe('StorageClient Class Tests', () => {
8686

8787
assert(ctx.log.debug.calledOnceWithExactly('Fetching product from R2:', 'org/site/store/view/products/sku1.json'));
8888
assert(ctx.env.CATALOG_BUCKET.get.calledOnceWithExactly('org/site/store/view/products/sku1.json'));
89-
assert.deepStrictEqual(product, { sku: 'sku1', name: 'Test Product' });
89+
assert.deepStrictEqual(product, {
90+
sku: 'sku1', attributeMap: {}, name: 'Test Product',
91+
});
9092
});
9193

9294
it('should throw 404 error if product not found', async () => {
@@ -117,33 +119,6 @@ describe('StorageClient Class Tests', () => {
117119
assert.strictEqual(thrownError, error);
118120
});
119121

120-
it('should throw error if JSON parsing fails', async () => {
121-
const ctx = {
122-
log: { debug: sinon.stub() },
123-
env: {
124-
CATALOG_BUCKET: {
125-
get: sinon.stub().resolves({
126-
text: sinon.stub().resolves('invalid json'),
127-
}),
128-
},
129-
},
130-
};
131-
132-
const sku = 'sku1';
133-
const client = new StorageClient(ctx, config);
134-
135-
let thrownError;
136-
try {
137-
await client.fetchProduct(sku);
138-
} catch (e) {
139-
thrownError = e;
140-
}
141-
142-
assert(ctx.log.debug.calledOnceWithExactly('Fetching product from R2:', 'org/site/store/view/products/sku1.json'));
143-
assert(ctx.env.CATALOG_BUCKET.get.calledOnceWithExactly('org/site/store/view/products/sku1.json'));
144-
assert(thrownError instanceof SyntaxError);
145-
});
146-
147122
it('should propagate errors from CATALOG_BUCKET.get', async () => {
148123
const ctx = {
149124
log: { debug: sinon.stub() },

0 commit comments

Comments
 (0)