@metamask/snaps-sdk 1.2.0 → 1.3.1
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.
- package/CHANGELOG.md +14 -1
- package/dist/cjs/images.js +54 -0
- package/dist/cjs/images.js.map +1 -0
- package/dist/cjs/index.js +6 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/images.js +84 -0
- package/dist/esm/images.js.map +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/types/images.d.ts +69 -0
- package/dist/types/index.d.ts +3 -0
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [1.3.1]
|
|
10
|
+
### Fixed
|
|
11
|
+
- Export error wrappers ([#2043](https://github.com/MetaMask/snaps/pull/2043))
|
|
12
|
+
|
|
13
|
+
## [1.3.0]
|
|
14
|
+
### Added
|
|
15
|
+
- Add image fetching utility functions ([#1995](https://github.com/MetaMask/snaps/pull/1995))
|
|
16
|
+
- This adds two functions:
|
|
17
|
+
- `getImageComponent` to get an `image` component from a PNG or JPEG URL.
|
|
18
|
+
- `getImageData` to get a base64 data string, which can be embedded in an SVG image.
|
|
19
|
+
|
|
9
20
|
## [1.2.0]
|
|
10
21
|
### Added
|
|
11
22
|
- Add `row` and `address` component ([#1968](https://github.com/MetaMask/snaps/pull/1968))
|
|
@@ -22,7 +33,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
22
33
|
### Added
|
|
23
34
|
- Initial release of this package.
|
|
24
35
|
|
|
25
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@1.
|
|
36
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@1.3.1...HEAD
|
|
37
|
+
[1.3.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@1.3.0...@metamask/snaps-sdk@1.3.1
|
|
38
|
+
[1.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@1.2.0...@metamask/snaps-sdk@1.3.0
|
|
26
39
|
[1.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@1.1.0...@metamask/snaps-sdk@1.2.0
|
|
27
40
|
[1.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@1.0.0...@metamask/snaps-sdk@1.1.0
|
|
28
41
|
[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
|
@@ -33,10 +33,16 @@ _export(exports, {
|
|
|
33
33
|
},
|
|
34
34
|
enumValue: function() {
|
|
35
35
|
return _internals.enumValue;
|
|
36
|
+
},
|
|
37
|
+
assert: function() {
|
|
38
|
+
return _utils.assert;
|
|
36
39
|
}
|
|
37
40
|
});
|
|
38
41
|
const _internals = require("./internals");
|
|
42
|
+
const _utils = require("@metamask/utils");
|
|
39
43
|
_export_star(require("./errors"), exports);
|
|
44
|
+
_export_star(require("./error-wrappers"), exports);
|
|
45
|
+
_export_star(require("./images"), exports);
|
|
40
46
|
_export_star(require("./types"), exports);
|
|
41
47
|
_export_star(require("./ui"), exports);
|
|
42
48
|
function _export_star(from, to) {
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -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 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';\n\nexport * from './errors';\nexport * from './types';\nexport * from './ui';\n"],"names":["getErrorData","getErrorMessage","getErrorStack","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE","literal","union","enumValue"],"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;;;
|
|
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 './error-wrappers';\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;qBACA"}
|
|
@@ -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,9 @@
|
|
|
1
1
|
// Only internals that are used by other Snaps packages should be exported here.
|
|
2
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 './error-wrappers';
|
|
6
|
+
export * from './images';
|
|
4
7
|
export * from './types';
|
|
5
8
|
export * from './ui';
|
|
6
9
|
|
package/dist/esm/index.js.map
CHANGED
|
@@ -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 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';\n\nexport * from './errors';\nexport * from './types';\nexport * from './ui';\n"],"names":["getErrorData","getErrorMessage","getErrorStack","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE","literal","union","enumValue"],"mappings":"AAAA,gFAAgF;AAEhF,SACEA,YAAY,EACZC,eAAe,EACfC,aAAa,EACbC,eAAe,EACfC,kBAAkB,EAClBC,OAAO,EACPC,KAAK,EACLC,SAAS,QACJ,cAAc;
|
|
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 './error-wrappers';\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,mBAAmB;AACjC,cAAc,WAAW;AACzB,cAAc,UAAU;AACxB,cAAc,OAAO"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get image data as data-string from a URL. This is useful for embedding images
|
|
3
|
+
* inside of SVGs. Only JPEG and PNG images are supported.
|
|
4
|
+
*
|
|
5
|
+
* Note: This function uses `fetch` to get the image data. This means that using
|
|
6
|
+
* it requires the `endowment:network-access` permission.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const imageData = await getImageData('https://cataas.com/cat');
|
|
10
|
+
* const svg = `
|
|
11
|
+
* <svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
|
|
12
|
+
* <image href="${imageData}" />
|
|
13
|
+
* </svg>
|
|
14
|
+
* `;
|
|
15
|
+
*
|
|
16
|
+
* // Render the SVG in a Snap UI.
|
|
17
|
+
* const ui = image(svg);
|
|
18
|
+
* @param url - The URL to get the image data from.
|
|
19
|
+
* @param options - The options to use when fetching the image data. This is
|
|
20
|
+
* passed directly to `fetch`.
|
|
21
|
+
* @returns A promise that resolves to the image data as a data-string.
|
|
22
|
+
*/
|
|
23
|
+
export declare function getImageData(url: string, options?: RequestInit): Promise<string>;
|
|
24
|
+
/**
|
|
25
|
+
* Options for getting an SVG image element from a URL.
|
|
26
|
+
*
|
|
27
|
+
* @property width - The width of the image.
|
|
28
|
+
* @property height - The height of the image. If this is not provided, the
|
|
29
|
+
* width will be used as the height.
|
|
30
|
+
* @property request - The options to use when fetching the image data. This is
|
|
31
|
+
* passed directly to `fetch`.
|
|
32
|
+
*/
|
|
33
|
+
export declare type ImageOptions = {
|
|
34
|
+
width: number;
|
|
35
|
+
height?: number;
|
|
36
|
+
request?: RequestInit;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Get an image component from a URL. This is useful for embedding images inside
|
|
40
|
+
* Snap UIs. Only JPEG and PNG images are supported.
|
|
41
|
+
*
|
|
42
|
+
* Note: This function uses `fetch` to get the image data. This means that using
|
|
43
|
+
* it requires the `endowment:network-access` permission.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* const component = await getImage('https://cataas.com/cat');
|
|
47
|
+
*
|
|
48
|
+
* return await snap.request({
|
|
49
|
+
* method: 'snap_dialog',
|
|
50
|
+
* params: {
|
|
51
|
+
* type: 'alert',
|
|
52
|
+
* content: panel([
|
|
53
|
+
* component,
|
|
54
|
+
* ]),
|
|
55
|
+
* },
|
|
56
|
+
* });
|
|
57
|
+
* @param url - The URL to get the image data from.
|
|
58
|
+
* @param options - The options to use when fetching and rendering the image.
|
|
59
|
+
* @param options.width - The width of the image.
|
|
60
|
+
* @param options.height - The height of the image. If this is not provided, the
|
|
61
|
+
* width will be used as the height.
|
|
62
|
+
* @param options.request - The options to use when fetching the image data.
|
|
63
|
+
* This is passed directly to `fetch`.
|
|
64
|
+
* @returns A promise that resolves to the image data as an image component.
|
|
65
|
+
*/
|
|
66
|
+
export declare function getImageComponent(url: string, { width, height, request }: ImageOptions): Promise<{
|
|
67
|
+
value: string;
|
|
68
|
+
type: import("./ui").NodeType.Image;
|
|
69
|
+
}>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
export type { EnumToUnion } from './internals';
|
|
2
2
|
export { getErrorData, getErrorMessage, getErrorStack, SNAP_ERROR_CODE, SNAP_ERROR_MESSAGE, literal, union, enumValue, } from './internals';
|
|
3
3
|
export type { Json, JsonRpcError, JsonRpcRequest, JsonRpcParams, } from '@metamask/utils';
|
|
4
|
+
export { assert } from '@metamask/utils';
|
|
4
5
|
export * from './errors';
|
|
6
|
+
export * from './error-wrappers';
|
|
7
|
+
export * from './images';
|
|
5
8
|
export * from './types';
|
|
6
9
|
export * from './ui';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/snaps-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/MetaMask/snaps.git"
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@metamask/key-tree": "^9.0.0",
|
|
39
|
-
"@metamask/providers": "^14.0.
|
|
39
|
+
"@metamask/providers": "^14.0.2",
|
|
40
40
|
"@metamask/rpc-errors": "^6.1.0",
|
|
41
41
|
"@metamask/utils": "^8.2.1",
|
|
42
42
|
"is-svg": "^4.4.0",
|
|
@@ -66,6 +66,7 @@
|
|
|
66
66
|
"eslint-plugin-promise": "^6.1.1",
|
|
67
67
|
"expect-type": "^0.17.3",
|
|
68
68
|
"jest": "^29.0.2",
|
|
69
|
+
"jest-fetch-mock": "^3.0.3",
|
|
69
70
|
"jest-it-up": "^2.0.0",
|
|
70
71
|
"prettier": "^2.7.1",
|
|
71
72
|
"prettier-plugin-packagejson": "^2.2.11",
|