import { corsHeaders, handleCorsPreflightRequest } from '../_shared/cors.ts' import { requireUser, authErrorResponse, type AuthError } from '../_shared/auth.ts' import { createBillingCatalog, type BillingCatalogTier, } from '../_shared/billing-catalog.ts' import { getPaypleConfig, PaypleConfigurationError, TIER_PRICE, } from '../_shared/payple.ts' const NO_STORE_HEADERS = { ...corsHeaders, 'Content-Type': 'application/json', 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff', } function jsonResponse(body: unknown, status = 200): Response { return new Response(JSON.stringify(body), { status, headers: NO_STORE_HEADERS }) } Deno.serve(async (req: Request) => { const preflight = handleCorsPreflightRequest(req) if (preflight) return preflight if (req.method !== 'POST') return jsonResponse({ error: 'method_not_allowed' }, 405) try { await requireUser(req) let payple: Record | null = null try { getPaypleConfig() payple = { pro: TIER_PRICE.pro, pro_plus: TIER_PRICE.pro_plus, } } catch (error) { if (!(error instanceof PaypleConfigurationError)) throw error } return jsonResponse(createBillingCatalog({ payple })) } catch (error) { if (error && typeof error === 'object' && 'status' in error && 'message' in error) { return authErrorResponse(error as AuthError, NO_STORE_HEADERS) } return jsonResponse({ error: 'billing_catalog_unavailable' }, 503) } })