UNPKG

@metamask/snaps-rpc-methods

Version:
64 lines 2.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createInterfaceHandler = void 0; const rpc_errors_1 = require("@metamask/rpc-errors"); const snaps_sdk_1 = require("@metamask/snaps-sdk"); const superstruct_1 = require("@metamask/superstruct"); const hookNames = { createInterface: true, }; exports.createInterfaceHandler = { methodNames: ['snap_createInterface'], implementation: getCreateInterfaceImplementation, hookNames, }; const CreateInterfaceParametersStruct = (0, superstruct_1.object)({ ui: snaps_sdk_1.ComponentOrElementStruct, context: (0, superstruct_1.optional)(snaps_sdk_1.InterfaceContextStruct), }); /** * The `snap_createInterface` 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.createInterface - The function to create the interface. * @returns Nothing. */ async function getCreateInterfaceImplementation(req, res, _next, end, { createInterface }) { const { params } = req; try { const validatedParams = getValidatedParams(params); const { ui, context } = validatedParams; res.result = await createInterface(ui, context); } catch (error) { return end(error); } return end(); } /** * Validate the createInterface 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 createInterface method parameter object. */ function getValidatedParams(params) { try { return (0, superstruct_1.create)(params, CreateInterfaceParametersStruct); } catch (error) { if (error instanceof superstruct_1.StructError) { throw rpc_errors_1.rpcErrors.invalidParams({ message: `Invalid params: ${error.message}.`, }); } /* istanbul ignore next */ throw rpc_errors_1.rpcErrors.internal(); } } //# sourceMappingURL=createInterface.cjs.map