Files
inq-roi-simulador-web/supabase/functions/main/index.ts
Marcos Benítez bc9f70419c
Some checks failed
/ Deploy to Cloudflare Pages (push) Has been cancelled
Resumen de lo entregado
2026-07-07 09:47:31 -03:00

38 lines
1.3 KiB
TypeScript

/**
* Edge Function: main (relay)
* Enruta todas las requests al worker de la función correspondiente.
* Requerido por supabase/edge-runtime en self-hosted (--main-service).
* No expuesto directamente — solo opera como router interno.
*/
const FUNCTIONS = ['invite-user', 'revoke-user']
Deno.serve(async (req: Request) => {
const url = new URL(req.url)
// Extraer nombre de función del path: /functions/v1/invite-user → invite-user
const segments = url.pathname.split('/').filter(Boolean)
const functionName = segments[segments.length - 1]
if (!functionName || !FUNCTIONS.includes(functionName)) {
return new Response(
JSON.stringify({ error: `Función no encontrada: ${functionName}` }),
{ status: 404, headers: { 'Content-Type': 'application/json' } },
)
}
const servicePath = `/home/deno/functions/${functionName}`
try {
// EdgeRuntime.userWorkers es el API del supabase/edge-runtime para crear workers por función
const worker = await EdgeRuntime.userWorkers.create({ servicePath })
return await worker.fetch(req)
} catch (err) {
const message = err instanceof Error ? err.message : String(err)
return new Response(
JSON.stringify({ error: message }),
{ status: 500, headers: { 'Content-Type': 'application/json' } },
)
}
})