@asyncswap/jsonrpc 0.4.3 → 0.4.5

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.5
4
+
5
+ ### Patch Changes
6
+
7
+ - 0d61f89: simplify method type and export error codes
8
+
9
+ ## 0.4.4
10
+
11
+ ### Patch Changes
12
+
13
+ - f7d28d9: (fix): error code now generated in build
14
+
3
15
  ## 0.4.3
4
16
 
5
17
  ### Patch Changes
package/README.md CHANGED
@@ -66,6 +66,7 @@ enum JsonRpcErrorCodes {
66
66
  INTERNAL_ERROR = -32603,
67
67
  PARSE_ERROR = -32700,
68
68
  REQUEST_ABORTED = -32800,
69
+ REQUEST_FAILED = -32801,
69
70
  }
70
71
  ```
71
72
 
@@ -77,8 +78,8 @@ enum JsonRpcErrorCodes {
77
78
 
78
79
  #### Methods
79
80
 
80
- - `call<Method, Result, Error>(method, params?, headers?)` - Make a JSON-RPC call
81
- - `notify<Method>(method, params?)` - Send a JSON-RPC notification
81
+ - `call<Result, Error>(request, headers?)` - Make a JSON-RPC call
82
+ - `notify(method, params?)` - Send a JSON-RPC notification
82
83
  - `buildRequest(method, params?)` - Build a JSON-RPC request object
83
84
 
84
85
  ## 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<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>;
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<Method = string>(method: Method, params?: unknown[]): Promise<void>;
8
+ notify(method: string, params?: unknown[]): Promise<void>;
9
9
  }
10
10
  //# sourceMappingURL=client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,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;IA+Cf,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE;CAYhE"}
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 CHANGED
@@ -1,3 +1,4 @@
1
+ import { JsonRpcErrorCode } from "./error";
1
2
  export class JsonRpcClient {
2
3
  url;
3
4
  id = 0;
@@ -28,9 +29,8 @@ export class JsonRpcClient {
28
29
  id: ++this.id,
29
30
  };
30
31
  }
31
- async request(req, customHeaders, timeout = 5000) {
32
+ async request(req, customHeaders) {
32
33
  const controller = new AbortController();
33
- const timeoutId = setTimeout(() => controller.abort(), timeout);
34
34
  try {
35
35
  const headers = {
36
36
  "Content-Type": "application/json",
@@ -45,21 +45,10 @@ export class JsonRpcClient {
45
45
  return (await res.json());
46
46
  }
47
47
  catch (err) {
48
- if (err instanceof Error && err.name === "AbortError") {
49
- return {
50
- jsonrpc: "2.0",
51
- error: {
52
- code: JsonRpcErrorCodes.REQUEST_ABORTED,
53
- message: `Request Aborted. Timeout after ${timeout}ms.`,
54
- },
55
- id: null,
56
- };
57
- }
58
- clearTimeout(timeoutId);
59
48
  return {
60
49
  jsonrpc: "2.0",
61
50
  error: {
62
- code: JsonRpcErrorCodes.REQUEST_FAILED,
51
+ code: JsonRpcErrorCode.REQUEST_FAILED,
63
52
  message: `Request failed: ${err}`,
64
53
  },
65
54
  id: null,
@@ -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,OAA0D,EAC1D,OAAgC;QAEhC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACtD,IAAI,OAAO,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9C,MAAM,OAAO,GAAmB;YAC/B,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"}
@@ -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/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from "./client";
2
+ export * from "./error";
2
3
  export * from "./server";
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -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
@@ -1,3 +1,4 @@
1
1
  export * from "./client";
2
+ export * from "./error";
2
3
  export * from "./server";
3
4
  //# sourceMappingURL=index.js.map
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,8 +1,9 @@
1
+ import { JsonRpcErrorCode } from "./error";
1
2
  export type Handler<Result> = (params: any) => any | Promise<Result>;
2
3
  export declare class JsonRpcServer {
3
4
  private methods;
4
5
  register(method: string, handler: Handler<any>): void;
5
- handle<Result, Method = string>(raw: unknown): Promise<JsonRpcResponse<Result, JsonRpcErrorCodes | number> | JsonRpcResponse<Result, JsonRpcErrorCodes | number>[] | null>;
6
+ handle<Result>(raw: unknown): Promise<JsonRpcResponse<Result, JsonRpcErrorCode | number> | JsonRpcResponse<Result, JsonRpcErrorCode | number>[] | null>;
6
7
  private error;
7
8
  }
8
9
  //# sourceMappingURL=server.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,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,iBAAiB,GAAG,MAAM,CAAC,GACnD,eAAe,CAAC,MAAM,EAAE,iBAAiB,GAAG,MAAM,CAAC,EAAE,GACrD,IAAI,CACN;IAsED,OAAO,CAAC,KAAK;CAYb"}
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,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 CHANGED
@@ -1,3 +1,4 @@
1
+ import { JsonRpcErrorCode } from "./error";
1
2
  export class JsonRpcServer {
2
3
  methods = new Map();
3
4
  register(method, handler) {
@@ -7,7 +8,7 @@ export class JsonRpcServer {
7
8
  // handle batch
8
9
  if (Array.isArray(raw)) {
9
10
  if (raw.length === 0) {
10
- return this.error(null, JsonRpcErrorCodes.INVALID_REQUEST, "Invalid request");
11
+ return this.error(null, JsonRpcErrorCode.INVALID_REQUEST, "Invalid request");
11
12
  }
12
13
  const responses = await Promise.all(raw.map((item) => this.handle(item)));
13
14
  const filtered = responses.filter((r) => r !== null);
@@ -19,7 +20,7 @@ export class JsonRpcServer {
19
20
  req === null ||
20
21
  req.jsonrpc !== "2.0" ||
21
22
  typeof req.method !== "string") {
22
- return this.error(null, JsonRpcErrorCodes.INVALID_REQUEST, "Invalid request");
23
+ return this.error(null, JsonRpcErrorCode.INVALID_REQUEST, "Invalid request");
23
24
  }
24
25
  const id = typeof req?.id === "string" ||
25
26
  typeof req?.id === "number" ||
@@ -30,7 +31,7 @@ export class JsonRpcServer {
30
31
  if (!handler) {
31
32
  return id === null
32
33
  ? null
33
- : this.error(req.id, JsonRpcErrorCodes.METHOD_NOT_FOUND, "Method not found");
34
+ : this.error(req.id, JsonRpcErrorCode.METHOD_NOT_FOUND, "Method not found");
34
35
  }
35
36
  try {
36
37
  const result = await handler(req.params);
@@ -43,7 +44,7 @@ export class JsonRpcServer {
43
44
  };
44
45
  }
45
46
  catch (err) {
46
- return this.error(req.id ?? null, JsonRpcErrorCodes.INTERNAL_ERROR, "Internal error", err instanceof Error ? err.message : err);
47
+ return this.error(req.id ?? null, JsonRpcErrorCode.INTERNAL_ERROR, "Internal error", err instanceof Error ? err.message : err);
47
48
  }
48
49
  }
49
50
  error(id, code, message, data) {
@@ -1 +1 @@
1
- {"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAEA,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,iBAAiB,CAAC,eAAe,EACjC,iBAAiB,CACjB,CAAC;YACH,CAAC;YACD,MAAM,SAAS,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YAC1E,MAAM,QAAQ,GAAG,SAAS,CAAC,MAAM,CAChC,CAAC,CAAC,EAA4D,EAAE,CAC/D,CAAC,KAAK,IAAI,CACX,CAAC;YACF,OAAO,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC;Q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B,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,EAAwB,EACxB,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/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.3",
5
+ "version": "0.4.5",
6
6
  "license": "MIT",
7
7
  "main": "dist/index.js",
8
8
  "module": "dist/index.mjs",
package/src/client.ts CHANGED
@@ -1,10 +1,11 @@
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
- async call<Method = string, Result = unknown, E = unknown>(
7
- request: JsonRpcRequest<Method> | JsonRpcRequest<Method>[],
7
+ async call<Result = unknown, E = unknown>(
8
+ request: JsonRpcRequest | JsonRpcRequest[],
8
9
  headers?: Record<string, string>,
9
10
  ): Promise<Result | Result[] | E> {
10
11
  const response = await this.request(request, headers);
@@ -22,10 +23,7 @@ export class JsonRpcClient {
22
23
  }
23
24
  }
24
25
 
25
- buildRequest<Method>(
26
- method: Method,
27
- params: unknown[] = [],
28
- ): JsonRpcRequest<Method> {
26
+ buildRequest(method: string, params: unknown[] = []): JsonRpcRequest {
29
27
  return {
30
28
  jsonrpc: "2.0" as const,
31
29
  method,
@@ -35,14 +33,12 @@ export class JsonRpcClient {
35
33
  }
36
34
 
37
35
  private async request(
38
- req: JsonRpcRequest<unknown> | JsonRpcRequest<unknown>[],
36
+ req: JsonRpcRequest | JsonRpcRequest[],
39
37
  customHeaders?: Record<string, string>,
40
- timeout: number = 5000,
41
38
  ): Promise<
42
39
  JsonRpcResponse<unknown, number> | JsonRpcResponse<unknown, number>[]
43
40
  > {
44
41
  const controller = new AbortController();
45
- const timeoutId = setTimeout(() => controller.abort(), timeout);
46
42
  try {
47
43
  const headers: Record<string, string> = {
48
44
  "Content-Type": "application/json",
@@ -59,21 +55,10 @@ export class JsonRpcClient {
59
55
  | JsonRpcResponse<unknown, number>
60
56
  | JsonRpcResponse<unknown, number>[];
61
57
  } catch (err) {
62
- if (err instanceof Error && err.name === "AbortError") {
63
- return {
64
- jsonrpc: "2.0",
65
- error: {
66
- code: JsonRpcErrorCodes.REQUEST_ABORTED,
67
- message: `Request Aborted. Timeout after ${timeout}ms.`,
68
- },
69
- id: null,
70
- };
71
- }
72
- clearTimeout(timeoutId);
73
58
  return {
74
59
  jsonrpc: "2.0",
75
60
  error: {
76
- code: JsonRpcErrorCodes.REQUEST_FAILED,
61
+ code: JsonRpcErrorCode.REQUEST_FAILED,
77
62
  message: `Request failed: ${err}`,
78
63
  },
79
64
  id: null,
@@ -81,8 +66,8 @@ export class JsonRpcClient {
81
66
  }
82
67
  }
83
68
 
84
- async notify<Method = string>(method: Method, params?: unknown[]) {
85
- const request: JsonRpcRequest<Method> = {
69
+ async notify(method: string, params?: unknown[]) {
70
+ const request: JsonRpcRequest = {
86
71
  jsonrpc: "2.0",
87
72
  method,
88
73
  params,
package/src/error.ts ADDED
@@ -0,0 +1,9 @@
1
+ export 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
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./client";
2
+ export * from "./error";
2
3
  export * from "./server";
package/src/server.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { JsonRpcErrorCode } from "./error";
1
2
  export type Handler<Result> = (params: any) => any | Promise<Result>;
2
3
 
3
4
  export class JsonRpcServer {
@@ -7,11 +8,11 @@ export class JsonRpcServer {
7
8
  this.methods.set(method, handler);
8
9
  }
9
10
 
10
- async handle<Result, Method = string>(
11
+ async handle<Result>(
11
12
  raw: unknown,
12
13
  ): Promise<
13
- | JsonRpcResponse<Result, JsonRpcErrorCodes | number>
14
- | JsonRpcResponse<Result, JsonRpcErrorCodes | number>[]
14
+ | JsonRpcResponse<Result, JsonRpcErrorCode | number>
15
+ | JsonRpcResponse<Result, JsonRpcErrorCode | number>[]
15
16
  | null
16
17
  > {
17
18
  // handle batch
@@ -19,20 +20,20 @@ export class JsonRpcServer {
19
20
  if (raw.length === 0) {
20
21
  return this.error(
21
22
  null,
22
- JsonRpcErrorCodes.INVALID_REQUEST,
23
+ JsonRpcErrorCode.INVALID_REQUEST,
23
24
  "Invalid request",
24
25
  );
25
26
  }
26
27
  const responses = await Promise.all(raw.map((item) => this.handle(item)));
27
28
  const filtered = responses.filter(
28
- (r): r is JsonRpcResponse<Result, JsonRpcErrorCodes | number> =>
29
+ (r): r is JsonRpcResponse<Result, JsonRpcErrorCode | number> =>
29
30
  r !== null,
30
31
  );
31
32
  return filtered.length > 0 ? filtered : null;
32
33
  }
33
34
 
34
35
  // handle single response
35
- const req = raw as Partial<JsonRpcRequest<Method>>;
36
+ const req = raw as Partial<JsonRpcRequest>;
36
37
 
37
38
  if (
38
39
  typeof req !== "object" ||
@@ -42,7 +43,7 @@ export class JsonRpcServer {
42
43
  ) {
43
44
  return this.error(
44
45
  null,
45
- JsonRpcErrorCodes.INVALID_REQUEST,
46
+ JsonRpcErrorCode.INVALID_REQUEST,
46
47
  "Invalid request",
47
48
  );
48
49
  }
@@ -60,7 +61,7 @@ export class JsonRpcServer {
60
61
  ? null
61
62
  : this.error(
62
63
  req.id,
63
- JsonRpcErrorCodes.METHOD_NOT_FOUND,
64
+ JsonRpcErrorCode.METHOD_NOT_FOUND,
64
65
  "Method not found",
65
66
  );
66
67
  }
@@ -76,19 +77,19 @@ export class JsonRpcServer {
76
77
  } catch (err) {
77
78
  return this.error(
78
79
  req.id ?? null,
79
- JsonRpcErrorCodes.INTERNAL_ERROR,
80
+ JsonRpcErrorCode.INTERNAL_ERROR,
80
81
  "Internal error",
81
82
  err instanceof Error ? err.message : err,
82
83
  );
83
84
  }
84
85
  }
85
86
 
86
- private error<Result, Method = string>(
87
- id: JsonRpcRequest<Method>["id"],
88
- code: JsonRpcErrorCodes | number,
87
+ private error<Result>(
88
+ id: JsonRpcRequest["id"],
89
+ code: JsonRpcErrorCode | number,
89
90
  message: string,
90
91
  data?: unknown,
91
- ): JsonRpcResponse<Result, JsonRpcErrorCodes | number> {
92
+ ): JsonRpcResponse<Result, JsonRpcErrorCode | number> {
92
93
  return {
93
94
  jsonrpc: "2.0",
94
95
  error: { code, message, data },
@@ -1,7 +1,7 @@
1
1
  declare global {
2
- export interface JsonRpcRequest<Method> {
2
+ export interface JsonRpcRequest {
3
3
  jsonrpc: "2.0";
4
- method: Method;
4
+ method: string;
5
5
  params?: unknown[];
6
6
  id?: JsonRpcId;
7
7
  }
@@ -1,13 +0,0 @@
1
- declare global {
2
- export enum JsonRpcErrorCodes {
3
- INVALID_REQUEST = -32600,
4
- METHOD_NOT_FOUND = -32601,
5
- INVALID_PARAMS = -32602,
6
- INTERNAL_ERROR = -32603,
7
- PARSE_ERROR = -32700,
8
- REQUEST_ABORTED = -32800,
9
- REQUEST_FAILED = -32801,
10
- }
11
- }
12
-
13
- export { };