Skip to content

Commit 90bce9d

Browse files
committed
fix: use ctx config
1 parent 23a75aa commit 90bce9d

14 files changed

+120
-121
lines changed

src/catalog/fetch.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ import { fetchProduct } from '../utils/r2.js';
1616
/**
1717
* Handles a GET request for a product.
1818
* @param {Context} ctx - The context object containing request information and utilities.
19-
* @param {Config} config - The configuration object with application settings.
2019
* @returns {Promise<Response>} - A promise that resolves to the product response.
2120
*/
22-
export async function handleProductFetchRequest(ctx, config) {
21+
export async function handleProductFetchRequest(ctx) {
2322
try {
2423
const sku = ctx.url.pathname.split('/').pop();
25-
const product = await fetchProduct(ctx, config, sku);
24+
const product = await fetchProduct(ctx, sku);
2625

2726
return new Response(JSON.stringify(product), {
2827
headers: { 'Content-Type': 'application/json' },

src/catalog/handler.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ const ALLOWED_METHODS = ['GET', 'PUT'];
2121
/**
2222
* Handles the catalog request.
2323
* @param {Context} ctx - The context object containing request information and utilities.
24-
* @param {Config} config - The configuration object with application settings.
2524
* @param {Request} request - The request object.
2625
* @returns {Promise<Response>} - A promise that resolves to the catalog response.
2726
*/
28-
export default async function catalogHandler(ctx, config, request) {
27+
export default async function catalogHandler(ctx, request) {
28+
const { config } = ctx;
2929
const { method } = ctx.info;
3030

3131
// Split the pathname into segments and filter out empty strings
@@ -56,13 +56,13 @@ export default async function catalogHandler(ctx, config, request) {
5656

5757
if (subRoute === 'lookup') {
5858
if (ctx.info.method === 'GET') {
59-
return handleProductLookupRequest(ctx, config);
59+
return handleProductLookupRequest(ctx);
6060
}
6161
return errorResponse(405, 'method not allowed');
6262
}
6363

6464
if (ctx.info.method === 'PUT') {
65-
return handleProductSaveRequest(ctx, config, request);
65+
return handleProductSaveRequest(ctx, request);
6666
}
67-
return handleProductFetchRequest(ctx, config);
67+
return handleProductFetchRequest(ctx);
6868
}

src/catalog/lookup.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ import { listAllProducts, lookupSku } from '../utils/r2.js';
1616
/**
1717
* Handles a product lookup request.
1818
* @param {Context} ctx - The context object.
19-
* @param {Config} config - The configuration object.
2019
* @returns {Promise<Response>} - A promise that resolves to the product response.
2120
*/
22-
export async function handleProductLookupRequest(ctx, config) {
21+
export async function handleProductLookupRequest(ctx) {
2322
try {
23+
const { config } = ctx;
2424
const { search } = ctx.url;
2525
const params = new URLSearchParams(search);
2626

2727
if (params.has('urlKey') || params.has('urlkey')) {
2828
const urlkey = params.get('urlKey') || params.get('urlkey');
29-
const sku = await lookupSku(ctx, config, urlkey);
29+
const sku = await lookupSku(ctx, urlkey);
3030
return new Response(undefined, {
3131
status: 301,
3232
headers: {
@@ -35,7 +35,7 @@ export async function handleProductLookupRequest(ctx, config) {
3535
});
3636
}
3737

38-
const products = await listAllProducts(ctx, config);
38+
const products = await listAllProducts(ctx);
3939

4040
const response = {
4141
total: products.length,

src/catalog/update.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,27 @@ import { saveProducts } from '../utils/r2.js';
1818
/**
1919
* Saves a product to R2.
2020
* @param {Context} ctx - The context object containing request information and utilities.
21-
* @param {Config} config - The configuration object with application settings.
2221
* @param {Object} product - The product object to be saved.
2322
* @returns {Promise<Object>} - A promise that resolves to the saved product.
2423
*/
25-
export async function putProduct(ctx, config, product) {
24+
export async function putProduct(ctx, product) {
2625
if (!product.sku) {
2726
throw errorWithResponse(400, 'invalid request body: missing sku');
2827
}
2928

30-
await saveProducts(ctx, config, [product]);
29+
await saveProducts(ctx, [product]);
3130
return product;
3231
}
3332

3433
/**
3534
* Handles a PUT request to update a product.
3635
* @param {Context} ctx - The context object containing request information and utilities.
37-
* @param {Config} config - The configuration object with application settings.
3836
* @param {Request} request - The request object.
3937
* @returns {Promise<Response>} - A promise that resolves to the product response.
4038
*/
41-
export async function handleProductSaveRequest(ctx, config, request) {
39+
export async function handleProductSaveRequest(ctx, request) {
4240
try {
41+
const { config } = ctx;
4342
let product;
4443

4544
if (config.sku === '*') {
@@ -74,7 +73,7 @@ export async function handleProductSaveRequest(ctx, config, request) {
7473
return errorResponse(404, 'no path patterns found');
7574
}
7675

77-
await putProduct(ctx, config, product);
76+
await putProduct(ctx, product);
7877

7978
const purgePaths = matchedPathPatterns.map(
8079
(pattern) => pattern

src/content/adobe-commerce.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ async function lookupProductSKU(urlkey, config) {
149149

150150
/**
151151
* @param {Context} ctx
152-
* @param {Config} config
153152
* @returns {Promise<Response>}
154153
*/
155-
export async function handle(ctx, config) {
154+
export async function handle(ctx) {
155+
const { config } = ctx;
156156
const { urlkey } = config.params;
157157
let { sku } = config.params;
158158

src/content/handler.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ const ALLOWED_METHODS = ['GET'];
1818

1919
/**
2020
* @param {Context} ctx
21-
* @param {Config} config
2221
* @returns {Promise<Response>}
2322
*/
24-
export default async function contentHandler(ctx, config) {
23+
export default async function contentHandler(ctx) {
2524
if (!ALLOWED_METHODS.includes(ctx.info.method)) {
2625
return errorResponse(405, 'method not allowed');
2726
}
2827

28+
const { config } = ctx;
2929
if (!config.pageType) {
3030
return errorResponse(400, 'invalid config for tenant site (missing pageType)');
3131
}
3232
console.log('config: ', JSON.stringify(config, null, 2));
3333

3434
if (config.catalogSource === 'helix') {
35-
return handleHelixCommerce(ctx, config);
35+
return handleHelixCommerce(ctx);
3636
}
37-
return handleAdobeCommerce(ctx, config);
37+
return handleAdobeCommerce(ctx);
3838
}

src/content/helix-commerce.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ import htmlTemplateFromContext from '../templates/html/index.js';
1616

1717
/**
1818
* @param {Context} ctx
19-
* @param {Config} config
2019
* @returns {Promise<Response>}
2120
*/
22-
export async function handle(ctx, config) {
21+
export async function handle(ctx) {
22+
const { config } = ctx;
2323
const { urlkey } = config.params;
2424
const { sku } = config.params;
2525

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

30-
const product = await fetchProduct(ctx, config, sku);
30+
const product = await fetchProduct(ctx, sku);
3131
const html = htmlTemplateFromContext(ctx, product, product.variants).render();
3232
return new Response(html, {
3333
status: 200,

src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import content from './content/handler.js';
1616
import catalog from './catalog/handler.js';
1717

1818
/**
19-
* @type {Record<string, (ctx: Context, config: Config, request: Request) => Promise<Response>>}
19+
* @type {Record<string, (ctx: Context, request: Request) => Promise<Response>>}
2020
*/
2121
const handlers = {
2222
content,
2323
catalog,
2424
// eslint-disable-next-line no-unused-vars
25-
graphql: async (ctx, config) => errorResponse(501, 'not implemented'),
25+
graphql: async (ctx) => errorResponse(501, 'not implemented'),
2626
};
2727

2828
/**
@@ -67,7 +67,7 @@ export default {
6767
return errorResponse(404, 'config not found');
6868
}
6969

70-
return await handlers[config.route](ctx, config, request);
70+
return await handlers[config.route](ctx, request);
7171
} catch (e) {
7272
if (e.response) {
7373
return e.response;

src/utils/r2.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import { errorWithResponse } from './http.js';
1717
/**
1818
* Load product by SKU
1919
* @param {Context} ctx - The context object.
20-
* @param {Config} config - The config object.
2120
* @param {string} sku - The SKU of the product.
2221
* @returns {Promise<Product>} - A promise that resolves to the product.
2322
*/
24-
export async function fetchProduct(ctx, config, sku) {
23+
export async function fetchProduct(ctx, sku) {
24+
const { config } = ctx;
2525
const { log } = ctx;
2626
const key = `${config.org}/${config.site}/${config.storeCode}/${config.storeViewCode}/products/${sku}.json`;
2727
log.debug('Fetching product from R2:', key);
@@ -42,11 +42,11 @@ export async function fetchProduct(ctx, config, sku) {
4242
/**
4343
* Save products
4444
* @param {Context} ctx - The context object.
45-
* @param {Config} config - The config object.
4645
* @param {Product[]} products - The products to save.
4746
* @returns {Promise<void>} - A promise that resolves when the products are saved.
4847
*/
49-
export async function saveProducts(ctx, config, products) {
48+
export async function saveProducts(ctx, products) {
49+
const { config } = ctx;
5050
const { log } = ctx;
5151
const BATCH_SIZE = 50;
5252

@@ -92,11 +92,11 @@ export async function saveProducts(ctx, config, products) {
9292
/**
9393
* Resolve SKU from a URL key
9494
* @param {Context} ctx - The context object.
95-
* @param {Config} config - The config object.
9695
* @param {string} urlKey - The URL key.
9796
* @returns {Promise<string>} - A promise that resolves to the SKU.
9897
*/
99-
export async function lookupSku(ctx, config, urlKey) {
98+
export async function lookupSku(ctx, urlKey) {
99+
const { config } = ctx;
100100
// Make a HEAD request to retrieve the SKU from metadata based on the URL key
101101
const urlKeyPath = `${config.org}/${config.site}/${config.storeCode}/${config.storeViewCode}/urlkeys/${urlKey}`;
102102
const headResponse = await ctx.env.CATALOG_BUCKET.head(urlKeyPath);
@@ -112,10 +112,10 @@ export async function lookupSku(ctx, config, urlKey) {
112112
/**
113113
* List all products from R2
114114
* @param {Context} ctx - The context object.
115-
* @param {Config} config - The config object.
116115
* @returns {Promise<Product[]>} - A promise that resolves to the products.
117116
*/
118-
export async function listAllProducts(ctx, config) {
117+
export async function listAllProducts(ctx) {
118+
const { config } = ctx;
119119
const bucket = ctx.env.CATALOG_BUCKET;
120120

121121
const listResponse = await bucket.list({ prefix: `${config.org}/${config.site}/${config.storeCode}/${config.storeViewCode}/products/` });

test/catalog/fetch.test.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ describe('handleProductFetchRequest', () => {
2020
let fetchProductStub;
2121
let errorResponseStub;
2222
let ctx;
23-
let config;
2423

2524
beforeEach(async () => {
2625
fetchProductStub = sinon.stub();
@@ -38,8 +37,8 @@ describe('handleProductFetchRequest', () => {
3837
log: {
3938
error: sinon.stub(),
4039
},
40+
config: {},
4141
};
42-
config = {};
4342
});
4443

4544
afterEach(async () => {
@@ -52,20 +51,20 @@ describe('handleProductFetchRequest', () => {
5251
const product = { id: sku, name: 'Test Product' };
5352
fetchProductStub.resolves(product);
5453

55-
const response = await handleProductFetchRequest(ctx, config);
54+
const response = await handleProductFetchRequest(ctx);
5655

5756
assert.equal(response.headers.get('Content-Type'), 'application/json');
5857
const responseBody = await response.text();
5958
assert.equal(responseBody, JSON.stringify(product));
60-
sinon.assert.calledWith(fetchProductStub, ctx, config, sku);
59+
sinon.assert.calledWith(fetchProductStub, ctx, sku);
6160
});
6261

6362
it('should return e.response when fetchProduct throws an error with a response property', async () => {
6463
const errorResponse = new Response('Not Found', { status: 404 });
6564
const error = new ResponseError('Product not found', errorResponse);
6665
fetchProductStub.rejects(error);
6766

68-
const response = await handleProductFetchRequest(ctx, config);
67+
const response = await handleProductFetchRequest(ctx);
6968

7069
assert.strictEqual(response, errorResponse);
7170
sinon.assert.notCalled(ctx.log.error);
@@ -77,7 +76,7 @@ describe('handleProductFetchRequest', () => {
7776
const errorResp = new Response('Internal Server Error', { status: 500 });
7877
errorResponseStub.returns(errorResp);
7978

80-
const response = await handleProductFetchRequest(ctx, config);
79+
const response = await handleProductFetchRequest(ctx);
8180

8281
assert.equal(response.status, 500);
8382
const responseBody = await response.text();

0 commit comments

Comments
 (0)