Skip to content

Commit 42cb1c0

Browse files
committed
feat(json-rpc): add defineJsonRpc and improve types
1 parent dc503c0 commit 42cb1c0

File tree

4 files changed

+46
-25
lines changed

4 files changed

+46
-25
lines changed

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,9 @@ export type {
204204
JsonRpcRequest,
205205
JsonRpcResponse,
206206
JsonRpcError,
207-
JsonRpcMethodHandler,
208-
JsonRpcMethodMap,
207+
JsonRpcMethod,
209208
} from "./utils/json-rpc.ts";
210-
export { defineJsonRpcHandler } from "./utils/json-rpc.ts";
209+
export { defineJsonRpcHandler, defineJsonRpc } from "./utils/json-rpc.ts";
211210

212211
// ---- Deprecated ----
213212

src/utils/json-rpc.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ export type JsonRpcResponse<O = unknown> =
5151
* A function that handles a JSON-RPC method call.
5252
* It receives the parameters from the request and the original H3Event.
5353
*/
54-
export type JsonRpcMethodHandler<
54+
export type JsonRpcMethod<
5555
O = unknown,
5656
I extends JsonRpcParams | undefined = JsonRpcParams | undefined,
5757
> = (data: JsonRpcRequest<I>, event: H3Event) => O | Promise<O>;
5858

5959
/**
60-
* A map of method names to their corresponding handler functions.
60+
* A map of method names to their corresponding functions.
6161
*/
62-
export type JsonRpcMethodMap = Record<string, JsonRpcMethodHandler>;
62+
type JsonRpcMethodMap = Record<string, JsonRpcMethod>;
6363

6464
// Official JSON-RPC 2.0 error codes.
6565
/**
@@ -114,6 +114,24 @@ const SERVER_ERROR_RATE_LIMITED = -32_029;
114114
*/
115115
const SERVER_ERROR = -32_000;
116116

117+
/**
118+
* Define a JSON-RPC method.
119+
*
120+
* @param method The method implementation function.
121+
* @returns The method function, unmodified.
122+
*
123+
* @example
124+
* const add = defineJsonRpc(({ params }) => {
125+
* return params.a + params.b;
126+
* });
127+
*/
128+
export function defineJsonRpc<
129+
O = unknown,
130+
I extends JsonRpcParams | undefined = JsonRpcParams | undefined,
131+
>(method: JsonRpcMethod<O, I>): JsonRpcMethod<O, I> {
132+
return method;
133+
}
134+
117135
/**
118136
* Creates an H3 event handler that implements the JSON-RPC 2.0 specification.
119137
*
@@ -140,7 +158,7 @@ export function defineJsonRpcHandler<RequestT extends EventHandlerRequest = Even
140158
* This ensures that method names like "__proto__", "constructor", "toString",
141159
* "hasOwnProperty", etc. cannot resolve to inherited Object.prototype properties.
142160
*/
143-
const methodMap: Record<string, JsonRpcMethodHandler> = Object.create(null);
161+
const methodMap: Record<string, JsonRpcMethod> = Object.create(null);
144162
for (const key of Object.keys(methods)) {
145163
methodMap[key] = methods[key];
146164
}

test/json-rpc.test.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
import { defineJsonRpcHandler, HTTPError } from "../src/index.ts";
1+
import { defineJsonRpcHandler, defineJsonRpc, HTTPError } from "../src/index.ts";
22
import { describeMatrix } from "./_setup.ts";
33

44
describeMatrix("json-rpc", (t, { describe, it, expect }) => {
5+
const echo = defineJsonRpc(({ params }, event) => {
6+
const message = Array.isArray(params) ? params[0] : params?.message;
7+
return `Received ${message} on path ${event.url.pathname}`;
8+
});
9+
const sum = defineJsonRpc(({ params }) => {
10+
if (
11+
!params ||
12+
typeof params !== "object" ||
13+
!("a" in params) ||
14+
typeof params.a !== "number" ||
15+
!("b" in params) ||
16+
typeof params.b !== "number"
17+
) {
18+
throw new Error("Invalid parameters for sum");
19+
}
20+
return params.a + params.b;
21+
});
22+
523
const eventHandler = defineJsonRpcHandler({
6-
echo: ({ params }, event) => {
7-
const message = Array.isArray(params) ? params[0] : params?.message;
8-
return `Received ${message} on path ${event.url.pathname}`;
9-
},
10-
sum: ({ params }) => {
11-
if (
12-
!params ||
13-
typeof params !== "object" ||
14-
!("a" in params) ||
15-
typeof params.a !== "number" ||
16-
!("b" in params) ||
17-
typeof params.b !== "number"
18-
) {
19-
throw new Error("Invalid parameters for sum");
20-
}
21-
return params.a + params.b;
22-
},
24+
echo,
25+
sum,
2326
error: () => {
2427
throw new Error("Handler error");
2528
},

test/unit/package.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe("h3 package", () => {
3131
"defaultContentType",
3232
"defineEventHandler",
3333
"defineHandler",
34+
"defineJsonRpc",
3435
"defineJsonRpcHandler",
3536
"defineLazyEventHandler",
3637
"defineMiddleware",

0 commit comments

Comments
 (0)