Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor #79

Merged
merged 7 commits into from
Feb 21, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: tweak catalog handler
maxakuru committed Feb 20, 2025
commit 5fc4c74ae7129b63939d8b1e85133bfe011a9707
13 changes: 5 additions & 8 deletions src/routes/catalog/handler.js
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@
* governing permissions and limitations under the License.
*/

import { hasUppercase } from '../../utils/product.js';
import { errorResponse } from '../../utils/http.js';
import lookup from './lookup.js';
import fetch from './fetch.js';
@@ -48,15 +47,13 @@ export default async function handler(ctx, request) {
config,
info: { method },
} = ctx;
// Split the pathname into segments and filter out empty strings
const pathSegments = ctx.url.pathname.split('/').filter(Boolean);
if (pathSegments.length !== 7) {
return errorResponse(404, 'invalid path');
}

const [storeCode, storeViewCode, subRoute, sku] = pathSegments.slice(3);
if (hasUppercase(sku)) {
return errorResponse(400, 'Invalid SKU: SKU cannot contain uppercase letters');

if (!Object.keys(handlers).includes(subRoute)
|| (subRoute === 'products' && !sku)
|| (subRoute === 'lookup' && sku)) {
return errorResponse(404, 'invalid path');
}

Object.assign(config, {
5 changes: 5 additions & 0 deletions src/utils/auth.js
Original file line number Diff line number Diff line change
@@ -25,6 +25,11 @@ export async function assertAuthorization(ctx) {
throw errorWithResponse(403, 'invalid key');
}

if (actual === ctx.env.SUPERUSER_KEY) {
ctx.log.info('acting as superuser');
return;
}

const expected = await ctx.env.KEYS.get(ctx.config.siteKey);
if (!expected) {
throw errorWithResponse(403, 'no key found for site');
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
@@ -82,4 +82,4 @@ VERSION = "@@VERSION@@"
ENVIRONMENT = "prod"

# [secrets]
# ...
# SUPERUSER_KEY