33// This enables autocomplete, go to definition, etc.
44
55import "@supabase/functions-js/edge-runtime" ;
6+ import { corsHeaders } from '@supabase/supabase-js/cors'
67import { createClient , type UserResponse } from "@supabase/supabase-js" ;
78import type { DGSupabaseClient } from "@repo/database/lib/client" ;
89
@@ -20,31 +21,37 @@ const isAllowedOrigin = (origin: string): boolean =>
2021Deno . serve ( async ( req ) => {
2122 const origin = req . headers . get ( "origin" ) ;
2223 const originIsAllowed = origin && isAllowedOrigin ( origin ) ;
24+ const myCorsHeaders = { ...corsHeaders , "Access-Control-Allow-Origin" : originIsAllowed ? origin :'' } ;
2325 if ( req . method === "OPTIONS" ) {
2426 return new Response ( null , {
2527 status : 204 ,
26- headers : {
27- ...( originIsAllowed ? { "Access-Control-Allow-Origin" : origin } : { } ) ,
28- "Access-Control-Allow-Methods" : "GET, POST, OPTIONS" ,
29- "Access-Control-Allow-Headers" :
30- "Content-Type, Authorization, x-vercel-protection-bypass, x-client-info, apikey" ,
31- "Access-Control-Max-Age" : "86400" ,
32- } ,
28+ headers : myCorsHeaders ,
3329 } ) ;
3430 }
3531 if ( req . method !== "POST" ) {
3632 return Response . json (
3733 { msg : 'Method not allowed' } ,
38- { status : 405 }
34+ { status : 405 ,
35+ headers : myCorsHeaders ,
36+ }
3937 ) ;
4038 }
41-
42- const input : { name ?: string } = await req . json ( ) ;
39+ let input : { name ?: string } = { }
40+ try {
41+ input = await req . json ( ) ;
42+ } catch ( error ) {
43+ return Response . json ( {
44+ msg : 'Invalid JSON in request body' , error : String ( error ?. message ?? error )
45+ } , {
46+ status : 400 ,
47+ headers : myCorsHeaders ,
48+ } ) ;
49+ }
4350 const groupName = input . name ;
4451 if ( groupName === undefined ) {
4552 return new Response ( "Missing group name" , {
4653 status : 400 ,
47- headers : { "Content-Type" : "application/json" } ,
54+ headers : myCorsHeaders ,
4855 } ) ;
4956 }
5057 // @ts -ignore Deno is not visible to the IDE
@@ -57,7 +64,7 @@ Deno.serve(async (req) => {
5764 if ( ! url || ! anon_key || ! service_key ) {
5865 return new Response ( "Missing SUPABASE_URL or SB_SECRET_KEY or SB_PUBLISHABLE_KEY" , {
5966 status : 500 ,
60- headers : { "Content-Type" : "application/json" } ,
67+ headers : myCorsHeaders ,
6168 } ) ;
6269 }
6370 const supabase = createClient ( url , anon_key )
@@ -67,6 +74,7 @@ Deno.serve(async (req) => {
6774 { msg : 'Missing authorization headers' } ,
6875 {
6976 status : 401 ,
77+ headers : myCorsHeaders ,
7078 }
7179 )
7280 }
@@ -79,6 +87,7 @@ Deno.serve(async (req) => {
7987 { msg : 'Invalid JWT' } ,
8088 {
8189 status : 401 ,
90+ headers : myCorsHeaders ,
8291 }
8392 )
8493 }
@@ -105,27 +114,21 @@ Deno.serve(async (req) => {
105114 { msg : 'A group by this name exists' } ,
106115 {
107116 status : 400 ,
117+ headers : myCorsHeaders ,
108118 } ) ;
109119 }
110- return Response . json ( { msg : 'Failed to create group user' , error : error . message } , { status : 500 } ) ;
120+ return Response . json ( { msg : 'Failed to create group user' , error : error . message } , { status : 500 , headers : myCorsHeaders } ) ;
111121 }
112122 // eslint-disable-next-line @typescript-eslint/naming-convention
113123 const group_id = userResponse . data . user . id ;
114124 // eslint-disable-next-line @typescript-eslint/naming-convention
115125 const membershipResponse = await supabaseAdmin . from ( "group_membership" ) . insert ( { group_id, member_id :data . claims . sub , admin :true } ) ;
116126 if ( membershipResponse . error )
117- return Response . json ( { msg : `Failed to create membership for group ${ group_id } ` , error : membershipResponse . error . message } , { status : 500 } ) ;
118-
119- const res = Response . json ( { group_id} ) ;
120-
121- if ( originIsAllowed ) {
122- res . headers . set ( "Access-Control-Allow-Origin" , origin as string ) ;
123- res . headers . set ( "Access-Control-Allow-Methods" , "GET, POST, OPTIONS" ) ;
124- res . headers . set (
125- "Access-Control-Allow-Headers" ,
126- "Content-Type, Authorization, x-vercel-protection-bypass, x-client-info, apikey" ,
127- ) ;
128- }
127+ return Response . json ( {
128+ msg : `Failed to create membership for group ${ group_id } ` ,
129+ error : membershipResponse . error . message
130+ } ,
131+ { status : 500 , headers : myCorsHeaders , } ) ;
129132
130- return res ;
133+ return Response . json ( { group_id } , { headers : myCorsHeaders } ) ;
131134} ) ;
0 commit comments