@kibinrpc/server 0.0.4 → 0.1.0
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 +12 -12
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -11,13 +11,13 @@ npm install @kibinrpc/server
|
|
|
11
11
|
## Quick start
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
|
-
import { ServerAction, defineActions, createRouter, KibinError } from
|
|
14
|
+
import { ServerAction, defineActions, createRouter, KibinError } from "@kibinrpc/server"
|
|
15
15
|
|
|
16
16
|
class UserActions {
|
|
17
17
|
@ServerAction()
|
|
18
18
|
async getUser(id: string) {
|
|
19
19
|
const user = await db.users.find(id)
|
|
20
|
-
if (!user) throw new KibinError(
|
|
20
|
+
if (!user) throw new KibinError("NOT_FOUND", "User not found")
|
|
21
21
|
return user
|
|
22
22
|
}
|
|
23
23
|
}
|
|
@@ -40,7 +40,7 @@ Mount on any framework that supports the Web `Request`/`Response` API:
|
|
|
40
40
|
|
|
41
41
|
```ts
|
|
42
42
|
// Hono
|
|
43
|
-
app.post(
|
|
43
|
+
app.post("/api/rpc", (c) => router.handler(c.req.raw))
|
|
44
44
|
|
|
45
45
|
// Next.js App Router
|
|
46
46
|
export const POST = router.handler
|
|
@@ -55,7 +55,7 @@ Only explicitly registered functions are callable. Everything else is rejected w
|
|
|
55
55
|
### Class decorator
|
|
56
56
|
|
|
57
57
|
```ts
|
|
58
|
-
import { ServerAction } from
|
|
58
|
+
import { ServerAction } from "@kibinrpc/server"
|
|
59
59
|
|
|
60
60
|
class UserActions {
|
|
61
61
|
@ServerAction()
|
|
@@ -69,7 +69,7 @@ class UserActions {
|
|
|
69
69
|
### Functional
|
|
70
70
|
|
|
71
71
|
```ts
|
|
72
|
-
import { defineActions, serverAction } from
|
|
72
|
+
import { defineActions, serverAction } from "@kibinrpc/server"
|
|
73
73
|
|
|
74
74
|
// Register a whole namespace at once
|
|
75
75
|
const postActions = defineActions({
|
|
@@ -88,8 +88,8 @@ Interceptors run for every call — including each item inside a batched request
|
|
|
88
88
|
```ts
|
|
89
89
|
const router = createRouter({ user, post }, {
|
|
90
90
|
beforeAction({ namespace, method, args, request }) {
|
|
91
|
-
const token = request.headers.get(
|
|
92
|
-
if (!token) throw new KibinError(
|
|
91
|
+
const token = request.headers.get("Authorization")
|
|
92
|
+
if (!token) throw new KibinError("UNAUTHORIZED", "Missing token")
|
|
93
93
|
},
|
|
94
94
|
|
|
95
95
|
afterAction({ namespace, method, result }) {
|
|
@@ -107,11 +107,11 @@ const router = createRouter({ user, post }, {
|
|
|
107
107
|
Throw `KibinError` to send a structured error to the client:
|
|
108
108
|
|
|
109
109
|
```ts
|
|
110
|
-
import { KibinError } from
|
|
110
|
+
import { KibinError } from "@kibinrpc/server"
|
|
111
111
|
|
|
112
|
-
throw new KibinError(
|
|
113
|
-
throw new KibinError(
|
|
114
|
-
throw new KibinError(
|
|
112
|
+
throw new KibinError("NOT_FOUND", "User not found")
|
|
113
|
+
throw new KibinError("UNAUTHORIZED", "Invalid token")
|
|
114
|
+
throw new KibinError("BAD_REQUEST", "Invalid input")
|
|
115
115
|
```
|
|
116
116
|
|
|
117
117
|
Any other thrown error becomes `{ code: 'INTERNAL_ERROR' }` — the original message is not leaked.
|
|
@@ -157,5 +157,5 @@ import type {
|
|
|
157
157
|
RpcRequest,
|
|
158
158
|
RpcResponse,
|
|
159
159
|
RpcBatchItemResponse,
|
|
160
|
-
} from
|
|
160
|
+
} from "@kibinrpc/server"
|
|
161
161
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kibinrpc/server",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Devloper-friendly RPC server router with full type inference",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "ixexel661",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "tsdown",
|
|
38
|
-
"dev": "tsdown --watch"
|
|
38
|
+
"dev": "tsdown --watch",
|
|
39
|
+
"test": "vitest run"
|
|
39
40
|
}
|
|
40
41
|
}
|