@asyncswap/jsonrpc 0.4.8 → 0.4.10

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @asyncswap/jsonrpc
2
2
 
3
+ ## 0.4.10
4
+
5
+ ### Patch Changes
6
+
7
+ - 69a2d7e: patch types updates
8
+
9
+ ## 0.4.9
10
+
11
+ ### Patch Changes
12
+
13
+ - dc792b1: (refactor): change to `withHeaders` option and update BaseClient source
14
+
3
15
  ## 0.4.8
4
16
 
5
17
  ### Patch Changes
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('add', ([a, b]: [number, number]) => a + b);
22
- server.register('ping', () => 'pong');
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: 'add',
27
+ method: 'eth_add',
28
28
  params: [2, 3],
29
29
  id: 1
30
30
  });
package/dist/base.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { JsonRpcClient } from "./client";
2
+ export declare abstract class BaseClient<MethodsSpec extends RpcSpecBase> {
3
+ rpc: JsonRpcClient<MethodsSpec>;
4
+ protected headers: Record<string, string>;
5
+ constructor(url: string);
6
+ withHeaders(headers: Record<string, string>): this;
7
+ }
8
+ //# 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,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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,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
@@ -1,3 +1,4 @@
1
+ export * from "./base";
1
2
  export * from "./client";
2
3
  export * from "./error";
3
4
  export * from "./server";
@@ -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
@@ -1,3 +1,4 @@
1
+ export * from "./base";
1
2
  export * from "./client";
2
3
  export * from "./error";
3
4
  export * from "./server";
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
@@ -2,7 +2,7 @@
2
2
  "name": "@asyncswap/jsonrpc",
3
3
  "description": "A minimal jsonrpc spec implementation.",
4
4
  "author": "Meek Msaki",
5
- "version": "0.4.8",
5
+ "version": "0.4.10",
6
6
  "license": "MIT",
7
7
  "main": "dist/index.js",
8
8
  "module": "dist/index.mjs",
package/src/base.ts ADDED
@@ -0,0 +1,43 @@
1
+ import { JsonRpcClient } from "./client";
2
+
3
+ export abstract class BaseClient<MethodsSpec extends RpcSpecBase> {
4
+ rpc: JsonRpcClient<MethodsSpec>;
5
+ protected headers: Record<string, string> = {};
6
+
7
+ constructor(url: string) {
8
+ this.rpc = new JsonRpcClient(url);
9
+
10
+ return new Proxy(this, {
11
+ get: (target: this, prop: string | symbol, receiver) => {
12
+ // let real properties / methods through
13
+ if (prop in target) {
14
+ const value = Reflect.get(target, prop, receiver);
15
+ if (typeof value === "function") {
16
+ return (...args: any[]) => {
17
+ const result = value.apply(target, args);
18
+ // if method returns target, return proxy instead
19
+ return result === target ? receiver : result;
20
+ };
21
+ }
22
+ return value;
23
+ }
24
+ // dynamic rpc
25
+ if (typeof prop !== "string") return undefined;
26
+
27
+ const method = prop as keyof MethodsSpec;
28
+
29
+ return (
30
+ ...params: MethodsSpec[typeof method]["params"]
31
+ ): MethodsSpec[typeof method]["result"] =>
32
+ this.rpc.call(this.rpc.buildRequest(method, params), this.headers);
33
+ },
34
+ });
35
+ }
36
+
37
+ withHeaders(headers: Record<string, string>) {
38
+ this.headers = {
39
+ ...headers,
40
+ };
41
+ return this;
42
+ }
43
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "./base";
1
2
  export * from "./client";
2
3
  export * from "./error";
3
4
  export * from "./server";
@@ -1,9 +1,8 @@
1
1
  declare global {
2
- export type RpcMethodSpec = {
3
- params: readonly unknown[];
4
- result: unknown;
5
- };
6
- export type RpcSpecBase = Record<Method, RpcMethodSpec>;
2
+ export type RpcSpecBase = Record<
3
+ string,
4
+ { params: readonly unknown[]; result: unknown }
5
+ >;
7
6
 
8
7
  export interface JsonRpcRequest<Method, Params extends readonly unknown[]> {
9
8
  jsonrpc: "2.0";