@metamask/snaps-utils
Version:
A collection of utilities for MetaMask Snaps
59 lines • 2.27 kB
JavaScript
import { instance, is, optional, refine, size, string, type, union, assert as assertSuperstruct } from "@metamask/superstruct";
import { definePattern, VersionStruct } from "@metamask/utils";
export var NpmSnapFileNames;
(function (NpmSnapFileNames) {
NpmSnapFileNames["PackageJson"] = "package.json";
NpmSnapFileNames["Manifest"] = "snap.manifest.json";
})(NpmSnapFileNames || (NpmSnapFileNames = {}));
export const NameStruct = size(definePattern('Snap Name', /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/u), 1, 214);
// Note we use `type` instead of `object` here, because the latter does not
// allow unknown keys.
export const NpmSnapPackageJsonStruct = type({
version: VersionStruct,
name: NameStruct,
main: optional(size(string(), 1, Infinity)),
repository: optional(type({
type: size(string(), 1, Infinity),
url: size(string(), 1, Infinity),
})),
});
/**
* The possible prefixes for snap ids.
*/
/* eslint-disable @typescript-eslint/naming-convention */
export var SnapIdPrefixes;
(function (SnapIdPrefixes) {
SnapIdPrefixes["npm"] = "npm:";
SnapIdPrefixes["local"] = "local:";
})(SnapIdPrefixes || (SnapIdPrefixes = {}));
/* eslint-enable @typescript-eslint/naming-convention */
/* eslint-disable @typescript-eslint/naming-convention */
export var SNAP_STREAM_NAMES;
(function (SNAP_STREAM_NAMES) {
SNAP_STREAM_NAMES["JSON_RPC"] = "jsonRpc";
SNAP_STREAM_NAMES["COMMAND"] = "command";
})(SNAP_STREAM_NAMES || (SNAP_STREAM_NAMES = {}));
export const uri = (opts = {}) => refine(union([string(), instance(URL)]), 'uri', (value) => {
try {
const url = new URL(value);
const UrlStruct = type(opts);
assertSuperstruct(url, UrlStruct);
return true;
}
catch {
return `Expected URL, got "${value.toString()}".`;
}
});
/**
* Returns whether a given value is a valid URL.
*
* @param url - The value to check.
* @param opts - Optional constraints for url checking.
* @returns Whether `url` is valid URL or not.
*/
export function isValidUrl(url, opts = {}) {
return is(url, uri(opts));
}
// redefining here to avoid circular dependency
export const WALLET_SNAP_PERMISSION_KEY = 'wallet_snap';
//# sourceMappingURL=types.mjs.map