@metamask/snaps-sdk
Version:
A library containing the core functionality for building MetaMask Snaps
1 lines • 8.07 kB
Source Map (JSON)
{"version":3,"file":"user-input.cjs","sourceRoot":"","sources":["../../../src/types/handlers/user-input.ts"],"names":[],"mappings":";;;AACA,uDAW+B;AAC/B,2CAAsD;AAItD;;;;;;;;GAQG;AACH,IAAY,kBAKX;AALD,WAAY,kBAAkB;IAC5B,2DAAqC,CAAA;IACrC,yDAAmC,CAAA;IACnC,2DAAqC,CAAA;IACrC,yDAAmC,CAAA;AACrC,CAAC,EALW,kBAAkB,kCAAlB,kBAAkB,QAK7B;AAEY,QAAA,kBAAkB,GAAG,IAAA,oBAAM,EAAC;IACvC,IAAI,EAAE,IAAA,oBAAM,GAAE;IACd,IAAI,EAAE,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC;CACzB,CAAC,CAAC;AAEU,QAAA,sBAAsB,GAAG,IAAA,oBAAM,EAC1C,0BAAkB,EAClB,IAAA,oBAAM,EAAC;IACL,IAAI,EAAE,IAAA,qBAAO,EAAC,kBAAkB,CAAC,gBAAgB,CAAC;IAClD,IAAI,EAAE,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC;CACzB,CAAC,CACH,CAAC;AAYW,QAAA,UAAU,GAAG,IAAA,oBAAM,EAAC;IAC/B,IAAI,EAAE,IAAA,oBAAM,GAAE;IACd,IAAI,EAAE,IAAA,oBAAM,GAAE;IACd,WAAW,EAAE,IAAA,oBAAM,GAAE;IACrB,QAAQ,EAAE,IAAA,oBAAM,GAAE;CACnB,CAAC,CAAC;AAaU,QAAA,wBAAwB,GAAG,IAAA,oBAAM,EAAC;IAC7C,KAAK,EAAE,2BAAmB;IAC1B,IAAI,EAAE,IAAA,oBAAM,GAAE;IACd,MAAM,EAAE,IAAA,oBAAM,GAAE;CACjB,CAAC,CAAC;AAWU,QAAA,qBAAqB,GAAG,IAAA,oBAAM,EACzC,0BAAkB,EAClB,IAAA,oBAAM,EAAC;IACL,IAAI,EAAE,IAAA,qBAAO,EAAC,kBAAkB,CAAC,eAAe,CAAC;IACjD,KAAK,EAAE,IAAA,oBAAM,EACX,IAAA,oBAAM,GAAE,EACR,IAAA,sBAAQ,EACN,IAAA,mBAAK,EAAC,CAAC,IAAA,oBAAM,GAAE,EAAE,kBAAU,EAAE,IAAA,qBAAO,GAAE,EAAE,gCAAwB,CAAC,CAAC,CACnE,CACF;IACD,IAAI,EAAE,IAAA,oBAAM,GAAE;CACf,CAAC,CACH,CAAC;AAiBW,QAAA,sBAAsB,GAAG,IAAA,oBAAM,EAC1C,0BAAkB,EAClB,IAAA,oBAAM,EAAC;IACL,IAAI,EAAE,IAAA,qBAAO,EAAC,kBAAkB,CAAC,gBAAgB,CAAC;IAClD,IAAI,EAAE,IAAA,oBAAM,GAAE;IACd,KAAK,EAAE,IAAA,mBAAK,EAAC,CAAC,IAAA,oBAAM,GAAE,EAAE,IAAA,qBAAO,GAAE,EAAE,gCAAwB,CAAC,CAAC;CAC9D,CAAC,CACH,CAAC;AAYW,QAAA,qBAAqB,GAAG,IAAA,oBAAM,EACzC,0BAAkB,EAClB,IAAA,oBAAM,EAAC;IACL,IAAI,EAAE,IAAA,qBAAO,EAAC,kBAAkB,CAAC,eAAe,CAAC;IACjD,IAAI,EAAE,IAAA,oBAAM,GAAE;IACd,IAAI,EAAE,IAAA,sBAAQ,EAAC,kBAAU,CAAC;CAC3B,CAAC,CACH,CAAC;AAcW,QAAA,oBAAoB,GAAG,IAAA,mBAAK,EAAC;IACxC,8BAAsB;IACtB,6BAAqB;IACrB,8BAAsB;IACtB,6BAAqB;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"]}