- Implemented subjects API with functions for creating, updating, and retrieving subjects, including history and bibliography. - Added tasks API for managing user tasks, including listing and marking tasks as completed. - Created hooks for managing AI interactions, authentication, subjects, tasks, and metadata queries. - Established query keys for caching and managing query states. - Introduced Supabase client and environment variable management for better configuration. - Defined types for database and domain models to ensure type safety across the application.
48 lines
1.1 KiB
TypeScript
48 lines
1.1 KiB
TypeScript
import type { SupabaseClient } from "@supabase/supabase-js";
|
|
import type { Database } from "../types/database";
|
|
import { supabaseBrowser } from "./client";
|
|
|
|
export type EdgeInvokeOptions = {
|
|
method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
headers?: Record<string, string>;
|
|
};
|
|
|
|
export class EdgeFunctionError extends Error {
|
|
constructor(
|
|
message: string,
|
|
public readonly functionName: string,
|
|
public readonly status?: number,
|
|
public readonly details?: unknown
|
|
) {
|
|
super(message);
|
|
this.name = "EdgeFunctionError";
|
|
}
|
|
}
|
|
|
|
export async function invokeEdge<TOut>(
|
|
functionName: string,
|
|
body?: unknown,
|
|
opts: EdgeInvokeOptions = {},
|
|
client?: SupabaseClient<Database>
|
|
): Promise<TOut> {
|
|
const supabase = client ?? supabaseBrowser();
|
|
|
|
const { data, error } = await supabase.functions.invoke(functionName, {
|
|
body,
|
|
method: opts.method ?? "POST",
|
|
headers: opts.headers,
|
|
});
|
|
|
|
if (error) {
|
|
const anyErr = error as any;
|
|
throw new EdgeFunctionError(
|
|
anyErr.message ?? "Error en Edge Function",
|
|
functionName,
|
|
anyErr.status,
|
|
anyErr
|
|
);
|
|
}
|
|
|
|
return data as TOut;
|
|
}
|