@asyncswap/jsonrpc 0.4.8 → 0.4.9
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 +6 -0
- package/README.md +3 -3
- package/dist/base.d.ts +9 -0
- package/dist/base.d.ts.map +1 -0
- package/dist/base.js +36 -0
- package/dist/base.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/example.ts +10 -1
- package/package.json +1 -1
- package/src/base.ts +45 -0
- package/src/index.ts +1 -0
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -18,13 +18,13 @@ import { JsonRpcServer } from '@asyncswap/jsonrpc';
|
|
|
18
18
|
const server = new JsonRpcServer();
|
|
19
19
|
|
|
20
20
|
// Register methods
|
|
21
|
-
server.register(
|
|
22
|
-
server.register(
|
|
21
|
+
server.register("eth_add", async ([a,b]: [number, number]) => a + b)
|
|
22
|
+
server.register("eth_ping", async () => "pong")
|
|
23
23
|
|
|
24
24
|
// Handle requests
|
|
25
25
|
const response = await server.handle({
|
|
26
26
|
jsonrpc: '2.0',
|
|
27
|
-
method: '
|
|
27
|
+
method: 'eth_add',
|
|
28
28
|
params: [2, 3],
|
|
29
29
|
id: 1
|
|
30
30
|
});
|
package/dist/base.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { JsonRpcClient } from "./client";
|
|
2
|
+
export type RpcSpecBase = Record<string, RpcMethodSpec>;
|
|
3
|
+
export declare abstract class BaseClient<MethodsSpec extends RpcSpecBase> {
|
|
4
|
+
rpc: JsonRpcClient<MethodsSpec>;
|
|
5
|
+
protected headers: Record<string, string>;
|
|
6
|
+
constructor(url: string);
|
|
7
|
+
withHeaders(headers: Record<string, string>): this;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;AAExD,8BAAsB,UAAU,CAAC,WAAW,SAAS,WAAW;IAC/D,GAAG,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC;IAChC,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAM;gBAEnC,GAAG,EAAE,MAAM;IA8BvB,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;CAM3C"}
|
package/dist/base.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { JsonRpcClient } from "./client";
|
|
2
|
+
export class BaseClient {
|
|
3
|
+
rpc;
|
|
4
|
+
headers = {};
|
|
5
|
+
constructor(url) {
|
|
6
|
+
this.rpc = new JsonRpcClient(url);
|
|
7
|
+
return new Proxy(this, {
|
|
8
|
+
get: (target, prop, receiver) => {
|
|
9
|
+
// let real properties / methods through
|
|
10
|
+
if (prop in target) {
|
|
11
|
+
const value = Reflect.get(target, prop, receiver);
|
|
12
|
+
if (typeof value === "function") {
|
|
13
|
+
return (...args) => {
|
|
14
|
+
const result = value.apply(target, args);
|
|
15
|
+
// if method returns target, return proxy instead
|
|
16
|
+
return result === target ? receiver : result;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
// dynamic rpc
|
|
22
|
+
if (typeof prop !== "string")
|
|
23
|
+
return undefined;
|
|
24
|
+
const method = prop;
|
|
25
|
+
return (...params) => this.rpc.call(this.rpc.buildRequest(method, params), this.headers);
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
withHeaders(headers) {
|
|
30
|
+
this.headers = {
|
|
31
|
+
...headers,
|
|
32
|
+
};
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=base.js.map
|
package/dist/base.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAIzC,MAAM,OAAgB,UAAU;IAC/B,GAAG,CAA6B;IACtB,OAAO,GAA2B,EAAE,CAAC;IAE/C,YAAY,GAAW;QACtB,IAAI,CAAC,GAAG,GAAG,IAAI,aAAa,CAAC,GAAG,CAAC,CAAC;QAElC,OAAO,IAAI,KAAK,CAAC,IAAI,EAAE;YACtB,GAAG,EAAE,CAAC,MAAY,EAAE,IAAqB,EAAE,QAAQ,EAAE,EAAE;gBACtD,wCAAwC;gBACxC,IAAI,IAAI,IAAI,MAAM,EAAE,CAAC;oBACpB,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;oBAClD,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE,CAAC;wBACjC,OAAO,CAAC,GAAG,IAAW,EAAE,EAAE;4BACzB,MAAM,MAAM,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;4BACzC,iDAAiD;4BACjD,OAAO,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC;wBAC9C,CAAC,CAAC;oBACH,CAAC;oBACD,OAAO,KAAK,CAAC;gBACd,CAAC;gBACD,cAAc;gBACd,IAAI,OAAO,IAAI,KAAK,QAAQ;oBAAE,OAAO,SAAS,CAAC;gBAE/C,MAAM,MAAM,GAAG,IAAyB,CAAC;gBAEzC,OAAO,CACN,GAAG,MAA4C,EACR,EAAE,CACzC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;YACrE,CAAC;SACD,CAAC,CAAC;IACJ,CAAC;IAED,WAAW,CAAC,OAA+B;QAC1C,IAAI,CAAC,OAAO,GAAG;YACd,GAAG,OAAO;SACV,CAAC;QACF,OAAO,IAAI,CAAC;IACb,CAAC;CACD"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|
package/example.ts
CHANGED
|
@@ -27,4 +27,13 @@ import { JsonRpcClient } from "./src";
|
|
|
27
27
|
const url = "http://localhost:4444";
|
|
28
28
|
const client = new JsonRpcClient(url);
|
|
29
29
|
const result = await client.call(client.buildRequest("eth_ping", []));
|
|
30
|
-
console.log(result);
|
|
30
|
+
console.log(result); // pong
|
|
31
|
+
|
|
32
|
+
const response = await server.handle({
|
|
33
|
+
jsonrpc: "2.0",
|
|
34
|
+
method: "eth_add",
|
|
35
|
+
params: [2, 3],
|
|
36
|
+
id: 1,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
console.log(response); // { jsonrpc: '2.0', result: 5, id: 1 }
|
package/package.json
CHANGED
package/src/base.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { JsonRpcClient } from "./client";
|
|
2
|
+
|
|
3
|
+
export type RpcSpecBase = Record<string, RpcMethodSpec>;
|
|
4
|
+
|
|
5
|
+
export abstract class BaseClient<MethodsSpec extends RpcSpecBase> {
|
|
6
|
+
rpc: JsonRpcClient<MethodsSpec>;
|
|
7
|
+
protected headers: Record<string, string> = {};
|
|
8
|
+
|
|
9
|
+
constructor(url: string) {
|
|
10
|
+
this.rpc = new JsonRpcClient(url);
|
|
11
|
+
|
|
12
|
+
return new Proxy(this, {
|
|
13
|
+
get: (target: this, prop: string | symbol, receiver) => {
|
|
14
|
+
// let real properties / methods through
|
|
15
|
+
if (prop in target) {
|
|
16
|
+
const value = Reflect.get(target, prop, receiver);
|
|
17
|
+
if (typeof value === "function") {
|
|
18
|
+
return (...args: any[]) => {
|
|
19
|
+
const result = value.apply(target, args);
|
|
20
|
+
// if method returns target, return proxy instead
|
|
21
|
+
return result === target ? receiver : result;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
// dynamic rpc
|
|
27
|
+
if (typeof prop !== "string") return undefined;
|
|
28
|
+
|
|
29
|
+
const method = prop as keyof MethodsSpec;
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
...params: MethodsSpec[typeof method]["params"]
|
|
33
|
+
): MethodsSpec[typeof method]["result"] =>
|
|
34
|
+
this.rpc.call(this.rpc.buildRequest(method, params), this.headers);
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
withHeaders(headers: Record<string, string>) {
|
|
40
|
+
this.headers = {
|
|
41
|
+
...headers,
|
|
42
|
+
};
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/index.ts
CHANGED