Skip to main content
Home

end-to-end typesafe router, inspired by elysiajs and hono

This package works with Node.js, Deno, BunIt is unknown whether this package works with Cloudflare Workers, Browsers
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
It is unknown whether this package works with Browsers
JSR Score
70%
Published
a year ago (0.1.3)

Router

made an end-to-end typesafe router, since i wanted to understand how backend frameworks work. its mostly just a tree and a middleware stack, the typescript was the hardest part

Usage

installation

# Deno
deno add @lvindotexe/router

runtime usage

# Deno
const router = new Router().get("/" (c) => c.text("hello world"))
Deno.serve((req) => router.request(req))

request validation

currently we use zod for request validation

const app = new Router()
    .post("/post",(c) => {
            const {name,age} = c.valid('json')
            await db.insert(new User({name,age}))
            return c.text(`${name} inserted`,{status:201}),
        },
        {json:z.object({name:z.string(),age:z.number()})}
    )

end-to-end typesafety

const app = new Router()
    .post("/post",(c) => {
            return c.json(c.valid('json'),{status:201}),
        },
        {json:z.object({name:z.string(),age:z.number()})}
    )

const client = rc<typeof app>("127.0.0.0.1:8000")
const res = await client.post.$post()
const {name,age} = await res.json()

decorating context

context is passed to every request handler. it can be extended with any proprty

const app = new Router()
    .decorate('logger',new Logger())
    .get("/",(c) => {
        c.logger("hello world")
        return c.text("logged")
    })

information based on request info can be used to decorate the context

    const app = new Router()
        .state('bearer',(ctx) => {
            const auth = ctx.headers('authorization')
            return auth.startsWith("Beaerer") ? auth.slice(7) : null
        })
        .get("/",(c) => {
            const {bearer} = c
            return c.text(bearer)
        })

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@lvindotexe/router

Import symbol

import * as router from "@lvindotexe/router";
or

Import directly with a jsr specifier

import * as router from "jsr:@lvindotexe/router";

Add Package

pnpm i jsr:@lvindotexe/router
or (using pnpm 10.8 or older)
pnpm dlx jsr add @lvindotexe/router

Import symbol

import * as router from "@lvindotexe/router";

Add Package

yarn add jsr:@lvindotexe/router
or (using Yarn 4.8 or older)
yarn dlx jsr add @lvindotexe/router

Import symbol

import * as router from "@lvindotexe/router";

Add Package

vlt install jsr:@lvindotexe/router

Import symbol

import * as router from "@lvindotexe/router";

Add Package

npx jsr add @lvindotexe/router

Import symbol

import * as router from "@lvindotexe/router";

Add Package

bunx jsr add @lvindotexe/router

Import symbol

import * as router from "@lvindotexe/router";