diff --git a/src/shapes/VideoChatShapeUtil.tsx b/src/shapes/VideoChatShapeUtil.tsx index 6065636..d235645 100644 --- a/src/shapes/VideoChatShapeUtil.tsx +++ b/src/shapes/VideoChatShapeUtil.tsx @@ -82,12 +82,15 @@ export class VideoChatShape extends BaseBoxShapeUtil { } try { + // First, request a room from our worker const response = await fetch(`${WORKER_URL}/daily/rooms`, { method: "POST", headers: { "Content-Type": "application/json", }, + // You might want to pass additional configuration if needed body: JSON.stringify({ + name: `room-${shape.id}`, // Add a unique identifier properties: { enable_recording: true, max_participants: 8, @@ -96,11 +99,13 @@ export class VideoChatShape extends BaseBoxShapeUtil { }) if (!response.ok) { - throw new Error("Failed to create room") + const errorData = (await response.json()) as { message: string } + throw new Error(errorData.message || "Failed to create room") } const data = await response.json() + // Update the shape with the room URL from the response this.editor.updateShape({ id: shape.id, type: "VideoChat", @@ -111,6 +116,7 @@ export class VideoChatShape extends BaseBoxShapeUtil { }) } catch (error) { console.error("Failed to create Daily room:", error) + throw error // Re-throw to handle in the component } } diff --git a/worker/worker.ts b/worker/worker.ts index a2f54d0..3d0eeb2 100644 --- a/worker/worker.ts +++ b/worker/worker.ts @@ -121,55 +121,49 @@ const router = AutoRouter({ .post("/daily/rooms", async (request, env) => { try { - // Log the request for debugging - console.log( - "Creating Daily room with API key:", - env.DAILY_API_KEY ? "present" : "missing", - ) + const { name, properties } = (await request.json()) as { + name: string + properties: Record + } + // Create a room using Daily.co API const response = await fetch("https://api.daily.co/v1/rooms", { method: "POST", headers: { - Authorization: `Bearer ${env.DAILY_API_KEY}`, "Content-Type": "application/json", + Authorization: `Bearer ${env.DAILY_API_KEY}`, }, - body: await request.text(), + body: JSON.stringify({ + name, + properties, + }), }) if (!response.ok) { - const errorText = await response.text() - console.error("Daily API error:", errorText) - return new Response(`Daily API error: ${errorText}`, { - status: response.status, + const error = (await response.json()) as { message: string } + return new Response(JSON.stringify({ message: error.message }), { + status: 400, + headers: { "Content-Type": "application/json" }, }) } const data = await response.json() - // Log successful response - console.log("Daily room created:", data) - return new Response( JSON.stringify({ - ...(data as Record), url: `https://${env.DAILY_DOMAIN}/${ (data as Record).name }`, }), { - headers: { - "Content-Type": "application/json", - // Add CORS headers specifically for this endpoint if needed - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Methods": "POST, OPTIONS", - "Access-Control-Allow-Headers": "Content-Type", - }, + headers: { "Content-Type": "application/json" }, }, ) } catch (error) { - console.error("Error creating Daily room:", error) return new Response( - JSON.stringify({ error: "Failed to create Daily room" }), + JSON.stringify({ + message: error instanceof Error ? error.message : "Unknown error", + }), { status: 500, headers: { "Content-Type": "application/json" },