UNPKG

@metamask/snaps-sdk

Version:

A library containing the core functionality for building MetaMask Snaps

1 lines 8.05 kB
{"version":3,"file":"user-input.mjs","sourceRoot":"","sources":["../../../src/types/handlers/user-input.ts"],"names":[],"mappings":"AACA,OAAO,EACL,MAAM,EACN,MAAM,EACN,OAAO,EACP,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,MAAM,EACN,MAAM,EACN,KAAK,EACL,OAAO,EACR,8BAA8B;AAC/B,OAAO,EAAE,mBAAmB,EAAE,wBAAwB;AAItD;;;;;;;;GAQG;AACH,MAAM,CAAN,IAAY,kBAKX;AALD,WAAY,kBAAkB;IAC5B,2DAAqC,CAAA;IACrC,yDAAmC,CAAA;IACnC,2DAAqC,CAAA;IACrC,yDAAmC,CAAA;AACrC,CAAC,EALW,kBAAkB,KAAlB,kBAAkB,QAK7B;AAED,MAAM,CAAC,MAAM,kBAAkB,GAAG,MAAM,CAAC;IACvC,IAAI,EAAE,MAAM,EAAE;IACd,IAAI,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC;CACzB,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAC1C,kBAAkB,EAClB,MAAM,CAAC;IACL,IAAI,EAAE,OAAO,CAAC,kBAAkB,CAAC,gBAAgB,CAAC;IAClD,IAAI,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC;CACzB,CAAC,CACH,CAAC;AAYF,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC;IAC/B,IAAI,EAAE,MAAM,EAAE;IACd,IAAI,EAAE,MAAM,EAAE;IACd,WAAW,EAAE,MAAM,EAAE;IACrB,QAAQ,EAAE,MAAM,EAAE;CACnB,CAAC,CAAC;AAaH,MAAM,CAAC,MAAM,wBAAwB,GAAG,MAAM,CAAC;IAC7C,KAAK,EAAE,mBAAmB;IAC1B,IAAI,EAAE,MAAM,EAAE;IACd,MAAM,EAAE,MAAM,EAAE;CACjB,CAAC,CAAC;AAWH,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CACzC,kBAAkB,EAClB,MAAM,CAAC;IACL,IAAI,EAAE,OAAO,CAAC,kBAAkB,CAAC,eAAe,CAAC;IACjD,KAAK,EAAE,MAAM,CACX,MAAM,EAAE,EACR,QAAQ,CACN,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,UAAU,EAAE,OAAO,EAAE,EAAE,wBAAwB,CAAC,CAAC,CACnE,CACF;IACD,IAAI,EAAE,MAAM,EAAE;CACf,CAAC,CACH,CAAC;AAiBF,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAC1C,kBAAkB,EAClB,MAAM,CAAC;IACL,IAAI,EAAE,OAAO,CAAC,kBAAkB,CAAC,gBAAgB,CAAC;IAClD,IAAI,EAAE,MAAM,EAAE;IACd,KAAK,EAAE,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,OAAO,EAAE,EAAE,wBAAwB,CAAC,CAAC;CAC9D,CAAC,CACH,CAAC;AAYF,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CACzC,kBAAkB,EAClB,MAAM,CAAC;IACL,IAAI,EAAE,OAAO,CAAC,kBAAkB,CAAC,eAAe,CAAC;IACjD,IAAI,EAAE,MAAM,EAAE;IACd,IAAI,EAAE,QAAQ,CAAC,UAAU,CAAC;CAC3B,CAAC,CACH,CAAC;AAcF,MAAM,CAAC,MAAM,oBAAoB,GAAG,KAAK,CAAC;IACxC,sBAAsB;IACtB,qBAAqB;IACrB,sBAAsB;IACtB,qBAAqB;CACtB,CAAC,CAAC","sourcesContent":["import type { Infer } from '@metamask/superstruct';\nimport {\n number,\n assign,\n literal,\n nullable,\n object,\n optional,\n record,\n string,\n union,\n boolean,\n} from '@metamask/superstruct';\nimport { CaipAssetTypeStruct } from '@metamask/utils';\n\nimport type { InterfaceContext } from '../interface';\n\n/**\n * The type of user input event fired.\n * Currently only three events are supported:\n *\n * - `ButtonClickEvent` - A button has been clicked in the UI.\n * - `FormSubmitEvent` - A Form has been submitted in the UI.\n * - `InputChangeEvent` - The value of an input field has changed in the UI.\n * - `FileUploadEvent` - A file has been uploaded in the UI.\n */\nexport enum UserInputEventType {\n ButtonClickEvent = 'ButtonClickEvent',\n FormSubmitEvent = 'FormSubmitEvent',\n InputChangeEvent = 'InputChangeEvent',\n FileUploadEvent = 'FileUploadEvent',\n}\n\nexport const GenericEventStruct = object({\n type: string(),\n name: optional(string()),\n});\n\nexport const ButtonClickEventStruct = assign(\n GenericEventStruct,\n object({\n type: literal(UserInputEventType.ButtonClickEvent),\n name: optional(string()),\n }),\n);\n\n/**\n * A button click event fired in the UI. This is passed to the params of the\n * `onUserInput` handler.\n *\n * @property type - The type of event fired. See {@link UserInputEventType} for\n * the different types. This is always `ButtonClickEvent`.\n * @property name - The optional component name that fired the event.\n */\nexport type ButtonClickEvent = Infer<typeof ButtonClickEventStruct>;\n\nexport const FileStruct = object({\n name: string(),\n size: number(),\n contentType: string(),\n contents: string(),\n});\n\n/**\n * A file object containing the file name, size, content type, and the base64\n * encoded contents of the file.\n *\n * @property name - The name of the file.\n * @property size - The size of the file in bytes.\n * @property contentType - The content type of the file.\n * @property contents - The base64 encoded contents of the file.\n */\nexport type File = Infer<typeof FileStruct>;\n\nexport const AssetSelectorStateStruct = object({\n asset: CaipAssetTypeStruct,\n name: string(),\n symbol: string(),\n});\n\n/**\n * The state of the asset selector component.\n *\n * @property asset - The CAIP-19 asset ID.\n * @property name - The name of the asset.\n * @property symbol - The symbol of the asset.\n */\nexport type AssetSelectorState = Infer<typeof AssetSelectorStateStruct>;\n\nexport const FormSubmitEventStruct = assign(\n GenericEventStruct,\n object({\n type: literal(UserInputEventType.FormSubmitEvent),\n value: record(\n string(),\n nullable(\n union([string(), FileStruct, boolean(), AssetSelectorStateStruct]),\n ),\n ),\n name: string(),\n }),\n);\n\n/**\n * A form submit event, which is fired when a submit button is clicked.\n *\n * @property type - The type of event fired. This is always `FormSubmitEvent`.\n * @property name - The name of the form that was submitted.\n * @property value - The form values submitted as an object. The keys are the\n * names of the form fields and the values are the values of the form fields. If\n * a form field is empty, the value is `null` or an empty string.\n * @property files - The files uploaded in the form. The keys are the names of\n * the file input fields and the values are the file objects containing the file\n * name, size, content type, and the base64 encoded contents of the file. See\n * {@link File}.\n */\nexport type FormSubmitEvent = Infer<typeof FormSubmitEventStruct>;\n\nexport const InputChangeEventStruct = assign(\n GenericEventStruct,\n object({\n type: literal(UserInputEventType.InputChangeEvent),\n name: string(),\n value: union([string(), boolean(), AssetSelectorStateStruct]),\n }),\n);\n\n/**\n * An input change event, which is fired when the value of an input field\n * changes.\n *\n * @property type - The type of event fired. This is always `InputChangeEvent`.\n * @property name - The name of the input field that changed.\n * @property value - The new value of the input field.\n */\nexport type InputChangeEvent = Infer<typeof InputChangeEventStruct>;\n\nexport const FileUploadEventStruct = assign(\n GenericEventStruct,\n object({\n type: literal(UserInputEventType.FileUploadEvent),\n name: string(),\n file: nullable(FileStruct),\n }),\n);\n\n/**\n * A file upload event, which is fired when a file is uploaded.\n *\n * @property type - The type of event fired. This is always `FileUploadEvent`.\n * @property name - The name of the file input field that was used to upload the\n * file.\n * @property file - The file object containing the file name, size,\n * content type, and the base64 encoded contents of the file.\n * @see File\n */\nexport type FileUploadEvent = Infer<typeof FileUploadEventStruct>;\n\nexport const UserInputEventStruct = union([\n ButtonClickEventStruct,\n FormSubmitEventStruct,\n InputChangeEventStruct,\n FileUploadEventStruct,\n]);\n\n/**\n * A user input event fired in the UI. This is passed to the params of the `onUserInput` handler.\n *\n * @property type - The type of event fired. See {@link UserInputEventType} for the different types.\n * @property name - The component name that fired the event. It is optional for\n * an {@link UserInputEventType.ButtonClickEvent}.\n * @property value - The value associated with the event. Only available when an\n * {@link UserInputEventType.FormSubmitEvent} is fired. It contains the form values submitted.\n */\nexport type UserInputEvent =\n | ButtonClickEvent\n | FormSubmitEvent\n | InputChangeEvent\n | FileUploadEvent;\n\n/**\n * The `onUserInput` handler. This is called when an user input event is fired in the UI.\n *\n * @param args - The user input event.\n * @param args.id - The user interface id.\n * @param args.event - The {@link UserInputEvent} object, containing the data about the fired event.\n */\nexport type OnUserInputHandler = (args: {\n id: string;\n event: UserInputEvent;\n context: InterfaceContext | null;\n}) => Promise<void>;\n"]}