UNPKG

@metamask/snaps-sdk

Version:

A library containing the core functionality for building MetaMask Snaps

45 lines 1.96 kB
import { assertStruct, isPlainObject } from "@metamask/utils"; /** * A function that returns a function to "build" a {@link Component}. It infers * the type of the component from the given struct, and performs validation on * the created component. * * The returned function can handle the node arguments in two ways: * 1. As a single object, with the keys corresponding to the node's properties, * excluding the `type` property. * 2. As an array of arguments, with the order corresponding to the given keys. * * @param type - The type of the component to build. * @param struct - The struct to use to validate the component. * @param keys - The keys of the component to use as arguments to the builder. * The order of the keys determines the order of the arguments. * @returns A function that builds a component of the given type. * @internal */ export function createBuilder(type, struct, keys = []) { return (...args) => { // Node passed as a single object. if (args.length === 1 && isPlainObject(args[0])) { const node = { ...args[0], type }; // The user could be passing invalid values to the builder, so we need to // validate them as per the component's struct. assertStruct(node, struct, `Invalid ${type} component`); return node; } // Node passed as an array of arguments. const node = keys.reduce((partialNode, key, index) => { if (args[index] !== undefined) { return { ...partialNode, [key]: args[index], }; } return partialNode; }, { type }); // The user could be passing invalid values to the builder, so we need to // validate them as per the component's struct. assertStruct(node, struct, `Invalid ${type} component`); return node; }; } //# sourceMappingURL=builder.mjs.map