Skip to content

Commit 272384c

Browse files
committed
fix: various fixes
1 parent 125c88f commit 272384c

File tree

7 files changed

+28
-14
lines changed

7 files changed

+28
-14
lines changed

src/index.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ async function parseData(req) {
2323
}
2424
if (['POST', 'PUT', 'PATCH'].includes(req.method)) {
2525
const text = await req.text();
26-
try {
27-
return JSON.parse(text);
28-
} catch {
29-
return text;
26+
if (text.trim().length) {
27+
try {
28+
return JSON.parse(text);
29+
} catch {
30+
return text;
31+
}
32+
}
33+
34+
const params = new URL(req.url).searchParams;
35+
if (params.size) {
36+
return Object.fromEntries(params.entries());
3037
}
3138
}
32-
return null;
39+
return {};
3340
}
3441

3542
/**

src/routes/config/handler.js

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import { errorResponse } from '../../utils/http.js';
1414
import { assertAuthorization } from '../../utils/auth.js';
1515
import { validate } from '../../utils/validation.js';
16+
import { updateToken } from '../auth/update.js';
1617
import ConfigSchema from '../../schemas/Config.js';
1718

1819
/**
@@ -46,7 +47,14 @@ export default async function configHandler(ctx) {
4647
}
4748

4849
// valid, persist it
50+
const exists = (await ctx.env.CONFIGS.list({ prefix: ctx.config.siteKey })).keys.length > 0;
4951
await ctx.env.CONFIGS.put(ctx.config.siteKey, JSON.stringify(json));
52+
53+
// add key
54+
if (!exists) {
55+
await updateToken(ctx);
56+
}
57+
5058
return new Response(JSON.stringify(json), {
5159
status: 200,
5260
headers: {

src/routes/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import content from './content/handler.js';
1414
import catalog from './catalog/handler.js';
1515
import config from './config/handler.js';
16-
import { errorResponse } from '../utils/http.js';
16+
import auth from './auth/handler.js';
1717

1818
/**
1919
* @type {Record<string, (ctx: Context, request: Request) => Promise<Response>>}
@@ -22,6 +22,5 @@ export default {
2222
content,
2323
catalog,
2424
config,
25-
// eslint-disable-next-line no-unused-vars
26-
graphql: async (ctx) => errorResponse(501, 'not implemented'),
25+
auth,
2726
};

src/schemas/Config.js

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const ConfigEntry = {
4444
offerVariantURLTemplate: { type: 'string' },
4545
liveSearchEnabled: { type: 'boolean' },
4646
attributeOverrides: AttributeOverrides,
47+
catalogSource: { type: 'string', enum: ['helix', 'magento'] },
4748
imageRoleOrder: {
4849
type: 'array',
4950
items: { type: 'string' },

src/types.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ declare global {
3434
/**
3535
* Override "escape hatch" for json-ld
3636
*/
37-
jsonld?: any;
37+
jsonld?: string;
3838

3939
/**
4040
* Additional data that can be retrieved via .json API

src/utils/auth.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ export async function assertAuthorization(ctx) {
2121
ctx.attributes.key = ctx.info.headers.authorization?.slice('Bearer '.length);
2222
actual = ctx.attributes.key;
2323
}
24-
if (!actual) {
25-
throw errorWithResponse(403, 'invalid key');
26-
}
27-
2824
if (actual === ctx.env.SUPERUSER_KEY) {
2925
ctx.log.info('acting as superuser');
3026
return;
3127
}
3228

29+
if (!actual) {
30+
throw errorWithResponse(403, 'invalid key');
31+
}
3332
const expected = await ctx.env.KEYS.get(ctx.config.siteKey);
3433
if (!expected) {
3534
throw errorWithResponse(403, 'no key found for site');

src/utils/http.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class ResponseError extends Error {
4848

4949
/**
5050
* @param {number} status - The HTTP status code.
51-
* @param {string} xError - The error message.
51+
* @param {string} [xError] - The error message.
5252
* @param {string|Record<string,unknown>} [body=''] - The response body.
5353
* @returns {Response} - A response object.
5454
*/

0 commit comments

Comments
 (0)