Skip to main content
Home

@km/subway@1.0.0

latest

Extra-flexible router that stores references in the Map

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 Score
76%
Published
10 months ago (1.0.0)
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);
});

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

Import symbol

import * as subway from "@km/subway";
or

Import directly with a jsr specifier

import * as 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 * as 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 * as subway from "@km/subway";

Add Package

vlt install jsr:@km/subway

Import symbol

import * as subway from "@km/subway";

Add Package

npx jsr add @km/subway

Import symbol

import * as subway from "@km/subway";

Add Package

bunx jsr add @km/subway

Import symbol

import * as subway from "@km/subway";