@asyncswap/jsonrpc 0.3.1 → 0.4.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/dist/client.d.ts +10 -4
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +22 -23
- package/dist/client.js.map +1 -1
- package/example.ts +2 -2
- package/package.json +1 -1
- package/src/client.ts +28 -28
package/dist/client.d.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
export declare class JsonRpcClient {
|
|
2
|
-
private
|
|
2
|
+
private url;
|
|
3
3
|
private id;
|
|
4
|
-
constructor(
|
|
5
|
-
call<Method = string, Result = unknown, E = unknown>(method: Method, params?: unknown[]): Promise<Result | E>;
|
|
4
|
+
constructor(url: string);
|
|
5
|
+
call<Method = string, Result = unknown, E = unknown>(method: Method, params?: unknown[], headers?: Record<string, string>): Promise<Result | E>;
|
|
6
|
+
buildRequest<Method>(method: Method, params?: unknown[]): {
|
|
7
|
+
jsonrpc: "2.0";
|
|
8
|
+
method: Method;
|
|
9
|
+
params: unknown[];
|
|
10
|
+
id: number;
|
|
11
|
+
};
|
|
12
|
+
private request;
|
|
6
13
|
notify<Method = string>(method: Method, params?: unknown[]): Promise<JsonRpcResponse<unknown, number>>;
|
|
7
14
|
}
|
|
8
|
-
export declare function initializeRpcClient(url: string, jwtToken?: string, timeout?: number): JsonRpcClient;
|
|
9
15
|
//# 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;
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,qBAAa,aAAa;IAGb,OAAO,CAAC,GAAG;IAFvB,OAAO,CAAC,EAAE,CAAK;gBAEK,GAAG,EAAE,MAAM;IAEzB,IAAI,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO,EACxD,MAAM,EAAE,MAAM,EACd,MAAM,GAAE,OAAO,EAAO,EACtB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAC9B,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;IAatB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAE,OAAO,EAAO;;;;;;YAS7C,OAAO;IA4BrB,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE;CAQ1D"}
|
package/dist/client.js
CHANGED
|
@@ -1,43 +1,35 @@
|
|
|
1
1
|
export class JsonRpcClient {
|
|
2
|
-
|
|
2
|
+
url;
|
|
3
3
|
id = 0;
|
|
4
|
-
constructor(
|
|
5
|
-
this.
|
|
4
|
+
constructor(url) {
|
|
5
|
+
this.url = url;
|
|
6
6
|
}
|
|
7
|
-
async call(method, params) {
|
|
8
|
-
const request =
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
params,
|
|
12
|
-
id: ++this.id,
|
|
13
|
-
};
|
|
14
|
-
const response = await this.send(request);
|
|
7
|
+
async call(method, params = [], headers) {
|
|
8
|
+
const request = this.buildRequest(method, params);
|
|
9
|
+
const response = await this.request(request, headers);
|
|
10
|
+
++this.id;
|
|
15
11
|
if ("error" in response) {
|
|
16
12
|
return response.error;
|
|
17
13
|
}
|
|
18
14
|
return response.result;
|
|
19
15
|
}
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
buildRequest(method, params = []) {
|
|
17
|
+
return {
|
|
22
18
|
jsonrpc: "2.0",
|
|
23
19
|
method,
|
|
24
20
|
params,
|
|
21
|
+
id: this.id,
|
|
25
22
|
};
|
|
26
|
-
return this.send(request);
|
|
27
23
|
}
|
|
28
|
-
|
|
29
|
-
export function initializeRpcClient(url, jwtToken, timeout = 5000) {
|
|
30
|
-
const client = new JsonRpcClient(async (req) => {
|
|
24
|
+
async request(req, customHeaders, timeout = 5000) {
|
|
31
25
|
const controller = new AbortController();
|
|
32
26
|
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
33
27
|
try {
|
|
34
28
|
const headers = {
|
|
35
29
|
"Content-Type": "application/json",
|
|
30
|
+
...(customHeaders ?? {}),
|
|
36
31
|
};
|
|
37
|
-
|
|
38
|
-
headers["Authorization"] = `Bearer ${jwtToken}`;
|
|
39
|
-
}
|
|
40
|
-
const res = await fetch(url, {
|
|
32
|
+
const res = await fetch(this.url, {
|
|
41
33
|
method: "POST",
|
|
42
34
|
headers,
|
|
43
35
|
body: JSON.stringify(req),
|
|
@@ -51,7 +43,14 @@ export function initializeRpcClient(url, jwtToken, timeout = 5000) {
|
|
|
51
43
|
}
|
|
52
44
|
throw err;
|
|
53
45
|
}
|
|
54
|
-
}
|
|
55
|
-
|
|
46
|
+
}
|
|
47
|
+
notify(method, params) {
|
|
48
|
+
const request = {
|
|
49
|
+
jsonrpc: "2.0",
|
|
50
|
+
method,
|
|
51
|
+
params,
|
|
52
|
+
};
|
|
53
|
+
return this.request(request);
|
|
54
|
+
}
|
|
56
55
|
}
|
|
57
56
|
//# sourceMappingURL=client.js.map
|
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;
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,aAAa;IAGL;IAFZ,EAAE,GAAG,CAAC,CAAC;IAEf,YAAoB,GAAW;QAAX,QAAG,GAAH,GAAG,CAAQ;IAAI,CAAC;IAEpC,KAAK,CAAC,IAAI,CACT,MAAc,EACd,SAAoB,EAAE,EACtB,OAAgC;QAEhC,MAAM,OAAO,GAA2B,IAAI,CAAC,YAAY,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B,OAAO,IAAI,CAAC,CAAC;YACnD,CAAC;YACD,MAAM,GAAG,CAAC;QACX,CAAC;IACF,CAAC;IAED,MAAM,CAAkB,MAAc,EAAE,MAAkB;QACzD,MAAM,OAAO,GAA2B;YACvC,OAAO,EAAE,KAAK;YACd,MAAM;YACN,MAAM;SACN,CAAC;QACF,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC;CACD"}
|
package/example.ts
CHANGED
|
@@ -22,9 +22,9 @@ Bun.serve({
|
|
|
22
22
|
|
|
23
23
|
console.log("JSON-RPC running on http://localhost:4444");
|
|
24
24
|
|
|
25
|
-
import {
|
|
25
|
+
import { JsonRpcClient } from "./src";
|
|
26
26
|
|
|
27
27
|
const url = "http://localhost:4444";
|
|
28
|
-
const client =
|
|
28
|
+
const client = new JsonRpcClient(url);
|
|
29
29
|
const result = await client.call("ping", []);
|
|
30
30
|
console.log(result);
|
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -1,55 +1,47 @@
|
|
|
1
1
|
export class JsonRpcClient {
|
|
2
2
|
private id = 0;
|
|
3
3
|
|
|
4
|
-
constructor(
|
|
5
|
-
private send: (
|
|
6
|
-
req: JsonRpcRequest<unknown>,
|
|
7
|
-
) => Promise<JsonRpcResponse<unknown, number>>,
|
|
8
|
-
) { }
|
|
4
|
+
constructor(private url: string) { }
|
|
9
5
|
|
|
10
6
|
async call<Method = string, Result = unknown, E = unknown>(
|
|
11
7
|
method: Method,
|
|
12
|
-
params
|
|
8
|
+
params: unknown[] = [],
|
|
9
|
+
headers?: Record<string, string>,
|
|
13
10
|
): Promise<Result | E> {
|
|
14
|
-
const request: JsonRpcRequest<Method> =
|
|
15
|
-
jsonrpc: "2.0",
|
|
11
|
+
const request: JsonRpcRequest<Method> = this.buildRequest<Method>(
|
|
16
12
|
method,
|
|
17
13
|
params,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
);
|
|
15
|
+
const response = await this.request(request, headers);
|
|
16
|
+
++this.id;
|
|
21
17
|
if ("error" in response) {
|
|
22
18
|
return response.error as E;
|
|
23
19
|
}
|
|
24
20
|
return response.result as Result;
|
|
25
21
|
}
|
|
26
22
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
jsonrpc: "2.0",
|
|
23
|
+
buildRequest<Method>(method: Method, params: unknown[] = []) {
|
|
24
|
+
return {
|
|
25
|
+
jsonrpc: "2.0" as const,
|
|
30
26
|
method,
|
|
31
27
|
params,
|
|
28
|
+
id: this.id,
|
|
32
29
|
};
|
|
33
|
-
return this.send(request);
|
|
34
30
|
}
|
|
35
|
-
}
|
|
36
31
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
)
|
|
42
|
-
const client = new JsonRpcClient(async (req: JsonRpcRequest<unknown>) => {
|
|
32
|
+
private async request(
|
|
33
|
+
req: JsonRpcRequest<unknown>,
|
|
34
|
+
customHeaders?: Record<string, string>,
|
|
35
|
+
timeout: number = 5000,
|
|
36
|
+
) {
|
|
43
37
|
const controller = new AbortController();
|
|
44
38
|
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
45
39
|
try {
|
|
46
40
|
const headers: Record<string, string> = {
|
|
47
41
|
"Content-Type": "application/json",
|
|
42
|
+
...(customHeaders ?? {}),
|
|
48
43
|
};
|
|
49
|
-
|
|
50
|
-
headers["Authorization"] = `Bearer ${jwtToken}`;
|
|
51
|
-
}
|
|
52
|
-
const res = await fetch(url, {
|
|
44
|
+
const res = await fetch(this.url, {
|
|
53
45
|
method: "POST",
|
|
54
46
|
headers,
|
|
55
47
|
body: JSON.stringify(req),
|
|
@@ -63,6 +55,14 @@ export function initializeRpcClient(
|
|
|
63
55
|
}
|
|
64
56
|
throw err;
|
|
65
57
|
}
|
|
66
|
-
}
|
|
67
|
-
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
notify<Method = string>(method: Method, params?: unknown[]) {
|
|
61
|
+
const request: JsonRpcRequest<Method> = {
|
|
62
|
+
jsonrpc: "2.0",
|
|
63
|
+
method,
|
|
64
|
+
params,
|
|
65
|
+
};
|
|
66
|
+
return this.request(request);
|
|
67
|
+
}
|
|
68
68
|
}
|