Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
Orpc File Based Router
https://github.com/zeeeeby/orpc-file-based-router
Admin
Tokens:
765
Snippets:
5
Trust Score:
5.6
Update:
1 year ago
Context
Chat
Benchmark
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### Wrapping Server Startup for Top-Level Await (TypeScript) Source: https://github.com/zeeeeby/orpc-file-based-router/blob/main/README.md This TypeScript example provides a pattern for environments that do not support top-level await. It wraps the router creation and handler initialization within an `async` function, which is then immediately invoked to start the server. ```typescript async function startServer() { const router = await createRouter(routesDir); const handler = new RPCHandler(router); // ... start your server } startServer(); ``` -------------------------------- ### Installing orpc-file-based-router Package (Bash) Source: https://github.com/zeeeeby/orpc-file-based-router/blob/main/README.md This snippet demonstrates how to install the `orpc-file-based-router` package using either npm or yarn, which is the first step to integrate the file-based routing into an oRPC project. ```bash npm install orpc-file-based-router # or yarn add orpc-file-based-router ``` -------------------------------- ### Consuming Generated Router in oRPC Client (TypeScript) Source: https://github.com/zeeeeby/orpc-file-based-router/blob/main/README.md This TypeScript example illustrates how the automatically generated `router.ts` file is structured and how it can be imported and used to configure an oRPC client, enabling type-safe client-side interactions with the server. ```typescript // 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) ``` -------------------------------- ### Creating oRPC Router and Handler (TypeScript) Source: https://github.com/zeeeeby/orpc-file-based-router/blob/main/README.md This TypeScript snippet shows how to initialize the oRPC router using `createRouter` from `orpc-file-based-router` based on a specified routes directory, and then create an `RPCHandler` with the generated router. It also notes a workaround for environments not supporting top-level await. ```typescript 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); ``` -------------------------------- ### Generating Type-Safe Router Configuration (TypeScript) Source: https://github.com/zeeeeby/orpc-file-based-router/blob/main/README.md This TypeScript snippet demonstrates how to use the `generateRouter` function to automatically create a type-safe router configuration file. This file can then be used by the oRPC client for improved type checking and development experience. ```typescript 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); ```