Add version files and update imports for trip model; enhance error handling
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
export const corsHeaders = {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type",
|
||||
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
||||
};
|
||||
|
||||
export function json(data: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(data), {
|
||||
status,
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function fail(message: string, status = 400): Response {
|
||||
return json({ error: message }, status);
|
||||
}
|
||||
|
||||
export function handleOptions(req: Request): Response | null {
|
||||
if (req.method !== "OPTIONS") return null;
|
||||
return new Response("ok", { headers: corsHeaders });
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
|
||||
|
||||
const supabaseUrl = Deno.env.get("SUPABASE_URL");
|
||||
const supabaseAnonKey = Deno.env.get("SUPABASE_ANON_KEY");
|
||||
const supabaseServiceRoleKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY");
|
||||
|
||||
if (!supabaseUrl || !supabaseAnonKey) {
|
||||
throw new Error("SUPABASE_URL and SUPABASE_ANON_KEY are required");
|
||||
}
|
||||
|
||||
export function createAuthedClient(req: Request) {
|
||||
const authHeader = req.headers.get("Authorization") ?? "";
|
||||
return createClient(supabaseUrl, supabaseAnonKey, {
|
||||
global: {
|
||||
headers: { Authorization: authHeader },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function createServiceClient() {
|
||||
if (!supabaseServiceRoleKey) {
|
||||
throw new Error("SUPABASE_SERVICE_ROLE_KEY is required");
|
||||
}
|
||||
return createClient(supabaseUrl, supabaseServiceRoleKey);
|
||||
}
|
||||
|
||||
export async function requireUser(req: Request) {
|
||||
const client = createAuthedClient(req);
|
||||
const { data, error } = await client.auth.getUser();
|
||||
if (error || !data.user) {
|
||||
return { client, user: null, error: error?.message ?? "Unauthorized" };
|
||||
}
|
||||
return { client, user: data.user, error: null };
|
||||
}
|
||||
|
||||
export function slugify(input: string): string {
|
||||
return input
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "")
|
||||
.slice(0, 64);
|
||||
}
|
||||
Reference in New Issue
Block a user