@metamask/snaps-sdk
Version:
A library containing the core functionality for building MetaMask Snaps
75 lines • 2.66 kB
JavaScript
import { number, assign, nullable, object, optional, record, string, union, boolean, array } from "@metamask/superstruct";
import { CaipAssetTypeStruct, CaipAccountIdStruct } from "@metamask/utils";
import { typedUnion, literal } from "../../internals/index.mjs";
/**
* The type of user input event fired.
* Currently only three events are supported:
*
* - `ButtonClickEvent` - A button has been clicked in the UI.
* - `FormSubmitEvent` - A Form has been submitted in the UI.
* - `InputChangeEvent` - The value of an input field has changed in the UI.
* - `FileUploadEvent` - A file has been uploaded in the UI.
*/
export var UserInputEventType;
(function (UserInputEventType) {
UserInputEventType["ButtonClickEvent"] = "ButtonClickEvent";
UserInputEventType["FormSubmitEvent"] = "FormSubmitEvent";
UserInputEventType["InputChangeEvent"] = "InputChangeEvent";
UserInputEventType["FileUploadEvent"] = "FileUploadEvent";
})(UserInputEventType || (UserInputEventType = {}));
export const GenericEventStruct = object({
type: string(),
name: optional(string()),
});
export const ButtonClickEventStruct = assign(GenericEventStruct, object({
type: literal(UserInputEventType.ButtonClickEvent),
name: optional(string()),
}));
export const AccountSelectorStateStruct = object({
accountId: string(),
addresses: array(CaipAccountIdStruct),
});
export const FileStruct = object({
name: string(),
size: number(),
contentType: string(),
contents: string(),
});
export const AssetSelectorStateStruct = object({
asset: CaipAssetTypeStruct,
name: string(),
symbol: string(),
});
export const FormSubmitEventStruct = assign(GenericEventStruct, object({
type: literal(UserInputEventType.FormSubmitEvent),
value: record(string(), nullable(union([
string(),
FileStruct,
boolean(),
AccountSelectorStateStruct,
AssetSelectorStateStruct,
]))),
name: string(),
}));
export const InputChangeEventStruct = assign(GenericEventStruct, object({
type: literal(UserInputEventType.InputChangeEvent),
name: string(),
value: nullable(union([
string(),
boolean(),
AccountSelectorStateStruct,
AssetSelectorStateStruct,
])),
}));
export const FileUploadEventStruct = assign(GenericEventStruct, object({
type: literal(UserInputEventType.FileUploadEvent),
name: string(),
file: nullable(FileStruct),
}));
export const UserInputEventStruct = typedUnion([
ButtonClickEventStruct,
FormSubmitEventStruct,
InputChangeEventStruct,
FileUploadEventStruct,
]);
//# sourceMappingURL=user-input.mjs.map