@metamask/snaps-sdk 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/CHANGELOG.md +18 -1
  2. package/dist/cjs/images.js +54 -0
  3. package/dist/cjs/images.js.map +1 -0
  4. package/dist/cjs/index.js +14 -0
  5. package/dist/cjs/index.js.map +1 -1
  6. package/dist/cjs/internals/index.js +1 -0
  7. package/dist/cjs/internals/index.js.map +1 -1
  8. package/dist/cjs/internals/structs.js +43 -0
  9. package/dist/cjs/internals/structs.js.map +1 -0
  10. package/dist/cjs/ui/components/address.js +30 -0
  11. package/dist/cjs/ui/components/address.js.map +1 -0
  12. package/dist/cjs/ui/components/index.js +2 -0
  13. package/dist/cjs/ui/components/index.js.map +1 -1
  14. package/dist/cjs/ui/components/panel.js +5 -1
  15. package/dist/cjs/ui/components/panel.js.map +1 -1
  16. package/dist/cjs/ui/components/row.js +57 -0
  17. package/dist/cjs/ui/components/row.js.map +1 -0
  18. package/dist/cjs/ui/nodes.js +2 -0
  19. package/dist/cjs/ui/nodes.js.map +1 -1
  20. package/dist/esm/images.js +84 -0
  21. package/dist/esm/images.js.map +1 -0
  22. package/dist/esm/index.js +3 -1
  23. package/dist/esm/index.js.map +1 -1
  24. package/dist/esm/internals/index.js +1 -0
  25. package/dist/esm/internals/index.js.map +1 -1
  26. package/dist/esm/internals/structs.js +59 -0
  27. package/dist/esm/internals/structs.js.map +1 -0
  28. package/dist/esm/ui/components/address.js +22 -0
  29. package/dist/esm/ui/components/address.js.map +1 -0
  30. package/dist/esm/ui/components/index.js +2 -0
  31. package/dist/esm/ui/components/index.js.map +1 -1
  32. package/dist/esm/ui/components/panel.js +5 -1
  33. package/dist/esm/ui/components/panel.js.map +1 -1
  34. package/dist/esm/ui/components/row.js +50 -0
  35. package/dist/esm/ui/components/row.js.map +1 -0
  36. package/dist/esm/ui/nodes.js +2 -0
  37. package/dist/esm/ui/nodes.js.map +1 -1
  38. package/dist/types/images.d.ts +69 -0
  39. package/dist/types/index.d.ts +3 -1
  40. package/dist/types/internals/index.d.ts +1 -0
  41. package/dist/types/internals/structs.d.ts +50 -0
  42. package/dist/types/ui/components/address.d.ts +34 -0
  43. package/dist/types/ui/components/index.d.ts +2 -0
  44. package/dist/types/ui/components/panel.d.ts +90 -0
  45. package/dist/types/ui/components/row.d.ts +104 -0
  46. package/dist/types/ui/nodes.d.ts +3 -1
  47. package/package.json +6 -4
