-
-
Notifications
You must be signed in to change notification settings - Fork 256
feat(json-rpc-engine): Add v2 scaffold middleware #6975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/json-rpc-engine/src/v2/createScaffoldMiddleware.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { rpcErrors } from '@metamask/rpc-errors'; | ||
|
|
||
| import type { MiddlewareScaffold } from './createScaffoldMiddleware'; | ||
| import { createScaffoldMiddleware } from './createScaffoldMiddleware'; | ||
| import { JsonRpcEngineV2 } from './JsonRpcEngineV2'; | ||
| import { makeRequest } from '../../tests/utils'; | ||
|
|
||
| describe('createScaffoldMiddleware', () => { | ||
| it('basic middleware test', async () => { | ||
| const scaffold: MiddlewareScaffold = { | ||
| method1: 'foo', | ||
| method2: () => 42, | ||
| method3: () => { | ||
| throw rpcErrors.internal({ message: 'method3' }); | ||
| }, | ||
| }; | ||
|
|
||
| const engine = JsonRpcEngineV2.create({ | ||
| middleware: [createScaffoldMiddleware(scaffold), () => 'passthrough'], | ||
| }); | ||
|
|
||
| const result1 = await engine.handle(makeRequest({ method: 'method1' })); | ||
| const result2 = await engine.handle(makeRequest({ method: 'method2' })); | ||
| const promise3 = engine.handle(makeRequest({ method: 'method3' })); | ||
| const result4 = await engine.handle(makeRequest({ method: 'unknown' })); | ||
|
|
||
| expect(result1).toBe('foo'); | ||
|
|
||
| expect(result2).toBe(42); | ||
|
|
||
| await expect(promise3).rejects.toThrow( | ||
| rpcErrors.internal({ message: 'method3' }), | ||
| ); | ||
|
|
||
| expect(result4).toBe('passthrough'); | ||
| }); | ||
| }); |
44 changes: 44 additions & 0 deletions
44
packages/json-rpc-engine/src/v2/createScaffoldMiddleware.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils'; | ||
|
|
||
| import type { JsonRpcMiddleware } from './JsonRpcEngineV2'; | ||
|
|
||
| // Only permit primitive values as hard-coded scaffold middleware results. | ||
| type JsonPrimitive = string | number | boolean | null; | ||
|
|
||
| export type ScaffoldMiddlewareHandler< | ||
| Params extends JsonRpcParams, | ||
| Result extends Json, | ||
| > = JsonRpcMiddleware<JsonRpcRequest<Params>, Result> | JsonPrimitive; | ||
|
|
||
| /** | ||
| * A record of RPC method handler functions or hard-coded results, keyed to particular method names. | ||
| * Only primitive JSON values are permitted as hard-coded results. | ||
| */ | ||
| export type MiddlewareScaffold = Record< | ||
| string, | ||
| ScaffoldMiddlewareHandler<JsonRpcParams, Json> | ||
| >; | ||
|
|
||
| /** | ||
| * Creates a middleware function from an object of RPC method handler functions, | ||
| * keyed to particular method names. If a method corresponding to a key of this | ||
| * object is requested, this middleware will pass it to the corresponding | ||
| * handler and return the result. | ||
| * | ||
| * @param handlers - The RPC method handler functions. | ||
| * @returns The scaffold middleware function. | ||
| */ | ||
| export function createScaffoldMiddleware( | ||
| handlers: MiddlewareScaffold, | ||
| ): JsonRpcMiddleware<JsonRpcRequest, Json> { | ||
| return ({ request, context, next }) => { | ||
| const handlerOrResult = handlers[request.method]; | ||
| if (handlerOrResult === undefined) { | ||
| return next(); | ||
| } | ||
|
|
||
| return typeof handlerOrResult === 'function' | ||
| ? handlerOrResult({ request, context, next }) | ||
| : handlerOrResult; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.