Skip to content

Commit

Permalink
feat: Add KV namespace binding for configuration (#63)
Browse files Browse the repository at this point in the history
* refactor: Update to get configuration from KV

* Refactor: Centralize configuration parsing.

* feat: Add KV namespace binding for configuration

BREAKING CHANGE: Upload config file to KV and binding workers with KV namespace. Or you can still continue to use API Config as file.

* refactor: Improve API configuration loading
  • Loading branch information
irensaltali authored Nov 7, 2024
1 parent 5e04619 commit e1956c2
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 335 deletions.
251 changes: 0 additions & 251 deletions src/api-config.json

This file was deleted.

3 changes: 1 addition & 2 deletions src/auth.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {jwtVerify, errors } from 'jose';
import apiConfig from './api-config.json';
import { AuthError } from "./types/error_types";

async function jwtAuth(request) {
async function jwtAuth(request, apiConfig) {
const secret = new TextEncoder().encode(apiConfig.authorizer?.secret);
const authHeader = request.headers.get('Authorization');
if (!authHeader || !authHeader.startsWith('Bearer ')) {
Expand Down
14 changes: 8 additions & 6 deletions src/cors.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import apiConfig from './api-config.json';

function setCorsHeaders(request, response) {
const corsConfig = apiConfig.cors;

function setCorsHeaders(request, response, corsConfig) {
console.log('Setting CORS headers');
console.log('Request headers:', request);
console.log('Response headers:', response.headers);
console.log('CORS config:', corsConfig);
const origin = request.headers.get('Origin');
const matchingOrigin = corsConfig.allow_origins.find((allowedOrigin) => allowedOrigin === origin);

Expand All @@ -14,11 +14,13 @@ function setCorsHeaders(request, response) {
headers.set('Access-Control-Allow-Credentials', corsConfig.allow_credentials.toString());
headers.set('Access-Control-Max-Age', corsConfig.max_age.toString());

return new Response(response.body, {
const newResponse = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: headers,
});
console.log('New response:', newResponse);
return newResponse;
}

export { setCorsHeaders };
Loading

0 comments on commit e1956c2

Please sign in to comment.