Skip to content

Commit b530ba9

Browse files
committed
refactor(rpc-methods): Force all implementations to be async
1 parent 6e878bc commit b530ba9

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

packages/rpc-methods/src/RpcService.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,28 @@ describe('RpcService', () => {
2727
});
2828

2929
describe('execute', () => {
30-
it('should be able to execute a method', () => {
30+
it('should execute a method', async () => {
3131
const service = new RpcService(getHandlers(), getHooks());
32-
expect(service.execute('method1', ['test'])).toBeNull();
32+
expect(await service.execute('method1', ['test'])).toBeNull();
3333
});
3434

35-
it('should be able to execute a method that uses a hook', () => {
35+
it('should be able to execute a method that uses a hook', async () => {
3636
const service = new RpcService(getHandlers(), getHooks());
37-
expect(service.execute('method2', [2])).toBe(4);
37+
expect(await service.execute('method2', [2])).toBe(4);
3838
});
3939

40-
it('should throw an error if the method is not found', () => {
40+
it('should throw an error if the method is not found', async () => {
4141
const service = new RpcService(getHandlers(), getHooks());
4242
// @ts-expect-error Intentional destructive testing
43-
expect(async () => service.execute('method3', [2])).not.toThrow(
43+
await expect(service.execute('method3', [2])).rejects.toThrow(
4444
// This is not a _good_ error, but we only care about type safety in this instance.
45-
'TypeError: Cannot read properties of undefined (reading "params")',
45+
"Cannot read properties of undefined (reading 'params')",
4646
);
4747
});
4848

49-
it('should throw if passed invalid params', () => {
49+
it('should throw if passed invalid params', async () => {
5050
const service = new RpcService(getHandlers(), getHooks());
51-
expect(async () => service.execute('method1', [2])).toThrow(
51+
await expect(service.execute('method1', [2])).rejects.toThrow(
5252
'Invalid params',
5353
);
5454
});

packages/rpc-methods/src/RpcService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,18 @@ export class RpcService<
7777
* @returns The result of the method execution.
7878
* @throws If the parameters are invalid.
7979
*/
80-
execute<Method extends ExtractMethods<Handlers[keyof Handlers]>>(
80+
async execute<Method extends ExtractMethods<Handlers[keyof Handlers]>>(
8181
method: Method,
8282
params: unknown,
83-
): ReturnType<Handlers[Method]['implementation']> {
83+
): Promise<ReturnType<Handlers[Method]['implementation']>> {
8484
const handler = this.#getHandler(method);
8585
assertParams(params, handler.params);
8686

8787
// Select only the hooks that the handler needs
8888
const selectedHooks = selectHooks(this.#hooks, handler.hooks);
8989

9090
// Execute the handler with the selected hooks
91-
return handler.implementation(selectedHooks, params);
91+
return await handler.implementation(selectedHooks, params);
9292
}
9393

9494
/**

packages/rpc-methods/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export type HandlerFunction<
5757
Params extends JsonRpcParams,
5858
Result extends Json,
5959
Hooks extends Record<string, unknown>,
60-
> = (hooks: Hooks, params: Params) => Result | Promise<Result>;
60+
> = (hooks: Hooks, params: Params) => Promise<Result>;
6161

6262
// Service-side types
6363

packages/rpc-methods/test/methods.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const getPartialHandlers = () =>
3030
({
3131
method1: {
3232
method: 'method1',
33-
implementation: (hooks, [_value]) => {
33+
implementation: async (hooks, [_value]) => {
3434
hooks.hook1();
3535
return null;
3636
},
@@ -43,7 +43,7 @@ export const getPartialHandlers = () =>
4343
>,
4444
method2: {
4545
method: 'method2',
46-
implementation: (hooks, [value]) => {
46+
implementation: async (hooks, [value]) => {
4747
hooks.hook3();
4848
return value * 2;
4949
},

0 commit comments

Comments
 (0)