fix
This commit is contained in:
@@ -7,8 +7,18 @@ import type { WritingFeedback } from "@/types"
|
||||
const AUTH_DAILY_LIMIT = 10
|
||||
const GUEST_DAILY_LIMIT = 3
|
||||
|
||||
const SUPABASE_URL = import.meta.env.VITE_SUPABASE_URL as string
|
||||
const SUPABASE_ANON_KEY = (import.meta.env.VITE_SUPABASE_ANON_KEY || import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY) as string
|
||||
// Resolve env at runtime — production injects window.__ENV__ via docker/entrypoint.sh,
|
||||
// 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.
|
||||
// SSE format emitted by the function: data: {"text":"..."} | data: [DONE]
|
||||
@@ -16,12 +26,16 @@ async function callEdgeFunction(
|
||||
content: string,
|
||||
onChunk?: (text: string) => void,
|
||||
): 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",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${SUPABASE_ANON_KEY}`,
|
||||
apikey: SUPABASE_ANON_KEY,
|
||||
Authorization: `Bearer ${key}`,
|
||||
apikey: key,
|
||||
},
|
||||
body: JSON.stringify({ content }),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user