@asyncswap/jsonrpc 0.4.2 → 0.4.4
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 +15 -3
- package/dist/client.d.ts +3 -8
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +29 -14
- package/dist/client.js.map +1 -1
- package/dist/error.d.ts +10 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +11 -0
- package/dist/error.js.map +1 -0
- package/dist/server.d.ts +2 -9
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +5 -13
- package/dist/server.js.map +1 -1
- package/example.ts +1 -1
- package/package.json +1 -1
- package/src/client.ts +39 -23
- package/src/error.ts +9 -0
- package/src/server.ts +10 -17
package/CHANGELOG.md
CHANGED
|
@@ -1,21 +1,33 @@
|
|
|
1
1
|
# @asyncswap/jsonrpc
|
|
2
2
|
|
|
3
|
+
## 0.4.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- f7d28d9: (fix): error code now generated in build
|
|
8
|
+
|
|
9
|
+
## 0.4.3
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 40a79dc: add support for batcing and streamlined error methods
|
|
14
|
+
|
|
3
15
|
## 0.4.2
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
6
18
|
|
|
7
19
|
- 9512db5: update documentation
|
|
8
20
|
|
|
9
|
-
## 0.
|
|
21
|
+
## 0.4.0
|
|
10
22
|
|
|
11
23
|
### Minor Changes
|
|
12
24
|
|
|
13
|
-
- 7e8300c: changes api for jsonrpc
|
|
25
|
+
- 7e8300c: changes api for jsonrpc construction
|
|
14
26
|
|
|
15
27
|
### Patch Changes
|
|
16
28
|
|
|
17
29
|
- 43de065: update docs
|
|
18
|
-
- 7126afa: add `otherHeaders` to
|
|
30
|
+
- 7126afa: add `otherHeaders` to initializedClient calls
|
|
19
31
|
|
|
20
32
|
## 0.3.1
|
|
21
33
|
|
package/dist/client.d.ts
CHANGED
|
@@ -2,14 +2,9 @@ export declare class JsonRpcClient {
|
|
|
2
2
|
private url;
|
|
3
3
|
private id;
|
|
4
4
|
constructor(url: string);
|
|
5
|
-
call<Method = string, Result = unknown, E = unknown>(
|
|
6
|
-
buildRequest<Method>(method: Method, params?: unknown[]):
|
|
7
|
-
jsonrpc: "2.0";
|
|
8
|
-
method: Method;
|
|
9
|
-
params: unknown[];
|
|
10
|
-
id: number;
|
|
11
|
-
};
|
|
5
|
+
call<Method = string, Result = unknown, E = unknown>(request: JsonRpcRequest<Method> | JsonRpcRequest<Method>[], headers?: Record<string, string>): Promise<Result | Result[] | E>;
|
|
6
|
+
buildRequest<Method>(method: Method, params?: unknown[]): JsonRpcRequest<Method>;
|
|
12
7
|
private request;
|
|
13
|
-
notify<Method = string>(method: Method, params?: unknown[]): Promise<
|
|
8
|
+
notify<Method = string>(method: Method, params?: unknown[]): Promise<void>;
|
|
14
9
|
}
|
|
15
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":"
|
|
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,MAAM,EAAE,MAAM,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO,EACxD,OAAO,EAAE,cAAc,CAAC,MAAM,CAAC,GAAG,cAAc,CAAC,MAAM,CAAC,EAAE,EAC1D,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAC9B,OAAO,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,CAAC;IAgBjC,YAAY,CAAC,MAAM,EAClB,MAAM,EAAE,MAAM,EACd,MAAM,GAAE,OAAO,EAAO,GACpB,cAAc,CAAC,MAAM,CAAC;YASX,OAAO;IAkCf,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE;CAYhE"}
|
package/dist/client.js
CHANGED
|
@@ -1,29 +1,36 @@
|
|
|
1
|
+
import { JsonRpcErrorCode } from "./error";
|
|
1
2
|
export class JsonRpcClient {
|
|
2
3
|
url;
|
|
3
4
|
id = 0;
|
|
4
5
|
constructor(url) {
|
|
5
6
|
this.url = url;
|
|
6
7
|
}
|
|
7
|
-
async call(
|
|
8
|
-
const request = this.buildRequest(method, params);
|
|
8
|
+
async call(request, headers) {
|
|
9
9
|
const response = await this.request(request, headers);
|
|
10
|
-
++this.id;
|
|
11
10
|
if ("error" in response) {
|
|
12
11
|
return response.error;
|
|
13
12
|
}
|
|
14
|
-
|
|
13
|
+
if ("result" in response) {
|
|
14
|
+
return response.result;
|
|
15
|
+
}
|
|
16
|
+
if (Array.isArray(response)) {
|
|
17
|
+
return response;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
// what else can result be? maybe indefined
|
|
21
|
+
return response;
|
|
22
|
+
}
|
|
15
23
|
}
|
|
16
24
|
buildRequest(method, params = []) {
|
|
17
25
|
return {
|
|
18
26
|
jsonrpc: "2.0",
|
|
19
27
|
method,
|
|
20
28
|
params,
|
|
21
|
-
id: this.id,
|
|
29
|
+
id: ++this.id,
|
|
22
30
|
};
|
|
23
31
|
}
|
|
24
|
-
async request(req, customHeaders
|
|
32
|
+
async request(req, customHeaders) {
|
|
25
33
|
const controller = new AbortController();
|
|
26
|
-
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
27
34
|
try {
|
|
28
35
|
const headers = {
|
|
29
36
|
"Content-Type": "application/json",
|
|
@@ -33,24 +40,32 @@ export class JsonRpcClient {
|
|
|
33
40
|
method: "POST",
|
|
34
41
|
headers,
|
|
35
42
|
body: JSON.stringify(req),
|
|
43
|
+
signal: controller.signal,
|
|
36
44
|
});
|
|
37
45
|
return (await res.json());
|
|
38
46
|
}
|
|
39
47
|
catch (err) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
48
|
+
return {
|
|
49
|
+
jsonrpc: "2.0",
|
|
50
|
+
error: {
|
|
51
|
+
code: JsonRpcErrorCode.REQUEST_FAILED,
|
|
52
|
+
message: `Request failed: ${err}`,
|
|
53
|
+
},
|
|
54
|
+
id: null,
|
|
55
|
+
};
|
|
45
56
|
}
|
|
46
57
|
}
|
|
47
|
-
notify(method, params) {
|
|
58
|
+
async notify(method, params) {
|
|
48
59
|
const request = {
|
|
49
60
|
jsonrpc: "2.0",
|
|
50
61
|
method,
|
|
51
62
|
params,
|
|
52
63
|
};
|
|
53
|
-
|
|
64
|
+
await fetch(this.url, {
|
|
65
|
+
method: "POST",
|
|
66
|
+
headers: { "Content-Type": "application/json" },
|
|
67
|
+
body: JSON.stringify(request),
|
|
68
|
+
});
|
|
54
69
|
}
|
|
55
70
|
}
|
|
56
71
|
//# 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;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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C;YACd,OAAO;gBACN,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE;oBACN,IAAI,EAAE,gBAAgB,CAAC,cAAc;oBACrC,OAAO,EAAE,mBAAmB,GAAG,EAAE;iBACjC;gBACD,EAAE,EAAE,IAAI;aACR,CAAC;QACH,CAAC;IACF,CAAC;IAED,KAAK,CAAC,MAAM,CAAkB,MAAc,EAAE,MAAkB;QAC/D,MAAM,OAAO,GAA2B;YACvC,OAAO,EAAE,KAAK;YACd,MAAM;YACN,MAAM;SACN,CAAC;QACF,MAAM,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE;YACrB,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;YAC/C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;SAC7B,CAAC,CAAC;IACJ,CAAC;CACD"}
|
package/dist/error.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare enum JsonRpcErrorCode {
|
|
2
|
+
INVALID_REQUEST = -32600,
|
|
3
|
+
METHOD_NOT_FOUND = -32601,
|
|
4
|
+
INVALID_PARAMS = -32602,
|
|
5
|
+
INTERNAL_ERROR = -32603,
|
|
6
|
+
PARSE_ERROR = -32700,
|
|
7
|
+
REQUEST_ABORTED = -32800,
|
|
8
|
+
REQUEST_FAILED = -32801
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,oBAAY,gBAAgB;IAC3B,eAAe,SAAS;IACxB,gBAAgB,SAAS;IACzB,cAAc,SAAS;IACvB,cAAc,SAAS;IACvB,WAAW,SAAS;IACpB,eAAe,SAAS;IACxB,cAAc,SAAS;CACvB"}
|
package/dist/error.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export var JsonRpcErrorCode;
|
|
2
|
+
(function (JsonRpcErrorCode) {
|
|
3
|
+
JsonRpcErrorCode[JsonRpcErrorCode["INVALID_REQUEST"] = -32600] = "INVALID_REQUEST";
|
|
4
|
+
JsonRpcErrorCode[JsonRpcErrorCode["METHOD_NOT_FOUND"] = -32601] = "METHOD_NOT_FOUND";
|
|
5
|
+
JsonRpcErrorCode[JsonRpcErrorCode["INVALID_PARAMS"] = -32602] = "INVALID_PARAMS";
|
|
6
|
+
JsonRpcErrorCode[JsonRpcErrorCode["INTERNAL_ERROR"] = -32603] = "INTERNAL_ERROR";
|
|
7
|
+
JsonRpcErrorCode[JsonRpcErrorCode["PARSE_ERROR"] = -32700] = "PARSE_ERROR";
|
|
8
|
+
JsonRpcErrorCode[JsonRpcErrorCode["REQUEST_ABORTED"] = -32800] = "REQUEST_ABORTED";
|
|
9
|
+
JsonRpcErrorCode[JsonRpcErrorCode["REQUEST_FAILED"] = -32801] = "REQUEST_FAILED";
|
|
10
|
+
})(JsonRpcErrorCode || (JsonRpcErrorCode = {}));
|
|
11
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,gBAQX;AARD,WAAY,gBAAgB;IAC3B,kFAAwB,CAAA;IACxB,oFAAyB,CAAA;IACzB,gFAAuB,CAAA;IACvB,gFAAuB,CAAA;IACvB,0EAAoB,CAAA;IACpB,kFAAwB,CAAA;IACxB,gFAAuB,CAAA;AACxB,CAAC,EARW,gBAAgB,KAAhB,gBAAgB,QAQ3B"}
|
package/dist/server.d.ts
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
INVALID_REQUEST = -32600,
|
|
3
|
-
METHOD_NOT_FOUND = -32601,
|
|
4
|
-
INVALID_PARAMS = -32602,
|
|
5
|
-
INTERNAL_ERROR = -32603,
|
|
6
|
-
PARSE_ERROR = -32700,
|
|
7
|
-
REQUEST_ABORTED = -32800
|
|
8
|
-
}
|
|
1
|
+
import { JsonRpcErrorCode } from "./error";
|
|
9
2
|
export type Handler<Result> = (params: any) => any | Promise<Result>;
|
|
10
3
|
export declare class JsonRpcServer {
|
|
11
4
|
private methods;
|
|
12
5
|
register(method: string, handler: Handler<any>): void;
|
|
13
|
-
handle<Result, Method = string>(raw: unknown): Promise<JsonRpcResponse<Result,
|
|
6
|
+
handle<Result, Method = string>(raw: unknown): Promise<JsonRpcResponse<Result, JsonRpcErrorCode | number> | JsonRpcResponse<Result, JsonRpcErrorCode | number>[] | null>;
|
|
14
7
|
private error;
|
|
15
8
|
}
|
|
16
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,
|
|
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,GAAG,KAAK,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAErE,qBAAa,aAAa;IACzB,OAAO,CAAC,OAAO,CAAmC;IAElD,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC;IAIxC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EACnC,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
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
(function (JsonRpcErrorCodes) {
|
|
3
|
-
JsonRpcErrorCodes[JsonRpcErrorCodes["INVALID_REQUEST"] = -32600] = "INVALID_REQUEST";
|
|
4
|
-
JsonRpcErrorCodes[JsonRpcErrorCodes["METHOD_NOT_FOUND"] = -32601] = "METHOD_NOT_FOUND";
|
|
5
|
-
JsonRpcErrorCodes[JsonRpcErrorCodes["INVALID_PARAMS"] = -32602] = "INVALID_PARAMS";
|
|
6
|
-
JsonRpcErrorCodes[JsonRpcErrorCodes["INTERNAL_ERROR"] = -32603] = "INTERNAL_ERROR";
|
|
7
|
-
JsonRpcErrorCodes[JsonRpcErrorCodes["PARSE_ERROR"] = -32700] = "PARSE_ERROR";
|
|
8
|
-
JsonRpcErrorCodes[JsonRpcErrorCodes["REQUEST_ABORTED"] = -32800] = "REQUEST_ABORTED";
|
|
9
|
-
})(JsonRpcErrorCodes || (JsonRpcErrorCodes = {}));
|
|
1
|
+
import { JsonRpcErrorCode } from "./error";
|
|
10
2
|
export class JsonRpcServer {
|
|
11
3
|
methods = new Map();
|
|
12
4
|
register(method, handler) {
|
|
@@ -16,7 +8,7 @@ export class JsonRpcServer {
|
|
|
16
8
|
// handle batch
|
|
17
9
|
if (Array.isArray(raw)) {
|
|
18
10
|
if (raw.length === 0) {
|
|
19
|
-
return this.error(null,
|
|
11
|
+
return this.error(null, JsonRpcErrorCode.INVALID_REQUEST, "Invalid request");
|
|
20
12
|
}
|
|
21
13
|
const responses = await Promise.all(raw.map((item) => this.handle(item)));
|
|
22
14
|
const filtered = responses.filter((r) => r !== null);
|
|
@@ -28,7 +20,7 @@ export class JsonRpcServer {
|
|
|
28
20
|
req === null ||
|
|
29
21
|
req.jsonrpc !== "2.0" ||
|
|
30
22
|
typeof req.method !== "string") {
|
|
31
|
-
return this.error(null,
|
|
23
|
+
return this.error(null, JsonRpcErrorCode.INVALID_REQUEST, "Invalid request");
|
|
32
24
|
}
|
|
33
25
|
const id = typeof req?.id === "string" ||
|
|
34
26
|
typeof req?.id === "number" ||
|
|
@@ -39,7 +31,7 @@ export class JsonRpcServer {
|
|
|
39
31
|
if (!handler) {
|
|
40
32
|
return id === null
|
|
41
33
|
? null
|
|
42
|
-
: this.error(req.id,
|
|
34
|
+
: this.error(req.id, JsonRpcErrorCode.METHOD_NOT_FOUND, "Method not found");
|
|
43
35
|
}
|
|
44
36
|
try {
|
|
45
37
|
const result = await handler(req.params);
|
|
@@ -52,7 +44,7 @@ export class JsonRpcServer {
|
|
|
52
44
|
};
|
|
53
45
|
}
|
|
54
46
|
catch (err) {
|
|
55
|
-
return this.error(req.id ?? null,
|
|
47
|
+
return this.error(req.id ?? null, JsonRpcErrorCode.INTERNAL_ERROR, "Internal error", err instanceof Error ? err.message : err);
|
|
56
48
|
}
|
|
57
49
|
}
|
|
58
50
|
error(id, code, message, data) {
|
package/dist/server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,
|
|
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,EAAwB,CAAC;IAElD,QAAQ,CAAC,MAAc,EAAE,OAAqB;QAC7C,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,CAAC;IAED,KAAK,CAAC,MAAM,CACX,GAAY;QAMZ,eAAe;QACf,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YACxB,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACtB,OAAO,IAAI,CAAC,KAAK,CAChB,IAAI,EACJ,gBAAgB,CAAC,eAAe,EAChC,iBAAiB,CACjB,CAAC;YACH,CAAC;YACD,M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M,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YACzC,IAAI,GAAG,CAAC,EAAE,KAAK,SAAS;gBAAE,OAAO,IAAI,CAAC,CAAC,eAAe;YACtD,OAAO;gBACN,OAAO,EAAE,KAAK;gBACd,MAAM;gBACN,EAAE,EAAE,GAAG,CAAC,EAAE;aACV,CAAC;QACH,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,OAAO,IAAI,CAAC,KAAK,CAChB,GAAG,CAAC,EAAE,IAAI,IAAI,EACd,gBAAgB,CAAC,cAAc,EAC/B,gBAAgB,EAChB,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CACxC,CAAC;QACH,CAAC;IACF,CAAC;IAEO,KAAK,CACZ,EAAgC,EAChC,IAA+B,EAC/B,OAAe,EACf,IAAc;QAEd,OAAO;YACN,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE;YAC9B,EAAE,EAAE,EAAE,IAAI,IAAI;SACd,CAAC;IACH,CAAC;CACD"}
|
package/example.ts
CHANGED
|
@@ -26,5 +26,5 @@ 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("ping", []);
|
|
29
|
+
const result = await client.call(client.buildRequest("ping", []));
|
|
30
30
|
console.log(result);
|
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -1,41 +1,47 @@
|
|
|
1
|
+
import { JsonRpcErrorCode } from "./error";
|
|
1
2
|
export class JsonRpcClient {
|
|
2
3
|
private id = 0;
|
|
3
4
|
|
|
4
5
|
constructor(private url: string) { }
|
|
5
6
|
|
|
6
7
|
async call<Method = string, Result = unknown, E = unknown>(
|
|
7
|
-
|
|
8
|
-
params: unknown[] = [],
|
|
8
|
+
request: JsonRpcRequest<Method> | JsonRpcRequest<Method>[],
|
|
9
9
|
headers?: Record<string, string>,
|
|
10
|
-
): Promise<Result | E> {
|
|
11
|
-
const request: JsonRpcRequest<Method> = this.buildRequest<Method>(
|
|
12
|
-
method,
|
|
13
|
-
params,
|
|
14
|
-
);
|
|
10
|
+
): Promise<Result | Result[] | E> {
|
|
15
11
|
const response = await this.request(request, headers);
|
|
16
|
-
++this.id;
|
|
17
12
|
if ("error" in response) {
|
|
18
13
|
return response.error as E;
|
|
19
14
|
}
|
|
20
|
-
|
|
15
|
+
if ("result" in response) {
|
|
16
|
+
return response.result as Result;
|
|
17
|
+
}
|
|
18
|
+
if (Array.isArray(response)) {
|
|
19
|
+
return response as Result[];
|
|
20
|
+
} else {
|
|
21
|
+
// what else can result be? maybe indefined
|
|
22
|
+
return response;
|
|
23
|
+
}
|
|
21
24
|
}
|
|
22
25
|
|
|
23
|
-
buildRequest<Method>(
|
|
26
|
+
buildRequest<Method>(
|
|
27
|
+
method: Method,
|
|
28
|
+
params: unknown[] = [],
|
|
29
|
+
): JsonRpcRequest<Method> {
|
|
24
30
|
return {
|
|
25
31
|
jsonrpc: "2.0" as const,
|
|
26
32
|
method,
|
|
27
33
|
params,
|
|
28
|
-
id: this.id,
|
|
34
|
+
id: ++this.id,
|
|
29
35
|
};
|
|
30
36
|
}
|
|
31
37
|
|
|
32
38
|
private async request(
|
|
33
|
-
req: JsonRpcRequest<unknown
|
|
39
|
+
req: JsonRpcRequest<unknown> | JsonRpcRequest<unknown>[],
|
|
34
40
|
customHeaders?: Record<string, string>,
|
|
35
|
-
|
|
36
|
-
|
|
41
|
+
): Promise<
|
|
42
|
+
JsonRpcResponse<unknown, number> | JsonRpcResponse<unknown, number>[]
|
|
43
|
+
> {
|
|
37
44
|
const controller = new AbortController();
|
|
38
|
-
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
39
45
|
try {
|
|
40
46
|
const headers: Record<string, string> = {
|
|
41
47
|
"Content-Type": "application/json",
|
|
@@ -45,24 +51,34 @@ export class JsonRpcClient {
|
|
|
45
51
|
method: "POST",
|
|
46
52
|
headers,
|
|
47
53
|
body: JSON.stringify(req),
|
|
54
|
+
signal: controller.signal,
|
|
48
55
|
});
|
|
49
56
|
|
|
50
|
-
return (await res.json()) as
|
|
57
|
+
return (await res.json()) as
|
|
58
|
+
| JsonRpcResponse<unknown, number>
|
|
59
|
+
| JsonRpcResponse<unknown, number>[];
|
|
51
60
|
} catch (err) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
61
|
+
return {
|
|
62
|
+
jsonrpc: "2.0",
|
|
63
|
+
error: {
|
|
64
|
+
code: JsonRpcErrorCode.REQUEST_FAILED,
|
|
65
|
+
message: `Request failed: ${err}`,
|
|
66
|
+
},
|
|
67
|
+
id: null,
|
|
68
|
+
};
|
|
57
69
|
}
|
|
58
70
|
}
|
|
59
71
|
|
|
60
|
-
notify<Method = string>(method: Method, params?: unknown[]) {
|
|
72
|
+
async notify<Method = string>(method: Method, params?: unknown[]) {
|
|
61
73
|
const request: JsonRpcRequest<Method> = {
|
|
62
74
|
jsonrpc: "2.0",
|
|
63
75
|
method,
|
|
64
76
|
params,
|
|
65
77
|
};
|
|
66
|
-
|
|
78
|
+
await fetch(this.url, {
|
|
79
|
+
method: "POST",
|
|
80
|
+
headers: { "Content-Type": "application/json" },
|
|
81
|
+
body: JSON.stringify(request),
|
|
82
|
+
});
|
|
67
83
|
}
|
|
68
84
|
}
|
package/src/error.ts
ADDED
package/src/server.ts
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
INVALID_REQUEST = -32600,
|
|
3
|
-
METHOD_NOT_FOUND = -32601,
|
|
4
|
-
INVALID_PARAMS = -32602,
|
|
5
|
-
INTERNAL_ERROR = -32603,
|
|
6
|
-
PARSE_ERROR = -32700,
|
|
7
|
-
REQUEST_ABORTED = -32800,
|
|
8
|
-
}
|
|
1
|
+
import { JsonRpcErrorCode } from "./error";
|
|
9
2
|
export type Handler<Result> = (params: any) => any | Promise<Result>;
|
|
10
3
|
|
|
11
4
|
export class JsonRpcServer {
|
|
@@ -18,8 +11,8 @@ export class JsonRpcServer {
|
|
|
18
11
|
async handle<Result, Method = string>(
|
|
19
12
|
raw: unknown,
|
|
20
13
|
): Promise<
|
|
21
|
-
| JsonRpcResponse<Result,
|
|
22
|
-
| JsonRpcResponse<Result,
|
|
14
|
+
| JsonRpcResponse<Result, JsonRpcErrorCode | number>
|
|
15
|
+
| JsonRpcResponse<Result, JsonRpcErrorCode | number>[]
|
|
23
16
|
| null
|
|
24
17
|
> {
|
|
25
18
|
// handle batch
|
|
@@ -27,13 +20,13 @@ export class JsonRpcServer {
|
|
|
27
20
|
if (raw.length === 0) {
|
|
28
21
|
return this.error(
|
|
29
22
|
null,
|
|
30
|
-
|
|
23
|
+
JsonRpcErrorCode.INVALID_REQUEST,
|
|
31
24
|
"Invalid request",
|
|
32
25
|
);
|
|
33
26
|
}
|
|
34
27
|
const responses = await Promise.all(raw.map((item) => this.handle(item)));
|
|
35
28
|
const filtered = responses.filter(
|
|
36
|
-
(r): r is JsonRpcResponse<Result,
|
|
29
|
+
(r): r is JsonRpcResponse<Result, JsonRpcErrorCode | number> =>
|
|
37
30
|
r !== null,
|
|
38
31
|
);
|
|
39
32
|
return filtered.length > 0 ? filtered : null;
|
|
@@ -50,7 +43,7 @@ export class JsonRpcServer {
|
|
|
50
43
|
) {
|
|
51
44
|
return this.error(
|
|
52
45
|
null,
|
|
53
|
-
|
|
46
|
+
JsonRpcErrorCode.INVALID_REQUEST,
|
|
54
47
|
"Invalid request",
|
|
55
48
|
);
|
|
56
49
|
}
|
|
@@ -68,7 +61,7 @@ export class JsonRpcServer {
|
|
|
68
61
|
? null
|
|
69
62
|
: this.error(
|
|
70
63
|
req.id,
|
|
71
|
-
|
|
64
|
+
JsonRpcErrorCode.METHOD_NOT_FOUND,
|
|
72
65
|
"Method not found",
|
|
73
66
|
);
|
|
74
67
|
}
|
|
@@ -84,7 +77,7 @@ export class JsonRpcServer {
|
|
|
84
77
|
} catch (err) {
|
|
85
78
|
return this.error(
|
|
86
79
|
req.id ?? null,
|
|
87
|
-
|
|
80
|
+
JsonRpcErrorCode.INTERNAL_ERROR,
|
|
88
81
|
"Internal error",
|
|
89
82
|
err instanceof Error ? err.message : err,
|
|
90
83
|
);
|
|
@@ -93,10 +86,10 @@ export class JsonRpcServer {
|
|
|
93
86
|
|
|
94
87
|
private error<Result, Method = string>(
|
|
95
88
|
id: JsonRpcRequest<Method>["id"],
|
|
96
|
-
code:
|
|
89
|
+
code: JsonRpcErrorCode | number,
|
|
97
90
|
message: string,
|
|
98
91
|
data?: unknown,
|
|
99
|
-
): JsonRpcResponse<Result,
|
|
92
|
+
): JsonRpcResponse<Result, JsonRpcErrorCode | number> {
|
|
100
93
|
return {
|
|
101
94
|
jsonrpc: "2.0",
|
|
102
95
|
error: { code, message, data },
|