@pyrpc/client 0.1.0-alpha.1 → 0.1.0-alpha.2
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 +71 -39
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -1,39 +1,71 @@
|
|
|
1
|
-
# @pyrpc/client
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install @pyrpc/client
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
1
|
+
# @pyrpc/client
|
|
2
|
+
|
|
3
|
+
Universal TypeScript client for [pyRPC](https://pyrpc.dev). Type-safe RPC calls to your Python backend — install, import, call.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @pyrpc/client
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The postinstall script in `@pyrpc/types` will prompt for your server URL and generate typed contracts automatically.
|
|
12
|
+
|
|
13
|
+
For CI, set the `PYRPC_URL` environment variable:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
PYRPC_URL=https://api.example.com npm install @pyrpc/client
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { createClient } from "@pyrpc/client";
|
|
23
|
+
import type { Types } from "@pyrpc/types";
|
|
24
|
+
|
|
25
|
+
const client = createClient<Types>({
|
|
26
|
+
baseUrl: "https://api.example.com",
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const user = await client.get_user(1);
|
|
30
|
+
console.log(user.name);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The proxy-based API lets you call any remote procedure as a local method. Parameters are passed positionally or as a single object for named arguments.
|
|
34
|
+
|
|
35
|
+
### Dynamic headers (auth)
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
const client = createClient({
|
|
39
|
+
baseUrl: "https://api.example.com",
|
|
40
|
+
headers: () => ({
|
|
41
|
+
Authorization: `Bearer ${localStorage.getItem("token")}`,
|
|
42
|
+
}),
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Error handling
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { createClient, PyRPCError } from "@pyrpc/client";
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
await client.delete_user(1);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (error instanceof PyRPCError) {
|
|
55
|
+
console.error(error.code, error.message);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## API
|
|
61
|
+
|
|
62
|
+
### `createClient<TTypes>(options?)`
|
|
63
|
+
|
|
64
|
+
Creates a proxy client that forwards method calls to the server.
|
|
65
|
+
|
|
66
|
+
- `baseUrl` — Server root URL (defaults to `window.location.origin` in browsers)
|
|
67
|
+
- `headers` — Static, dynamic, or async `HeadersInit`
|
|
68
|
+
|
|
69
|
+
## License
|
|
70
|
+
|
|
71
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyrpc/client",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.2",
|
|
4
4
|
"description": "Universal TypeScript client for pyRPC",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -23,6 +23,9 @@
|
|
|
23
23
|
],
|
|
24
24
|
"author": "",
|
|
25
25
|
"license": "MIT",
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@pyrpc/types": "^0.1.0-alpha.1"
|
|
28
|
+
},
|
|
26
29
|
"devDependencies": {
|
|
27
30
|
"@types/node": "^20.19.39",
|
|
28
31
|
"tsup": "^8.5.1",
|