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 +6 -5
- package/dist/index.cjs +12 -10
- package/dist/index.d.cts +5 -4
- package/dist/index.d.mts +5 -4
- package/dist/index.d.ts +5 -4
- package/dist/index.mjs +12 -10
- package/package.json +1 -1
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
|
-
|
|
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 {
|
|
68
|
+
import { lazy, createRouter } from "orpc-file-based-router";
|
|
69
69
|
|
|
70
70
|
const routesDir = new URL("./routes", import.meta.url).pathname;
|
|
71
|
-
|
|
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
|
|
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.
|
|
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
|
-
|
|
40
|
-
let
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
|
3
|
-
|
|
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 {
|
|
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
|
|
3
|
-
|
|
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 {
|
|
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
|
|
3
|
-
|
|
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 {
|
|
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
|
-
|
|
34
|
-
let
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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 {
|
|
130
|
+
export { createRouter, generateRouter, lazy };
|
package/package.json
CHANGED