orpc-file-based-router 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -62,17 +62,18 @@ const router = await createRouter(routesDir);
62
62
  const handler = new RPCHandler(router);
63
63
 
64
64
  ```
65
- > **Note:** If your environment doesn't support top-level await, just use `cachedRouter` for example in expressjs it could be:
65
+ **Note:** If your environment doesn't support top-level await, just use `lazy` for example in expressjs it could be:
66
66
  ```typescript
67
67
  import { RPCHandler } from "@orpc/server/node";
68
- import { cachedRouter } from "orpc-file-based-router";
68
+ import { lazy, createRouter } from "orpc-file-based-router";
69
69
 
70
70
  const routesDir = new URL("./routes", import.meta.url).pathname;
71
- const router = cachedRouter(routesDir)
71
+
72
+ const router = lazy(() => createRouter(routesDir))
72
73
 
73
74
  app.use('/rpc{/*path}', async (req, res, next) => {
74
75
 
75
- const handler = new RPCHandler(await router.getRouter());
76
+ const handler = new RPCHandler(await router());
76
77
 
77
78
  const { matched } = await handler.handle(req, res, {
78
79
  prefix: '/rpc',
@@ -90,7 +91,7 @@ app.use('/rpc{/*path}', async (req, res, next) => {
90
91
 
91
92
  For users of the [oRPC client](https://orpc.unnoq.com/docs/client/client-side), we provide automatic configuration generation for enhanced type safety and improved developer experience.
92
93
 
93
- 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:
94
+ 1. You can add this code either directly in your server project (e.g., in server.ts or main.ts) or put it into a separate script (e.g., router-gen.ts).
94
95
 
95
96
  ```typescript
96
97
  import { generateRouter } from "orpc-file-based-router";
package/dist/index.cjs CHANGED
@@ -36,17 +36,19 @@ async function createRouter(routesDir) {
36
36
  return r.exports[e].route({ path: `${r.path}` });
37
37
  });
38
38
  }
39
- async function cachedRouter(routesDir) {
40
- let router = null;
41
- return {
42
- getRouter: async () => {
43
- if (!router) {
44
- router = await createRouter(routesDir);
45
- }
46
- return router;
39
+ const lazy = (create) => {
40
+ let cache = null;
41
+ const fn = () => {
42
+ if (!cache) {
43
+ cache = Promise.resolve(create());
47
44
  }
45
+ return cache;
48
46
  };
49
- }
47
+ fn.reset = () => {
48
+ cache = null;
49
+ };
50
+ return fn;
51
+ };
50
52
  async function generateRouter(routesDir, outputFile, options) {
51
53
  const files = walkTree(
52
54
  routesDir
@@ -131,6 +133,6 @@ async function generateRoutes(files) {
131
133
  return routes;
132
134
  }
133
135
 
134
- exports.cachedRouter = cachedRouter;
135
136
  exports.createRouter = createRouter;
136
137
  exports.generateRouter = generateRouter;
138
+ exports.lazy = lazy;
package/dist/index.d.cts CHANGED
@@ -1,7 +1,8 @@
1
1
  declare function createRouter(routesDir: string): Promise<Router>;
2
- declare function cachedRouter(routesDir: string): Promise<{
3
- getRouter: () => Promise<Router>;
4
- }>;
2
+ declare const lazy: <T>(create: () => Promise<T> | T) => {
3
+ (): Promise<T>;
4
+ reset(): void;
5
+ };
5
6
  type GeneratorOptions = {
6
7
  /**
7
8
  * File extension to append to import statements in the generated router.
@@ -14,4 +15,4 @@ type GeneratorOptions = {
14
15
  declare function generateRouter(routesDir: string, outputFile: string, options?: GeneratorOptions): Promise<void>;
15
16
  type Router = Record<string, any>;
16
17
 
17
- export { cachedRouter, createRouter, generateRouter };
18
+ export { createRouter, generateRouter, lazy };
package/dist/index.d.mts CHANGED
@@ -1,7 +1,8 @@
1
1
  declare function createRouter(routesDir: string): Promise<Router>;
2
- declare function cachedRouter(routesDir: string): Promise<{
3
- getRouter: () => Promise<Router>;
4
- }>;
2
+ declare const lazy: <T>(create: () => Promise<T> | T) => {
3
+ (): Promise<T>;
4
+ reset(): void;
5
+ };
5
6
  type GeneratorOptions = {
6
7
  /**
7
8
  * File extension to append to import statements in the generated router.
@@ -14,4 +15,4 @@ type GeneratorOptions = {
14
15
  declare function generateRouter(routesDir: string, outputFile: string, options?: GeneratorOptions): Promise<void>;
15
16
  type Router = Record<string, any>;
16
17
 
17
- export { cachedRouter, createRouter, generateRouter };
18
+ export { createRouter, generateRouter, lazy };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  declare function createRouter(routesDir: string): Promise<Router>;
2
- declare function cachedRouter(routesDir: string): Promise<{
3
- getRouter: () => Promise<Router>;
4
- }>;
2
+ declare const lazy: <T>(create: () => Promise<T> | T) => {
3
+ (): Promise<T>;
4
+ reset(): void;
5
+ };
5
6
  type GeneratorOptions = {
6
7
  /**
7
8
  * File extension to append to import statements in the generated router.
@@ -14,4 +15,4 @@ type GeneratorOptions = {
14
15
  declare function generateRouter(routesDir: string, outputFile: string, options?: GeneratorOptions): Promise<void>;
15
16
  type Router = Record<string, any>;
16
17
 
17
- export { cachedRouter, createRouter, generateRouter };
18
+ export { createRouter, generateRouter, lazy };
package/dist/index.mjs CHANGED
@@ -30,17 +30,19 @@ async function createRouter(routesDir) {
30
30
  return r.exports[e].route({ path: `${r.path}` });
31
31
  });
32
32
  }
33
- async function cachedRouter(routesDir) {
34
- let router = null;
35
- return {
36
- getRouter: async () => {
37
- if (!router) {
38
- router = await createRouter(routesDir);
39
- }
40
- return router;
33
+ const lazy = (create) => {
34
+ let cache = null;
35
+ const fn = () => {
36
+ if (!cache) {
37
+ cache = Promise.resolve(create());
41
38
  }
39
+ return cache;
42
40
  };
43
- }
41
+ fn.reset = () => {
42
+ cache = null;
43
+ };
44
+ return fn;
45
+ };
44
46
  async function generateRouter(routesDir, outputFile, options) {
45
47
  const files = walkTree(
46
48
  routesDir
@@ -125,4 +127,4 @@ async function generateRoutes(files) {
125
127
  return routes;
126
128
  }
127
129
 
128
- export { cachedRouter, createRouter, generateRouter };
130
+ export { createRouter, generateRouter, lazy };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orpc-file-based-router",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "File-based router plugin for oRPC - automatically generate oRPC router from your file structure",
5
5
  "author": "zeeeeby",
6
6
  "license": "MIT",