@jokio/rpc 0.1.0 → 0.1.1
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 +42 -39
- package/package.json +4 -8
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @jokio/rpc
|
|
2
2
|
|
|
3
3
|
A type-safe RPC framework for TypeScript with Zod validation, designed for Express servers and HTTP clients.
|
|
4
4
|
|
|
@@ -15,7 +15,7 @@ A type-safe RPC framework for TypeScript with Zod validation, designed for Expre
|
|
|
15
15
|
## Installation
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
-
npm install
|
|
18
|
+
npm install @jokio/rpc zod express
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
## Usage
|
|
@@ -23,14 +23,14 @@ npm install jokrpc zod express
|
|
|
23
23
|
### 1. Define Your Router Configuration
|
|
24
24
|
|
|
25
25
|
```typescript
|
|
26
|
-
import { defineRouterConfig } from
|
|
27
|
-
import { z } from
|
|
26
|
+
import { defineRouterConfig } from "@jokio/rpc";
|
|
27
|
+
import { z } from "zod";
|
|
28
28
|
|
|
29
29
|
const routerConfig = defineRouterConfig({
|
|
30
30
|
GET: {
|
|
31
|
-
|
|
31
|
+
"/users/:id": {
|
|
32
32
|
query: z.object({
|
|
33
|
-
include: z.enum([
|
|
33
|
+
include: z.enum(["posts", "comments"]).optional(),
|
|
34
34
|
}),
|
|
35
35
|
result: z.object({
|
|
36
36
|
id: z.string(),
|
|
@@ -40,7 +40,7 @@ const routerConfig = defineRouterConfig({
|
|
|
40
40
|
},
|
|
41
41
|
},
|
|
42
42
|
POST: {
|
|
43
|
-
|
|
43
|
+
"/users": {
|
|
44
44
|
body: z.object({
|
|
45
45
|
name: z.string(),
|
|
46
46
|
email: z.string().email(),
|
|
@@ -55,66 +55,66 @@ const routerConfig = defineRouterConfig({
|
|
|
55
55
|
}),
|
|
56
56
|
},
|
|
57
57
|
},
|
|
58
|
-
})
|
|
58
|
+
});
|
|
59
59
|
```
|
|
60
60
|
|
|
61
61
|
### 2. Set Up the Server
|
|
62
62
|
|
|
63
63
|
```typescript
|
|
64
|
-
import express from
|
|
65
|
-
import { applyConfigToExpressRouter } from
|
|
64
|
+
import express from "express";
|
|
65
|
+
import { applyConfigToExpressRouter } from "@jokio/rpc";
|
|
66
66
|
|
|
67
|
-
const app = express()
|
|
68
|
-
app.use(express.json())
|
|
67
|
+
const app = express();
|
|
68
|
+
app.use(express.json());
|
|
69
69
|
|
|
70
|
-
const router = express.Router()
|
|
70
|
+
const router = express.Router();
|
|
71
71
|
|
|
72
72
|
applyConfigToExpressRouter(router, routerConfig, {
|
|
73
73
|
GET: {
|
|
74
|
-
|
|
74
|
+
"/users/:id": async ({ query }) => {
|
|
75
75
|
// Handler implementation
|
|
76
76
|
return {
|
|
77
|
-
id:
|
|
78
|
-
name:
|
|
79
|
-
email:
|
|
80
|
-
}
|
|
77
|
+
id: "1",
|
|
78
|
+
name: "John Doe",
|
|
79
|
+
email: "john@example.com",
|
|
80
|
+
};
|
|
81
81
|
},
|
|
82
82
|
},
|
|
83
83
|
POST: {
|
|
84
|
-
|
|
84
|
+
"/users": async ({ body, query }) => {
|
|
85
85
|
// Handler implementation
|
|
86
86
|
return {
|
|
87
|
-
id:
|
|
87
|
+
id: "2",
|
|
88
88
|
name: body.name,
|
|
89
89
|
email: body.email,
|
|
90
|
-
}
|
|
90
|
+
};
|
|
91
91
|
},
|
|
92
92
|
},
|
|
93
|
-
})
|
|
93
|
+
});
|
|
94
94
|
|
|
95
|
-
app.use(
|
|
96
|
-
app.listen(3000)
|
|
95
|
+
app.use("/api", router);
|
|
96
|
+
app.listen(3000);
|
|
97
97
|
```
|
|
98
98
|
|
|
99
99
|
### 3. Create a Type-Safe Client
|
|
100
100
|
|
|
101
101
|
```typescript
|
|
102
|
-
import { createClient } from
|
|
102
|
+
import { createClient } from "@jokio/rpc";
|
|
103
103
|
|
|
104
104
|
const client = createClient(routerConfig, {
|
|
105
|
-
baseUrl:
|
|
105
|
+
baseUrl: "http://localhost:3000/api",
|
|
106
106
|
validateRequest: true, // Optional: validate requests on client-side
|
|
107
|
-
})
|
|
107
|
+
});
|
|
108
108
|
|
|
109
109
|
// Fully typed API calls
|
|
110
|
-
const user = await client.GET[
|
|
111
|
-
query: { include:
|
|
112
|
-
})
|
|
110
|
+
const user = await client.GET["/users/:id"]({
|
|
111
|
+
query: { include: "posts" },
|
|
112
|
+
});
|
|
113
113
|
|
|
114
|
-
const newUser = await client.POST[
|
|
115
|
-
body: { name:
|
|
114
|
+
const newUser = await client.POST["/users"]({
|
|
115
|
+
body: { name: "Jane Doe", email: "jane@example.com" },
|
|
116
116
|
query: { sendEmail: true },
|
|
117
|
-
})
|
|
117
|
+
});
|
|
118
118
|
```
|
|
119
119
|
|
|
120
120
|
## API Reference
|
|
@@ -128,6 +128,7 @@ Helper function to define a router configuration with type inference.
|
|
|
128
128
|
Applies route handlers to an Express router with automatic validation.
|
|
129
129
|
|
|
130
130
|
**Parameters:**
|
|
131
|
+
|
|
131
132
|
- `router`: Express Router instance
|
|
132
133
|
- `config`: Router configuration object
|
|
133
134
|
- `handlers`: Handler functions for each route
|
|
@@ -137,6 +138,7 @@ Applies route handlers to an Express router with automatic validation.
|
|
|
137
138
|
Creates a type-safe HTTP client.
|
|
138
139
|
|
|
139
140
|
**Options:**
|
|
141
|
+
|
|
140
142
|
- `baseUrl`: Base URL for API requests
|
|
141
143
|
- `headers`: Optional default headers
|
|
142
144
|
- `fetch`: Optional custom fetch function (useful for Node.js or testing)
|
|
@@ -148,26 +150,27 @@ The library provides end-to-end type safety:
|
|
|
148
150
|
|
|
149
151
|
```typescript
|
|
150
152
|
// TypeScript knows the exact shape of requests and responses
|
|
151
|
-
const result = await client.POST[
|
|
153
|
+
const result = await client.POST["/users"]({
|
|
152
154
|
body: {
|
|
153
|
-
name:
|
|
154
|
-
email:
|
|
155
|
+
name: "John",
|
|
156
|
+
email: "invalid-email", // Zod will catch this at runtime
|
|
155
157
|
},
|
|
156
|
-
})
|
|
158
|
+
});
|
|
157
159
|
|
|
158
160
|
// result is typed as { id: string; name: string; email: string }
|
|
159
|
-
console.log(result.id)
|
|
161
|
+
console.log(result.id);
|
|
160
162
|
```
|
|
161
163
|
|
|
162
164
|
## Error Handling
|
|
163
165
|
|
|
164
166
|
The library throws errors for:
|
|
167
|
+
|
|
165
168
|
- HTTP errors (non-2xx responses)
|
|
166
169
|
- Validation errors (invalid request/response data)
|
|
167
170
|
|
|
168
171
|
```typescript
|
|
169
172
|
try {
|
|
170
|
-
await client.POST[
|
|
173
|
+
await client.POST["/users"]({ body: invalidData });
|
|
171
174
|
} catch (error) {
|
|
172
175
|
// Handle validation or HTTP errors
|
|
173
176
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jokio/rpc",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Type-safe RPC framework with Zod validation for Express and TypeScript",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -33,17 +33,13 @@
|
|
|
33
33
|
],
|
|
34
34
|
"author": "",
|
|
35
35
|
"license": "MIT",
|
|
36
|
-
"peerDependencies": {
|
|
37
|
-
"express": "^4.0.0",
|
|
38
|
-
"zod": "^4.0.0"
|
|
39
|
-
},
|
|
40
36
|
"devDependencies": {
|
|
41
37
|
"@types/express": "^4.17.21",
|
|
42
38
|
"@types/node": "^20.10.0",
|
|
43
|
-
"express": "^
|
|
39
|
+
"express": "^5.2.1",
|
|
44
40
|
"tsup": "^8.0.1",
|
|
45
|
-
"typescript": "^5.
|
|
46
|
-
"zod": "^3.
|
|
41
|
+
"typescript": "^5.9.3",
|
|
42
|
+
"zod": "^4.3.4"
|
|
47
43
|
},
|
|
48
44
|
"repository": {
|
|
49
45
|
"type": "git",
|