Skip to main content
Home

@km/subway@1.0.0

latest
Works with
This package works with Node.js, Deno, Bun, BrowsersIt is unknown whether this package works with Cloudflare Workers
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
This package works with Browsers
JSR Score76%
Downloads3/wk
Published11 months ago (1.0.0)

Extra-flexible router that stores references in the Map

class Subway
implements SubwaySub<R>

Represents a Subway router that manages routes and their handlers.

Examples

Example 1

import { type SubwaySub, Subway } from 'jsr:@km/subway';
import { SimpleRoute } from 'jsr:@km/subway/routes/simple.ts';

type Result = Promise<any>;
class Context {
  constructor(public readonly req: Request) {}
}
class Route extends SimpleRoute<Context, Result> {
  obtain_valid_json<T>(schema: { validate: (data: unknown) => Promise<T> }) {
    return async (ctx: Context) => schema.validate(await ctx.req.json());
  }
}

const Router = new Subway(Route);

const sample_route_middleware = (route: Route) => {
  route.use(async (ctx, next) => {
    console.log('before');
    const result = await next(ctx);
    console.log('after');
    return result;
  });
};

const sample_group_middleware = (scope: SubwaySub<Route>) => {
  scope.use(sample_route_middleware);
};

Router.sub('user', (scope) => {
  scope.sub('friends', (scope) => {
    sample_group_middleware(scope);

    scope.add('get', async (ctx) => {});
  });

  scope.cast('set', (route) => {
    sample_route_middleware(route);
    const obtain = route.obtain_valid_json({ validate: async (data) => data });

    return async (ctx) => {
      const data = await obtain(ctx);
    };
  });
});

Deno.serve(async (req) => {
  const route = Router.find(new URL(req.url).pathname)!;
  const result = await route.execute(new Context(req));
  return new Response(result);
});

Constructors

new
Subway(Route: RC<R>)

Type Parameters

R extends AnyRoute = AnyRoute

The type of route.

Properties

private
registry: Registry<R>

Methods

add(
action: Action,
handler: Handler<R>,
): R

Adds a new route with the specified action and handler.

cast(
action: Action,
factory: Factory<R>,
): R

Casts a new route with the specified action using the provided factory.

cast_root(factory: Factory<R>): R

Casts a root route using the provided factory.

clear(): void

Clears all routes from the router.

find(action: string): R | undefined

Finds a route by its action.

group(factory: (sub: Subway<R>) => void): Group<R>

Creates a group of routes using the provided factory.

inject(
prefix: Action,
group: Group<R>,
): void

Injects a group of routes with the specified prefix.

sub(
prefix: string,
factory: (sub: Sub<R>) => void,
): void

Creates a sub-router with the specified prefix and callback.

through(): IterableIterator<[Action | undefined, R]>

Returns an iterator for all routes in the router.

use(middleware: Middleware<R>): void

Adds a middleware to the router.

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:@km/subway

Import symbol

import { Subway } from "@km/subway";
or

Import directly with a jsr specifier

import { Subway } from "jsr:@km/subway";

Add Package

pnpm i jsr:@km/subway
or (using pnpm 10.8 or older)
pnpm dlx jsr add @km/subway

Import symbol

import { Subway } from "@km/subway";

Add Package

yarn add jsr:@km/subway
or (using Yarn 4.8 or older)
yarn dlx jsr add @km/subway

Import symbol

import { Subway } from "@km/subway";

Add Package

vlt install jsr:@km/subway

Import symbol

import { Subway } from "@km/subway";

Add Package

npx jsr add @km/subway

Import symbol

import { Subway } from "@km/subway";

Add Package

bunx jsr add @km/subway

Import symbol

import { Subway } from "@km/subway";