Skip to content

Commit

Permalink
fix: rework. cleanup and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dylandepass committed Nov 14, 2024
1 parent 79bd8bd commit c71243b
Show file tree
Hide file tree
Showing 21 changed files with 6,484 additions and 2,691 deletions.
5,380 changes: 3,414 additions & 1,966 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"prepare": "husky",
"build": "node build.js",
"lint": "eslint . --ext js,cjs,mjs",
"lint": "eslint . --ext js,cjs,mjs --fix",
"test": "c8 --all --include 'src/**/*.js' mocha -i -g 'Post-Deploy' --spec 'test/**/*.test.js'",
"dev": "if test -e .dev.vars; then wrangler dev --; else echo \"Need a .dev.vars files before starting local dev server\"; fi",
"dev:remote": "wrangler dev --remote",
Expand Down
14 changes: 9 additions & 5 deletions src/catalog/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
* governing permissions and limitations under the License.
*/

/* eslint-disable no-await-in-loop */

import { deleteProducts } from '../utils/r2.js';
import { errorResponse } from '../utils/http.js';

export async function handleProductDeleteRequest(ctx, config) {
/**
* Handles a DELETE request for a product.
* @param {Context} ctx - The context object containing request information and utilities.
* @param {StorageClient} storage - The storage object.
* @returns {Promise<Response>} - A promise that resolves to the product response.
*/
export async function handleProductDeleteRequest(ctx, storage) {
const { config } = ctx;
const { sku } = config;

if (sku === '*') {
Expand All @@ -26,7 +30,7 @@ export async function handleProductDeleteRequest(ctx, config) {
throw errorResponse(400, 'Helix API key is required to delete or unpublish products.');
}

const deleteResults = await deleteProducts(ctx, config, [sku]);
const deleteResults = await storage.deleteProducts([sku]);

ctx.log.info({
action: 'delete_products',
Expand Down
8 changes: 3 additions & 5 deletions src/catalog/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@
* governing permissions and limitations under the License.
*/

import { fetchProduct } from '../utils/r2.js';

/**
* Handles a GET request for a product.
* @param {Context} ctx - The context object containing request information and utilities.
* @param {Config} config - The configuration object with application settings.
* @param {StorageClient} storage - The storage object.
* @returns {Promise<Response>} - A promise that resolves to the product response.
*/
export async function handleProductFetchRequest(ctx, config) {
export async function handleProductFetchRequest(ctx, storage) {
const sku = ctx.url.pathname.split('/').pop();
const product = await fetchProduct(ctx, config, sku);
const product = await storage.fetchProduct(sku);

return new Response(JSON.stringify(product), {
headers: { 'Content-Type': 'application/json' },
Expand Down
33 changes: 19 additions & 14 deletions src/catalog/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ import { handleProductLookupRequest } from './lookup.js';
import { handleProductFetchRequest } from './fetch.js';
import { handleProductSaveRequest } from './update.js';
import { handleProductDeleteRequest } from './delete.js';
import StorageR2 from './storage/r2.js';

const ALLOWED_METHODS = ['GET', 'PUT', 'DELETE'];

/**
* Handles the catalog request.
* @param {Context} ctx - The context object containing request information and utilities.
* @param {Config} config - The configuration object with application settings.
* @param {Request} request - The request object.
* @returns {Promise<Response>} - A promise that resolves to the catalog response.
*/
export default async function catalogHandler(ctx, config, request) {
export default async function catalogHandler(ctx, request) {
const { config } = ctx;
const { method } = ctx.info;
// Split the pathname into segments and filter out empty strings
const pathSegments = ctx.url.pathname.split('/').filter(Boolean);
Expand Down Expand Up @@ -54,19 +55,23 @@ export default async function catalogHandler(ctx, config, request) {
storeCode, storeViewCode, subRoute, sku,
});

if (subRoute === 'lookup') {
if (ctx.info.method === 'GET') {
return handleProductLookupRequest(ctx, config);
}
return errorResponse(405, 'method not allowed');
}
const storage = new StorageR2(ctx, config);

if (ctx.info.method === 'PUT') {
return handleProductSaveRequest(ctx, config, request);
}
const routeHandlers = {
lookup: {
GET: () => handleProductLookupRequest(ctx, storage),
},
product: {
GET: () => handleProductFetchRequest(ctx, storage),
PUT: () => handleProductSaveRequest(ctx, request, storage),
DELETE: () => handleProductDeleteRequest(ctx, storage),
},
};

if (ctx.info.method === 'DELETE') {
return handleProductDeleteRequest(ctx, config);
const handler = routeHandlers[subRoute]?.[method];
if (!handler) {
return errorResponse(405, 'method not allowed');
}
return handleProductFetchRequest(ctx, config);

return handler();
}
10 changes: 4 additions & 6 deletions src/catalog/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@
* governing permissions and limitations under the License.
*/

import { listAllProducts, lookupSku } from '../utils/r2.js';

/**
* Handles a product lookup request.
* @param {Context} ctx - The context object.
* @param {Config} config - The configuration object.
* @returns {Promise<Response>} - A promise that resolves to the product response.
*/
export async function handleProductLookupRequest(ctx, config) {
export async function handleProductLookupRequest(ctx, storage) {
const { config } = ctx;
const { search } = ctx.url;
const params = new URLSearchParams(search);

if (params.has('urlKey') || params.has('urlkey')) {
const urlkey = params.get('urlKey') || params.get('urlkey');
const sku = await lookupSku(ctx, config, urlkey);
const sku = await storage.lookupSku(urlkey);

const origin = (ctx.env.ENVIRONMENT === 'dev') ? 'https://adobe-commerce-api-ci.adobeaem.workers.dev' : ctx.url.origin;
return new Response(undefined, {
Expand All @@ -35,7 +33,7 @@ export async function handleProductLookupRequest(ctx, config) {
});
}

const products = await listAllProducts(ctx, config);
const products = await storage.listAllProducts(ctx);

const response = {
total: products.length,
Expand Down
Loading

0 comments on commit c71243b

Please sign in to comment.