File tree Expand file tree Collapse file tree 4 files changed +62
-0
lines changed
Expand file tree Collapse file tree 4 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -189,6 +189,7 @@ export async function installSnap<
189189 onUpdate,
190190 onNameLookup,
191191 onProtocolRequest,
192+ onClientRequest,
192193 mockJsonRpc,
193194 close,
194195 } = await getEnvironment ( ) . installSnap ( ...resolvedOptions ) ;
@@ -209,6 +210,7 @@ export async function installSnap<
209210 onUpdate,
210211 onNameLookup,
211212 onProtocolRequest,
213+ onClientRequest,
212214 mockJsonRpc,
213215 close : async ( ) => {
214216 log ( 'Closing execution service.' ) ;
Original file line number Diff line number Diff line change @@ -761,6 +761,34 @@ describe('helpers', () => {
761761 } ) ;
762762 } ) ;
763763
764+ describe ( 'onClientRequest' , ( ) => {
765+ it ( 'sends a onClientRequest request and returns the result' , async ( ) => {
766+ jest . spyOn ( console , 'log' ) . mockImplementation ( ) ;
767+
768+ const { snapId, close : closeServer } = await getMockServer ( {
769+ sourceCode : `
770+ module.exports.onClientRequest = async ({ request }) => {
771+ return request.method;
772+ };
773+ ` ,
774+ } ) ;
775+
776+ const { onClientRequest, close } = await installSnap ( snapId ) ;
777+ const response = await onClientRequest ( { method : 'foo' } ) ;
778+
779+ expect ( response ) . toStrictEqual (
780+ expect . objectContaining ( {
781+ response : {
782+ result : 'foo' ,
783+ } ,
784+ } ) ,
785+ ) ;
786+
787+ await close ( ) ;
788+ await closeServer ( ) ;
789+ } ) ;
790+ } ) ;
791+
764792 describe ( 'mockJsonRpc' , ( ) => {
765793 it ( 'mocks a JSON-RPC method' , async ( ) => {
766794 jest . spyOn ( console , 'log' ) . mockImplementation ( ) ;
Original file line number Diff line number Diff line change @@ -180,6 +180,14 @@ export type SnapHelpers = {
180180 request : RequestOptions ,
181181 ) : Promise < SnapResponseWithoutInterface > ;
182182
183+ /**
184+ * Send a JSON-RPC client request to the Snap.
185+ *
186+ * @param request - The JSON-RPC request.
187+ * @returns The response promise, with extra {@link SnapRequestObject} fields.
188+ */
189+ onClientRequest ( request : Omit < RequestOptions , 'origin' > ) : SnapRequest ;
190+
183191 /**
184192 * Mock a JSON-RPC request. This will cause the snap to respond with the
185193 * specified response when a request with the specified method is sent.
@@ -491,6 +499,22 @@ export function getHelpers({
491499 return response ;
492500 } ,
493501
502+ // This can't be async because it returns a `SnapRequest`.
503+ // eslint-disable-next-line @typescript-eslint/promise-function-async
504+ onClientRequest : ( request ) => {
505+ log ( 'Sending client request.' ) ;
506+
507+ return handleRequest ( {
508+ snapId,
509+ store,
510+ executionService,
511+ controllerMessenger,
512+ runSaga,
513+ handler : HandlerType . OnClientRequest ,
514+ request,
515+ } ) ;
516+ } ,
517+
494518 mockJsonRpc ( mock : JsonRpcMockOptions ) {
495519 log ( 'Mocking JSON-RPC request %o.' , mock ) ;
496520
Original file line number Diff line number Diff line change @@ -514,6 +514,14 @@ export type Snap = {
514514 request : RequestOptions ,
515515 ) : Promise < SnapResponseWithoutInterface > ;
516516
517+ /**
518+ * Send a JSON-RPC client request to the Snap.
519+ *
520+ * @param request - The JSON-RPC request.
521+ * @returns The response promise, with extra {@link SnapRequestObject} fields.
522+ */
523+ onClientRequest ( request : Omit < RequestOptions , 'origin' > ) : SnapRequest ;
524+
517525 /**
518526 * Mock a JSON-RPC request. This will cause the snap to respond with the
519527 * specified response when a request with the specified method is sent.
You can’t perform that action at this time.
0 commit comments