myc0punkz-website/functions/api/waitlist.ts

65 lines
1.8 KiB
TypeScript

// Cloudflare Pages Function for waitlist
// This will run on Cloudflare's edge network
interface Env {
WAITLIST: KVNamespace
}
export async function onRequestPost(context: { request: Request; env: Env }) {
try {
const { email } = await context.request.json()
if (!email || !email.includes('@')) {
return new Response(
JSON.stringify({ error: 'Valid email required' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
)
}
// Check if email already exists using Cloudflare KV
const existing = await context.env.WAITLIST?.get(email)
if (existing) {
return new Response(
JSON.stringify({ error: 'Already in the network' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
)
}
// Store email with timestamp in KV
await context.env.WAITLIST?.put(email, JSON.stringify({
email,
timestamp: new Date().toISOString(),
}))
return new Response(
JSON.stringify({
success: true,
message: 'Welcome to the underground',
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
)
} catch (error) {
return new Response(
JSON.stringify({ error: 'Network error occurred' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
)
}
}
// Optional: GET endpoint to check waitlist count
export async function onRequestGet(context: { env: Env }) {
try {
const list = await context.env.WAITLIST?.list()
return new Response(
JSON.stringify({ count: list?.keys.length || 0 }),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
)
} catch (error) {
return new Response(
JSON.stringify({ error: 'Network error' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
)
}
}