Skip to main content
Home

Built and signed on GitHub Actions

Build tool to generate a TypeScript router module from routes discovered in the filesystem (like Fresh, Next, etc).

This package works with Node.js, Deno, Bun
This package works with Node.js
This package works with Deno
This package works with Bun
JSR Score
88%
Published
2 months ago (0.26.1)

Filesystem based router code generation

Many frameworks provide some form of filesystem based routing (Next.js, Fresh, etc), but they all have their own opinions, and bake the mechanism deep into the framework.

This package builds upon @http/discovery, to turn the discovered routes into a static TypeScript module.

Example

import { generateRoutesModule } from "@http/generate/generate-routes-module";
import { dprintFormatModule } from "@http/generate/dprint-format-module";

await generateRoutesModule({
  pattern: "/",
  fileRootUrl: import.meta.resolve("./routes"),
  moduleOutUrl: import.meta.resolve("./routes.ts"),
  pathMapper: "@http/discovery/fresh-path-mapper",
  formatModule: dprintFormatModule(),
  verbose: true,
});

This script will discover routes using discoverRoutes, and generate a static routes.ts module, something like this...

// IMPORTANT: This file has been automatically generated, DO NOT edit by hand.

import { byMethod } from "@http/route/by-method";
import { byPattern } from "@http/route/by-pattern";
import { cascade } from "@http/route/cascade";
import { lazy } from "@http/route/lazy";

export default cascade(
  byPattern("/user/:name", lazy(() => import("./routes/user/[name].ts"))),
  byPattern(
    "/methods",
    lazy(async () => byMethod(await import("./routes/methods.ts"))),
  ),
  byPattern("/", lazy(() => import("./routes/index.ts"))),
);

This can then be used as part of the router for a server:

import routes from "./routes.ts";
import { withFallback } from "@http/route/with-fallback";

await Deno.serve(withFallback(routes)).finished;

or for use with the new deno serve command:

import routes from "./routes.ts";
import { withFallback } from "@http/route/with-fallback";

export default {
  fetch: withFallback(routes),
};

I recommend generating the modules at development time and commit to git along with the rest of your code. You can also trigger generation with your dev server.

Take a look at the full example for this:

import generateRoutes from "../scripts/gen.ts";
import init from "@http/host-deno-local/init";

await generateRoutes();

// This allows loading of a new or modified routes.ts module
const handler = lazy(import.meta.resolve("./handler.ts"));

await Deno.serve(await init(handler)).finished;
Built and signed on
GitHub Actions

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.