package/CHANGELOG.md CHANGED
@@ -6,6 +6,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.3.0]
10
+ ### Added
11
+ - Add image fetching utility functions ([#1995](https://github.com/MetaMask/snaps/pull/1995))
12
+ - This adds two functions:
13
+ - `getImageComponent` to get an `image` component from a PNG or JPEG URL.
14
+ - `getImageData` to get a base64 data string, which can be embedded in an SVG image.
15
+
16
+ ## [1.2.0]
17
+ ### Added
18
+ - Add `row` and `address` component ([#1968](https://github.com/MetaMask/snaps/pull/1968))
19
+ - Add `enumValue`, `literal` and `union` from `snaps-utils` ([#1968](https://github.com/MetaMask/snaps/pull/1968))
20
+
21
+ ### Changed
22
+ - Bump several MetaMask dependencies ([#1964](https://github.com/MetaMask/snaps/pull/1964))
23
+
9
24
  ## [1.1.0]
10
25
  ### Added
11
26
  - Add Snap error wrappers of JSON-RPC errors ([#1924](https://github.com/MetaMask/snaps/pull/1924))
@@ -14,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
14
29
  ### Added
15
30
  - Initial release of this package.
16
31
 
17
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@1.1.0...HEAD
32
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@1.3.0...HEAD
33
+ [1.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@1.2.0...@metamask/snaps-sdk@1.3.0
34
+ [1.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@1.1.0...@metamask/snaps-sdk@1.2.0
18
35
  [1.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@1.0.0...@metamask/snaps-sdk@1.1.0
19
36
  [1.0.0]: https://github.com/MetaMask/snaps/releases/tag/@metamask/snaps-sdk@1.0.0
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ getImageData: function() {
13
+ return getImageData;
14
+ },
15
+ getImageComponent: function() {
16
+ return getImageComponent;
17
+ }
18
+ });
19
+ const _utils = require("@metamask/utils");
20
+ const _ui = require("./ui");
21
+ /**
22
+ * Get raw image data from a URL.
23
+ *
24
+ * @param url - The URL to get the image data from.
25
+ * @param options - The options to use when fetching the image data. This is
26
+ * passed directly to `fetch`.
27
+ * @returns A promise that resolves to the image data as a blob.
28
+ */ async function getRawImageData(url, options) {
29
+ if (typeof fetch !== 'function') {
30
+ throw new Error(`Failed to fetch image data from "${url}": Using this function requires the "endowment:network-access" permission.`);
31
+ }
32
+ return fetch(url, options).then(async (response)=>{
33
+ if (!response.ok) {
34
+ throw new Error(`Failed to fetch image data from "${url}": ${response.status} ${response.statusText}`);
35
+ }
36
+ const blob = await response.blob();
37
+ (0, _utils.assert)(blob.type === 'image/jpeg' || blob.type === 'image/png', 'Expected image data to be a JPEG or PNG image.');
38
+ return blob;
39
+ });
40
+ }
41
+ async function getImageData(url, options) {
42
+ const blob = await getRawImageData(url, options);
43
+ const bytes = new Uint8Array(await blob.arrayBuffer());
44
+ return `data:${blob.type};base64,${(0, _utils.bytesToBase64)(bytes)}`;
45
+ }
46
+ async function getImageComponent(url, { width, height = width, request }) {
47
+ (0, _utils.assert)(typeof width === 'number' && width > 0, 'Expected width to be a number greater than 0.');
48
+ (0, _utils.assert)(typeof height === 'number' && height > 0, 'Expected height to be a number greater than 0.');
49
+ const imageData = await getImageData(url, request);
50
+ const size = `width="${width}" height="${height}"`;
51
+ return (0, _ui.image)(`<svg ${size.trim()} xmlns="http://www.w3.org/2000/svg"><image ${size.trim()} href="${imageData}" /></svg>`);
52
+ }
53
+
54
+ //# sourceMappingURL=images.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/images.ts"],"sourcesContent":["import { assert, bytesToBase64 } from '@metamask/utils';\n\nimport { image } from './ui';\n\n/**\n * Get raw image data from a URL.\n *\n * @param url - The URL to get the image data from.\n * @param options - The options to use when fetching the image data. This is\n * passed directly to `fetch`.\n * @returns A promise that resolves to the image data as a blob.\n */\nasync function getRawImageData(url: string, options?: RequestInit) {\n if (typeof fetch !== 'function') {\n throw new Error(\n `Failed to fetch image data from \"${url}\": Using this function requires the \"endowment:network-access\" permission.`,\n );\n }\n\n return fetch(url, options).then(async (response) => {\n if (!response.ok) {\n throw new Error(\n `Failed to fetch image data from \"${url}\": ${response.status} ${response.statusText}`,\n );\n }\n\n const blob = await response.blob();\n assert(\n blob.type === 'image/jpeg' || blob.type === 'image/png',\n 'Expected image data to be a JPEG or PNG image.',\n );\n\n return blob;\n });\n}\n\n/**\n * Get image data as data-string from a URL. This is useful for embedding images\n * inside of SVGs. Only JPEG and PNG images are supported.\n *\n * Note: This function uses `fetch` to get the image data. This means that using\n * it requires the `endowment:network-access` permission.\n *\n * @example\n * const imageData = await getImageData('https://cataas.com/cat');\n * const svg = `\n * <svg width=\"100\" height=\"100\" xmlns=\"http://www.w3.org/2000/svg\">\n * <image href=\"${imageData}\" />\n * </svg>\n * `;\n *\n * // Render the SVG in a Snap UI.\n * const ui = image(svg);\n * @param url - The URL to get the image data from.\n * @param options - The options to use when fetching the image data. This is\n * passed directly to `fetch`.\n * @returns A promise that resolves to the image data as a data-string.\n */\nexport async function getImageData(url: string, options?: RequestInit) {\n const blob = await getRawImageData(url, options);\n const bytes = new Uint8Array(await blob.arrayBuffer());\n\n return `data:${blob.type};base64,${bytesToBase64(bytes)}`;\n}\n\n/**\n * Options for getting an SVG image element from a URL.\n *\n * @property width - The width of the image.\n * @property height - The height of the image. If this is not provided, the\n * width will be used as the height.\n * @property request - The options to use when fetching the image data. This is\n * passed directly to `fetch`.\n */\nexport type ImageOptions = {\n width: number;\n height?: number;\n request?: RequestInit;\n};\n\n/**\n * Get an image component from a URL. This is useful for embedding images inside\n * Snap UIs. Only JPEG and PNG images are supported.\n *\n * Note: This function uses `fetch` to get the image data. This means that using\n * it requires the `endowment:network-access` permission.\n *\n * @example\n * const component = await getImage('https://cataas.com/cat');\n *\n * return await snap.request({\n * method: 'snap_dialog',\n * params: {\n * type: 'alert',\n * content: panel([\n * component,\n * ]),\n * },\n * });\n * @param url - The URL to get the image data from.\n * @param options - The options to use when fetching and rendering the image.\n * @param options.width - The width of the image.\n * @param options.height - The height of the image. If this is not provided, the\n * width will be used as the height.\n * @param options.request - The options to use when fetching the image data.\n * This is passed directly to `fetch`.\n * @returns A promise that resolves to the image data as an image component.\n */\nexport async function getImageComponent(\n url: string,\n { width, height = width, request }: ImageOptions,\n) {\n assert(\n typeof width === 'number' && width > 0,\n 'Expected width to be a number greater than 0.',\n );\n\n assert(\n typeof height === 'number' && height > 0,\n 'Expected height to be a number greater than 0.',\n );\n\n const imageData = await getImageData(url, request);\n const size = `width=\"${width}\" height=\"${height}\"`;\n\n return image(\n `<svg ${size.trim()} xmlns=\"http://www.w3.org/2000/svg\"><image ${size.trim()} href=\"${imageData}\" /></svg>`,\n );\n}\n"],"names":["getImageData","getImageComponent","getRawImageData","url","options","fetch","Error","then","response","ok","status","statusText","blob","assert","type","bytes","Uint8Array","arrayBuffer","bytesToBase64","width","height","request","imageData","size","image","trim"],"mappings":";;;;;;;;;;;IA0DsBA,YAAY;eAAZA;;IAkDAC,iBAAiB;eAAjBA;;;uBA5GgB;oBAEhB;AAEtB;;;;;;;CAOC,GACD,eAAeC,gBAAgBC,GAAW,EAAEC,OAAqB;IAC/D,IAAI,OAAOC,UAAU,YAAY;QAC/B,MAAM,IAAIC,MACR,CAAC,iCAAiC,EAAEH,IAAI,0EAA0E,CAAC;IAEvH;IAEA,OAAOE,MAAMF,KAAKC,SAASG,IAAI,CAAC,OAAOC;QACrC,IAAI,CAACA,SAASC,EAAE,EAAE;YAChB,MAAM,IAAIH,MACR,CAAC,iCAAiC,EAAEH,IAAI,GAAG,EAAEK,SAASE,MAAM,CAAC,CAAC,EAAEF,SAASG,UAAU,CAAC,CAAC;QAEzF;QAEA,MAAMC,OAAO,MAAMJ,SAASI,IAAI;QAChCC,IAAAA,aAAM,EACJD,KAAKE,IAAI,KAAK,gBAAgBF,KAAKE,IAAI,KAAK,aAC5C;QAGF,OAAOF;IACT;AACF;AAwBO,eAAeZ,aAAaG,GAAW,EAAEC,OAAqB;IACnE,MAAMQ,OAAO,MAAMV,gBAAgBC,KAAKC;IACxC,MAAMW,QAAQ,IAAIC,WAAW,MAAMJ,KAAKK,WAAW;IAEnD,OAAO,CAAC,KAAK,EAAEL,KAAKE,IAAI,CAAC,QAAQ,EAAEI,IAAAA,oBAAa,EAACH,OAAO,CAAC;AAC3D;AA6CO,eAAed,kBACpBE,GAAW,EACX,EAAEgB,KAAK,EAAEC,SAASD,KAAK,EAAEE,OAAO,EAAgB;IAEhDR,IAAAA,aAAM,EACJ,OAAOM,UAAU,YAAYA,QAAQ,GACrC;IAGFN,IAAAA,aAAM,EACJ,OAAOO,WAAW,YAAYA,SAAS,GACvC;IAGF,MAAME,YAAY,MAAMtB,aAAaG,KAAKkB;IAC1C,MAAME,OAAO,CAAC,OAAO,EAAEJ,MAAM,UAAU,EAAEC,OAAO,CAAC,CAAC;IAElD,OAAOI,IAAAA,SAAK,EACV,CAAC,KAAK,EAAED,KAAKE,IAAI,GAAG,2CAA2C,EAAEF,KAAKE,IAAI,GAAG,OAAO,EAAEH,UAAU,UAAU,CAAC;AAE/G"}
package/dist/cjs/index.js CHANGED
@@ -24,10 +24,24 @@ _export(exports, {
24
24
  },
25
25
  SNAP_ERROR_MESSAGE: function() {
26
26
  return _internals.SNAP_ERROR_MESSAGE;
27
+ },
28
+ literal: function() {
29
+ return _internals.literal;
30
+ },
31
+ union: function() {
32
+ return _internals.union;
33
+ },
34
+ enumValue: function() {
35
+ return _internals.enumValue;
36
+ },
37
+ assert: function() {
38
+ return _utils.assert;
27
39
  }
28
40
  });
29
41
  const _internals = require("./internals");
42
+ const _utils = require("@metamask/utils");
30
43
  _export_star(require("./errors"), exports);
44
+ _export_star(require("./images"), exports);
31
45
  _export_star(require("./types"), exports);
32
46
  _export_star(require("./ui"), exports);
33
47
  function _export_star(from, to) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["// Only internals that are used by other Snaps packages should be exported here.\nexport type { EnumToUnion } from './internals';\nexport {\n getErrorData,\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from './internals';\n\n// Re-exported from `@metamask/utils` for convenience.\nexport type {\n Json,\n JsonRpcError,\n JsonRpcRequest,\n JsonRpcParams,\n} from '@metamask/utils';\n\nexport * from './errors';\nexport * from './types';\nexport * from './ui';\n"],"names":["getErrorData","getErrorMessage","getErrorStack","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE"],"mappings":"AAAA,gFAAgF;;;;;;;;;;;;IAG9EA,YAAY;eAAZA,uBAAY;;IACZC,eAAe;eAAfA,0BAAe;;IACfC,aAAa;eAAbA,wBAAa;;IACbC,eAAe;eAAfA,0BAAe;;IACfC,kBAAkB;eAAlBA,6BAAkB;;;2BACb;qBAUO;qBACA;qBACA"}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["// Only internals that are used by other Snaps packages should be exported here.\nexport type { EnumToUnion } from './internals';\nexport {\n getErrorData,\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n literal,\n union,\n enumValue,\n} from './internals';\n\n// Re-exported from `@metamask/utils` for convenience.\nexport type {\n Json,\n JsonRpcError,\n JsonRpcRequest,\n JsonRpcParams,\n} from '@metamask/utils';\nexport { assert } from '@metamask/utils';\n\nexport * from './errors';\nexport * from './images';\nexport * from './types';\nexport * from './ui';\n"],"names":["getErrorData","getErrorMessage","getErrorStack","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE","literal","union","enumValue","assert"],"mappings":"AAAA,gFAAgF;;;;;;;;;;;;IAG9EA,YAAY;eAAZA,uBAAY;;IACZC,eAAe;eAAfA,0BAAe;;IACfC,aAAa;eAAbA,wBAAa;;IACbC,eAAe;eAAfA,0BAAe;;IACfC,kBAAkB;eAAlBA,6BAAkB;;IAClBC,OAAO;eAAPA,kBAAO;;IACPC,KAAK;eAALA,gBAAK;;IACLC,SAAS;eAATA,oBAAS;;IAUFC,MAAM;eAANA,aAAM;;;2BATR;uBASgB;qBAET;qBACA;qBACA;qBACA"}
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  _export_star(require("./error-wrappers"), exports);
6
6
  _export_star(require("./errors"), exports);
7
7
  _export_star(require("./helpers"), exports);
8
+ _export_star(require("./structs"), exports);
8
9
  function _export_star(from, to) {
9
10
  Object.keys(from).forEach(function(k) {
10
11
  if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/internals/index.ts"],"sourcesContent":["export * from './error-wrappers';\nexport * from './errors';\nexport * from './helpers';\n"],"names":[],"mappings":";;;;qBAAc;qBACA;qBACA"}
1
+ {"version":3,"sources":["../../../src/internals/index.ts"],"sourcesContent":["export * from './error-wrappers';\nexport * from './errors';\nexport * from './helpers';\nexport * from './structs';\n"],"names":[],"mappings":";;;;qBAAc;qBACA;qBACA;qBACA"}
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ literal: function() {
13
+ return literal;
14
+ },
15
+ union: function() {
16
+ return union;
17
+ },
18
+ enumValue: function() {
19
+ return enumValue;
20
+ }
21
+ });
22
+ const _superstruct = require("superstruct");
23
+ function literal(value) {
24
+ return (0, _superstruct.define)(JSON.stringify(value), (0, _superstruct.literal)(value).validator);
25
+ }
26
+ function union([head, ...tail]) {
27
+ const struct = (0, _superstruct.union)([
28
+ head,
29
+ ...tail
30
+ ]);
31
+ return new _superstruct.Struct({
32
+ ...struct,
33
+ schema: [
34
+ head,
35
+ ...tail
36
+ ]
37
+ });
38
+ }
39
+ function enumValue(constant) {
40
+ return literal(constant);
41
+ }
42
+
43
+ //# sourceMappingURL=structs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/internals/structs.ts"],"sourcesContent":["import type { Infer } from 'superstruct';\nimport {\n Struct,\n define,\n literal as superstructLiteral,\n union as superstructUnion,\n} from 'superstruct';\nimport type { AnyStruct, InferStructTuple } from 'superstruct/dist/utils';\n\nimport type { EnumToUnion } from './helpers';\n\n/**\n * A wrapper of `superstruct`'s `literal` struct that also defines the name of\n * the struct as the literal value.\n *\n * This is useful for improving the error messages returned by `superstruct`.\n * For example, instead of returning an error like:\n *\n * ```\n * Expected the value to satisfy a union of `literal | literal`, but received: \\\"baz\\\"\n * ```\n *\n * This struct will return an error like:\n *\n * ```\n * Expected the value to satisfy a union of `\"foo\" | \"bar\"`, but received: \\\"baz\\\"\n * ```\n *\n * @param value - The literal value.\n * @returns The `superstruct` struct, which validates that the value is equal\n * to the literal value.\n */\nexport function literal<Type extends string | number | boolean>(value: Type) {\n return define<Type>(\n JSON.stringify(value),\n superstructLiteral(value).validator,\n );\n}\n\n/**\n * A wrapper of `superstruct`'s `union` struct that also defines the schema as\n * the union of the schemas of the structs.\n *\n * This is useful for improving the error messages returned by `superstruct`.\n *\n * @param structs - The structs to union.\n * @param structs.\"0\" - The first struct.\n * @param structs.\"1\" - The remaining structs.\n * @returns The `superstruct` struct, which validates that the value satisfies\n * one of the structs.\n */\nexport function union<Head extends AnyStruct, Tail extends AnyStruct[]>([\n head,\n ...tail\n]: [head: Head, ...tail: Tail]): Struct<\n Infer<Head> | InferStructTuple<Tail>[number],\n [head: Head, ...tail: Tail]\n> {\n const struct = superstructUnion([head, ...tail]);\n\n return new Struct({\n ...struct,\n schema: [head, ...tail],\n });\n}\n\n/**\n * Superstruct struct for validating an enum value. This allows using both the\n * enum string values and the enum itself as values.\n *\n * @param constant - The enum to validate against.\n * @returns The superstruct struct.\n */\nexport function enumValue<Type extends string>(\n constant: Type,\n): Struct<EnumToUnion<Type>, null> {\n return literal(constant as EnumToUnion<Type>);\n}\n"],"names":["literal","union","enumValue","value","define","JSON","stringify","superstructLiteral","validator","head","tail","struct","superstructUnion","Struct","schema","constant"],"mappings":";;;;;;;;;;;IAgCgBA,OAAO;eAAPA;;IAmBAC,KAAK;eAALA;;IAsBAC,SAAS;eAATA;;;6BAnET;AA0BA,SAASF,QAAgDG,KAAW;IACzE,OAAOC,IAAAA,mBAAM,EACXC,KAAKC,SAAS,CAACH,QACfI,IAAAA,oBAAkB,EAACJ,OAAOK,SAAS;AAEvC;AAcO,SAASP,MAAwD,CACtEQ,MACA,GAAGC,KACyB;IAI5B,MAAMC,SAASC,IAAAA,kBAAgB,EAAC;QAACH;WAASC;KAAK;IAE/C,OAAO,IAAIG,mBAAM,CAAC;QAChB,GAAGF,MAAM;QACTG,QAAQ;YAACL;eAASC;SAAK;IACzB;AACF;AASO,SAASR,UACda,QAAc;IAEd,OAAOf,QAAQe;AACjB"}
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ AddressStruct: function() {
13
+ return AddressStruct;
14
+ },
15
+ address: function() {
16
+ return address;
17
+ }
18
+ });
19
+ const _superstruct = require("superstruct");
20
+ const _builder = require("../builder");
21
+ const _nodes = require("../nodes");
22
+ const AddressStruct = (0, _superstruct.assign)(_nodes.LiteralStruct, (0, _superstruct.object)({
23
+ type: (0, _superstruct.literal)(_nodes.NodeType.Address),
24
+ value: (0, _superstruct.pattern)((0, _superstruct.string)(), /0x[a-fA-F0-9]{40}/u)
25
+ }));
26
+ const address = (0, _builder.createBuilder)(_nodes.NodeType.Address, AddressStruct, [
27
+ 'value'
28
+ ]);
29
+
30
+ //# sourceMappingURL=address.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/ui/components/address.ts"],"sourcesContent":["import type { Infer } from 'superstruct';\nimport { assign, literal, object, pattern, string } from 'superstruct';\n\nimport { createBuilder } from '../builder';\nimport { LiteralStruct, NodeType } from '../nodes';\n\nexport const AddressStruct = assign(\n LiteralStruct,\n object({\n type: literal(NodeType.Address),\n value: pattern(string(), /0x[a-fA-F0-9]{40}/u),\n }),\n);\n\n/**\n * A address node, that renders an EVM-like address and its icon.\n *\n * @property type - The type of the node. Must be the string `address`.\n * @property value - The address in hexadecimal, including 0x.\n */\nexport type Address = Infer<typeof AddressStruct>;\n\n/**\n * Create an {@link Address} node.\n *\n * @param args - The node arguments. This can either be a string, or an object\n * with the `value` property.\n * @param args.value - The address to be rendered.\n * @returns The address node as an object.\n * @example\n * const node = address({ value: '0x4bbeeb066ed09b7aed07bf39eee0460dfa261520' });\n * const node = address('0x4bbeeb066ed09b7aed07bf39eee0460dfa261520');\n */\nexport const address = createBuilder(NodeType.Address, AddressStruct, [\n 'value',\n]);\n"],"names":["AddressStruct","address","assign","LiteralStruct","object","type","literal","NodeType","Address","value","pattern","string","createBuilder"],"mappings":";;;;;;;;;;;IAMaA,aAAa;eAAbA;;IA2BAC,OAAO;eAAPA;;;6BAhC4C;yBAE3B;uBACU;AAEjC,MAAMD,gBAAgBE,IAAAA,mBAAM,EACjCC,oBAAa,EACbC,IAAAA,mBAAM,EAAC;IACLC,MAAMC,IAAAA,oBAAO,EAACC,eAAQ,CAACC,OAAO;IAC9BC,OAAOC,IAAAA,oBAAO,EAACC,IAAAA,mBAAM,KAAI;AAC3B;AAsBK,MAAMV,UAAUW,IAAAA,sBAAa,EAACL,eAAQ,CAACC,OAAO,EAAER,eAAe;IACpE;CACD"}
@@ -25,6 +25,7 @@ _export(exports, {
25
25
  return _panel.PanelStruct;
26
26
  }
27
27
  });
28
+ _export_star(require("./address"), exports);
28
29
  _export_star(require("./copyable"), exports);
29
30
  _export_star(require("./divider"), exports);
30
31
  _export_star(require("./heading"), exports);
@@ -32,6 +33,7 @@ const _image = require("./image");
32
33
  const _panel = require("./panel");
33
34
  _export_star(require("./spinner"), exports);
34
35
  _export_star(require("./text"), exports);
36
+ _export_star(require("./row"), exports);
35
37
  function _export_star(from, to) {
36
38
  Object.keys(from).forEach(function(k) {
37
39
  if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/ui/components/index.ts"],"sourcesContent":["export * from './copyable';\nexport * from './divider';\nexport * from './heading';\nexport type { Image } from './image';\nexport { image, ImageStruct } from './image';\nexport type { Component, Panel } from './panel';\nexport { ComponentStruct, panel, PanelStruct } from './panel';\nexport * from './spinner';\nexport * from './text';\n"],"names":["image","ImageStruct","ComponentStruct","panel","PanelStruct"],"mappings":";;;;;;;;;;;IAISA,KAAK;eAALA,YAAK;;IAAEC,WAAW;eAAXA,kBAAW;;IAElBC,eAAe;eAAfA,sBAAe;;IAAEC,KAAK;eAALA,YAAK;;IAAEC,WAAW;eAAXA,kBAAW;;;qBAN9B;qBACA;qBACA;uBAEqB;uBAEiB;qBACtC;qBACA"}
1
+ {"version":3,"sources":["../../../../src/ui/components/index.ts"],"sourcesContent":["export * from './address';\nexport * from './copyable';\nexport * from './divider';\nexport * from './heading';\nexport type { Image } from './image';\nexport { image, ImageStruct } from './image';\nexport type { Component, Panel } from './panel';\nexport { ComponentStruct, panel, PanelStruct } from './panel';\nexport * from './spinner';\nexport * from './text';\nexport * from './row';\n"],"names":["image","ImageStruct","ComponentStruct","panel","PanelStruct"],"mappings":";;;;;;;;;;;IAKSA,KAAK;eAALA,YAAK;;IAAEC,WAAW;eAAXA,kBAAW;;IAElBC,eAAe;eAAfA,sBAAe;;IAAEC,KAAK;eAALA,YAAK;;IAAEC,WAAW;eAAXA,kBAAW;;;qBAP9B;qBACA;qBACA;qBACA;uBAEqB;uBAEiB;qBACtC;qBACA;qBACA"}
@@ -25,10 +25,12 @@ _export(exports, {
25
25
  const _superstruct = require("superstruct");
26
26
  const _builder = require("../builder");
27
27
  const _nodes = require("../nodes");
28
+ const _address = require("./address");
28
29
  const _copyable = require("./copyable");
29
30
  const _divider = require("./divider");
30
31
  const _heading = require("./heading");
31
32
  const _image = require("./image");
33
+ const _row = require("./row");
32
34
  const _spinner = require("./spinner");
33
35
  const _text = require("./text");
34
36
  const ParentStruct = (0, _superstruct.assign)(_nodes.NodeStruct, (0, _superstruct.object)({
@@ -49,7 +51,9 @@ const ComponentStruct = (0, _superstruct.union)([
49
51
  _image.ImageStruct,
50
52
  PanelStruct,
51
53
  _spinner.SpinnerStruct,
52
- _text.TextStruct
54
+ _text.TextStruct,
55
+ _row.RowStruct,
56
+ _address.AddressStruct
53
57
  ]);
54
58
 
55
59
  //# sourceMappingURL=panel.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/ui/components/panel.ts"],"sourcesContent":["import type { Infer, Struct } from 'superstruct';\nimport { array, assign, lazy, literal, object, union } from 'superstruct';\n\nimport { createBuilder } from '../builder';\nimport { NodeStruct, NodeType } from '../nodes';\nimport { CopyableStruct } from './copyable';\nimport { DividerStruct } from './divider';\nimport { HeadingStruct } from './heading';\nimport { ImageStruct } from './image';\nimport { SpinnerStruct } from './spinner';\nimport { TextStruct } from './text';\n\n/**\n * @internal\n */\nexport const ParentStruct = assign(\n NodeStruct,\n object({\n // This node references itself indirectly, so we need to use `lazy()`.\n // eslint-disable-next-line @typescript-eslint/no-use-before-define\n children: array(lazy(() => ComponentStruct)),\n }),\n);\n\n/**\n * A node which supports child nodes. This is used for nodes that render their\n * children, such as {@link Panel}.\n *\n * @property type - The type of the node.\n * @property children - The children of the node\n * @internal\n */\nexport type Parent = Infer<typeof ParentStruct>;\n\n/**\n * @internal\n */\nexport const PanelStruct: Struct<Panel> = assign(\n ParentStruct,\n object({\n type: literal(NodeType.Panel),\n }),\n);\n\n/**\n * A panel node, which renders its children.\n *\n * @property type - The type of the node, must be the string 'text'.\n * @property value - The text content of the node, either as plain text, or as a\n * markdown string.\n */\n// This node references itself indirectly, so it cannot be inferred.\nexport type Panel = {\n type: NodeType.Panel;\n children: Component[];\n};\n\n/**\n * Create a {@link Panel} node.\n *\n * @param args - The node arguments. This can be either an array of children, or\n * an object with a `children` property.\n * @param args.children - The child nodes of the panel. This can be any valid\n * {@link Component}.\n * @returns The panel node as object.\n * @example\n * const node = panel({\n * children: [\n * heading({ text: 'Hello, world!' }),\n * text({ text: 'This is a panel.' }),\n * ],\n * });\n *\n * const node = panel([\n * heading('Hello, world!'),\n * text('This is a panel.'),\n * ]);\n */\nexport const panel = createBuilder(NodeType.Panel, PanelStruct, ['children']);\n\n// This is defined separately from `Component` to avoid circular dependencies.\nexport const ComponentStruct = union([\n CopyableStruct,\n DividerStruct,\n HeadingStruct,\n ImageStruct,\n PanelStruct,\n SpinnerStruct,\n TextStruct,\n]);\n\n/**\n * All supported component types.\n */\nexport type Component = Infer<typeof ComponentStruct>;\n"],"names":["ParentStruct","PanelStruct","panel","ComponentStruct","assign","NodeStruct","object","children","array","lazy","type","literal","NodeType","Panel","createBuilder","union","CopyableStruct","DividerStruct","HeadingStruct","ImageStruct","SpinnerStruct","TextStruct"],"mappings":";;;;;;;;;;;IAeaA,YAAY;eAAZA;;IAsBAC,WAAW;eAAXA;;IAyCAC,KAAK;eAALA;;IAGAC,eAAe;eAAfA;;;6BAhF+C;yBAE9B;uBACO;0BACN;yBACD;yBACA;uBACF;yBACE;sBACH;AAKpB,MAAMH,eAAeI,IAAAA,mBAAM,EAChCC,iBAAU,EACVC,IAAAA,mBAAM,EAAC;IACL,sEAAsE;IACtE,mEAAmE;IACnEC,UAAUC,IAAAA,kBAAK,EAACC,IAAAA,iBAAI,EAAC,IAAMN;AAC7B;AAgBK,MAAMF,cAA6BG,IAAAA,mBAAM,EAC9CJ,cACAM,IAAAA,mBAAM,EAAC;IACLI,MAAMC,IAAAA,oBAAO,EAACC,eAAQ,CAACC,KAAK;AAC9B;AAqCK,MAAMX,QAAQY,IAAAA,sBAAa,EAACF,eAAQ,CAACC,KAAK,EAAEZ,aAAa;IAAC;CAAW;AAGrE,MAAME,kBAAkBY,IAAAA,kBAAK,EAAC;IACnCC,wBAAc;IACdC,sBAAa;IACbC,sBAAa;IACbC,kBAAW;IACXlB;IACAmB,sBAAa;IACbC,gBAAU;CACX"}
1
+ {"version":3,"sources":["../../../../src/ui/components/panel.ts"],"sourcesContent":["import type { Infer, Struct } from 'superstruct';\nimport { array, assign, lazy, literal, object, union } from 'superstruct';\n\nimport { createBuilder } from '../builder';\nimport { NodeStruct, NodeType } from '../nodes';\nimport { AddressStruct } from './address';\nimport { CopyableStruct } from './copyable';\nimport { DividerStruct } from './divider';\nimport { HeadingStruct } from './heading';\nimport { ImageStruct } from './image';\nimport { RowStruct } from './row';\nimport { SpinnerStruct } from './spinner';\nimport { TextStruct } from './text';\n\n/**\n * @internal\n */\nexport const ParentStruct = assign(\n NodeStruct,\n object({\n // This node references itself indirectly, so we need to use `lazy()`.\n // eslint-disable-next-line @typescript-eslint/no-use-before-define\n children: array(lazy(() => ComponentStruct)),\n }),\n);\n\n/**\n * A node which supports child nodes. This is used for nodes that render their\n * children, such as {@link Panel}.\n *\n * @property type - The type of the node.\n * @property children - The children of the node\n * @internal\n */\nexport type Parent = Infer<typeof ParentStruct>;\n\n/**\n * @internal\n */\nexport const PanelStruct: Struct<Panel> = assign(\n ParentStruct,\n object({\n type: literal(NodeType.Panel),\n }),\n);\n\n/**\n * A panel node, which renders its children.\n *\n * @property type - The type of the node, must be the string 'text'.\n * @property value - The text content of the node, either as plain text, or as a\n * markdown string.\n */\n// This node references itself indirectly, so it cannot be inferred.\nexport type Panel = {\n type: NodeType.Panel;\n children: Component[];\n};\n\n/**\n * Create a {@link Panel} node.\n *\n * @param args - The node arguments. This can be either an array of children, or\n * an object with a `children` property.\n * @param args.children - The child nodes of the panel. This can be any valid\n * {@link Component}.\n * @returns The panel node as object.\n * @example\n * const node = panel({\n * children: [\n * heading({ text: 'Hello, world!' }),\n * text({ text: 'This is a panel.' }),\n * ],\n * });\n *\n * const node = panel([\n * heading('Hello, world!'),\n * text('This is a panel.'),\n * ]);\n */\nexport const panel = createBuilder(NodeType.Panel, PanelStruct, ['children']);\n\n// This is defined separately from `Component` to avoid circular dependencies.\nexport const ComponentStruct = union([\n CopyableStruct,\n DividerStruct,\n HeadingStruct,\n ImageStruct,\n PanelStruct,\n SpinnerStruct,\n TextStruct,\n RowStruct,\n AddressStruct,\n]);\n\n/**\n * All supported component types.\n */\nexport type Component = Infer<typeof ComponentStruct>;\n"],"names":["ParentStruct","PanelStruct","panel","ComponentStruct","assign","NodeStruct","object","children","array","lazy","type","literal","NodeType","Panel","createBuilder","union","CopyableStruct","DividerStruct","HeadingStruct","ImageStruct","SpinnerStruct","TextStruct","RowStruct","AddressStruct"],"mappings":";;;;;;;;;;;IAiBaA,YAAY;eAAZA;;IAsBAC,WAAW;eAAXA;;IAyCAC,KAAK;eAALA;;IAGAC,eAAe;eAAfA;;;6BAlF+C;yBAE9B;uBACO;yBACP;0BACC;yBACD;yBACA;uBACF;qBACF;yBACI;sBACH;AAKpB,MAAMH,eAAeI,IAAAA,mBAAM,EAChCC,iBAAU,EACVC,IAAAA,mBAAM,EAAC;IACL,sEAAsE;IACtE,mEAAmE;IACnEC,UAAUC,IAAAA,kBAAK,EAACC,IAAAA,iBAAI,EAAC,IAAMN;AAC7B;AAgBK,MAAMF,cAA6BG,IAAAA,mBAAM,EAC9CJ,cACAM,IAAAA,mBAAM,EAAC;IACLI,MAAMC,IAAAA,oBAAO,EAACC,eAAQ,CAACC,KAAK;AAC9B;AAqCK,MAAMX,QAAQY,IAAAA,sBAAa,EAACF,eAAQ,CAACC,KAAK,EAAEZ,aAAa;IAAC;CAAW;AAGrE,MAAME,kBAAkBY,IAAAA,kBAAK,EAAC;IACnCC,wBAAc;IACdC,sBAAa;IACbC,sBAAa;IACbC,kBAAW;IACXlB;IACAmB,sBAAa;IACbC,gBAAU;IACVC,cAAS;IACTC,sBAAa;CACd"}
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ RowVariant: function() {
13
+ return RowVariant;
14
+ },
15
+ RowStruct: function() {
16
+ return RowStruct;
17
+ },
18
+ row: function() {
19
+ return row;
20
+ }
21
+ });
22
+ const _superstruct = require("superstruct");
23
+ const _internals = require("../../internals");
24
+ const _builder = require("../builder");
25
+ const _nodes = require("../nodes");
26
+ const _address = require("./address");
27
+ const _image = require("./image");
28
+ const _text = require("./text");
29
+ var RowVariant;
30
+ (function(RowVariant) {
31
+ RowVariant["Default"] = 'default';
32
+ RowVariant["Critical"] = 'critical';
33
+ RowVariant["Warning"] = 'warning';
34
+ })(RowVariant || (RowVariant = {}));
35
+ // A subset of components made available to the row
36
+ const RowComponentStruct = (0, _superstruct.union)([
37
+ _image.ImageStruct,
38
+ _text.TextStruct,
39
+ _address.AddressStruct
40
+ ]);
41
+ const RowStruct = (0, _superstruct.assign)(_nodes.LiteralStruct, (0, _superstruct.object)({
42
+ type: (0, _superstruct.literal)(_nodes.NodeType.Row),
43
+ variant: (0, _superstruct.optional)((0, _superstruct.union)([
44
+ (0, _internals.enumValue)(RowVariant.Default),
45
+ (0, _internals.enumValue)(RowVariant.Critical),
46
+ (0, _internals.enumValue)(RowVariant.Warning)
47
+ ])),
48
+ label: (0, _superstruct.string)(),
49
+ value: RowComponentStruct
50
+ }));
51
+ const row = (0, _builder.createBuilder)(_nodes.NodeType.Row, RowStruct, [
52
+ 'label',
53
+ 'value',
54
+ 'variant'
55
+ ]);
56
+
57
+ //# sourceMappingURL=row.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/ui/components/row.ts"],"sourcesContent":["import type { Infer } from 'superstruct';\nimport { assign, literal, object, string, optional, union } from 'superstruct';\n\nimport { enumValue } from '../../internals';\nimport { createBuilder } from '../builder';\nimport { LiteralStruct, NodeType } from '../nodes';\nimport { AddressStruct } from './address';\nimport { ImageStruct } from './image';\nimport { TextStruct } from './text';\n\nexport enum RowVariant {\n Default = 'default',\n Critical = 'critical',\n Warning = 'warning',\n}\n\n// A subset of components made available to the row\nconst RowComponentStruct = union([ImageStruct, TextStruct, AddressStruct]);\n\nexport const RowStruct = assign(\n LiteralStruct,\n object({\n type: literal(NodeType.Row),\n variant: optional(\n union([\n enumValue(RowVariant.Default),\n enumValue(RowVariant.Critical),\n enumValue(RowVariant.Warning),\n ]),\n ),\n label: string(),\n value: RowComponentStruct,\n }),\n);\n\n/**\n * A row node, that renders a row with a label and a value.\n *\n * @property type - The type of the node. Must be the string `row`.\n * @property label - The label for the row.\n * @property value - A sub component to be rendered\n * on one side of the row.\n * @property variant - Optional variant for styling.\n */\nexport type Row = Infer<typeof RowStruct>;\n\n/**\n * Create a {@link Row} node.\n *\n * @param args - The node arguments. This can either be a string, a component and an optional variant or an object\n * with the properties: `label`, `value` and `variant`.\n * @param args.label - The label for the row.\n * @param args.value - Another component, is currently limited to `image`, `text` and `address`.\n * @param args.variant - An optional variant, either `default`, `warning` or `critical`.\n * @returns The row node as an object.\n * @example\n * const node = row({ label: 'Address', value: address('0x4bbeeb066ed09b7aed07bf39eee0460dfa261520') });\n * const node = row({ label: 'Address', value: address('0x4bbeeb066ed09b7aed07bf39eee0460dfa261520'), variant: RowVariant.Warning });\n * const node = row('Address', address('0x4bbeeb066ed09b7aed07bf39eee0460dfa261520'));\n * const node = row('Address', address('0x4bbeeb066ed09b7aed07bf39eee0460dfa261520'), RowVariant.Warning);\n */\nexport const row = createBuilder(NodeType.Row, RowStruct, [\n 'label',\n 'value',\n 'variant',\n]);\n"],"names":["RowStruct","row","RowVariant","Default","Critical","Warning","RowComponentStruct","union","ImageStruct","TextStruct","AddressStruct","assign","LiteralStruct","object","type","literal","NodeType","Row","variant","optional","enumValue","label","string","value","createBuilder"],"mappings":";;;;;;;;;;;;;;IAmBaA,SAAS;eAATA;;IA0CAC,GAAG;eAAHA;;;6BA5DoD;2BAEvC;yBACI;uBACU;yBACV;uBACF;sBACD;IAEpB;UAAKC,UAAU;IAAVA,WACVC,aAAU;IADAD,WAEVE,cAAW;IAFDF,WAGVG,aAAU;GAHAH,eAAAA;AAMZ,mDAAmD;AACnD,MAAMI,qBAAqBC,IAAAA,kBAAK,EAAC;IAACC,kBAAW;IAAEC,gBAAU;IAAEC,sBAAa;CAAC;AAElE,MAAMV,YAAYW,IAAAA,mBAAM,EAC7BC,oBAAa,EACbC,IAAAA,mBAAM,EAAC;IACLC,MAAMC,IAAAA,oBAAO,EAACC,eAAQ,CAACC,GAAG;IAC1BC,SAASC,IAAAA,qBAAQ,EACfZ,IAAAA,kBAAK,EAAC;QACJa,IAAAA,oBAAS,EAAClB,WAAWC,OAAO;QAC5BiB,IAAAA,oBAAS,EAAClB,WAAWE,QAAQ;QAC7BgB,IAAAA,oBAAS,EAAClB,WAAWG,OAAO;KAC7B;IAEHgB,OAAOC,IAAAA,mBAAM;IACbC,OAAOjB;AACT;AA6BK,MAAML,MAAMuB,IAAAA,sBAAa,EAACR,eAAQ,CAACC,GAAG,EAAEjB,WAAW;IACxD;IACA;IACA;CACD"}
@@ -30,6 +30,8 @@ var NodeType;
30
30
  NodeType[// eslint-disable-next-line @typescript-eslint/no-shadow
31
31
  "Text"] = 'text';
32
32
  NodeType["Image"] = 'image';
33
+ NodeType["Row"] = 'row';
34
+ NodeType["Address"] = 'address';
33
35
  })(NodeType || (NodeType = {}));
34
36
  const NodeStruct = (0, _superstruct.object)({
35
37
  type: (0, _superstruct.string)()
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/ui/nodes.ts"],"sourcesContent":["import type { Infer } from 'superstruct';\nimport { assign, object, string, unknown } from 'superstruct';\n\n/**\n * The supported node types. This is based on SIP-7.\n *\n * @see https://metamask.github.io/SIPs/SIPS/sip-7\n */\nexport enum NodeType {\n Copyable = 'copyable',\n Divider = 'divider',\n Heading = 'heading',\n Panel = 'panel',\n Spinner = 'spinner',\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Text = 'text',\n Image = 'image',\n}\n\n/**\n * @internal\n */\nexport const NodeStruct = object({\n type: string(),\n});\n\n/**\n * The base node type. All nodes extend this type.\n *\n * @property type - The type of the node. See {@link NodeType} for the supported\n * node types.\n * @internal\n */\nexport type Node = Infer<typeof NodeStruct>;\n\n/**\n * @internal\n */\nexport const LiteralStruct = assign(\n NodeStruct,\n object({\n value: unknown(),\n }),\n);\n\n/**\n * A node with a value. This is used for nodes that render a value, such as\n * {@link Text}.\n *\n * @property type - The type of the node.\n * @property value - The value of the node. The type of the value depends on the\n * node type.\n * @internal\n */\nexport type Literal = Infer<typeof LiteralStruct>;\n"],"names":["NodeStruct","LiteralStruct","NodeType","Copyable","Divider","Heading","Panel","Spinner","Text","Image","object","type","string","assign","value","unknown"],"mappings":";;;;;;;;;;;;;;IAsBaA,UAAU;eAAVA;;IAgBAC,aAAa;eAAbA;;;6BArCmC;IAOzC;UAAKC,QAAQ;IAARA,SACVC,cAAW;IADDD,SAEVE,aAAU;IAFAF,SAGVG,aAAU;IAHAH,SAIVI,WAAQ;IAJEJ,SAKVK,aAAU;IALAL,SAMV,wDAAwD;IACxDM,UAAO;IAPGN,SAQVO,WAAQ;GAREP,aAAAA;AAcL,MAAMF,aAAaU,IAAAA,mBAAM,EAAC;IAC/BC,MAAMC,IAAAA,mBAAM;AACd;AAcO,MAAMX,gBAAgBY,IAAAA,mBAAM,EACjCb,YACAU,IAAAA,mBAAM,EAAC;IACLI,OAAOC,IAAAA,oBAAO;AAChB"}
1
+ {"version":3,"sources":["../../../src/ui/nodes.ts"],"sourcesContent":["import type { Infer } from 'superstruct';\nimport { assign, object, string, unknown } from 'superstruct';\n\n/**\n * The supported node types. This is based on SIP-7.\n *\n * @see https://metamask.github.io/SIPs/SIPS/sip-7\n */\nexport enum NodeType {\n Copyable = 'copyable',\n Divider = 'divider',\n Heading = 'heading',\n Panel = 'panel',\n Spinner = 'spinner',\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Text = 'text',\n Image = 'image',\n Row = 'row',\n Address = 'address',\n}\n\n/**\n * @internal\n */\nexport const NodeStruct = object({\n type: string(),\n});\n\n/**\n * The base node type. All nodes extend this type.\n *\n * @property type - The type of the node. See {@link NodeType} for the supported\n * node types.\n * @internal\n */\nexport type Node = Infer<typeof NodeStruct>;\n\n/**\n * @internal\n */\nexport const LiteralStruct = assign(\n NodeStruct,\n object({\n value: unknown(),\n }),\n);\n\n/**\n * A node with a value. This is used for nodes that render a value, such as\n * {@link Text}.\n *\n * @property type - The type of the node.\n * @property value - The value of the node. The type of the value depends on the\n * node type.\n * @internal\n */\nexport type Literal = Infer<typeof LiteralStruct>;\n"],"names":["NodeStruct","LiteralStruct","NodeType","Copyable","Divider","Heading","Panel","Spinner","Text","Image","Row","Address","object","type","string","assign","value","unknown"],"mappings":";;;;;;;;;;;;;;IAwBaA,UAAU;eAAVA;;IAgBAC,aAAa;eAAbA;;;6BAvCmC;IAOzC;UAAKC,QAAQ;IAARA,SACVC,cAAW;IADDD,SAEVE,aAAU;IAFAF,SAGVG,aAAU;IAHAH,SAIVI,WAAQ;IAJEJ,SAKVK,aAAU;IALAL,SAMV,wDAAwD;IACxDM,UAAO;IAPGN,SAQVO,WAAQ;IAREP,SASVQ,SAAM;IATIR,SAUVS,aAAU;GAVAT,aAAAA;AAgBL,MAAMF,aAAaY,IAAAA,mBAAM,EAAC;IAC/BC,MAAMC,IAAAA,mBAAM;AACd;AAcO,MAAMb,gBAAgBc,IAAAA,mBAAM,EACjCf,YACAY,IAAAA,mBAAM,EAAC;IACLI,OAAOC,IAAAA,oBAAO;AAChB"}
@@ -0,0 +1,84 @@
1
+ import { assert, bytesToBase64 } from '@metamask/utils';
2
+ import { image } from './ui';
3
+ /**
4
+ * Get raw image data from a URL.
5
+ *
6
+ * @param url - The URL to get the image data from.
7
+ * @param options - The options to use when fetching the image data. This is
8
+ * passed directly to `fetch`.
9
+ * @returns A promise that resolves to the image data as a blob.
10
+ */ async function getRawImageData(url, options) {
11
+ if (typeof fetch !== 'function') {
12
+ throw new Error(`Failed to fetch image data from "${url}": Using this function requires the "endowment:network-access" permission.`);
13
+ }
14
+ return fetch(url, options).then(async (response)=>{
15
+ if (!response.ok) {
16
+ throw new Error(`Failed to fetch image data from "${url}": ${response.status} ${response.statusText}`);
17
+ }
18
+ const blob = await response.blob();
19
+ assert(blob.type === 'image/jpeg' || blob.type === 'image/png', 'Expected image data to be a JPEG or PNG image.');
20
+ return blob;
21
+ });
22
+ }
23
+ /**
24
+ * Get image data as data-string from a URL. This is useful for embedding images
25
+ * inside of SVGs. Only JPEG and PNG images are supported.
26
+ *
27
+ * Note: This function uses `fetch` to get the image data. This means that using
28
+ * it requires the `endowment:network-access` permission.
29
+ *
30
+ * @example
31
+ * const imageData = await getImageData('https://cataas.com/cat');
32
+ * const svg = `
33
+ * <svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
34
+ * <image href="${imageData}" />
35
+ * </svg>
36
+ * `;
37
+ *
38
+ * // Render the SVG in a Snap UI.
39
+ * const ui = image(svg);
40
+ * @param url - The URL to get the image data from.
41
+ * @param options - The options to use when fetching the image data. This is
42
+ * passed directly to `fetch`.
43
+ * @returns A promise that resolves to the image data as a data-string.
44
+ */ export async function getImageData(url, options) {
45
+ const blob = await getRawImageData(url, options);
46
+ const bytes = new Uint8Array(await blob.arrayBuffer());
47
+ return `data:${blob.type};base64,${bytesToBase64(bytes)}`;
48
+ }
49
+ /**
50
+ * Get an image component from a URL. This is useful for embedding images inside
51
+ * Snap UIs. Only JPEG and PNG images are supported.
52
+ *
53
+ * Note: This function uses `fetch` to get the image data. This means that using
54
+ * it requires the `endowment:network-access` permission.
55
+ *
56
+ * @example
57
+ * const component = await getImage('https://cataas.com/cat');
58
+ *
59
+ * return await snap.request({
60
+ * method: 'snap_dialog',
61
+ * params: {
62
+ * type: 'alert',
63
+ * content: panel([
64
+ * component,
65
+ * ]),
66
+ * },
67
+ * });
68
+ * @param url - The URL to get the image data from.
69
+ * @param options - The options to use when fetching and rendering the image.
70
+ * @param options.width - The width of the image.
71
+ * @param options.height - The height of the image. If this is not provided, the
72
+ * width will be used as the height.
73
+ * @param options.request - The options to use when fetching the image data.
74
+ * This is passed directly to `fetch`.
75
+ * @returns A promise that resolves to the image data as an image component.
76
+ */ export async function getImageComponent(url, { width, height = width, request }) {
77
+ assert(typeof width === 'number' && width > 0, 'Expected width to be a number greater than 0.');
78
+ assert(typeof height === 'number' && height > 0, 'Expected height to be a number greater than 0.');
79
+ const imageData = await getImageData(url, request);
80
+ const size = `width="${width}" height="${height}"`;
81
+ return image(`<svg ${size.trim()} xmlns="http://www.w3.org/2000/svg"><image ${size.trim()} href="${imageData}" /></svg>`);
82
+ }
83
+
84
+ //# sourceMappingURL=images.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/images.ts"],"sourcesContent":["import { assert, bytesToBase64 } from '@metamask/utils';\n\nimport { image } from './ui';\n\n/**\n * Get raw image data from a URL.\n *\n * @param url - The URL to get the image data from.\n * @param options - The options to use when fetching the image data. This is\n * passed directly to `fetch`.\n * @returns A promise that resolves to the image data as a blob.\n */\nasync function getRawImageData(url: string, options?: RequestInit) {\n if (typeof fetch !== 'function') {\n throw new Error(\n `Failed to fetch image data from \"${url}\": Using this function requires the \"endowment:network-access\" permission.`,\n );\n }\n\n return fetch(url, options).then(async (response) => {\n if (!response.ok) {\n throw new Error(\n `Failed to fetch image data from \"${url}\": ${response.status} ${response.statusText}`,\n );\n }\n\n const blob = await response.blob();\n assert(\n blob.type === 'image/jpeg' || blob.type === 'image/png',\n 'Expected image data to be a JPEG or PNG image.',\n );\n\n return blob;\n });\n}\n\n/**\n * Get image data as data-string from a URL. This is useful for embedding images\n * inside of SVGs. Only JPEG and PNG images are supported.\n *\n * Note: This function uses `fetch` to get the image data. This means that using\n * it requires the `endowment:network-access` permission.\n *\n * @example\n * const imageData = await getImageData('https://cataas.com/cat');\n * const svg = `\n * <svg width=\"100\" height=\"100\" xmlns=\"http://www.w3.org/2000/svg\">\n * <image href=\"${imageData}\" />\n * </svg>\n * `;\n *\n * // Render the SVG in a Snap UI.\n * const ui = image(svg);\n * @param url - The URL to get the image data from.\n * @param options - The options to use when fetching the image data. This is\n * passed directly to `fetch`.\n * @returns A promise that resolves to the image data as a data-string.\n */\nexport async function getImageData(url: string, options?: RequestInit) {\n const blob = await getRawImageData(url, options);\n const bytes = new Uint8Array(await blob.arrayBuffer());\n\n return `data:${blob.type};base64,${bytesToBase64(bytes)}`;\n}\n\n/**\n * Options for getting an SVG image element from a URL.\n *\n * @property width - The width of the image.\n * @property height - The height of the image. If this is not provided, the\n * width will be used as the height.\n * @property request - The options to use when fetching the image data. This is\n * passed directly to `fetch`.\n */\nexport type ImageOptions = {\n width: number;\n height?: number;\n request?: RequestInit;\n};\n\n/**\n * Get an image component from a URL. This is useful for embedding images inside\n * Snap UIs. Only JPEG and PNG images are supported.\n *\n * Note: This function uses `fetch` to get the image data. This means that using\n * it requires the `endowment:network-access` permission.\n *\n * @example\n * const component = await getImage('https://cataas.com/cat');\n *\n * return await snap.request({\n * method: 'snap_dialog',\n * params: {\n * type: 'alert',\n * content: panel([\n * component,\n * ]),\n * },\n * });\n * @param url - The URL to get the image data from.\n * @param options - The options to use when fetching and rendering the image.\n * @param options.width - The width of the image.\n * @param options.height - The height of the image. If this is not provided, the\n * width will be used as the height.\n * @param options.request - The options to use when fetching the image data.\n * This is passed directly to `fetch`.\n * @returns A promise that resolves to the image data as an image component.\n */\nexport async function getImageComponent(\n url: string,\n { width, height = width, request }: ImageOptions,\n) {\n assert(\n typeof width === 'number' && width > 0,\n 'Expected width to be a number greater than 0.',\n );\n\n assert(\n typeof height === 'number' && height > 0,\n 'Expected height to be a number greater than 0.',\n );\n\n const imageData = await getImageData(url, request);\n const size = `width=\"${width}\" height=\"${height}\"`;\n\n return image(\n `<svg ${size.trim()} xmlns=\"http://www.w3.org/2000/svg\"><image ${size.trim()} href=\"${imageData}\" /></svg>`,\n );\n}\n"],"names":["assert","bytesToBase64","image","getRawImageData","url","options","fetch","Error","then","response","ok","status","statusText","blob","type","getImageData","bytes","Uint8Array","arrayBuffer","getImageComponent","width","height","request","imageData","size","trim"],"mappings":"AAAA,SAASA,MAAM,EAAEC,aAAa,QAAQ,kBAAkB;AAExD,SAASC,KAAK,QAAQ,OAAO;AAE7B;;;;;;;CAOC,GACD,eAAeC,gBAAgBC,GAAW,EAAEC,OAAqB;IAC/D,IAAI,OAAOC,UAAU,YAAY;QAC/B,MAAM,IAAIC,MACR,CAAC,iCAAiC,EAAEH,IAAI,0EAA0E,CAAC;IAEvH;IAEA,OAAOE,MAAMF,KAAKC,SAASG,IAAI,CAAC,OAAOC;QACrC,IAAI,CAACA,SAASC,EAAE,EAAE;YAChB,MAAM,IAAIH,MACR,CAAC,iCAAiC,EAAEH,IAAI,GAAG,EAAEK,SAASE,MAAM,CAAC,CAAC,EAAEF,SAASG,UAAU,CAAC,CAAC;QAEzF;QAEA,MAAMC,OAAO,MAAMJ,SAASI,IAAI;QAChCb,OACEa,KAAKC,IAAI,KAAK,gBAAgBD,KAAKC,IAAI,KAAK,aAC5C;QAGF,OAAOD;IACT;AACF;AAEA;;;;;;;;;;;;;;;;;;;;;CAqBC,GACD,OAAO,eAAeE,aAAaX,GAAW,EAAEC,OAAqB;IACnE,MAAMQ,OAAO,MAAMV,gBAAgBC,KAAKC;IACxC,MAAMW,QAAQ,IAAIC,WAAW,MAAMJ,KAAKK,WAAW;IAEnD,OAAO,CAAC,KAAK,EAAEL,KAAKC,IAAI,CAAC,QAAQ,EAAEb,cAAce,OAAO,CAAC;AAC3D;AAiBA;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2BC,GACD,OAAO,eAAeG,kBACpBf,GAAW,EACX,EAAEgB,KAAK,EAAEC,SAASD,KAAK,EAAEE,OAAO,EAAgB;IAEhDtB,OACE,OAAOoB,UAAU,YAAYA,QAAQ,GACrC;IAGFpB,OACE,OAAOqB,WAAW,YAAYA,SAAS,GACvC;IAGF,MAAME,YAAY,MAAMR,aAAaX,KAAKkB;IAC1C,MAAME,OAAO,CAAC,OAAO,EAAEJ,MAAM,UAAU,EAAEC,OAAO,CAAC,CAAC;IAElD,OAAOnB,MACL,CAAC,KAAK,EAAEsB,KAAKC,IAAI,GAAG,2CAA2C,EAAED,KAAKC,IAAI,GAAG,OAAO,EAAEF,UAAU,UAAU,CAAC;AAE/G"}
package/dist/esm/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  // Only internals that are used by other Snaps packages should be exported here.
2
- export { getErrorData, getErrorMessage, getErrorStack, SNAP_ERROR_CODE, SNAP_ERROR_MESSAGE } from './internals';
2
+ export { getErrorData, getErrorMessage, getErrorStack, SNAP_ERROR_CODE, SNAP_ERROR_MESSAGE, literal, union, enumValue } from './internals';
3
+ export { assert } from '@metamask/utils';
3
4
  export * from './errors';
5
+ export * from './images';
4
6
  export * from './types';
5
7
  export * from './ui';
6
8
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["// Only internals that are used by other Snaps packages should be exported here.\nexport type { EnumToUnion } from './internals';\nexport {\n getErrorData,\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from './internals';\n\n// Re-exported from `@metamask/utils` for convenience.\nexport type {\n Json,\n JsonRpcError,\n JsonRpcRequest,\n JsonRpcParams,\n} from '@metamask/utils';\n\nexport * from './errors';\nexport * from './types';\nexport * from './ui';\n"],"names":["getErrorData","getErrorMessage","getErrorStack","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE"],"mappings":"AAAA,gFAAgF;AAEhF,SACEA,YAAY,EACZC,eAAe,EACfC,aAAa,EACbC,eAAe,EACfC,kBAAkB,QACb,cAAc;AAUrB,cAAc,WAAW;AACzB,cAAc,UAAU;AACxB,cAAc,OAAO"}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["// Only internals that are used by other Snaps packages should be exported here.\nexport type { EnumToUnion } from './internals';\nexport {\n getErrorData,\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n literal,\n union,\n enumValue,\n} from './internals';\n\n// Re-exported from `@metamask/utils` for convenience.\nexport type {\n Json,\n JsonRpcError,\n JsonRpcRequest,\n JsonRpcParams,\n} from '@metamask/utils';\nexport { assert } from '@metamask/utils';\n\nexport * from './errors';\nexport * from './images';\nexport * from './types';\nexport * from './ui';\n"],"names":["getErrorData","getErrorMessage","getErrorStack","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE","literal","union","enumValue","assert"],"mappings":"AAAA,gFAAgF;AAEhF,SACEA,YAAY,EACZC,eAAe,EACfC,aAAa,EACbC,eAAe,EACfC,kBAAkB,EAClBC,OAAO,EACPC,KAAK,EACLC,SAAS,QACJ,cAAc;AASrB,SAASC,MAAM,QAAQ,kBAAkB;AAEzC,cAAc,WAAW;AACzB,cAAc,WAAW;AACzB,cAAc,UAAU;AACxB,cAAc,OAAO"}
@@ -1,5 +1,6 @@
1
1
  export * from './error-wrappers';
2
2
  export * from './errors';
3
3
  export * from './helpers';
4
+ export * from './structs';
4
5
 
5
6
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/internals/index.ts"],"sourcesContent":["export * from './error-wrappers';\nexport * from './errors';\nexport * from './helpers';\n"],"names":[],"mappings":"AAAA,cAAc,mBAAmB;AACjC,cAAc,WAAW;AACzB,cAAc,YAAY"}
1
+ {"version":3,"sources":["../../../src/internals/index.ts"],"sourcesContent":["export * from './error-wrappers';\nexport * from './errors';\nexport * from './helpers';\nexport * from './structs';\n"],"names":[],"mappings":"AAAA,cAAc,mBAAmB;AACjC,cAAc,WAAW;AACzB,cAAc,YAAY;AAC1B,cAAc,YAAY"}
@@ -0,0 +1,59 @@
1
+ import { Struct, define, literal as superstructLiteral, union as superstructUnion } from 'superstruct';
2
+ /**
3
+ * A wrapper of `superstruct`'s `literal` struct that also defines the name of
4
+ * the struct as the literal value.
5
+ *
6
+ * This is useful for improving the error messages returned by `superstruct`.
7
+ * For example, instead of returning an error like:
8
+ *
9
+ * ```
10
+ * Expected the value to satisfy a union of `literal | literal`, but received: \"baz\"
11
+ * ```
12
+ *
13
+ * This struct will return an error like:
14
+ *
15
+ * ```
16
+ * Expected the value to satisfy a union of `"foo" | "bar"`, but received: \"baz\"
17
+ * ```
18
+ *
19
+ * @param value - The literal value.
20
+ * @returns The `superstruct` struct, which validates that the value is equal
21
+ * to the literal value.
22
+ */ export function literal(value) {
23
+ return define(JSON.stringify(value), superstructLiteral(value).validator);
24
+ }
25
+ /**
26
+ * A wrapper of `superstruct`'s `union` struct that also defines the schema as
27
+ * the union of the schemas of the structs.
28
+ *
29
+ * This is useful for improving the error messages returned by `superstruct`.
30
+ *
31
+ * @param structs - The structs to union.
32
+ * @param structs."0" - The first struct.
33
+ * @param structs."1" - The remaining structs.
34
+ * @returns The `superstruct` struct, which validates that the value satisfies
35
+ * one of the structs.
36
+ */ export function union([head, ...tail]) {
37
+ const struct = superstructUnion([
38
+ head,
39
+ ...tail
40
+ ]);
41
+ return new Struct({
42
+ ...struct,
43
+ schema: [
44
+ head,
45
+ ...tail
46
+ ]
47
+ });
48
+ }
49
+ /**
50
+ * Superstruct struct for validating an enum value. This allows using both the
51
+ * enum string values and the enum itself as values.
52
+ *
53
+ * @param constant - The enum to validate against.
54
+ * @returns The superstruct struct.
55
+ */ export function enumValue(constant) {
56
+ return literal(constant);
57
+ }
58
+
59
+ //# sourceMappingURL=structs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/internals/structs.ts"],"sourcesContent":["import type { Infer } from 'superstruct';\nimport {\n Struct,\n define,\n literal as superstructLiteral,\n union as superstructUnion,\n} from 'superstruct';\nimport type { AnyStruct, InferStructTuple } from 'superstruct/dist/utils';\n\nimport type { EnumToUnion } from './helpers';\n\n/**\n * A wrapper of `superstruct`'s `literal` struct that also defines the name of\n * the struct as the literal value.\n *\n * This is useful for improving the error messages returned by `superstruct`.\n * For example, instead of returning an error like:\n *\n * ```\n * Expected the value to satisfy a union of `literal | literal`, but received: \\\"baz\\\"\n * ```\n *\n * This struct will return an error like:\n *\n * ```\n * Expected the value to satisfy a union of `\"foo\" | \"bar\"`, but received: \\\"baz\\\"\n * ```\n *\n * @param value - The literal value.\n * @returns The `superstruct` struct, which validates that the value is equal\n * to the literal value.\n */\nexport function literal<Type extends string | number | boolean>(value: Type) {\n return define<Type>(\n JSON.stringify(value),\n superstructLiteral(value).validator,\n );\n}\n\n/**\n * A wrapper of `superstruct`'s `union` struct that also defines the schema as\n * the union of the schemas of the structs.\n *\n * This is useful for improving the error messages returned by `superstruct`.\n *\n * @param structs - The structs to union.\n * @param structs.\"0\" - The first struct.\n * @param structs.\"1\" - The remaining structs.\n * @returns The `superstruct` struct, which validates that the value satisfies\n * one of the structs.\n */\nexport function union<Head extends AnyStruct, Tail extends AnyStruct[]>([\n head,\n ...tail\n]: [head: Head, ...tail: Tail]): Struct<\n Infer<Head> | InferStructTuple<Tail>[number],\n [head: Head, ...tail: Tail]\n> {\n const struct = superstructUnion([head, ...tail]);\n\n return new Struct({\n ...struct,\n schema: [head, ...tail],\n });\n}\n\n/**\n * Superstruct struct for validating an enum value. This allows using both the\n * enum string values and the enum itself as values.\n *\n * @param constant - The enum to validate against.\n * @returns The superstruct struct.\n */\nexport function enumValue<Type extends string>(\n constant: Type,\n): Struct<EnumToUnion<Type>, null> {\n return literal(constant as EnumToUnion<Type>);\n}\n"],"names":["Struct","define","literal","superstructLiteral","union","superstructUnion","value","JSON","stringify","validator","head","tail","struct","schema","enumValue","constant"],"mappings":"AACA,SACEA,MAAM,EACNC,MAAM,EACNC,WAAWC,kBAAkB,EAC7BC,SAASC,gBAAgB,QACpB,cAAc;AAKrB;;;;;;;;;;;;;;;;;;;;CAoBC,GACD,OAAO,SAASH,QAAgDI,KAAW;IACzE,OAAOL,OACLM,KAAKC,SAAS,CAACF,QACfH,mBAAmBG,OAAOG,SAAS;AAEvC;AAEA;;;;;;;;;;;CAWC,GACD,OAAO,SAASL,MAAwD,CACtEM,MACA,GAAGC,KACyB;IAI5B,MAAMC,SAASP,iBAAiB;QAACK;WAASC;KAAK;IAE/C,OAAO,IAAIX,OAAO;QAChB,GAAGY,MAAM;QACTC,QAAQ;YAACH;eAASC;SAAK;IACzB;AACF;AAEA;;;;;;CAMC,GACD,OAAO,SAASG,UACdC,QAAc;IAEd,OAAOb,QAAQa;AACjB"}