orpc-file-based-router 0.1.1 → 0.1.2
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 +23 -9
- package/dist/index.cjs +12 -0
- package/dist/index.d.cts +4 -1
- package/dist/index.d.mts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.mjs +12 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,15 +62,29 @@ 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,
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
65
|
+
> **Note:** If your environment doesn't support top-level await, just use `cachedRouter` for example in expressjs it could be:
|
|
66
|
+
```typescript
|
|
67
|
+
import { RPCHandler } from "@orpc/server/node";
|
|
68
|
+
import { cachedRouter } from "orpc-file-based-router";
|
|
69
|
+
|
|
70
|
+
const routesDir = new URL("./routes", import.meta.url).pathname;
|
|
71
|
+
const router = cachedRouter(routesDir)
|
|
72
|
+
|
|
73
|
+
app.use('/rpc{/*path}', async (req, res, next) => {
|
|
74
|
+
|
|
75
|
+
const handler = new RPCHandler(await router.getRouter());
|
|
76
|
+
|
|
77
|
+
const { matched } = await handler.handle(req, res, {
|
|
78
|
+
prefix: '/rpc',
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
if (matched) {
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
next()
|
|
86
|
+
})
|
|
87
|
+
```
|
|
74
88
|
|
|
75
89
|
## 🔒 Type-Safe Client Configuration (Optional)
|
|
76
90
|
|
package/dist/index.cjs
CHANGED
|
@@ -36,6 +36,17 @@ 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;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
39
50
|
async function generateRouter(routesDir, outputFile, options) {
|
|
40
51
|
const files = walkTree(
|
|
41
52
|
routesDir
|
|
@@ -120,5 +131,6 @@ async function generateRoutes(files) {
|
|
|
120
131
|
return routes;
|
|
121
132
|
}
|
|
122
133
|
|
|
134
|
+
exports.cachedRouter = cachedRouter;
|
|
123
135
|
exports.createRouter = createRouter;
|
|
124
136
|
exports.generateRouter = generateRouter;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
declare function createRouter(routesDir: string): Promise<Router>;
|
|
2
|
+
declare function cachedRouter(routesDir: string): Promise<{
|
|
3
|
+
getRouter: () => Promise<Router>;
|
|
4
|
+
}>;
|
|
2
5
|
type GeneratorOptions = {
|
|
3
6
|
/**
|
|
4
7
|
* File extension to append to import statements in the generated router.
|
|
@@ -11,4 +14,4 @@ type GeneratorOptions = {
|
|
|
11
14
|
declare function generateRouter(routesDir: string, outputFile: string, options?: GeneratorOptions): Promise<void>;
|
|
12
15
|
type Router = Record<string, any>;
|
|
13
16
|
|
|
14
|
-
export { createRouter, generateRouter };
|
|
17
|
+
export { cachedRouter, createRouter, generateRouter };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
declare function createRouter(routesDir: string): Promise<Router>;
|
|
2
|
+
declare function cachedRouter(routesDir: string): Promise<{
|
|
3
|
+
getRouter: () => Promise<Router>;
|
|
4
|
+
}>;
|
|
2
5
|
type GeneratorOptions = {
|
|
3
6
|
/**
|
|
4
7
|
* File extension to append to import statements in the generated router.
|
|
@@ -11,4 +14,4 @@ type GeneratorOptions = {
|
|
|
11
14
|
declare function generateRouter(routesDir: string, outputFile: string, options?: GeneratorOptions): Promise<void>;
|
|
12
15
|
type Router = Record<string, any>;
|
|
13
16
|
|
|
14
|
-
export { createRouter, generateRouter };
|
|
17
|
+
export { cachedRouter, createRouter, generateRouter };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
declare function createRouter(routesDir: string): Promise<Router>;
|
|
2
|
+
declare function cachedRouter(routesDir: string): Promise<{
|
|
3
|
+
getRouter: () => Promise<Router>;
|
|
4
|
+
}>;
|
|
2
5
|
type GeneratorOptions = {
|
|
3
6
|
/**
|
|
4
7
|
* File extension to append to import statements in the generated router.
|
|
@@ -11,4 +14,4 @@ type GeneratorOptions = {
|
|
|
11
14
|
declare function generateRouter(routesDir: string, outputFile: string, options?: GeneratorOptions): Promise<void>;
|
|
12
15
|
type Router = Record<string, any>;
|
|
13
16
|
|
|
14
|
-
export { createRouter, generateRouter };
|
|
17
|
+
export { cachedRouter, createRouter, generateRouter };
|
package/dist/index.mjs
CHANGED
|
@@ -30,6 +30,17 @@ 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;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
33
44
|
async function generateRouter(routesDir, outputFile, options) {
|
|
34
45
|
const files = walkTree(
|
|
35
46
|
routesDir
|
|
@@ -114,4 +125,4 @@ async function generateRoutes(files) {
|
|
|
114
125
|
return routes;
|
|
115
126
|
}
|
|
116
127
|
|
|
117
|
-
export { createRouter, generateRouter };
|
|
128
|
+
export { cachedRouter, createRouter, generateRouter };
|
package/package.json
CHANGED