From d11c98952164c6b129fcdf1a77ed9b012a9b4080 Mon Sep 17 00:00:00 2001 From: "yongen.loong" Date: Sun, 28 Jul 2024 20:52:55 +0800 Subject: [PATCH] return build errors --- data/build.ts => app/api/build/route.ts | 12 ++++++------ app/workspace/[id]/page.tsx | 1 - components/workspace/use-cli-commands.tsx | 13 ++++++++----- 3 files changed, 14 insertions(+), 12 deletions(-) rename data/build.ts => app/api/build/route.ts (70%) diff --git a/data/build.ts b/app/api/build/route.ts similarity index 70% rename from data/build.ts rename to app/api/build/route.ts index d724e47..48d141b 100644 --- a/data/build.ts +++ b/app/api/build/route.ts @@ -1,11 +1,11 @@ -"use server"; - +import { FileContent } from "@/data/db"; import { getBuildServerBaseUrl } from "@/lib/env"; import { strToU8, Zippable, zipSync } from "fflate"; -import { FileContent } from "./db"; +import { type NextRequest } from "next/server"; import { v4 as uuidv4 } from "uuid"; -export async function build(files: FileContent[]) { +export async function POST(request: NextRequest) { + const { files } = (await request.json()) as { files: FileContent[] }; const data: Zippable = files.reduce((acc, { path, contents }) => { acc[path] = strToU8(contents); @@ -35,8 +35,8 @@ export async function build(files: FileContent[]) { if (!response.ok) { const { message } = await response.json(); - throw new Error(message); + return Response.json({ error: message }, { status: response.status }); } - return await response.text(); + return Response.json({ dll: await response.text() }); } diff --git a/app/workspace/[id]/page.tsx b/app/workspace/[id]/page.tsx index 55877fa..9e42e20 100644 --- a/app/workspace/[id]/page.tsx +++ b/app/workspace/[id]/page.tsx @@ -2,7 +2,6 @@ import { BuildDeployPanel } from "@/components/build-deploy-panel"; import TopBottom from "@/components/top-bottom"; -import { Button } from "@/components/ui/button"; import { ResizablePanelGroup, ResizablePanel, diff --git a/components/workspace/use-cli-commands.tsx b/components/workspace/use-cli-commands.tsx index 8f303ea..13bacd5 100644 --- a/components/workspace/use-cli-commands.tsx +++ b/components/workspace/use-cli-commands.tsx @@ -1,4 +1,3 @@ -import { build } from "@/data/build"; import { db } from "@/data/db"; import { useWallet } from "@/data/wallet"; import { Loader2 } from "lucide-react"; @@ -57,9 +56,13 @@ export function useCliCommands() { ); try { - const str = await build(files); - if (typeof str === "string") { - await db.workspaces.update(id, { dll: str }); + const res = await fetch(`/api/build`, { + method: "POST", + body: JSON.stringify({ files }), + }); + const { dll, error } = await res.json(); + if (typeof dll === "string") { + await db.workspaces.update(id, { dll }); terminalContext.setBufferedContent( <>

Build successful.

@@ -70,7 +73,7 @@ export function useCliCommands() { terminalContext.setBufferedContent( <> {terminalContext.bufferedContent} -

Build failed.

+

{error}

); return;