Files
supabase/apps/studio/lib/api/self-hosted-admin.ts
T

23 lines
1.0 KiB
TypeScript

import { createClient, SupabaseClient } from '@supabase/supabase-js'
// Lazy admin client for self-hosted API routes under
// `pages/api/platform/{auth,storage}/**`. SUPABASE_URL and
// SUPABASE_SERVICE_KEY are only set on self-hosted deployments — the
// platform build doesn't need these env vars. But on the TanStack Start
// server, every API route's module gets evaluated when the single function
// handler loads, regardless of whether its URL is hit. Without a lazy
// wrapper, constructing the client at module scope with undefined
// credentials would crash every request on platform.
//
// Proxy defers client construction until a property is actually accessed,
// which only happens inside the handler (i.e. on self-hosted where the env
// vars are set).
let _client: SupabaseClient | undefined
export const selfHostedSupabaseAdmin = new Proxy({} as SupabaseClient, {
get(_target, prop) {
_client ??= createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_SERVICE_KEY!)
return Reflect.get(_client, prop)
},
})