@asyncswap/jsonrpc 0.4.4 → 0.4.6
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 +12 -0
- package/README.md +11 -3
- package/dist/client.d.ts +3 -3
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js.map +1 -1
- 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/dist/server.d.ts +3 -3
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +6 -9
- package/src/index.ts +1 -0
- package/src/server.ts +7 -7
- package/src/types/request.d.ts +2 -2
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -53,9 +53,16 @@ await client.notify('log', ['Hello world']);
|
|
|
53
53
|
|
|
54
54
|
#### Methods
|
|
55
55
|
|
|
56
|
-
- `register(method: string, handler: Handler<
|
|
56
|
+
- `register<Result>(method: string, handler: Handler<Result>)` - Register a method handler
|
|
57
57
|
- `handle(request: any)` - Process a JSON-RPC request
|
|
58
58
|
|
|
59
|
+
***Types***:
|
|
60
|
+
|
|
61
|
+
- `type Handler<Params, Result> = (params: Params) => Promise<Result>` - type of rpc function dispatched on the server.
|
|
62
|
+
- `type JsonRpcResponse<Result, ErrorCode> =
|
|
63
|
+
| JsonRpcSuccess<Result>
|
|
64
|
+
| JsonRpcError<ErrorCode>;` - JSON-RPC response type returns either a result or error.
|
|
65
|
+
|
|
59
66
|
#### Error Codes
|
|
60
67
|
|
|
61
68
|
```typescript
|
|
@@ -66,6 +73,7 @@ enum JsonRpcErrorCodes {
|
|
|
66
73
|
INTERNAL_ERROR = -32603,
|
|
67
74
|
PARSE_ERROR = -32700,
|
|
68
75
|
REQUEST_ABORTED = -32800,
|
|
76
|
+
REQUEST_FAILED = -32801,
|
|
69
77
|
}
|
|
70
78
|
```
|
|
71
79
|
|
|
@@ -77,8 +85,8 @@ enum JsonRpcErrorCodes {
|
|
|
77
85
|
|
|
78
86
|
#### Methods
|
|
79
87
|
|
|
80
|
-
- `call<
|
|
81
|
-
- `notify
|
|
88
|
+
- `call<Result, Error>(request, headers?)` - Make a JSON-RPC call
|
|
89
|
+
- `notify(method, params?)` - Send a JSON-RPC notification
|
|
82
90
|
- `buildRequest(method, params?)` - Build a JSON-RPC request object
|
|
83
91
|
|
|
84
92
|
## Examples
|
package/dist/client.d.ts
CHANGED
|
@@ -2,9 +2,9 @@ export declare class JsonRpcClient {
|
|
|
2
2
|
private url;
|
|
3
3
|
private id;
|
|
4
4
|
constructor(url: string);
|
|
5
|
-
call<
|
|
6
|
-
buildRequest
|
|
5
|
+
call<Result = unknown, E = unknown>(request: JsonRpcRequest | JsonRpcRequest[], headers?: Record<string, string>): Promise<Result | Result[] | E>;
|
|
6
|
+
buildRequest(method: string, params?: unknown[]): JsonRpcRequest;
|
|
7
7
|
private request;
|
|
8
|
-
notify
|
|
8
|
+
notify(method: string, params?: unknown[]): Promise<void>;
|
|
9
9
|
}
|
|
10
10
|
//# 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":"AACA,qBAAa,aAAa;IAGb,OAAO,CAAC,GAAG;IAFvB,OAAO,CAAC,EAAE,CAAK;gBAEK,GAAG,EAAE,MAAM;IAEzB,IAAI,CAAC,MAAM,GAAG,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AACA,qBAAa,aAAa;IAGb,OAAO,CAAC,GAAG;IAFvB,OAAO,CAAC,EAAE,CAAK;gBAEK,GAAG,EAAE,MAAM;IAEzB,IAAI,CAAC,MAAM,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO,EACvC,OAAO,EAAE,cAAc,GAAG,cAAc,EAAE,EAC1C,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAC9B,OAAO,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,CAAC;IAgBjC,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,GAAE,OAAO,EAAO,GAAG,cAAc;YAStD,OAAO;IAkCf,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE;CAY/C"}
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAC3C,MAAM,OAAO,aAAa;IAGL;IAFZ,EAAE,GAAG,CAAC,CAAC;IAEf,YAAoB,GAAW;QAAX,QAAG,GAAH,GAAG,CAAQ;IAAI,CAAC;IAEpC,KAAK,CAAC,IAAI,CACT,
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAC3C,MAAM,OAAO,aAAa;IAGL;IAFZ,EAAE,GAAG,CAAC,CAAC;IAEf,YAAoB,GAAW;QAAX,QAAG,GAAH,GAAG,CAAQ;IAAI,CAAC;IAEpC,KAAK,CAAC,IAAI,CACT,OAA0C,EAC1C,OAAgC;QAEhC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACtD,IAAI,OAAO,IAAI,QAAQ,EAAE,CAAC;YACzB,OAAO,QAAQ,CAAC,KAAU,CAAC;QAC5B,CAAC;QACD,IAAI,QAAQ,IAAI,QAAQ,EAAE,CAAC;YAC1B,OAAO,QAAQ,CAAC,MAAgB,CAAC;QAClC,CAAC;QACD,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,E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AO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;YAC/C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;SAC7B,CAAC,CAAC;IACJ,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,UAAU,CAAC"}
|
|
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"}
|
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,UAAU,CAAC"}
|
|
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"}
|
package/dist/server.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { JsonRpcErrorCode } from "./error";
|
|
2
|
-
export type Handler<Result> = (params:
|
|
2
|
+
export type Handler<Params, Result> = (params: Params) => Promise<Result>;
|
|
3
3
|
export declare class JsonRpcServer {
|
|
4
4
|
private methods;
|
|
5
|
-
register(method: string, handler: Handler<
|
|
6
|
-
handle<Result
|
|
5
|
+
register<Params, Result>(method: string, handler: Handler<Params, Result>): void;
|
|
6
|
+
handle<Result>(raw: unknown): Promise<JsonRpcResponse<Result, JsonRpcErrorCode | number> | JsonRpcResponse<Result, JsonRpcErrorCode | number>[] | null>;
|
|
7
7
|
private error;
|
|
8
8
|
}
|
|
9
9
|
//# sourceMappingURL=server.d.ts.map
|
package/dist/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAC3C,MAAM,MAAM,OAAO,CAAC,MAAM,IAAI,CAAC,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAC3C,MAAM,MAAM,OAAO,CAAC,MAAM,EAAE,MAAM,IAAI,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;AAE1E,qBAAa,aAAa;IACzB,OAAO,CAAC,OAAO,CAAwC;IAEvD,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC;IAInE,MAAM,CAAC,MAAM,EAClB,GAAG,EAAE,OAAO,GACV,OAAO,CACP,eAAe,CAAC,MAAM,EAAE,gBAAgB,GAAG,MAAM,CAAC,GAClD,eAAe,CAAC,MAAM,EAAE,gBAAgB,GAAG,MAAM,CAAC,EAAE,GACpD,IAAI,CACN;IAsED,OAAO,CAAC,KAAK;CAYb"}
|
package/dist/server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAG3C,MAAM,OAAO,aAAa;IACjB,OAAO,GAAG,IAAI,GAAG,
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,S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
@@ -4,8 +4,8 @@ export class JsonRpcClient {
|
|
|
4
4
|
|
|
5
5
|
constructor(private url: string) { }
|
|
6
6
|
|
|
7
|
-
async call<
|
|
8
|
-
request: JsonRpcRequest
|
|
7
|
+
async call<Result = unknown, E = unknown>(
|
|
8
|
+
request: JsonRpcRequest | JsonRpcRequest[],
|
|
9
9
|
headers?: Record<string, string>,
|
|
10
10
|
): Promise<Result | Result[] | E> {
|
|
11
11
|
const response = await this.request(request, headers);
|
|
@@ -23,10 +23,7 @@ export class JsonRpcClient {
|
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
buildRequest
|
|
27
|
-
method: Method,
|
|
28
|
-
params: unknown[] = [],
|
|
29
|
-
): JsonRpcRequest<Method> {
|
|
26
|
+
buildRequest(method: string, params: unknown[] = []): JsonRpcRequest {
|
|
30
27
|
return {
|
|
31
28
|
jsonrpc: "2.0" as const,
|
|
32
29
|
method,
|
|
@@ -36,7 +33,7 @@ export class JsonRpcClient {
|
|
|
36
33
|
}
|
|
37
34
|
|
|
38
35
|
private async request(
|
|
39
|
-
req: JsonRpcRequest
|
|
36
|
+
req: JsonRpcRequest | JsonRpcRequest[],
|
|
40
37
|
customHeaders?: Record<string, string>,
|
|
41
38
|
): Promise<
|
|
42
39
|
JsonRpcResponse<unknown, number> | JsonRpcResponse<unknown, number>[]
|
|
@@ -69,8 +66,8 @@ export class JsonRpcClient {
|
|
|
69
66
|
}
|
|
70
67
|
}
|
|
71
68
|
|
|
72
|
-
async notify
|
|
73
|
-
const request: JsonRpcRequest
|
|
69
|
+
async notify(method: string, params?: unknown[]) {
|
|
70
|
+
const request: JsonRpcRequest = {
|
|
74
71
|
jsonrpc: "2.0",
|
|
75
72
|
method,
|
|
76
73
|
params,
|
package/src/index.ts
CHANGED
package/src/server.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { JsonRpcErrorCode } from "./error";
|
|
2
|
-
export type Handler<Result> = (params:
|
|
2
|
+
export type Handler<Params, Result> = (params: Params) => Promise<Result>;
|
|
3
3
|
|
|
4
4
|
export class JsonRpcServer {
|
|
5
|
-
private methods = new Map<string, Handler<any>>();
|
|
5
|
+
private methods = new Map<string, Handler<any, any>>();
|
|
6
6
|
|
|
7
|
-
register(method: string, handler: Handler<
|
|
7
|
+
register<Params, Result>(method: string, handler: Handler<Params, Result>) {
|
|
8
8
|
this.methods.set(method, handler);
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
async handle<Result
|
|
11
|
+
async handle<Result>(
|
|
12
12
|
raw: unknown,
|
|
13
13
|
): Promise<
|
|
14
14
|
| JsonRpcResponse<Result, JsonRpcErrorCode | number>
|
|
@@ -33,7 +33,7 @@ export class JsonRpcServer {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
// handle single response
|
|
36
|
-
const req = raw as Partial<JsonRpcRequest
|
|
36
|
+
const req = raw as Partial<JsonRpcRequest>;
|
|
37
37
|
|
|
38
38
|
if (
|
|
39
39
|
typeof req !== "object" ||
|
|
@@ -84,8 +84,8 @@ export class JsonRpcServer {
|
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
private error<Result
|
|
88
|
-
id: JsonRpcRequest
|
|
87
|
+
private error<Result>(
|
|
88
|
+
id: JsonRpcRequest["id"],
|
|
89
89
|
code: JsonRpcErrorCode | number,
|
|
90
90
|
message: string,
|
|
91
91
|
data?: unknown,
|