Skip to content

Commit

Permalink
fix: use sku from path and sanitize
Browse files Browse the repository at this point in the history
  • Loading branch information
dylandepass committed Oct 22, 2024
1 parent e6fbd50 commit 18d0009
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/catalog/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* governing permissions and limitations under the License.
*/

import { hasUppercase } from '../utils/product.js';
import { errorResponse } from '../utils/http.js';
import { handleProductLookupRequest } from './lookup.js';
import { handleProductFetchRequest } from './fetch.js';
Expand Down Expand Up @@ -45,6 +46,10 @@ export default async function catalogHandler(ctx, config, request) {

const [storeCode, storeViewCode, subRoute, sku] = pathSegments.slice(catalogIndex + 1);

if (hasUppercase(sku)) {
return errorResponse(400, 'Invalid SKU: SKU cannot contain uppercase letters');
}

Object.assign(config, {
storeCode, storeViewCode, subRoute, sku,
});
Expand Down
2 changes: 2 additions & 0 deletions src/utils/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* governing permissions and limitations under the License.
*/

export const hasUppercase = (str) => /[A-Z]/.test(str);

/**
* This function combines an array of strings with interpolated
* parameters to create a GraphQL query string.
Expand Down
3 changes: 2 additions & 1 deletion src/utils/r2.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export async function saveProducts(ctx, config, products) {
const storeProductsBatch = async (batch) => {
const storePromises = batch.map(async (product) => {
try {
const { sku, name, urlKey } = product;
const { name, urlKey } = product;
const { sku } = config;
const key = `${config.org}/${config.site}/${config.env}/${config.storeCode}/${config.storeViewCode}/products/${sku}.json`;
const body = JSON.stringify(product);
const customMetadata = { sku, name, urlKey };
Expand Down

0 comments on commit 18d0009

Please sign in to comment.