UNPKG

@metamask/snaps-rpc-methods

Version:
59 lines 1.94 kB
import { rpcErrors } from "@metamask/rpc-errors"; import { StructError, create, object, string } from "@metamask/superstruct"; const hookNames = { getInterfaceContext: true, }; export const getInterfaceContextHandler = { methodNames: ['snap_getInterfaceContext'], implementation: getInterfaceContextImplementation, hookNames, }; const GetInterfaceContextParametersStruct = object({ id: string(), }); /** * The `snap_getInterfaceContext` method implementation. * * @param req - The JSON-RPC request object. * @param res - The JSON-RPC response object. * @param _next - The `json-rpc-engine` "next" callback. Not used by this * function. * @param end - The `json-rpc-engine` "end" callback. * @param hooks - The RPC method hooks. * @param hooks.getInterfaceContext - The function to get the interface context. * @returns Noting. */ function getInterfaceContextImplementation(req, res, _next, end, { getInterfaceContext }) { const { params } = req; try { const validatedParams = getValidatedParams(params); const { id } = validatedParams; res.result = getInterfaceContext(id); } catch (error) { return end(error); } return end(); } /** * Validate the getInterfaceContext method `params` and returns them cast to the correct * type. Throws if validation fails. * * @param params - The unvalidated params object from the method request. * @returns The validated getInterfaceContext method parameter object. */ function getValidatedParams(params) { try { return create(params, GetInterfaceContextParametersStruct); } catch (error) { if (error instanceof StructError) { throw rpcErrors.invalidParams({ message: `Invalid params: ${error.message}.`, }); } /* istanbul ignore next */ throw rpcErrors.internal(); } } //# sourceMappingURL=getInterfaceContext.mjs.map