o2o-infinith-demo/src/lib/supabase.ts

81 lines
2.0 KiB
TypeScript

import { createClient } from "@supabase/supabase-js";
const supabaseUrl = import.meta.env.VITE_SUPABASE_URL;
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY;
export const supabase = createClient(supabaseUrl, supabaseAnonKey);
export async function generateMarketingReport(url: string, clinicName?: string) {
const response = await fetch(
`${supabaseUrl}/functions/v1/generate-report`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url, clinicName }),
}
);
if (!response.ok) {
throw new Error(`Report generation failed: ${response.statusText}`);
}
return response.json();
}
export async function fetchReportById(reportId: string) {
const { data, error } = await supabase
.from("marketing_reports")
.select("*")
.eq("id", reportId)
.single();
if (error) throw new Error(`Failed to fetch report: ${error.message}`);
return data;
}
export interface EnrichChannelsRequest {
reportId: string;
clinicName: string;
instagramHandle?: string;
youtubeChannelId?: string;
address?: string;
}
/**
* Fire-and-forget: triggers background channel enrichment.
* Returns enrichment result when the Edge Function completes (~27s).
*/
export async function enrichChannels(params: EnrichChannelsRequest) {
const response = await fetch(
`${supabaseUrl}/functions/v1/enrich-channels`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(params),
}
);
if (!response.ok) {
throw new Error(`Channel enrichment failed: ${response.statusText}`);
}
return response.json();
}
export async function scrapeWebsite(url: string, clinicName?: string) {
const response = await fetch(
`${supabaseUrl}/functions/v1/scrape-website`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url, clinicName }),
}
);
if (!response.ok) {
throw new Error(`Scraping failed: ${response.statusText}`);
}
return response.json();
}