Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
latest
EthanThatOneKid/rtxWorks with
•JSR Score94%•This package works with DenoIt is unknown whether this package works with Cloudflare Workers, Node.js, Bun, Browsers




Downloads144/wk
•Published3 months ago (0.0.16)
Minimal HTTP router library based on the URLPattern API in JSX.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126// deno-lint-ignore-file no-explicit-any import type { Route } from "jsr:/@std/http@^1.0.21/unstable-route"; import type { DefaultHandler, ErrorHandler, RtRoute } from "jsr:@fartlabs/rt@^0.0.11"; import { Router } from "jsr:@fartlabs/rt@^0.0.11"; export { RouteComponent as Route, RouterComponent as Router }; /** * RouterProps are the props for the router component. */ export interface RouterProps { children?: Array<Router<any> | Router<any>[]>; default?: DefaultHandler; error?: ErrorHandler; } /** * RouterComponent is the router component. */ function RouterComponent(props: RouterProps): Router<any> { const router = new Router<any>(); for ( const child of ((props.children) ?? []).flat() ) { if (child instanceof Router) { router.use(child); continue; } throw new Error("Invalid child of Router"); } if (props.default) { router.default(props.default); } if (props.error) { router.error(props.error); } return router; } export function StandardRoute(props: Route): Router { return new Router().with({ ...props, handler: ({ request, params, info }) => { return props.handler(request, params, info); }, }); } /** * RouteProps are the props for a route component. */ export interface RouteProps extends RtRoute<any> {} /** * RouteComponent is the route component. */ export function RouteComponent(props: RouteProps): Router { return new Router().with(props); } /** * Connect is the route component for a CONNECT route. */ export function Connect(props: RouteProps): Router { return new Router().connect(props.pattern, props.handler); } /** * Delete is the route component for a DELETE route. */ export function Delete(props: RouteProps): Router { return new Router().delete(props.pattern, props.handler); } /** * Get is the route component for a GET route. */ export function Get(props: RouteProps): Router { return new Router().get(props.pattern, props.handler); } /** * Head is the route component for a HEAD route. */ export function Head(props: RouteProps): Router { return new Router().head(props.pattern, props.handler); } /** * Options is the route component for a OPTIONS route. */ export function Options(props: RouteProps): Router { return new Router().options(props.pattern, props.handler); } /** * Patch is the route component for a PATCH route. */ export function Patch(props: RouteProps): Router { return new Router().patch(props.pattern, props.handler); } /** * Post is the route component for a POST route. */ export function Post(props: RouteProps): Router { return new Router().post(props.pattern, props.handler); } /** * Put is the route component for a PUT route. */ export function Put(props: RouteProps): Router { return new Router().put(props.pattern, props.handler); } /** * Trace is the route component for a TRACE route. */ export function Trace(props: RouteProps): Router { return new Router().trace(props.pattern, props.handler); }