From f8f9ba6dd1b139ab618dd34cc783b990346de4bb Mon Sep 17 00:00:00 2001 From: EthanThatOneKid <31261035+EthanThatOneKid@users.noreply.github.com> Date: Sat, 6 Apr 2024 13:51:39 -0700 Subject: [PATCH] replace rt with rtx --- cli/{main.ts => main.tsx} | 75 +++++++++++++++++++++++---------------- deno.json | 13 ++++--- 2 files changed, 54 insertions(+), 34 deletions(-) rename cli/{main.ts => main.tsx} (55%) diff --git a/cli/main.ts b/cli/main.tsx similarity index 55% rename from cli/main.ts rename to cli/main.tsx index 3a4a9a8..8741f1b 100644 --- a/cli/main.ts +++ b/cli/main.tsx @@ -1,4 +1,5 @@ -import { createRouter } from "@fartlabs/rt"; +import { Delete } from "@fartlabs/rtx"; +import { Get, Post, Router } from "@fartlabs/rtx"; import { go } from "go/go.ts"; type Shortlinks = Record; @@ -61,37 +62,51 @@ function isAuthorized(headers: Headers): boolean { if (import.meta.main) { const kv = await Deno.openKv(); const goService = new GoService(kv); - const router = createRouter() - // TODO: Use rtx to define the routes. Use htx to define the HTML index page. - .post("/api", async (ctx) => { - if (!isAuthorized(ctx.request.headers)) { - return Response.json({ error: "Unauthorized" }, { status: 401 }); - } + const router = ( + + { + if (!isAuthorized(ctx.request.headers)) { + return Response.json({ error: "Unauthorized" }, { status: 401 }); + } - const body = await ctx.request.json(); - await goService.add(body.alias, body.destination, body.force); - return Response.json({ message: "Shortlink created." }, { status: 201 }); - }) - .delete("/api", async (ctx) => { - if (!isAuthorized(ctx.request.headers)) { - return Response.json({ error: "Unauthorized" }, { status: 401 }); - } + const body = await ctx.request.json(); + await goService.add(body.alias, body.destination, body.force); + return Response.json( + { message: "Shortlink created." }, + { status: 201 }, + ); + }} + /> + { + if (!isAuthorized(ctx.request.headers)) { + return Response.json({ error: "Unauthorized" }, { status: 401 }); + } - const body = await ctx.request.json(); - await goService.delete(body.alias); - return Response.json({ message: "Shortlink deleted." }); - }) - .get("/:path*", async (ctx) => { - const shortlinks = await goService.shortlinks(); - const destination = go(ctx.url, shortlinks); - return new Response( - `Going to ${destination.href}...`, - { - status: 302, - headers: { "Location": destination.href }, - }, - ); - }); + const body = await ctx.request.json(); + await goService.delete(body.alias); + return Response.json({ message: "Shortlink deleted." }); + }} + /> + { + const shortlinks = await goService.shortlinks(); + const destination = go(ctx.url, shortlinks); + return new Response( + `Going to ${destination.href}...`, + { + status: 302, + headers: { "Location": destination.href }, + }, + ); + }} + /> + + ); Deno.serve((request) => router.fetch(request)); } diff --git a/deno.json b/deno.json index 795a06b..27bfb18 100644 --- a/deno.json +++ b/deno.json @@ -1,11 +1,16 @@ { "lock": false, "imports": { - "go/": "./", - "@fartlabs/rt": "jsr:@fartlabs/rt@^0.0.1", - "@std/assert": "jsr:@std/assert@^0.221.0" + "@fartlabs/jsonx": "jsr:@fartlabs/jsonx@^0.0.10", + "@fartlabs/rtx": "jsr:@fartlabs/rtx@^0.0.2", + "@std/assert": "jsr:@std/assert@^0.221.0", + "go/": "./" + }, + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "@fartlabs/jsonx" }, "tasks": { - "start": "deno run --unstable-kv --env -A cli/main.ts" + "start": "deno run --unstable-kv --env -A cli/main.tsx" } }