npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

orpc-file-based-router

v0.1.1

Published

File-based router plugin for oRPC - automatically generate oRPC router from your file structure

Readme

orpc-file-based-router

A plugin for oRPC that automatically generates an oRPC router configuration based on your file structure, inspired by Next.js and express-file-routing approaches.

✨ Highlights

  • 📁 File-based Structure: Organize your API endpoints intuitively through your filesystem
  • 🔄 Zero Configuration: Generate routes automatically based on your directory structure
  • ⚡️ Development Speed: Eliminate boilerplate code and reduce maintenance overhead
  • 🔍 Dynamic Routing: Support for path parameters using {param} syntax in file names
  • 📑 Index Routes: Support for index routes via index.ts files

⚠️ IMPORTANT: At this time, the plugin's functionality is only guaranteed in nodejs runtime

Quickstart

  1. If you're new to oRPC, read the oRPC documentation. Install and configure a basic oRPC server

  2. Install package

npm install orpc-file-based-router
# or
yarn add orpc-file-based-router
  1. Create a routes directory structure (for example):
src/routes
  ├── auth
  │    ├── me.ts 
  │    ├── signin.ts 
  │    └── signup.ts 
  │
  ├── planets
  │    ├── {id}
  │    │    ├── find.ts 
  │    │    └── update.ts 
  │    │
  │    ├── create.ts 
  │    ├── index.ts 
  │    └── list.ts 
  │
  └── sse.ts
  1. Each file should export an oRPC function

  2. Simply replace router in your handlers with the result of the createRouter function:

import { RPCHandler } from "@orpc/server/node";
import { createRouter } from "orpc-file-based-router";

const routesDir = new URL("./routes", import.meta.url).pathname;
const router = await createRouter(routesDir);

const handler = new RPCHandler(router);

Note: If your environment doesn't support top-level await, wrap your server startup code in an async function:

async function startServer() {
  const router = await createRouter(routesDir);
  const handler = new RPCHandler(router);
  // ... start your server
}
startServer();

🔒 Type-Safe Client Configuration (Optional)

For users of the oRPC client, we provide automatic configuration generation for enhanced type safety and improved developer experience.

  1. Add the following code to your main server file (e.g., server.ts or main.ts). This will automatically regenerate the router configuration each time your server starts:
import { generateRouter } from "orpc-file-based-router";

const routesDir = new URL("./routes", import.meta.url).pathname;
const outputFile = new URL("./router.ts", import.meta.url).pathname;
generateRouter(routesDir, outputFile);
  1. Generated router is ready to use in client:
// router.ts
import { me } from "./routes/auth/me";
import { signin } from "./routes/auth/signin";
import { signup } from "./routes/auth/signup";
import { createPlanet } from "./routes/planets/create";
import { indexRoute } from "./routes/planets";
import { listPlanets } from "./routes/planets/list";
import { findPlanet } from "./routes/planets/{id}/find";
import { updatePlanet } from "./routes/planets/{id}/update";
import { sse } from "./routes/sse";

export const router = {
  auth: {
    me: me.route({ path: "/auth/me" }),
    signin: signin.route({ path: "/auth/signin" }),
    signup: signup.route({ path: "/auth/signup" }),
  },
  planets: {
    create: createPlanet.route({ path: "/planets/create" }),
    indexRoute: indexRoute.route({ path: "/planets" }),
    list: listPlanets.route({ path: "/planets/list" }),
    find: findPlanet.route({ path: "/planets/{id}/find" }),
    update: updatePlanet.route({ path: "/planets/{id}/update" }),
  },
  sse: sse.route({ path: "/sse" }),
};

// lib/orpc.ts
const client: RouterClient<typeof router> = createORPCClient(link)

🛠 Configuration Options

When using generateRouter, you can provide additional options to customize the output:

| Field | Type | Required | Default Value | Description | |-------------------|----------|--------------|-----------------------|------------------------------------------------------------------------------------------------------------------------------| | importExtension | string | false | ""(No extension) | File extension to append to import statements in the generated router. Useful when your build setup requires specific extensions. Example: .jsimport { me } from "./routes/auth/me.js" |

📄 License

MIT License - feel free to use this in your own projects!