@asyncswap/jsonrpc 0.4.7 → 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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @asyncswap/jsonrpc
2
2
 
3
+ ## 0.4.9
4
+
5
+ ### Patch Changes
6
+
7
+ - dc792b1: (refactor): change to `withHeaders` option and update BaseClient source
8
+
9
+ ## 0.4.8
10
+
11
+ ### Patch Changes
12
+
13
+ - e9fc1df: remove biome dep
14
+
3
15
  ## 0.4.7
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
  });
@@ -53,7 +53,7 @@ await client.notify('log', ['Hello world']);
53
53
 
54
54
  #### Methods
55
55
 
56
- - `register<Result>(method: string, handler: Handler<Result>)` - Register a method handler
56
+ - `register<Method, Params, Result>(method: Method, handler: Handler<Params, Result>)` - Register a method handler
57
57
  - `handle(request: any)` - Process a JSON-RPC request
58
58
 
59
59
  ***Types***:
@@ -85,8 +85,8 @@ enum JsonRpcErrorCodes {
85
85
 
86
86
  #### Methods
87
87
 
88
- - `call<Result, Error>(request, headers?)` - Make a JSON-RPC call
89
- - `notify(method, params?)` - Send a JSON-RPC notification
88
+ - `call(request, headers?)` - Make a JSON-RPC call
89
+ - `notify(method: Method, params?)` - Send a JSON-RPC notification
90
90
  - `buildRequest(method, params?)` - Build a JSON-RPC request object
91
91
 
92
92
  ## Examples
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
@@ -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
@@ -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
@@ -2,8 +2,8 @@ import { JsonRpcServer } from "./src";
2
2
 
3
3
  const server = new JsonRpcServer();
4
4
 
5
- server.register("add", async ([a, b]: [number, number]) => a + b);
6
- server.register("ping", async () => "pong");
5
+ server.register("eth_add", async ([a, b]: [number, number]) => a + b);
6
+ server.register("eth_ping", async () => "pong");
7
7
 
8
8
  Bun.serve({
9
9
  port: 4444,
@@ -26,5 +26,14 @@ import { JsonRpcClient } from "./src";
26
26
 
27
27
  const url = "http://localhost:4444";
28
28
  const client = new JsonRpcClient(url);
29
- const result = await client.call(client.buildRequest("ping", []));
30
- console.log(result);
29
+ const result = await client.call(client.buildRequest("eth_ping", []));
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.7",
5
+ "version": "0.4.9",
6
6
  "license": "MIT",
7
7
  "main": "dist/index.js",
8
8
  "module": "dist/index.mjs",
@@ -32,7 +32,6 @@
32
32
  "format": "bun biome format --write"
33
33
  },
34
34
  "devDependencies": {
35
- "@biomejs/biome": "2.3.11",
36
35
  "@types/bun": "latest"
37
36
  },
38
37
  "peerDependencies": {
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
@@ -1,3 +1,4 @@
1
+ export * from "./base";
1
2
  export * from "./client";
2
3
  export * from "./error";
3
4
  export * from "./server";
package/tsconfig.json CHANGED
@@ -1,9 +1,7 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  // Environment setup & latest features
4
- "lib": [
5
- "ESNext"
6
- ],
4
+ "lib": ["ESNext"],
7
5
  "target": "ESNext",
8
6
  "module": "Preserve",
9
7
  "moduleDetection": "force",
@@ -30,11 +28,6 @@
30
28
  "noUnusedParameters": false,
31
29
  "noPropertyAccessFromIndexSignature": false
32
30
  },
33
- "include": [
34
- "src/**/*",
35
- "global.d.ts"
36
- ],
37
- "exclude": [
38
- "example.ts"
39
- ]
31
+ "include": ["src/**/*", "global.d.ts"],
32
+ "exclude": ["example.ts"]
40
33
  }