@msaki/jsonrpc 0.2.2 → 0.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/CHANGELOG.md +17 -0
- package/README.md +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +23 -12
- package/dist/client.js.map +1 -1
- package/example.ts +30 -0
- package/global.d.ts +3 -0
- package/package.json +5 -9
- package/src/client.ts +23 -12
- package/tsconfig.json +40 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @msaki/jsonrpc
|
|
2
|
+
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- dc1718c: refactor of types dir to global types
|
|
8
|
+
- 00ba692: - Converts error codes as enums
|
|
9
|
+
- Uses generic types on rpc request and response methods, and results
|
|
10
|
+
- 07dc4e5: improve docs example and updates to build config
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- e15733c: update scripts and make changeset in monorepo
|
|
15
|
+
- e3ac747: patch
|
|
16
|
+
- abddcdc: add source map to typescript build build
|
|
17
|
+
- ded3e6b: update
|
package/README.md
CHANGED
package/dist/client.d.ts
CHANGED
|
@@ -5,5 +5,5 @@ export declare class JsonRpcClient {
|
|
|
5
5
|
call<Method = string, Result = unknown, E = unknown>(method: Method, params?: unknown[]): Promise<Result | E>;
|
|
6
6
|
notify<Method = string>(method: Method, params?: unknown[]): Promise<JsonRpcResponse<unknown, number>>;
|
|
7
7
|
}
|
|
8
|
-
export declare function initializeRpcClient(url: string, jwtToken?: string): JsonRpcClient;
|
|
8
|
+
export declare function initializeRpcClient(url: string, jwtToken?: string, timeout?: number): JsonRpcClient;
|
|
9
9
|
//# sourceMappingURL=client.d.ts.map
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,qBAAa,aAAa;IAIxB,OAAO,CAAC,IAAI;IAHb,OAAO,CAAC,EAAE,CAAK;gBAGN,IAAI,EAAE,CACb,GAAG,EAAE,cAAc,CAAC,OAAO,CAAC,KACxB,OAAO,CAAC,eAAe,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAGzC,IAAI,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO,EACxD,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,OAAO,EAAE,GAChB,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;IActB,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE;CAQ1D;AAED,wBAAgB,mBAAmB,CAClC,GAAG,EAAE,MAAM,EACX,QAAQ,CAAC,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,qBAAa,aAAa;IAIxB,OAAO,CAAC,IAAI;IAHb,OAAO,CAAC,EAAE,CAAK;gBAGN,IAAI,EAAE,CACb,GAAG,EAAE,cAAc,CAAC,OAAO,CAAC,KACxB,OAAO,CAAC,eAAe,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAGzC,IAAI,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO,EACxD,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,OAAO,EAAE,GAChB,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;IActB,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE;CAQ1D;AAED,wBAAgB,mBAAmB,CAClC,GAAG,EAAE,MAAM,EACX,QAAQ,CAAC,EAAE,MAAM,EACjB,OAAO,GAAE,MAAa,GACpB,aAAa,CA2Bf"}
|
package/dist/client.js
CHANGED
|
@@ -26,20 +26,31 @@ export class JsonRpcClient {
|
|
|
26
26
|
return this.send(request);
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
-
export function initializeRpcClient(url, jwtToken) {
|
|
29
|
+
export function initializeRpcClient(url, jwtToken, timeout = 5000) {
|
|
30
30
|
const client = new JsonRpcClient(async (req) => {
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
const controller = new AbortController();
|
|
32
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
33
|
+
try {
|
|
34
|
+
const headers = {
|
|
35
|
+
"Content-Type": "application/json",
|
|
36
|
+
};
|
|
37
|
+
if (jwtToken) {
|
|
38
|
+
headers["Authorization"] = `Bearer ${jwtToken}`;
|
|
39
|
+
}
|
|
40
|
+
const res = await fetch(url, {
|
|
41
|
+
method: "POST",
|
|
42
|
+
headers,
|
|
43
|
+
body: JSON.stringify(req),
|
|
44
|
+
});
|
|
45
|
+
return (await res.json());
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
clearTimeout(timeoutId);
|
|
49
|
+
if (err instanceof Error && err.name === "AbortError") {
|
|
50
|
+
throw new Error(`Req timeout after ${timeout}ms`);
|
|
51
|
+
}
|
|
52
|
+
throw err;
|
|
36
53
|
}
|
|
37
|
-
const res = await fetch(url, {
|
|
38
|
-
method: "POST",
|
|
39
|
-
headers,
|
|
40
|
-
body: JSON.stringify(req),
|
|
41
|
-
});
|
|
42
|
-
return (await res.json());
|
|
43
54
|
});
|
|
44
55
|
return client;
|
|
45
56
|
}
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,aAAa;IAIhB;IAHD,EAAE,GAAG,CAAC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IACH,OAAO,MAAM,CAAC;AACf,CAAC"}
|
package/example.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { JsonRpcServer } from "./src";
|
|
2
|
+
|
|
3
|
+
const server = new JsonRpcServer();
|
|
4
|
+
|
|
5
|
+
server.register("add", ([a, b]: [number, number]) => a + b);
|
|
6
|
+
server.register("ping", () => "pong");
|
|
7
|
+
|
|
8
|
+
Bun.serve({
|
|
9
|
+
port: 4444,
|
|
10
|
+
async fetch(req) {
|
|
11
|
+
if (req.method !== "POST") {
|
|
12
|
+
return new Response("JSON-RPC only", { status: 405 });
|
|
13
|
+
}
|
|
14
|
+
const payload = await req.json();
|
|
15
|
+
const response = await server.handle(payload);
|
|
16
|
+
if (!response) {
|
|
17
|
+
return new Response(null, { status: 204 });
|
|
18
|
+
}
|
|
19
|
+
return Response.json(response);
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
console.log("JSON-RPC running on http://localhost:4444");
|
|
24
|
+
|
|
25
|
+
import { initializeRpcClient } from "./src";
|
|
26
|
+
|
|
27
|
+
const url = "http://localhost:4444";
|
|
28
|
+
const client = initializeRpcClient(url);
|
|
29
|
+
const result = await client.call("ping", []);
|
|
30
|
+
console.log(result);
|
package/global.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,30 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@msaki/jsonrpc",
|
|
3
|
-
"description": "A minimal jsonrpc
|
|
3
|
+
"description": "A minimal jsonrpc spec implementation.",
|
|
4
4
|
"author": "Meek Msaki",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.3.0",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"module": "dist/index.mjs",
|
|
9
9
|
"types": "dist/index.d.ts",
|
|
10
|
-
"files": [
|
|
11
|
-
"dist",
|
|
12
|
-
"src"
|
|
13
|
-
],
|
|
14
10
|
"keywords": [
|
|
15
11
|
"jsonrpc",
|
|
16
12
|
"2.0",
|
|
17
13
|
"rpc",
|
|
18
14
|
"bun"
|
|
19
15
|
],
|
|
20
|
-
"homepage": "https://github.com/mmsaki/libs/tree/main/packages/jsonrpc",
|
|
16
|
+
"homepage": "https://github.com/mmsaki/eth-libs/tree/main/packages/jsonrpc",
|
|
21
17
|
"repository": {
|
|
22
18
|
"type": "git",
|
|
23
|
-
"url": "git+https://github.com/mmsaki/libs.git",
|
|
19
|
+
"url": "git+https://github.com/mmsaki/eth-libs.git",
|
|
24
20
|
"directory": "packages/jsonrpc"
|
|
25
21
|
},
|
|
26
22
|
"bugs": {
|
|
27
|
-
"url": "https://github.com/mmsaki/libs/issues"
|
|
23
|
+
"url": "https://github.com/mmsaki/eth-libs/issues"
|
|
28
24
|
},
|
|
29
25
|
"publishConfig": {
|
|
30
26
|
"access": "public",
|
package/src/client.ts
CHANGED
|
@@ -37,21 +37,32 @@ export class JsonRpcClient {
|
|
|
37
37
|
export function initializeRpcClient(
|
|
38
38
|
url: string,
|
|
39
39
|
jwtToken?: string,
|
|
40
|
+
timeout: number = 5000,
|
|
40
41
|
): JsonRpcClient {
|
|
41
42
|
const client = new JsonRpcClient(async (req: JsonRpcRequest<unknown>) => {
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
43
|
+
const controller = new AbortController();
|
|
44
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
45
|
+
try {
|
|
46
|
+
const headers: Record<string, string> = {
|
|
47
|
+
"Content-Type": "application/json",
|
|
48
|
+
};
|
|
49
|
+
if (jwtToken) {
|
|
50
|
+
headers["Authorization"] = `Bearer ${jwtToken}`;
|
|
51
|
+
}
|
|
52
|
+
const res = await fetch(url, {
|
|
53
|
+
method: "POST",
|
|
54
|
+
headers,
|
|
55
|
+
body: JSON.stringify(req),
|
|
56
|
+
});
|
|
53
57
|
|
|
54
|
-
|
|
58
|
+
return (await res.json()) as JsonRpcResponse<unknown, number>;
|
|
59
|
+
} catch (err) {
|
|
60
|
+
clearTimeout(timeoutId);
|
|
61
|
+
if (err instanceof Error && err.name === "AbortError") {
|
|
62
|
+
throw new Error(`Req timeout after ${timeout}ms`);
|
|
63
|
+
}
|
|
64
|
+
throw err;
|
|
65
|
+
}
|
|
55
66
|
});
|
|
56
67
|
return client;
|
|
57
68
|
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// Environment setup & latest features
|
|
4
|
+
"lib": [
|
|
5
|
+
"ESNext"
|
|
6
|
+
],
|
|
7
|
+
"target": "ESNext",
|
|
8
|
+
"module": "Preserve",
|
|
9
|
+
"moduleDetection": "force",
|
|
10
|
+
"jsx": "react-jsx",
|
|
11
|
+
"allowJs": true,
|
|
12
|
+
// Bundler mode
|
|
13
|
+
"sourceMap": true,
|
|
14
|
+
"declaration": true,
|
|
15
|
+
"declarationMap": true,
|
|
16
|
+
"moduleResolution": "bundler",
|
|
17
|
+
"outDir": "dist",
|
|
18
|
+
"rootDir": "src",
|
|
19
|
+
"allowImportingTsExtensions": false,
|
|
20
|
+
"verbatimModuleSyntax": true,
|
|
21
|
+
"noEmit": false,
|
|
22
|
+
// Best practices
|
|
23
|
+
"strict": true,
|
|
24
|
+
"skipLibCheck": true,
|
|
25
|
+
"noFallthroughCasesInSwitch": true,
|
|
26
|
+
"noUncheckedIndexedAccess": true,
|
|
27
|
+
"noImplicitOverride": true,
|
|
28
|
+
// Some stricter flags (disabled by default)
|
|
29
|
+
"noUnusedLocals": false,
|
|
30
|
+
"noUnusedParameters": false,
|
|
31
|
+
"noPropertyAccessFromIndexSignature": false
|
|
32
|
+
},
|
|
33
|
+
"include": [
|
|
34
|
+
"src/**/*",
|
|
35
|
+
"global.d.ts"
|
|
36
|
+
],
|
|
37
|
+
"exclude": [
|
|
38
|
+
"example.ts"
|
|
39
|
+
]
|
|
40
|
+
}
|