@fy-tools/rpc-server-elysia 0.0.119-alpha.10387 → 0.0.119-alpha.10388
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 +142 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,7 +1,145 @@
|
|
|
1
|
-
# rpc-server-elysia
|
|
1
|
+
# @fy-tools/rpc-server-elysia
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Elysia adapter for `@fy-tools/rpc-server`. Pass your app schema and an Elysia instance — routes and groups are registered automatically, and handlers are fully type-safe.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
```bash
|
|
8
|
+
npm install @fy-tools/rpc-server-elysia elysia
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### 1. Define a schema
|
|
14
|
+
|
|
15
|
+
Use `@fy-tools/rpc-server` to define your API surface. See its [README](../rpc-server/README.md) for full details.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// schema.ts
|
|
19
|
+
import { App, Controller, HttpMethod, Route } from '@fy-tools/rpc-server';
|
|
20
|
+
import { z } from 'zod';
|
|
21
|
+
|
|
22
|
+
export const Schema = new App()
|
|
23
|
+
.controller(
|
|
24
|
+
new Controller('auth')
|
|
25
|
+
.route(
|
|
26
|
+
new Route('login', HttpMethod.POST)
|
|
27
|
+
.body(z.object({ email: z.string(), password: z.string() }))
|
|
28
|
+
.response(z.object({ token: z.string() }))
|
|
29
|
+
)
|
|
30
|
+
)
|
|
31
|
+
.controller(
|
|
32
|
+
new Controller('users')
|
|
33
|
+
.route(
|
|
34
|
+
new Route('/', HttpMethod.GET)
|
|
35
|
+
.response(z.object({
|
|
36
|
+
items: z.array(z.object({ id: z.string(), email: z.string() })),
|
|
37
|
+
}))
|
|
38
|
+
)
|
|
39
|
+
.route(
|
|
40
|
+
new Route(':id', HttpMethod.GET)
|
|
41
|
+
.params(z.object({ id: z.string() }))
|
|
42
|
+
.response(z.object({ id: z.string(), email: z.string() }))
|
|
43
|
+
)
|
|
44
|
+
.route(
|
|
45
|
+
new Route(':id', HttpMethod.DELETE)
|
|
46
|
+
.params(z.object({ id: z.string() }))
|
|
47
|
+
)
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
export type Schema = typeof Schema;
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 2. Create the server
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { App } from '@fy-tools/rpc-server-elysia';
|
|
57
|
+
import { Elysia } from 'elysia';
|
|
58
|
+
import { Schema } from './schema';
|
|
59
|
+
|
|
60
|
+
const server = new App(new Elysia(), Schema);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 3. Add middleware
|
|
64
|
+
|
|
65
|
+
Use `.build()` to apply Elysia plugins before handlers run. The returned `App` carries the updated Elysia type, so resolved/derived values are available in every handler.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
const authMiddleware = new Elysia({ name: 'auth' }).resolve(
|
|
69
|
+
{ as: 'global' },
|
|
70
|
+
async ({ headers }) => {
|
|
71
|
+
const token = headers.authorization?.split(' ')[1];
|
|
72
|
+
return { userId: await verifyToken(token) };
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const server = new App(new Elysia(), Schema)
|
|
77
|
+
.build((app) => app.use(authMiddleware));
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### 4. Implement handlers
|
|
81
|
+
|
|
82
|
+
Access controllers via `.C` and routes via `.R`. For path encoding rules, see the [rpc-server README](../rpc-server/README.md#path-encoding).
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
server.C.auth.R.post_login.handler(async (ctx) => {
|
|
86
|
+
const token = await authenticate(ctx.body.email, ctx.body.password);
|
|
87
|
+
return { token };
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
server.C.users.R.get_default.handler(async (ctx) => {
|
|
91
|
+
return { items: await db.users.findAll({ userId: ctx.userId }) };
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
server.C.users.R.get_$id.handler(async (ctx) => {
|
|
95
|
+
return db.users.findById(ctx.params.id);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
server.C.users.R.delete_$id.handler(async (ctx) => {
|
|
99
|
+
await db.users.delete(ctx.params.id);
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
`ctx.body`, `ctx.query`, `ctx.params` are typed from the schema. Anything added via `.build()` (e.g. `ctx.userId` above) is also fully typed.
|
|
104
|
+
|
|
105
|
+
### 5. Start the server
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
server._app.listen(3000);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## API reference
|
|
114
|
+
|
|
115
|
+
### `App`
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
new App(app: AnyElysia, schema: AnyApp)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Registers all controllers from the schema onto the Elysia instance.
|
|
122
|
+
|
|
123
|
+
| Member | Description |
|
|
124
|
+
|---|---|
|
|
125
|
+
| `.C` | Typed proxy of all controllers, keyed by encoded path |
|
|
126
|
+
| `.build(fn)` | Applies middleware and returns a new `App` with the updated type |
|
|
127
|
+
| `._app` | The underlying Elysia instance — call `.listen()` on this |
|
|
128
|
+
|
|
129
|
+
### `Controller`
|
|
130
|
+
|
|
131
|
+
Instantiated automatically by `App`. Wraps a single controller schema as an Elysia group.
|
|
132
|
+
|
|
133
|
+
| Member | Description |
|
|
134
|
+
|---|---|
|
|
135
|
+
| `.R` | Typed proxy of all routes in the controller, keyed by encoded method + path |
|
|
136
|
+
| `.build(fn)` | Applies middleware scoped to this controller |
|
|
137
|
+
|
|
138
|
+
### `Route`
|
|
139
|
+
|
|
140
|
+
Instantiated automatically by `Controller`. Wraps a single route schema.
|
|
141
|
+
|
|
142
|
+
| Member | Description |
|
|
143
|
+
|---|---|
|
|
144
|
+
| `.handler(fn)` | Sets the request handler — called from `.R` on a controller |
|
|
145
|
+
| `.build(fn)` | Applies middleware scoped to this route |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fy-tools/rpc-server-elysia",
|
|
3
|
-
"version": "0.0.119-alpha.
|
|
3
|
+
"version": "0.0.119-alpha.10388",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/festusyuma/fy-tools.git",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"!**/*.tsbuildinfo"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@fy-tools/rpc-server": "0.0.119-alpha.
|
|
25
|
+
"@fy-tools/rpc-server": "0.0.119-alpha.10388",
|
|
26
26
|
"elysia": "^1.4.29",
|
|
27
27
|
"tslib": "^2.3.0",
|
|
28
28
|
"@standard-schema/spec": "^1.0.0"
|