This commit is contained in:
2026-04-21 11:44:29 +07:00
parent 309609fccb
commit 285ab987fd

View File

@@ -7,8 +7,18 @@ import type { WritingFeedback } from "@/types"
const AUTH_DAILY_LIMIT = 10 const AUTH_DAILY_LIMIT = 10
const GUEST_DAILY_LIMIT = 3 const GUEST_DAILY_LIMIT = 3
const SUPABASE_URL = import.meta.env.VITE_SUPABASE_URL as string // Resolve env at runtime — production injects window.__ENV__ via docker/entrypoint.sh,
const SUPABASE_ANON_KEY = (import.meta.env.VITE_SUPABASE_ANON_KEY || import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY) as string // dev reads from Vite's import.meta.env. Must match src/lib/supabase.ts.
function resolveSupabaseEnv() {
const runtime = (window as unknown as { __ENV__?: Record<string, string> }).__ENV__ ?? {}
const url = runtime.VITE_SUPABASE_URL || import.meta.env.VITE_SUPABASE_URL
const key =
runtime.VITE_SUPABASE_ANON_KEY ||
runtime.VITE_SUPABASE_PUBLISHABLE_KEY ||
import.meta.env.VITE_SUPABASE_ANON_KEY ||
import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY
return { url: url as string | undefined, key: key as string | undefined }
}
// Calls the writing-check-dbiz Supabase edge function. // Calls the writing-check-dbiz Supabase edge function.
// SSE format emitted by the function: data: {"text":"..."} | data: [DONE] // SSE format emitted by the function: data: {"text":"..."} | data: [DONE]
@@ -16,12 +26,16 @@ async function callEdgeFunction(
content: string, content: string,
onChunk?: (text: string) => void, onChunk?: (text: string) => void,
): Promise<WritingFeedback> { ): Promise<WritingFeedback> {
const res = await fetch(`${SUPABASE_URL}/functions/v1/writing-check-dbiz`, { const { url, key } = resolveSupabaseEnv()
if (!url || !key) {
throw new Error("Supabase chưa được cấu hình. Vui lòng kiểm tra biến môi trường.")
}
const res = await fetch(`${url}/functions/v1/writing-check-dbiz`, {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Bearer ${SUPABASE_ANON_KEY}`, Authorization: `Bearer ${key}`,
apikey: SUPABASE_ANON_KEY, apikey: key,
}, },
body: JSON.stringify({ content }), body: JSON.stringify({ content }),
}) })