Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 12 additions & 4 deletions src/pages/_api/api/faucet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,26 @@ async function fund(address: `0x${string}`, headers: Record<string, string>): Pr
}
}

function cors(origin: string | null): Record<string, string> {
function isAllowedOrigin(origin: string): boolean {
const allowedOrigins = ['https://tempo.xyz', 'https://docs.tempo.xyz']

if (origin?.includes('vercel.app')) allowedOrigins.push(origin)
if (process.env.NODE_ENV === 'development') allowedOrigins.push('http://localhost:5173')
if (allowedOrigins.includes(origin)) return true

try {
const { protocol, hostname } = new URL(origin)
return protocol === 'https:' && hostname.endsWith('.vercel.app')
} catch {
return false
}
}

function cors(origin: string | null): Record<string, string> {
const headers: Record<string, string> = {
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, x-api-token',
}

if (origin && allowedOrigins.includes(origin)) headers['Access-Control-Allow-Origin'] = origin
if (origin && isAllowedOrigin(origin)) headers['Access-Control-Allow-Origin'] = origin

return headers
}
17 changes: 12 additions & 5 deletions src/pages/_api/api/index-supply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,26 @@ export async function OPTIONS(request: Request): Promise<Response> {
return new Response(null, { status: 200, headers: cors(origin) })
}

function cors(origin: string | null): Record<string, string> {
function isAllowedOrigin(origin: string): boolean {
const allowedOrigins = ['https://tempo.xyz', 'https://mainnet.docs.tempo.xyz']

if (origin?.includes('vercel.app')) allowedOrigins.push(origin)
if (process.env.NODE_ENV === 'development') allowedOrigins.push('http://localhost:5173')
if (allowedOrigins.includes(origin)) return true

try {
const { protocol, hostname } = new URL(origin)
return protocol === 'https:' && hostname.endsWith('.vercel.app')
} catch {
return false
}
}

function cors(origin: string | null): Record<string, string> {
const headers: Record<string, string> = {
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, x-api-token',
}

if (origin && allowedOrigins.some((allowed) => origin.startsWith(allowed)))
headers['Access-Control-Allow-Origin'] = origin
if (origin && isAllowedOrigin(origin)) headers['Access-Control-Allow-Origin'] = origin

return headers
}