From ec2985f783d2324516eb48acc3c194b7f87b8fe0 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Thu, 27 Mar 2025 22:30:26 +0000 Subject: [PATCH] Fixing CORS issues --- src/index.js | 3 ++- src/routes/blogs.js | 15 +++++++-------- src/routes/contact.js | 11 ++++++----- src/routes/socials.js | 8 ++++---- src/routes/utils.js | 7 ++----- src/utils/api.js | 4 ++++ 6 files changed, 25 insertions(+), 23 deletions(-) create mode 100644 src/utils/api.js diff --git a/src/index.js b/src/index.js index f469372..6e18c9e 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import { handleContactRequest } from './routes/contact.js'; import { handleSocialsRequest } from './routes/socials.js'; import { handleBlogsListRequest, handleBlogsViewRequest } from './routes/blogs.js'; import { handleFlushCacheRequest } from './routes/utils.js'; +import { globalHeaders } from './utils/api.js'; async function handleRequest(request) { if (request.method === 'POST' && request.url.split('?')[0].endsWith('/api/contact')) { @@ -25,7 +26,7 @@ async function handleRequest(request) { } // Return 404 if the route is not found - return new Response('Not Found', { status: 404 }); + return new Response('Not Found', { status: 404, headers: globalHeaders }); } addEventListener('fetch', (event) => { diff --git a/src/routes/blogs.js b/src/routes/blogs.js index f5ce653..32f9cd4 100644 --- a/src/routes/blogs.js +++ b/src/routes/blogs.js @@ -1,5 +1,6 @@ import { getNotionDatabaseWithCache, getNotionBlocksWithCache } from '../utils/notion.js'; import { generateBlockHTML } from '../utils/htmlgen.js'; +import { globalHeaders } from '../utils/api.js'; const blogsDB = BLOGS_DB; export async function handleBlogsListRequest(request, env) { @@ -29,7 +30,7 @@ export async function handleBlogsListRequest(request, env) { }), { status: 404, - headers: { 'Content-Type': 'application/json' }, + headers: globalHeaders, }, ); } @@ -60,8 +61,7 @@ export async function handleBlogsListRequest(request, env) { console.log('Finished listing blogs.'); return new Response(JSON.stringify(blogsResponse), { headers: { - 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*', // Enable CORS + ...globalHeaders, 'Cache-Control': 'public, max-age=600', // 10 minute browser cache }, }); @@ -74,7 +74,7 @@ export async function handleBlogsListRequest(request, env) { }), { status: 500, - headers: { 'Content-Type': 'application/json' }, + headers: globalHeaders, }, ); } @@ -109,7 +109,7 @@ export async function handleBlogsViewRequest(request, env) { }), { status: 404, - headers: { 'Content-Type': 'application/json' }, + headers: globalHeaders, }, ); } @@ -131,8 +131,7 @@ export async function handleBlogsViewRequest(request, env) { console.log('Finished listing blogs.'); return new Response(JSON.stringify(blogResponse), { headers: { - 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*', // Enable CORS + ...globalHeaders, 'Cache-Control': 'public, max-age=600', // 10 minute browser cache }, }); @@ -145,7 +144,7 @@ export async function handleBlogsViewRequest(request, env) { }), { status: 500, - headers: { 'Content-Type': 'application/json' }, + headers: globalHeaders, }, ); } diff --git a/src/routes/contact.js b/src/routes/contact.js index db18a44..50fcf06 100644 --- a/src/routes/contact.js +++ b/src/routes/contact.js @@ -1,5 +1,6 @@ import { getLocation } from '../utils/geolocation.js'; import { addToNotionDatabase, getNotionDatabaseWithCache } from '../utils/notion.js'; +import { globalHeaders } from '../utils/api.js'; const TURNSTILE_SECRET_KEY = TURNSTILE_AUTH; @@ -9,7 +10,7 @@ export async function handleContactRequest(request) { const { email, token } = await request.json(); if (!email || !token) { - return new Response('Email and token are required', { status: 400 }); + return new Response('Email and token are required', { status: 400, headers: globalHeaders }); } // Extract the IP address from the request headers @@ -23,7 +24,7 @@ export async function handleContactRequest(request) { // Verify the Turnstile token const verificationResponse = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', { method: 'POST', - headers: { 'Content-Type': 'application/json' }, + headers: globalHeaders, body: JSON.stringify({ secret: TURNSTILE_SECRET_KEY, response: token, @@ -33,7 +34,7 @@ export async function handleContactRequest(request) { const verificationData = await verificationResponse.json(); if (!verificationData.success) { - return new Response('Turnstile verification failed', { status: 400 }); + return new Response('Turnstile verification failed', { status: 400, headers: globalHeaders }); } } @@ -41,9 +42,9 @@ export async function handleContactRequest(request) { try { await addToNotionDatabase({ Name: email.split('@')[0], Email: email, Location: locationString }, '1abdd26d60b68076a886fb0525ff0a4f'); return new Response(JSON.stringify({ success: true, message: 'Email processed and added to Notion' }), { - headers: { 'Content-Type': 'application/json' }, + headers: globalHeaders, }); } catch (error) { - return new Response('Error processing email', { status: 500 }); + return new Response('Error processing email', { status: 500, headers: globalHeaders }); } } diff --git a/src/routes/socials.js b/src/routes/socials.js index fb1f975..49218a8 100644 --- a/src/routes/socials.js +++ b/src/routes/socials.js @@ -1,4 +1,5 @@ import { getNotionDatabaseWithCache } from '../utils/notion.js'; +import { globalHeaders } from '../utils/api.js'; const socialsDB = SOCIALS_DB; const referrersDB = REFERRERS_DB; @@ -32,7 +33,7 @@ export async function handleSocialsRequest(request, env) { }), { status: 404, - headers: { 'Content-Type': 'application/json' }, + headers: globalHeaders, }, ); } @@ -56,8 +57,7 @@ export async function handleSocialsRequest(request, env) { console.log('Finished listing socials.'); return new Response(JSON.stringify(socialsResponse), { headers: { - 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*', // Enable CORS + ...globalHeaders, 'Cache-Control': 'public, max-age=600', // 10 minute browser cache }, }); @@ -70,7 +70,7 @@ export async function handleSocialsRequest(request, env) { }), { status: 500, - headers: { 'Content-Type': 'application/json' }, + headers: globalHeaders, }, ); } diff --git a/src/routes/utils.js b/src/routes/utils.js index 5ca1e85..dcbba9e 100644 --- a/src/routes/utils.js +++ b/src/routes/utils.js @@ -1,4 +1,5 @@ import { flushCache } from '../utils/notion.js'; +import { globalHeaders } from '../utils/api.js'; export async function handleFlushCacheRequest(request) { await flushCache(); @@ -6,10 +7,6 @@ export async function handleFlushCacheRequest(request) { result: 'ok', }; return new Response(JSON.stringify(response), { - headers: { - 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*', // Enable CORS - 'Cache-Control': 'public, max-age=600', // 10 minute browser cache - }, + headers: globalHeaders, }); } diff --git a/src/utils/api.js b/src/utils/api.js new file mode 100644 index 0000000..2b3a9e4 --- /dev/null +++ b/src/utils/api.js @@ -0,0 +1,4 @@ +export const globalHeaders = { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': CORS_ORIGIN, +};