@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
66 lines • 2.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateInterfaceHandler = 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 = {
updateInterface: true,
};
exports.updateInterfaceHandler = {
methodNames: ['snap_updateInterface'],
implementation: getUpdateInterfaceImplementation,
hookNames,
};
const UpdateInterfaceParametersStruct = (0, superstruct_1.object)({
id: (0, superstruct_1.string)(),
ui: snaps_sdk_1.ComponentOrElementStruct,
context: (0, superstruct_1.optional)(snaps_sdk_1.InterfaceContextStruct),
});
/**
* The `snap_updateInterface` 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.updateInterface - The function to update the interface.
* @returns Nothing.
*/
async function getUpdateInterfaceImplementation(req, res, _next, end, { updateInterface }) {
const { params } = req;
try {
const validatedParams = getValidatedParams(params);
const { id, ui, context } = validatedParams;
await updateInterface(id, ui, context);
res.result = null;
}
catch (error) {
return end(error);
}
return end();
}
/**
* Validate the updateInterface 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 updateInterface method parameter object.
*/
function getValidatedParams(params) {
try {
return (0, superstruct_1.create)(params, UpdateInterfaceParametersStruct);
}
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=updateInterface.cjs.map