@seam-rpc/core 4.1.0 → 4.3.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 +1 -285
- package/dist/index.d.ts +24 -0
- package/dist/index.js +11 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,285 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
# SeamRPC
|
|
4
|
-
|
|
5
|
-
## About
|
|
6
|
-
SeamRPC is a simple RPC library for client-server communication using TypeScript using Express for the server.
|
|
7
|
-
|
|
8
|
-
Making requests to the server is as simple as calling a function and SeamRPC sends it to server for you under the hood.
|
|
9
|
-
|
|
10
|
-
## Setup
|
|
11
|
-
### Server
|
|
12
|
-
Implement your API functions in a TypeScript file. It's recommended to split different routes into different files, all inside the same folder. You can also optionally include JSDoc comments for the functions. The returned value of an API function is sent from the server to the client. If an error is thrown in the API function in the server, the function throws an error in the client as well (Seam RPC internally responds with HTTP code 400 which the client interprets as an error).
|
|
13
|
-
|
|
14
|
-
> **Note:** For consistency reasons between server and client API functions, Seam RPC requires all API functions to return a Promise.
|
|
15
|
-
|
|
16
|
-
**Example:**
|
|
17
|
-
```
|
|
18
|
-
server-app
|
|
19
|
-
├─ index.ts
|
|
20
|
-
└─ api
|
|
21
|
-
├─ users.ts
|
|
22
|
-
└─ posts.ts
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
`api/users.ts`
|
|
26
|
-
```ts
|
|
27
|
-
import { SeamFile } from "@seam-rpc/server";
|
|
28
|
-
|
|
29
|
-
export interface User {
|
|
30
|
-
id: string;
|
|
31
|
-
name: string;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const users: User[] = [];
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Creates a new user and returns its ID.
|
|
38
|
-
* @param name The name of the user.
|
|
39
|
-
* @returns ID of the newly created user.
|
|
40
|
-
*/
|
|
41
|
-
export async function createUser(name: string): Promise<string> {
|
|
42
|
-
const user = {
|
|
43
|
-
id: Date.now().toString(),
|
|
44
|
-
name
|
|
45
|
-
};
|
|
46
|
-
users.push(user);
|
|
47
|
-
return user.id;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Gets a user by ID.
|
|
52
|
-
* @param id The ID of the user.
|
|
53
|
-
* @returns The user object.
|
|
54
|
-
*/
|
|
55
|
-
export async function getUser(id: string): Promise<User | undefined> {
|
|
56
|
-
const user = users.find(e => e.id == id);
|
|
57
|
-
if (user)
|
|
58
|
-
return user;
|
|
59
|
-
else
|
|
60
|
-
throw new Error("user not found");
|
|
61
|
-
}
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
#### Create a Seam Space
|
|
65
|
-
|
|
66
|
-
A Seam Space is linked to an Express app and is what you defined routers to. You define one Seam Space for your API, which can then be separated in to different routers. Each router can be any kind of structure with functions (e.g. an object or a module). This example uses files as modules.
|
|
67
|
-
|
|
68
|
-
```ts
|
|
69
|
-
import express from "express";
|
|
70
|
-
import { createSeamSpace } from "@seam-rpc/server";
|
|
71
|
-
|
|
72
|
-
// Import as modules
|
|
73
|
-
import * as usersRouter from "./api/users.js";
|
|
74
|
-
import * as postsRouter from "./api/posts.js";
|
|
75
|
-
|
|
76
|
-
// Create express app
|
|
77
|
-
const app = express();
|
|
78
|
-
|
|
79
|
-
// Create Seam Space with express app
|
|
80
|
-
const seamSpace = await createSeamSpace(app);
|
|
81
|
-
|
|
82
|
-
// Create routers
|
|
83
|
-
seamSpace.createRouter("/users", usersRouter);
|
|
84
|
-
seamSpace.createRouter("/posts", postsRouter);
|
|
85
|
-
|
|
86
|
-
// Start express server
|
|
87
|
-
app.listen(3000, () => {
|
|
88
|
-
console.log("Listening on port 3000");
|
|
89
|
-
});
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### Client
|
|
93
|
-
The client needs to have the same schema as your API so you can call the API functions and have autocomplete. Behind the scenes these functions will send an HTTP requests to the server. SeamRPC can automatically generate the client schema files. To do this, you can either run the command `seam-rpc gen-client <input-files> <output-folder>` or [define a config file](#config-file) and then run the command `seam-rpc gen-client`.
|
|
94
|
-
|
|
95
|
-
- `input-files` - Specify what files to generate the client files from. You can use [glob pattern](https://en.wikipedia.org/wiki/Glob_(programming)) to specify the files.
|
|
96
|
-
- `output-folder` - Specify the folder where to store the generated client api files.
|
|
97
|
-
|
|
98
|
-
**Example:**
|
|
99
|
-
`seam-rpc gen-client ./src/api/* ../server-app/src/api`
|
|
100
|
-
|
|
101
|
-
```
|
|
102
|
-
client-app
|
|
103
|
-
├─ index.ts
|
|
104
|
-
└─ api
|
|
105
|
-
├─ users.ts
|
|
106
|
-
└─ posts.ts
|
|
107
|
-
```
|
|
108
|
-
The api folder in the client contains the generated API client files, and should not be manually edited.
|
|
109
|
-
|
|
110
|
-
The generated `api/users.ts` file:
|
|
111
|
-
> Notice that the JSDoc comments are included in the client files.
|
|
112
|
-
```ts
|
|
113
|
-
import { callApi, SeamFile, ISeamFile } from "@seam-rpc/client";
|
|
114
|
-
export interface User {
|
|
115
|
-
id: string;
|
|
116
|
-
name: string;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Creates a new user and returns its ID.
|
|
120
|
-
* @param name The name of the user.
|
|
121
|
-
* @returns ID of the newly created user.
|
|
122
|
-
*/
|
|
123
|
-
export function createUser(name: string): Promise<string> { return callApi("users", "createUser", [name]); }
|
|
124
|
-
/**
|
|
125
|
-
* Gets a user by ID.
|
|
126
|
-
* @param id The ID of the user.
|
|
127
|
-
* @returns The user object.
|
|
128
|
-
*/
|
|
129
|
-
export function getUser(id: string): Promise<User | undefined> { return callApi("users", "getUser", [id]); }
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
#### Connect client to server
|
|
133
|
-
To establish the connection from the client to the server, you need to specify which URL to call. This example is using a self-hosted server running on port 3000 so it uses `http://localhost:3000`. Just call `createClient` to create the client and specify the URL.
|
|
134
|
-
|
|
135
|
-
```ts
|
|
136
|
-
createClient("http://localhost:3000");
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
### Config file
|
|
140
|
-
If you don't want to specify the input files and output folder every time you want to generate the client files, you can create a config file where you define these paths. You can create a `seam-rpc.config.json` file at the root of your project and use the following data:
|
|
141
|
-
```json
|
|
142
|
-
{
|
|
143
|
-
"inputFiles": "./src/api/*",
|
|
144
|
-
"outputFolder": "../client/src/api"
|
|
145
|
-
}
|
|
146
|
-
```
|
|
147
|
-
or you can automatically generate a file using `seam-rpc gen-config [input-files] [output-folder]`. If you don't specify the input files and output folder, it will use the default paths (see JSON above).
|
|
148
|
-
|
|
149
|
-
## Uploading and downloading files
|
|
150
|
-
Both server and client can send files seamlessly. Just use the SeamFile class for this. You can have a parameter as a file or an array/object containing a file. You can have deeply nested files inside objects.
|
|
151
|
-
|
|
152
|
-
A SeamFile has 3 properties:
|
|
153
|
-
- `data` - binary data
|
|
154
|
-
- `fileName` (optional) - name of the file
|
|
155
|
-
- `mimeType` (optional) - The MIME type of the file ([Learn more](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types))
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
**Example:**
|
|
159
|
-
```ts
|
|
160
|
-
interface UserData {
|
|
161
|
-
id: string;
|
|
162
|
-
name: string;
|
|
163
|
-
avatar: SeamFile;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export async function updateUser(userId: string, userData: UserData): Promise<void> {
|
|
167
|
-
if (userData.avatar.mimeType != "image/png" && userData.avatar.mimeType != "image/jpeg")
|
|
168
|
-
throw new Error("Only PNGs and JPEGs allowed for avatar.");
|
|
169
|
-
|
|
170
|
-
users[userId].name = userData.name;
|
|
171
|
-
users[userId].avatar = userData.avatar.fileName;
|
|
172
|
-
writeFileSync(`../avatars/${userData.avatar.fileName}`, userData.avatar.data);
|
|
173
|
-
}
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
## Important notices
|
|
177
|
-
- The generated client files contain all imports from the api implementation file in the backend that import from the current relative folder (`./`). This is the simplest way I have to include imports (at least for now). It may import functions and unused symbols but that shouldn't be too worrying.
|
|
178
|
-
- Don't include backend/server functions inside the server api files.
|
|
179
|
-
- Only exported functions will be included in the client generated files.
|
|
180
|
-
|
|
181
|
-
## Supported types
|
|
182
|
-
SeamRPC supports the following types (at least for now):
|
|
183
|
-
- `string`
|
|
184
|
-
- `number`
|
|
185
|
-
- `boolean`
|
|
186
|
-
- `null`
|
|
187
|
-
- `undefined`
|
|
188
|
-
- arrays
|
|
189
|
-
- objects
|
|
190
|
-
|
|
191
|
-
Classes are technically supported, in that the data is serialized to JSON.
|
|
192
|
-
|
|
193
|
-
Other JavaScript types are not supported, although SeamRPC doesn't prevent you from using them, in which case they might lead to unexpected beahviour or even errors.
|
|
194
|
-
|
|
195
|
-
The Date object type is not supported (at least for now). However, you can use `number` and pass `Date.now()` or `string` and pass `new Date().toString()`. This is not different than a normal HTTP request using JSON. SeamRPC also uses JSON behind the scenes, that's why there's these limitations, which could be overcome but I've decided not to because it would probably add more overhead to the logic.
|
|
196
|
-
|
|
197
|
-
## Context parameter
|
|
198
|
-
|
|
199
|
-
### Server
|
|
200
|
-
If you want, you can get access to the request, response and next function from Express. Just add a parameter of type SeamContext at the end of the API function. You can name the parameter whatever you like. This parameter is optional. This parameter is not included in the client generated files.
|
|
201
|
-
|
|
202
|
-
Example:
|
|
203
|
-
```ts
|
|
204
|
-
export async function createUser(name: string, context: SeamContext): Promise<string> {
|
|
205
|
-
// Using the context to read the request path.
|
|
206
|
-
console.log("Request path:", context.request.originalUrl);
|
|
207
|
-
|
|
208
|
-
const user = {
|
|
209
|
-
id: Date.now().toString(),
|
|
210
|
-
name
|
|
211
|
-
};
|
|
212
|
-
users.push(user);
|
|
213
|
-
return user.id;
|
|
214
|
-
}
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
### Client
|
|
218
|
-
The client currently doesn't support access to the response object from the fetch.
|
|
219
|
-
|
|
220
|
-
## Error handling
|
|
221
|
-
|
|
222
|
-
### Server
|
|
223
|
-
To catch errors across router functions in the server, you can use the `apiError` and `internalError` events.
|
|
224
|
-
- `apiError` - Error ocurred when calling or during execution of your API function.
|
|
225
|
-
- `internalError` - SeamRPC internal error. Please report if you find an error that seems like a bug or requires improvement.
|
|
226
|
-
|
|
227
|
-
Example:
|
|
228
|
-
```ts
|
|
229
|
-
const seamSpace = await createSeamSpace(app);
|
|
230
|
-
|
|
231
|
-
seamSpace.on("apiError", (error, context) => {
|
|
232
|
-
console.error(`API Error at ${context.functionName}!`, error);
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
seamSpace.on("internalError", (error, context) => {
|
|
236
|
-
console.error(`Internal Error at ${context.functionName}!`, error);
|
|
237
|
-
});
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
> **Note:** The above example is to illustrate the use of error handlers and is not a complete example. Please consult the rest of the README or the examples in the examples directory.
|
|
241
|
-
|
|
242
|
-
## Middleware
|
|
243
|
-
|
|
244
|
-
### Client
|
|
245
|
-
You can add middleware functions in the client. There's two types:
|
|
246
|
-
- Pre-request - Before the request is sent. You might for example want to add some header to the request.
|
|
247
|
-
- Post-request - After the request was sent. You might for example want to read some header from reponse.
|
|
248
|
-
|
|
249
|
-
There's two ways to add middleware, either when creating the client:
|
|
250
|
-
```ts
|
|
251
|
-
createClient("http://localhost:3000", {
|
|
252
|
-
middleware: {
|
|
253
|
-
request: [
|
|
254
|
-
ctx => {
|
|
255
|
-
ctx.request.headers = {
|
|
256
|
-
...ctx.request.headers,
|
|
257
|
-
"X-MyHeader": "Test"
|
|
258
|
-
};
|
|
259
|
-
},
|
|
260
|
-
],
|
|
261
|
-
response: [
|
|
262
|
-
ctx => {
|
|
263
|
-
console.log(ctx.response.headers.get("X-SomeHeader"));
|
|
264
|
-
}
|
|
265
|
-
]
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
```
|
|
269
|
-
... or after creating the client:
|
|
270
|
-
```ts
|
|
271
|
-
const client = createClient("http://localhost:3000");
|
|
272
|
-
|
|
273
|
-
client.preRequest(ctx => {
|
|
274
|
-
ctx.request.headers = {
|
|
275
|
-
...ctx.request.headers,
|
|
276
|
-
"X-MyHeader": "Test"
|
|
277
|
-
};
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
client.postRequest(ctx => {
|
|
281
|
-
console.log(ctx.response.headers.get("X-SomeHeader"));
|
|
282
|
-
});
|
|
283
|
-
```
|
|
284
|
-
|
|
285
|
-
> You can add as many middleware functions as you like.
|
|
1
|
+
[Documentation](https://github.com/DanielC49/seam-rpc/blob/main/README.md)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
export type Result<Data, Error = undefined> = {
|
|
2
|
+
ok: true;
|
|
3
|
+
data: Data;
|
|
4
|
+
} | {
|
|
5
|
+
ok: false;
|
|
6
|
+
error: Error;
|
|
7
|
+
};
|
|
8
|
+
export type ResError = {
|
|
9
|
+
rpcError: false;
|
|
10
|
+
} | {
|
|
11
|
+
rpcError: true;
|
|
12
|
+
error: {
|
|
13
|
+
code: string;
|
|
14
|
+
data: unknown;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export declare class RpcError<Code extends string = any, Data = unknown> {
|
|
18
|
+
private _code;
|
|
19
|
+
private _data;
|
|
20
|
+
constructor(code: Code, data: Data);
|
|
21
|
+
get code(): Code;
|
|
22
|
+
get data(): Data;
|
|
23
|
+
toString(): string;
|
|
24
|
+
}
|
|
1
25
|
export declare function extractFiles(input: unknown): {
|
|
2
26
|
json: any;
|
|
3
27
|
files: File[];
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
export class RpcError {
|
|
2
|
+
constructor(code, data) {
|
|
3
|
+
this._code = code;
|
|
4
|
+
this._data = data;
|
|
5
|
+
}
|
|
6
|
+
get code() { return this._code; }
|
|
7
|
+
get data() { return this._data; }
|
|
8
|
+
toString() {
|
|
9
|
+
return this._code + ": " + JSON.stringify(this._data);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
1
12
|
export function extractFiles(input) {
|
|
2
13
|
const files = [];
|
|
3
14
|
const paths = [];
|