Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions handlers/generate_urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,37 @@ export const generateUrlsHandler = async (
}

try {
const body = await req.json()
const { fs_map, entrypoint } = body
const body = ctx.requestBody
if (!body || typeof body !== "object") {
return new Response(
JSON.stringify({
ok: false,
error: "Invalid or missing JSON body",
}),
{ status: 400, headers: { "Content-Type": "application/json" } },
)
}

const { fs_map, entrypoint } = body as {
fs_map?: Record<string, string>
entrypoint?: string
}

const fsMap = fs_map ?? ctx.fsMap
const resolvedEntrypoint = entrypoint ?? ctx.entrypoint

if (!fs_map) {
if (!fsMap) {
return new Response(
JSON.stringify({ ok: false, error: "No fsMap provided" }),
{ status: 400, headers: { "Content-Type": "application/json" } },
)
}

return new Response(
getHtmlForGeneratedUrlPage({ fsMap: fs_map, entrypoint }, ctx.host),
getHtmlForGeneratedUrlPage(
{ fsMap, entrypoint: resolvedEntrypoint },
ctx.host,
),
{
headers: { "Content-Type": "text/html" },
},
Expand Down
26 changes: 26 additions & 0 deletions tests/generate-urls.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { test, expect } from "bun:test"
import { getTestServer } from "./fixtures/get-test-server"

test("POST /generate_urls returns HTML without re-reading the body", async () => {
const { serverUrl } = await getTestServer()

const response = await fetch(`${serverUrl}/generate_urls`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
fs_map: {
"index.tsx": `export default () => (
<board width="10mm" height="10mm">
<resistor resistance="1k" footprint="0402" name="R1" schX={3} pcbX={3} />
</board>
)`,
},
entrypoint: "index.tsx",
}),
})

expect(response.status).toBe(200)
expect(response.headers.get("content-type")).toContain("text/html")
const html = await response.text()
expect(html).toContain("svg.tscircuit.com - Generated URLs")
})