@seam-rpc/client 1.0.2 → 1.0.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 +144 -0
- package/package.json +13 -2
package/README.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
<img width="1940" height="829" alt="image" src="https://github.com/user-attachments/assets/8a4a8a8b-1b57-4c1e-b6bb-ebab81ba8a32" />
|
|
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
|
+
### Client
|
|
65
|
+
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`.
|
|
66
|
+
|
|
67
|
+
- `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.
|
|
68
|
+
- `output-folder` - Specify the folder where to store the generated client api files.
|
|
69
|
+
|
|
70
|
+
**Example:**
|
|
71
|
+
`seam-rpc gen-client ./src/api/* ../server-app/src/api`
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
client-app
|
|
75
|
+
├─ index.ts
|
|
76
|
+
└─ api
|
|
77
|
+
├─ users.ts
|
|
78
|
+
└─ posts.ts
|
|
79
|
+
```
|
|
80
|
+
The api folder in the client contains the generated API client files, and should not be manually edited.
|
|
81
|
+
|
|
82
|
+
The generated `api/users.ts` file:
|
|
83
|
+
> Notice that the JSDoc comments are included in the client files.
|
|
84
|
+
```ts
|
|
85
|
+
import { callApi, SeamFile, ISeamFile } from "@seam-rpc/client";
|
|
86
|
+
export interface User {
|
|
87
|
+
id: string;
|
|
88
|
+
name: string;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Creates a new user and returns its ID.
|
|
92
|
+
* @param name The name of the user.
|
|
93
|
+
* @returns ID of the newly created user.
|
|
94
|
+
*/
|
|
95
|
+
export function createUser(name: string): Promise<string> { return callApi("users", "createUser", [name]); }
|
|
96
|
+
/**
|
|
97
|
+
* Gets a user by ID.
|
|
98
|
+
* @param id The ID of the user.
|
|
99
|
+
* @returns The user object.
|
|
100
|
+
*/
|
|
101
|
+
export function getUser(id: string): Promise<User | undefined> { return callApi("users", "getUser", [id]); }
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Config file
|
|
105
|
+
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:
|
|
106
|
+
```json
|
|
107
|
+
{
|
|
108
|
+
"inputFiles": "./src/api/*",
|
|
109
|
+
"outputFolder": "../client/src/api"
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
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).
|
|
113
|
+
|
|
114
|
+
## Uploading and downloading files
|
|
115
|
+
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.
|
|
116
|
+
|
|
117
|
+
A SeamFile has 3 properties:
|
|
118
|
+
- `data` - binary data
|
|
119
|
+
- `fileName` (optional) - name of the file
|
|
120
|
+
- `mimeType` (optional) - The MIME type of the file ([Learn more](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types))
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
**Example:**
|
|
124
|
+
```ts
|
|
125
|
+
interface UserData {
|
|
126
|
+
id: string;
|
|
127
|
+
name: string;
|
|
128
|
+
avatar: SeamFile;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export async function updateUser(userId: string, userData: UserData): Promise<void> {
|
|
132
|
+
if (userData.avatar.mimeType != "image/png" && userData.avatar.mimeType != "image/jpeg")
|
|
133
|
+
throw new Error("Only PNGs and JPEGs allowed for avatar.");
|
|
134
|
+
|
|
135
|
+
users[userId].name = userData.name;
|
|
136
|
+
users[userId].avatar = userData.avatar.fileName;
|
|
137
|
+
writeFileSync(`../avatars/${userData.avatar.fileName}`, userData.avatar.data);
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Important notices
|
|
142
|
+
- 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.
|
|
143
|
+
- Don't include backend/server functions inside the server api files.
|
|
144
|
+
- Only exported functions will be included in the client generated files.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seam-rpc/client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -12,9 +12,20 @@
|
|
|
12
12
|
"author": "Daniel Clímaco",
|
|
13
13
|
"license": "ISC",
|
|
14
14
|
"description": "",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/DanielC49/seam-rpc.git",
|
|
18
|
+
"directory": "packages/client"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/DanielC49/seam-rpc#readme",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/DanielC49/seam-rpc/issues"
|
|
23
|
+
},
|
|
15
24
|
"devDependencies": {
|
|
16
25
|
"@types/node": "^24.3.1",
|
|
17
|
-
"typescript": "^5.9.2"
|
|
26
|
+
"typescript": "^5.9.2"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
18
29
|
"@seam-rpc/core": "1.0.0"
|
|
19
30
|
}
|
|
20
31
|
}
|