The self-hosted client was typed as a full Supabase client, so an old scoring poll called .single() on a plain promise and failed. The new DataClient only lists methods the adapter implements. Co-Authored-By: Grok (agent loop)
46 lines
1.7 KiB
TypeScript
46 lines
1.7 KiB
TypeScript
import "server-only";
|
|
|
|
import { createServerClient } from "@supabase/ssr";
|
|
import { cookies, headers } from "next/headers";
|
|
import type { DataClient } from "@/lib/db/data-client";
|
|
import { createLocalPostgresDataClient } from "@/lib/db/local-postgres-client";
|
|
import { readDatabaseUrl } from "@/lib/db/config";
|
|
import { getIdentityAuthServices } from "@/modules/identity/auth";
|
|
import { readIdentitySession } from "@/modules/identity/session";
|
|
import { getSupabasePublicConfig } from "./config";
|
|
|
|
export async function createServerSupabaseClient(options?: {
|
|
readonly disableCookieCache?: boolean;
|
|
}): Promise<DataClient> {
|
|
if (process.env.AUTH_PROVIDER?.trim() === "self-hosted") {
|
|
const requestHeaders = new Headers(await headers());
|
|
const session = await readIdentitySession(
|
|
getIdentityAuthServices().user.api,
|
|
requestHeaders,
|
|
options?.disableCookieCache ? { disableCookieCache: true } : undefined,
|
|
);
|
|
return createLocalPostgresDataClient(
|
|
readDatabaseUrl(process.env, "APP_DATABASE_URL"),
|
|
session ? { id: session.user.id, email: session.user.email } : null,
|
|
) as DataClient;
|
|
}
|
|
const { url, anonKey } = getSupabasePublicConfig();
|
|
const cookieStore = await cookies();
|
|
|
|
// 回滚分支,运行时形状是超集
|
|
return createServerClient(url, anonKey, {
|
|
cookies: {
|
|
getAll: () => cookieStore.getAll(),
|
|
setAll: (cookiesToSet) => {
|
|
try {
|
|
cookiesToSet.forEach(({ name, value, options }) => {
|
|
cookieStore.set(name, value, options);
|
|
});
|
|
} catch {
|
|
// Server Components cannot write cookies; Route Handlers can.
|
|
}
|
|
},
|
|
},
|
|
}) as unknown as DataClient;
|
|
}
|