a70f0c52a9
CI / test (pull_request) Failing after 8s
- Added CORS handling for the create-chat-conversation function. - Implemented health check endpoint for the function. - Created endpoints for managing conversations and messages with OpenAI. - Added error handling and response formatting for better API usability. - Introduced utility functions for environment variable management and Supabase client creation. - Enhanced schema handling for structured responses from OpenAI. - Implemented conversation archiving and retrieval logic.
31 lines
1.0 KiB
TypeScript
31 lines
1.0 KiB
TypeScript
import { createClient } from "jsr:@supabase/supabase-js";
|
|
import { mustGetEnv } from "./env.ts";
|
|
import { HttpError } from "./errors.ts";
|
|
|
|
export function getSupabaseServiceClient() {
|
|
const url = mustGetEnv("SUPABASE_URL");
|
|
const key = mustGetEnv("SUPABASE_SERVICE_ROLE_KEY");
|
|
return createClient(url, key, { auth: { persistSession: false } });
|
|
}
|
|
|
|
export function getSupabaseAnonClient(authHeader?: string) {
|
|
const url = mustGetEnv("SUPABASE_URL");
|
|
const key = mustGetEnv("SUPABASE_ANON_KEY");
|
|
return createClient(url, key, {
|
|
auth: { persistSession: false },
|
|
global: authHeader ? { headers: { Authorization: authHeader } } : undefined,
|
|
});
|
|
}
|
|
|
|
export async function requireUser(authHeader?: string) {
|
|
if (!authHeader) {
|
|
throw new HttpError(401, "missing_auth", "Missing Authorization header");
|
|
}
|
|
const anon = getSupabaseAnonClient(authHeader);
|
|
const { data, error } = await anon.auth.getUser();
|
|
if (error || !data?.user) {
|
|
throw new HttpError(401, "invalid_auth", "Invalid or expired token", error);
|
|
}
|
|
return data.user;
|
|
}
|