@metamask/snaps-utils 0.37.1-flask.1 → 0.38.0-flask.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 +11 -282
- package/dist/cjs/enum.js +2 -2
- package/dist/cjs/enum.js.map +1 -1
- package/dist/cjs/errors.js +19 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/eval-worker.js.map +1 -1
- package/dist/cjs/eval.js +54 -6
- package/dist/cjs/eval.js.map +1 -1
- package/dist/cjs/handlers.js +59 -0
- package/dist/cjs/handlers.js.map +1 -1
- package/dist/cjs/index.browser.js +3 -0
- package/dist/cjs/index.browser.js.map +1 -1
- package/dist/cjs/index.js +3 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/manifest/manifest.js +4 -4
- package/dist/cjs/manifest/manifest.js.map +1 -1
- package/dist/cjs/snaps.js.map +1 -1
- package/dist/cjs/strings.js +21 -0
- package/dist/cjs/strings.js.map +1 -0
- package/dist/cjs/structs.js +163 -0
- package/dist/cjs/structs.js.map +1 -0
- package/dist/cjs/types.js +2 -10
- package/dist/cjs/types.js.map +1 -1
- package/dist/esm/enum.js +1 -1
- package/dist/esm/enum.js.map +1 -1
- package/dist/esm/errors.js +17 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/eval-worker.js.map +1 -1
- package/dist/esm/eval.js +43 -3
- package/dist/esm/eval.js.map +1 -1
- package/dist/esm/handlers.js +45 -1
- package/dist/esm/handlers.js.map +1 -1
- package/dist/esm/index.browser.js +3 -0
- package/dist/esm/index.browser.js.map +1 -1
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/manifest/manifest.js +5 -4
- package/dist/esm/manifest/manifest.js.map +1 -1
- package/dist/esm/snaps.js.map +1 -1
- package/dist/esm/strings.js +11 -0
- package/dist/esm/strings.js.map +1 -0
- package/dist/esm/structs.js +230 -0
- package/dist/esm/structs.js.map +1 -0
- package/dist/esm/types.js +2 -7
- package/dist/esm/types.js.map +1 -1
- package/dist/types/enum.d.ts +1 -1
- package/dist/types/errors.d.ts +10 -0
- package/dist/types/eval.d.ts +9 -1
- package/dist/types/handlers.d.ts +82 -6
- package/dist/types/index.browser.d.ts +3 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/manifest/manifest.d.ts +3 -1
- package/dist/types/snaps.d.ts +5 -6
- package/dist/types/strings.d.ts +8 -0
- package/dist/types/structs.d.ts +158 -0
- package/dist/types/types.d.ts +1 -5
- package/package.json +6 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/structs.ts"],"sourcesContent":["import { isObject } from '@metamask/utils';\nimport { bold, green, red } from 'chalk';\nimport { resolve } from 'path';\nimport type { Failure, Infer } from 'superstruct';\nimport {\n Struct,\n StructError,\n define,\n literal as superstructLiteral,\n union as superstructUnion,\n create,\n string,\n coerce,\n} from 'superstruct';\nimport type { AnyStruct, InferStructTuple } from 'superstruct/dist/utils';\n\nimport { indent } from './strings';\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 * A wrapper of `superstruct`'s `string` struct that coerces a value to a string\n * and resolves it relative to the current working directory. This is useful\n * for specifying file paths in a configuration file, as it allows the user to\n * use both relative and absolute paths.\n *\n * @returns The `superstruct` struct, which validates that the value is a\n * string, and resolves it relative to the current working directory.\n * @example\n * ```ts\n * const config = struct({\n * file: file(),\n * // ...\n * });\n *\n * const value = create({ file: 'path/to/file' }, config);\n * console.log(value.file); // /process/cwd/path/to/file\n * ```\n */\nexport function file() {\n return coerce(string(), string(), (value) => {\n return resolve(process.cwd(), value);\n });\n}\n\n/**\n * Define a struct, and also define the name of the struct as the given name.\n *\n * This is useful for improving the error messages returned by `superstruct`.\n *\n * @param name - The name of the struct.\n * @param struct - The struct.\n * @returns The struct.\n */\nexport function named<Type, Schema>(\n name: string,\n struct: Struct<Type, Schema>,\n) {\n return new Struct({\n ...struct,\n type: name,\n });\n}\n\nexport class SnapsStructError<Type, Schema> extends StructError {\n constructor(\n struct: Struct<Type, Schema>,\n prefix: string,\n suffix: string,\n failure: StructError,\n failures: () => Generator<Failure>,\n ) {\n super(failure, failures);\n\n this.name = 'SnapsStructError';\n this.message = `${prefix}.\\n\\n${getStructErrorMessage(struct, [\n ...failures(),\n ])}${suffix ? `\\n\\n${suffix}` : ''}`;\n }\n}\n\ntype GetErrorOptions<Type, Schema> = {\n struct: Struct<Type, Schema>;\n prefix: string;\n suffix?: string;\n error: StructError;\n};\n\n/**\n * Converts an array to a generator function that yields the items in the\n * array.\n *\n * @param array - The array.\n * @returns A generator function.\n * @yields The items in the array.\n */\nexport function* arrayToGenerator<Type>(\n array: Type[],\n): Generator<Type, void, undefined> {\n for (const item of array) {\n yield item;\n }\n}\n\n/**\n * Returns a `SnapsStructError` with the given prefix and suffix.\n *\n * @param options - The options.\n * @param options.struct - The struct that caused the error.\n * @param options.prefix - The prefix to add to the error message.\n * @param options.suffix - The suffix to add to the error message. Defaults to\n * an empty string.\n * @param options.error - The `superstruct` error to wrap.\n * @returns The `SnapsStructError`.\n */\nexport function getError<Type, Schema>({\n struct,\n prefix,\n suffix = '',\n error,\n}: GetErrorOptions<Type, Schema>) {\n return new SnapsStructError(struct, prefix, suffix, error, () =>\n arrayToGenerator(error.failures()),\n );\n}\n\n/**\n * A wrapper of `superstruct`'s `create` function that throws a\n * `SnapsStructError` instead of a `StructError`. This is useful for improving\n * the error messages returned by `superstruct`.\n *\n * @param value - The value to validate.\n * @param struct - The `superstruct` struct to validate the value against.\n * @param prefix - The prefix to add to the error message.\n * @param suffix - The suffix to add to the error message. Defaults to an empty\n * string.\n * @returns The validated value.\n */\nexport function createFromStruct<Type, Schema>(\n value: unknown,\n struct: Struct<Type, Schema>,\n prefix: string,\n suffix = '',\n) {\n try {\n return create(value, struct);\n } catch (error) {\n if (error instanceof StructError) {\n throw getError({ struct, prefix, suffix, error });\n }\n\n throw error;\n }\n}\n\n/**\n * Get a struct from a failure path.\n *\n * @param struct - The struct.\n * @param path - The failure path.\n * @returns The struct at the failure path.\n */\nexport function getStructFromPath<Type, Schema>(\n struct: Struct<Type, Schema>,\n path: string[],\n) {\n return path.reduce<AnyStruct>((result, key) => {\n if (isObject(struct.schema) && struct.schema[key]) {\n return struct.schema[key] as AnyStruct;\n }\n\n return result;\n }, struct);\n}\n\n/**\n * Get the union struct names from a struct.\n *\n * @param struct - The struct.\n * @returns The union struct names, or `null` if the struct is not a union\n * struct.\n */\nexport function getUnionStructNames<Type, Schema>(\n struct: Struct<Type, Schema>,\n) {\n if (Array.isArray(struct.schema)) {\n return struct.schema.map(({ type }) => green(type));\n }\n\n return null;\n}\n\n/**\n * Get a error prefix from a `superstruct` failure. This is useful for\n * formatting the error message returned by `superstruct`.\n *\n * @param failure - The `superstruct` failure.\n * @returns The error prefix.\n */\nexport function getStructErrorPrefix(failure: Failure) {\n if (failure.type === 'never' || failure.path.length === 0) {\n return '';\n }\n\n return `At path: ${bold(failure.path.join('.'))} — `;\n}\n\n/**\n * Get a string describing the failure. This is similar to the `message`\n * property of `superstruct`'s `Failure` type, but formats the value in a more\n * readable way.\n *\n * @param struct - The struct that caused the failure.\n * @param failure - The `superstruct` failure.\n * @returns A string describing the failure.\n */\nexport function getStructFailureMessage<Type, Schema>(\n struct: Struct<Type, Schema>,\n failure: Failure,\n) {\n const received = red(JSON.stringify(failure.value));\n const prefix = getStructErrorPrefix(failure);\n\n if (failure.type === 'union') {\n const childStruct = getStructFromPath(struct, failure.path);\n const unionNames = getUnionStructNames(childStruct);\n\n if (unionNames) {\n return `${prefix}Expected the value to be one of: ${unionNames.join(\n ', ',\n )}, but received: ${received}.`;\n }\n\n return `${prefix}${failure.message}.`;\n }\n\n if (failure.type === 'literal') {\n // Superstruct's failure does not provide information about which literal\n // value was expected, so we need to parse the message to get the literal.\n const message = failure.message\n .replace(/the literal `(.+)`,/u, `the value to be \\`${green('$1')}\\`,`)\n .replace(/, but received: (.+)/u, `, but received: ${red('$1')}`);\n\n return `${prefix}${message}.`;\n }\n\n if (failure.type === 'never') {\n return `Unknown key: ${bold(\n failure.path.join('.'),\n )}, received: ${received}.`;\n }\n\n return `${prefix}Expected a value of type ${green(\n failure.type,\n )}, but received: ${received}.`;\n}\n\n/**\n * Get a string describing the errors. This formats all the errors in a\n * human-readable way.\n *\n * @param struct - The struct that caused the failures.\n * @param failures - The `superstruct` failures.\n * @returns A string describing the errors.\n */\nexport function getStructErrorMessage<Type, Schema>(\n struct: Struct<Type, Schema>,\n failures: Failure[],\n) {\n const formattedFailures = failures.map((failure) =>\n indent(`• ${getStructFailureMessage(struct, failure)}`),\n );\n\n return formattedFailures.join('\\n');\n}\n"],"names":["isObject","bold","green","red","resolve","Struct","StructError","define","literal","superstructLiteral","union","superstructUnion","create","string","coerce","indent","value","JSON","stringify","validator","head","tail","struct","schema","file","process","cwd","named","name","type","SnapsStructError","constructor","prefix","suffix","failure","failures","message","getStructErrorMessage","arrayToGenerator","array","item","getError","error","createFromStruct","getStructFromPath","path","reduce","result","key","getUnionStructNames","Array","isArray","map","getStructErrorPrefix","length","join","getStructFailureMessage","received","childStruct","unionNames","replace","formattedFailures"],"mappings":"AAAA,SAASA,QAAQ,QAAQ,kBAAkB;AAC3C,SAASC,IAAI,EAAEC,KAAK,EAAEC,GAAG,QAAQ,QAAQ;AACzC,SAASC,OAAO,QAAQ,OAAO;AAE/B,SACEC,MAAM,EACNC,WAAW,EACXC,MAAM,EACNC,WAAWC,kBAAkB,EAC7BC,SAASC,gBAAgB,EACzBC,MAAM,EACNC,MAAM,EACNC,MAAM,QACD,cAAc;AAGrB,SAASC,MAAM,QAAQ,YAAY;AAEnC;;;;;;;;;;;;;;;;;;;;CAoBC,GACD,OAAO,SAASP,QAAgDQ,KAAW;IACzE,OAAOT,OACLU,KAAKC,SAAS,CAACF,QACfP,mBAAmBO,OAAOG,SAAS;AAEvC;AAEA;;;;;;;;;;;CAWC,GACD,OAAO,SAAST,MAAwD,CACtEU,MACA,GAAGC,KACyB;IAI5B,MAAMC,SAASX,iBAAiB;QAACS;WAASC;KAAK;IAE/C,OAAO,IAAIhB,OAAO;QAChB,GAAGiB,MAAM;QACTC,QAAQ;YAACH;eAASC;SAAK;IACzB;AACF;AAEA;;;;;;;;;;;;;;;;;;CAkBC,GACD,OAAO,SAASG;IACd,OAAOV,OAAOD,UAAUA,UAAU,CAACG;QACjC,OAAOZ,QAAQqB,QAAQC,GAAG,IAAIV;IAChC;AACF;AAEA;;;;;;;;CAQC,GACD,OAAO,SAASW,MACdC,IAAY,EACZN,MAA4B;IAE5B,OAAO,IAAIjB,OAAO;QAChB,GAAGiB,MAAM;QACTO,MAAMD;IACR;AACF;AAEA,OAAO,MAAME,yBAAuCxB;IAClD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AIF,QAAQL,IAAI,KAAK,SAAS;QAC5B,OAAO,CAAC,aAAa,EAAE5B,KACrBiC,QAAQW,IAAI,CAACU,IAAI,CAAC,MAClB,YAAY,EAAEE,SAAS,CAAC,CAAC;IAC7B;IAEA,OAAO,CAAC,EAAEzB,OAAO,yBAAyB,EAAE9B,MAC1CgC,QAAQL,IAAI,EACZ,gBAAgB,EAAE4B,SAAS,CAAC,CAAC;AACjC;AAEA;;;;;;;CAOC,GACD,OAAO,SAASpB,sBACdf,MAA4B,EAC5Ba,QAAmB;IAEnB,MAAM0B,oBAAoB1B,SAASiB,GAAG,CAAC,CAAClB,UACtCnB,OAAO,CAAC,EAAE,EAAEyC,wBAAwBlC,QAAQY,SAAS,CAAC;IAGxD,OAAO2B,kBAAkBN,IAAI,CAAC;AAChC"}
|
package/dist/esm/types.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { assertStruct, VersionStruct } from '@metamask/utils';
|
|
2
2
|
import { instance, is, object, optional, pattern, refine, size, string, type, union, assert as assertSuperstruct } from 'superstruct';
|
|
3
|
+
import { HandlerType } from './handlers';
|
|
3
4
|
export var NpmSnapFileNames;
|
|
4
5
|
(function(NpmSnapFileNames) {
|
|
5
6
|
NpmSnapFileNames["PackageJson"] = 'package.json';
|
|
@@ -50,13 +51,7 @@ export var SNAP_STREAM_NAMES;
|
|
|
50
51
|
SNAP_STREAM_NAMES["JSON_RPC"] = 'jsonRpc';
|
|
51
52
|
SNAP_STREAM_NAMES["COMMAND"] = 'command';
|
|
52
53
|
})(SNAP_STREAM_NAMES || (SNAP_STREAM_NAMES = {}));
|
|
53
|
-
export
|
|
54
|
-
(function(HandlerType) {
|
|
55
|
-
HandlerType["OnRpcRequest"] = 'onRpcRequest';
|
|
56
|
-
HandlerType["OnTransaction"] = 'onTransaction';
|
|
57
|
-
HandlerType["OnCronjob"] = 'onCronjob';
|
|
58
|
-
})(HandlerType || (HandlerType = {}));
|
|
59
|
-
export const SNAP_EXPORT_NAMES = Object.values(HandlerType);
|
|
54
|
+
/* eslint-enable @typescript-eslint/naming-convention */ export const SNAP_EXPORT_NAMES = Object.values(HandlerType);
|
|
60
55
|
export const uri = (opts = {})=>refine(union([
|
|
61
56
|
string(),
|
|
62
57
|
instance(URL)
|
package/dist/esm/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/types.ts"],"sourcesContent":["import type { Json } from '@metamask/utils';\nimport { assertStruct, VersionStruct } from '@metamask/utils';\nimport type { Infer, Struct } from 'superstruct';\nimport {\n instance,\n is,\n object,\n optional,\n pattern,\n refine,\n size,\n string,\n type,\n union,\n assert as assertSuperstruct,\n} from 'superstruct';\n\nimport type { SnapCaveatType } from './caveats';\nimport type { SnapFunctionExports } from './handlers';\nimport type { SnapManifest } from './manifest';\nimport type { VirtualFile } from './virtual-file';\n\nexport enum NpmSnapFileNames {\n PackageJson = 'package.json',\n Manifest = 'snap.manifest.json',\n}\n\nexport const NameStruct = size(\n pattern(\n string(),\n /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\\/)?[a-z0-9-~][a-z0-9-._~]*$/u,\n ),\n 1,\n 214,\n);\n\n// Note we use `type` instead of `object` here, because the latter does not\n// allow unknown keys.\nexport const NpmSnapPackageJsonStruct = type({\n version: VersionStruct,\n name: NameStruct,\n main: optional(size(string(), 1, Infinity)),\n repository: optional(\n object({\n type: size(string(), 1, Infinity),\n url: size(string(), 1, Infinity),\n }),\n ),\n});\n\nexport type NpmSnapPackageJson = Infer<typeof NpmSnapPackageJsonStruct> &\n Record<string, any>;\n\n/**\n * Check if the given value is a valid {@link NpmSnapPackageJson} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link NpmSnapPackageJson} object.\n */\nexport function isNpmSnapPackageJson(\n value: unknown,\n): value is NpmSnapPackageJson {\n return is(value, NpmSnapPackageJsonStruct);\n}\n\n/**\n * Asserts that the given value is a valid {@link NpmSnapPackageJson} object.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid {@link NpmSnapPackageJson} object.\n */\nexport function assertIsNpmSnapPackageJson(\n value: unknown,\n): asserts value is NpmSnapPackageJson {\n assertStruct(\n value,\n NpmSnapPackageJsonStruct,\n `\"${NpmSnapFileNames.PackageJson}\" is invalid`,\n );\n}\n\n/**\n * An object for storing parsed but unvalidated Snap file contents.\n */\nexport type UnvalidatedSnapFiles = {\n manifest?: VirtualFile<Json>;\n packageJson?: VirtualFile<Json>;\n sourceCode?: VirtualFile;\n svgIcon?: VirtualFile;\n};\n\n/**\n * An object for storing the contents of Snap files that have passed JSON\n * Schema validation, or are non-empty if they are strings.\n */\nexport type SnapFiles = {\n manifest: VirtualFile<SnapManifest>;\n packageJson: VirtualFile<NpmSnapPackageJson>;\n sourceCode: VirtualFile;\n svgIcon?: VirtualFile;\n};\n\n/**\n * The possible prefixes for snap ids.\n */\n/* eslint-disable @typescript-eslint/naming-convention */\nexport enum SnapIdPrefixes {\n npm = 'npm:',\n local = 'local:',\n}\n/* eslint-enable @typescript-eslint/naming-convention */\n\nexport type SnapId = string;\n\n/**\n * Snap validation failure reason codes that are programmatically fixable\n * if validation occurs during development.\n */\nexport enum SnapValidationFailureReason {\n NameMismatch = '\"name\" field mismatch',\n VersionMismatch = '\"version\" field mismatch',\n RepositoryMismatch = '\"repository\" field mismatch',\n ShasumMismatch = '\"shasum\" field mismatch',\n}\n\n/* eslint-disable @typescript-eslint/naming-convention */\nexport enum SNAP_STREAM_NAMES {\n JSON_RPC = 'jsonRpc',\n COMMAND = 'command',\n}\n/* eslint-enable @typescript-eslint/naming-convention */\n\nexport
|
|
1
|
+
{"version":3,"sources":["../../src/types.ts"],"sourcesContent":["import type { Json } from '@metamask/utils';\nimport { assertStruct, VersionStruct } from '@metamask/utils';\nimport type { Infer, Struct } from 'superstruct';\nimport {\n instance,\n is,\n object,\n optional,\n pattern,\n refine,\n size,\n string,\n type,\n union,\n assert as assertSuperstruct,\n} from 'superstruct';\n\nimport type { SnapCaveatType } from './caveats';\nimport type { SnapFunctionExports } from './handlers';\nimport { HandlerType } from './handlers';\nimport type { SnapManifest } from './manifest';\nimport type { VirtualFile } from './virtual-file';\n\nexport enum NpmSnapFileNames {\n PackageJson = 'package.json',\n Manifest = 'snap.manifest.json',\n}\n\nexport const NameStruct = size(\n pattern(\n string(),\n /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\\/)?[a-z0-9-~][a-z0-9-._~]*$/u,\n ),\n 1,\n 214,\n);\n\n// Note we use `type` instead of `object` here, because the latter does not\n// allow unknown keys.\nexport const NpmSnapPackageJsonStruct = type({\n version: VersionStruct,\n name: NameStruct,\n main: optional(size(string(), 1, Infinity)),\n repository: optional(\n object({\n type: size(string(), 1, Infinity),\n url: size(string(), 1, Infinity),\n }),\n ),\n});\n\nexport type NpmSnapPackageJson = Infer<typeof NpmSnapPackageJsonStruct> &\n Record<string, any>;\n\n/**\n * Check if the given value is a valid {@link NpmSnapPackageJson} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link NpmSnapPackageJson} object.\n */\nexport function isNpmSnapPackageJson(\n value: unknown,\n): value is NpmSnapPackageJson {\n return is(value, NpmSnapPackageJsonStruct);\n}\n\n/**\n * Asserts that the given value is a valid {@link NpmSnapPackageJson} object.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid {@link NpmSnapPackageJson} object.\n */\nexport function assertIsNpmSnapPackageJson(\n value: unknown,\n): asserts value is NpmSnapPackageJson {\n assertStruct(\n value,\n NpmSnapPackageJsonStruct,\n `\"${NpmSnapFileNames.PackageJson}\" is invalid`,\n );\n}\n\n/**\n * An object for storing parsed but unvalidated Snap file contents.\n */\nexport type UnvalidatedSnapFiles = {\n manifest?: VirtualFile<Json>;\n packageJson?: VirtualFile<Json>;\n sourceCode?: VirtualFile;\n svgIcon?: VirtualFile;\n};\n\n/**\n * An object for storing the contents of Snap files that have passed JSON\n * Schema validation, or are non-empty if they are strings.\n */\nexport type SnapFiles = {\n manifest: VirtualFile<SnapManifest>;\n packageJson: VirtualFile<NpmSnapPackageJson>;\n sourceCode: VirtualFile;\n svgIcon?: VirtualFile;\n};\n\n/**\n * The possible prefixes for snap ids.\n */\n/* eslint-disable @typescript-eslint/naming-convention */\nexport enum SnapIdPrefixes {\n npm = 'npm:',\n local = 'local:',\n}\n/* eslint-enable @typescript-eslint/naming-convention */\n\nexport type SnapId = string;\n\n/**\n * Snap validation failure reason codes that are programmatically fixable\n * if validation occurs during development.\n */\nexport enum SnapValidationFailureReason {\n NameMismatch = '\"name\" field mismatch',\n VersionMismatch = '\"version\" field mismatch',\n RepositoryMismatch = '\"repository\" field mismatch',\n ShasumMismatch = '\"shasum\" field mismatch',\n}\n\n/* eslint-disable @typescript-eslint/naming-convention */\nexport enum SNAP_STREAM_NAMES {\n JSON_RPC = 'jsonRpc',\n COMMAND = 'command',\n}\n/* eslint-enable @typescript-eslint/naming-convention */\n\nexport const SNAP_EXPORT_NAMES = Object.values(HandlerType);\n\nexport type SnapRpcHookArgs = {\n origin: string;\n handler: HandlerType;\n request: Record<string, unknown>;\n};\n\n// The snap is the callee\nexport type SnapRpcHook = (options: SnapRpcHookArgs) => Promise<unknown>;\n\ntype ObjectParameters<\n Type extends Record<string, (...args: any[]) => unknown>,\n> = Parameters<Type[keyof Type]>;\n\nexport type SnapExportsParameters = ObjectParameters<SnapFunctionExports>;\n\ntype UriOptions<Type extends string> = {\n protocol?: Struct<Type>;\n hash?: Struct<Type>;\n port?: Struct<Type>;\n hostname?: Struct<Type>;\n pathname?: Struct<Type>;\n search?: Struct<Type>;\n};\n\nexport const uri = (opts: UriOptions<any> = {}) =>\n refine(union([string(), instance(URL)]), 'uri', (value) => {\n try {\n const url = new URL(value);\n\n const UrlStruct = type(opts);\n assertSuperstruct(url, UrlStruct);\n return true;\n } catch {\n return `Expected URL, got \"${value.toString()}\".`;\n }\n });\n\n/**\n * Returns whether a given value is a valid URL.\n *\n * @param url - The value to check.\n * @param opts - Optional constraints for url checking.\n * @returns Whether `url` is valid URL or not.\n */\nexport function isValidUrl(\n url: unknown,\n opts: UriOptions<any> = {},\n): url is string | URL {\n return is(url, uri(opts));\n}\n\n// redefining here to avoid circular dependency\nexport const WALLET_SNAP_PERMISSION_KEY = 'wallet_snap';\n\nexport type SnapsPermissionRequest = {\n [WALLET_SNAP_PERMISSION_KEY]: {\n caveats: [\n {\n type: SnapCaveatType.SnapIds;\n value: Record<string, Json>;\n },\n ];\n };\n};\n"],"names":["assertStruct","VersionStruct","instance","is","object","optional","pattern","refine","size","string","type","union","assert","assertSuperstruct","HandlerType","NpmSnapFileNames","PackageJson","Manifest","NameStruct","NpmSnapPackageJsonStruct","version","name","main","Infinity","repository","url","isNpmSnapPackageJson","value","assertIsNpmSnapPackageJson","SnapIdPrefixes","npm","local","SnapValidationFailureReason","NameMismatch","VersionMismatch","RepositoryMismatch","ShasumMismatch","SNAP_STREAM_NAMES","JSON_RPC","COMMAND","SNAP_EXPORT_NAMES","Object","values","uri","opts","URL","UrlStruct","toString","isValidUrl","WALLET_SNAP_PERMISSION_KEY"],"mappings":"AACA,SAASA,YAAY,EAAEC,aAAa,QAAQ,kBAAkB;AAE9D,SACEC,QAAQ,EACRC,EAAE,EACFC,MAAM,EACNC,QAAQ,EACRC,OAAO,EACPC,MAAM,EACNC,IAAI,EACJC,MAAM,EACNC,IAAI,EACJC,KAAK,EACLC,UAAUC,iBAAiB,QACtB,cAAc;AAIrB,SAASC,WAAW,QAAQ,aAAa;WAIlC;UAAKC,gBAAgB;IAAhBA,iBACVC,iBAAc;IADJD,iBAEVE,cAAW;GAFDF,qBAAAA;AAKZ,OAAO,MAAMG,aAAaV,KACxBF,QACEG,UACA,gEAEF,GACA,KACA;AAEF,2EAA2E;AAC3E,sBAAsB;AACtB,OAAO,MAAMU,2BAA2BT,KAAK;IAC3CU,SAASnB;IACToB,MAAMH;IACNI,MAAMjB,SAASG,KAAKC,UAAU,GAAGc;IACjCC,YAAYnB,SACVD,OAAO;QACLM,MAAMF,KAAKC,UAAU,GAAGc;QACxB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
27
27
|
* @param constant - The enum to validate against.
|
|
28
28
|
* @returns The superstruct struct.
|
|
29
29
|
*/
|
|
30
|
-
export declare function enumValue<Type extends string>(constant: Type): Struct<EnumToUnion<Type>,
|
|
30
|
+
export declare function enumValue<Type extends string>(constant: Type): Struct<EnumToUnion<Type>, null>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the error message from an unknown error type.
|
|
3
|
+
*
|
|
4
|
+
* - If the error is an object with a `message` property, return the message.
|
|
5
|
+
* - Otherwise, return the error converted to a string.
|
|
6
|
+
*
|
|
7
|
+
* @param error - The error to get the message from.
|
|
8
|
+
* @returns The error message.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getErrorMessage(error: unknown): string;
|
package/dist/types/eval.d.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
export declare type EvalOutput = {
|
|
2
|
+
stdout: string;
|
|
3
|
+
stderr: string;
|
|
4
|
+
};
|
|
5
|
+
export declare class SnapEvalError extends Error {
|
|
6
|
+
readonly output: EvalOutput;
|
|
7
|
+
constructor(message: string, output: EvalOutput);
|
|
8
|
+
}
|
|
1
9
|
/**
|
|
2
10
|
* Spawn a new process to run the provided bundle in.
|
|
3
11
|
*
|
|
@@ -5,4 +13,4 @@
|
|
|
5
13
|
* @returns `null` if the worker ran successfully.
|
|
6
14
|
* @throws If the worker failed to run successfully.
|
|
7
15
|
*/
|
|
8
|
-
export declare function evalBundle(bundlePath: string): Promise<
|
|
16
|
+
export declare function evalBundle(bundlePath: string): Promise<EvalOutput>;
|
package/dist/types/handlers.d.ts
CHANGED
|
@@ -1,5 +1,60 @@
|
|
|
1
1
|
import type { Component } from '@metamask/snaps-ui';
|
|
2
|
-
import type { Json, JsonRpcRequest } from '@metamask/utils';
|
|
2
|
+
import type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';
|
|
3
|
+
export declare enum HandlerType {
|
|
4
|
+
OnRpcRequest = "onRpcRequest",
|
|
5
|
+
OnTransaction = "onTransaction",
|
|
6
|
+
OnCronjob = "onCronjob",
|
|
7
|
+
OnInstall = "onInstall",
|
|
8
|
+
OnUpdate = "onUpdate"
|
|
9
|
+
}
|
|
10
|
+
declare type SnapHandler = {
|
|
11
|
+
/**
|
|
12
|
+
* The type of handler.
|
|
13
|
+
*/
|
|
14
|
+
type: HandlerType;
|
|
15
|
+
/**
|
|
16
|
+
* Whether the handler is required, i.e., whether the request will fail if the
|
|
17
|
+
* handler is called, but the snap does not export it.
|
|
18
|
+
*
|
|
19
|
+
* This is primarily used for the lifecycle handlers, which are optional.
|
|
20
|
+
*/
|
|
21
|
+
required: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Validate the given snap export. This should return a type guard for the
|
|
24
|
+
* handler type.
|
|
25
|
+
*
|
|
26
|
+
* @param snapExport - The export to validate.
|
|
27
|
+
* @returns Whether the export is valid.
|
|
28
|
+
*/
|
|
29
|
+
validator: (snapExport: unknown) => boolean;
|
|
30
|
+
};
|
|
31
|
+
export declare const SNAP_EXPORTS: {
|
|
32
|
+
readonly onRpcRequest: {
|
|
33
|
+
readonly type: HandlerType.OnRpcRequest;
|
|
34
|
+
readonly required: true;
|
|
35
|
+
readonly validator: (snapExport: unknown) => snapExport is OnRpcRequestHandler<Record<string, Json> | Json[] | undefined>;
|
|
36
|
+
};
|
|
37
|
+
readonly onTransaction: {
|
|
38
|
+
readonly type: HandlerType.OnTransaction;
|
|
39
|
+
readonly required: true;
|
|
40
|
+
readonly validator: (snapExport: unknown) => snapExport is OnTransactionHandler;
|
|
41
|
+
};
|
|
42
|
+
readonly onCronjob: {
|
|
43
|
+
readonly type: HandlerType.OnCronjob;
|
|
44
|
+
readonly required: true;
|
|
45
|
+
readonly validator: (snapExport: unknown) => snapExport is OnCronjobHandler<Record<string, Json> | Json[] | undefined>;
|
|
46
|
+
};
|
|
47
|
+
readonly onInstall: {
|
|
48
|
+
readonly type: HandlerType.OnInstall;
|
|
49
|
+
readonly required: false;
|
|
50
|
+
readonly validator: (snapExport: unknown) => snapExport is LifecycleEventHandler;
|
|
51
|
+
};
|
|
52
|
+
readonly onUpdate: {
|
|
53
|
+
readonly type: HandlerType.OnUpdate;
|
|
54
|
+
readonly required: false;
|
|
55
|
+
readonly validator: (snapExport: unknown) => snapExport is LifecycleEventHandler;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
3
58
|
/**
|
|
4
59
|
* The `onRpcRequest` handler. This is called whenever a JSON-RPC request is
|
|
5
60
|
* made to the snap.
|
|
@@ -10,7 +65,7 @@ import type { Json, JsonRpcRequest } from '@metamask/utils';
|
|
|
10
65
|
* @param args.request - The JSON-RPC request sent to the snap.
|
|
11
66
|
* @returns The JSON-RPC response. This must be a JSON-serializable value.
|
|
12
67
|
*/
|
|
13
|
-
export declare type OnRpcRequestHandler<Params extends
|
|
68
|
+
export declare type OnRpcRequestHandler<Params extends JsonRpcParams = JsonRpcParams> = (args: {
|
|
14
69
|
origin: string;
|
|
15
70
|
request: JsonRpcRequest<Params>;
|
|
16
71
|
}) => Promise<unknown>;
|
|
@@ -51,18 +106,39 @@ export declare type OnTransactionHandler = (args: {
|
|
|
51
106
|
* @param args - The request arguments.
|
|
52
107
|
* @param args.request - The JSON-RPC request sent to the snap.
|
|
53
108
|
*/
|
|
54
|
-
export declare type OnCronjobHandler<Params extends
|
|
109
|
+
export declare type OnCronjobHandler<Params extends JsonRpcParams = JsonRpcParams> = (args: {
|
|
55
110
|
request: JsonRpcRequest<Params>;
|
|
56
111
|
}) => Promise<unknown>;
|
|
112
|
+
/**
|
|
113
|
+
* A handler that can be used for the lifecycle hooks.
|
|
114
|
+
*/
|
|
115
|
+
export declare type LifecycleEventHandler = (args: {
|
|
116
|
+
request: JsonRpcRequest;
|
|
117
|
+
}) => Promise<unknown>;
|
|
118
|
+
/**
|
|
119
|
+
* The `onInstall` handler. This is called after the snap is installed.
|
|
120
|
+
*
|
|
121
|
+
* This type is an alias for {@link LifecycleEventHandler}.
|
|
122
|
+
*/
|
|
123
|
+
export declare type OnInstallHandler = LifecycleEventHandler;
|
|
124
|
+
/**
|
|
125
|
+
* The `onUpdate` handler. This is called after the snap is updated.
|
|
126
|
+
*
|
|
127
|
+
* This type is an alias for {@link LifecycleEventHandler}.
|
|
128
|
+
*/
|
|
129
|
+
export declare type OnUpdateHandler = LifecycleEventHandler;
|
|
130
|
+
/**
|
|
131
|
+
* Utility type for getting the handler function type from a handler type.
|
|
132
|
+
*/
|
|
133
|
+
export declare type HandlerFunction<Type extends SnapHandler> = Type['validator'] extends (snapExport: unknown) => snapExport is infer Handler ? Handler : never;
|
|
57
134
|
/**
|
|
58
135
|
* All the function-based handlers that a snap can implement.
|
|
59
136
|
*/
|
|
60
137
|
export declare type SnapFunctionExports = {
|
|
61
|
-
|
|
62
|
-
onTransaction?: OnTransactionHandler;
|
|
63
|
-
onCronjob?: OnCronjobHandler;
|
|
138
|
+
[Key in keyof typeof SNAP_EXPORTS]?: HandlerFunction<typeof SNAP_EXPORTS[Key]>;
|
|
64
139
|
};
|
|
65
140
|
/**
|
|
66
141
|
* All handlers that a snap can implement.
|
|
67
142
|
*/
|
|
68
143
|
export declare type SnapExports = SnapFunctionExports;
|
|
144
|
+
export {};
|
|
@@ -6,6 +6,7 @@ export * from './deep-clone';
|
|
|
6
6
|
export * from './default-endowments';
|
|
7
7
|
export * from './entropy';
|
|
8
8
|
export * from './enum';
|
|
9
|
+
export * from './errors';
|
|
9
10
|
export * from './handlers';
|
|
10
11
|
export * from './iframe';
|
|
11
12
|
export * from './json';
|
|
@@ -15,6 +16,8 @@ export * from './manifest/index.browser';
|
|
|
15
16
|
export * from './namespace';
|
|
16
17
|
export * from './path';
|
|
17
18
|
export * from './snaps';
|
|
19
|
+
export * from './strings';
|
|
20
|
+
export * from './structs';
|
|
18
21
|
export * from './types';
|
|
19
22
|
export * from './validation';
|
|
20
23
|
export * from './versions';
|
package/dist/types/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from './default-endowments';
|
|
|
7
7
|
export * from './entropy';
|
|
8
8
|
export * from './enum';
|
|
9
9
|
export * from './eval';
|
|
10
|
+
export * from './errors';
|
|
10
11
|
export * from './fs';
|
|
11
12
|
export * from './handlers';
|
|
12
13
|
export * from './iframe';
|
|
@@ -20,6 +21,8 @@ export * from './npm';
|
|
|
20
21
|
export * from './path';
|
|
21
22
|
export * from './post-process';
|
|
22
23
|
export * from './snaps';
|
|
24
|
+
export * from './strings';
|
|
25
|
+
export * from './structs';
|
|
23
26
|
export * from './types';
|
|
24
27
|
export * from './validation';
|
|
25
28
|
export * from './versions';
|
|
@@ -23,6 +23,7 @@ export declare type CheckManifestResult = {
|
|
|
23
23
|
warnings: string[];
|
|
24
24
|
errors: string[];
|
|
25
25
|
};
|
|
26
|
+
export declare type WriteFileFunction = (path: string, data: string) => Promise<void>;
|
|
26
27
|
/**
|
|
27
28
|
* Validates a snap.manifest.json file. Attempts to fix the manifest and write
|
|
28
29
|
* the fixed version to disk if `writeManifest` is true. Throws if validation
|
|
@@ -31,10 +32,11 @@ export declare type CheckManifestResult = {
|
|
|
31
32
|
* @param basePath - The path to the folder with the manifest files.
|
|
32
33
|
* @param writeManifest - Whether to write the fixed manifest to disk.
|
|
33
34
|
* @param sourceCode - The source code of the Snap.
|
|
35
|
+
* @param writeFileFn - The function to use to write the manifest to disk.
|
|
34
36
|
* @returns Whether the manifest was updated, and an array of warnings that
|
|
35
37
|
* were encountered during processing of the manifest files.
|
|
36
38
|
*/
|
|
37
|
-
export declare function checkManifest(basePath: string, writeManifest?: boolean, sourceCode?: string): Promise<CheckManifestResult>;
|
|
39
|
+
export declare function checkManifest(basePath: string, writeManifest?: boolean, sourceCode?: string, writeFileFn?: WriteFileFunction): Promise<CheckManifestResult>;
|
|
38
40
|
/**
|
|
39
41
|
* Given the relevant Snap files (manifest, `package.json`, and bundle) and a
|
|
40
42
|
* Snap manifest validation error, fixes the fault in the manifest that caused
|
package/dist/types/snaps.d.ts
CHANGED
|
@@ -44,12 +44,7 @@ export declare type VersionHistory = {
|
|
|
44
44
|
version: string;
|
|
45
45
|
date: number;
|
|
46
46
|
};
|
|
47
|
-
export declare type PersistedSnap = Snap
|
|
48
|
-
/**
|
|
49
|
-
* The source code of the Snap.
|
|
50
|
-
*/
|
|
51
|
-
sourceCode: string;
|
|
52
|
-
};
|
|
47
|
+
export declare type PersistedSnap = Snap;
|
|
53
48
|
/**
|
|
54
49
|
* A Snap as it exists in {@link SnapController} state.
|
|
55
50
|
*/
|
|
@@ -67,6 +62,10 @@ export declare type Snap = {
|
|
|
67
62
|
* installed.
|
|
68
63
|
*/
|
|
69
64
|
initialPermissions: SnapPermissions;
|
|
65
|
+
/**
|
|
66
|
+
* The source code of the Snap.
|
|
67
|
+
*/
|
|
68
|
+
sourceCode: string;
|
|
70
69
|
/**
|
|
71
70
|
* The Snap's manifest file.
|
|
72
71
|
*/
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Indent a message by adding a number of spaces to the beginning of each line.
|
|
3
|
+
*
|
|
4
|
+
* @param message - The message to indent.
|
|
5
|
+
* @param spaces - The number of spaces to indent by. Defaults to 2.
|
|
6
|
+
* @returns The indented message.
|
|
7
|
+
*/
|
|
8
|
+
export declare function indent(message: string, spaces?: number): string;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type { Failure, Infer } from 'superstruct';
|
|
2
|
+
import { Struct, StructError } from 'superstruct';
|
|
3
|
+
import type { AnyStruct, InferStructTuple } from 'superstruct/dist/utils';
|
|
4
|
+
/**
|
|
5
|
+
* A wrapper of `superstruct`'s `literal` struct that also defines the name of
|
|
6
|
+
* the struct as the literal value.
|
|
7
|
+
*
|
|
8
|
+
* This is useful for improving the error messages returned by `superstruct`.
|
|
9
|
+
* For example, instead of returning an error like:
|
|
10
|
+
*
|
|
11
|
+
* ```
|
|
12
|
+
* Expected the value to satisfy a union of `literal | literal`, but received: \"baz\"
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* This struct will return an error like:
|
|
16
|
+
*
|
|
17
|
+
* ```
|
|
18
|
+
* Expected the value to satisfy a union of `"foo" | "bar"`, but received: \"baz\"
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @param value - The literal value.
|
|
22
|
+
* @returns The `superstruct` struct, which validates that the value is equal
|
|
23
|
+
* to the literal value.
|
|
24
|
+
*/
|
|
25
|
+
export declare function literal<Type extends string | number | boolean>(value: Type): Struct<Type, null>;
|
|
26
|
+
/**
|
|
27
|
+
* A wrapper of `superstruct`'s `union` struct that also defines the schema as
|
|
28
|
+
* the union of the schemas of the structs.
|
|
29
|
+
*
|
|
30
|
+
* This is useful for improving the error messages returned by `superstruct`.
|
|
31
|
+
*
|
|
32
|
+
* @param structs - The structs to union.
|
|
33
|
+
* @param structs."0" - The first struct.
|
|
34
|
+
* @param structs."1" - The remaining structs.
|
|
35
|
+
* @returns The `superstruct` struct, which validates that the value satisfies
|
|
36
|
+
* one of the structs.
|
|
37
|
+
*/
|
|
38
|
+
export declare function union<Head extends AnyStruct, Tail extends AnyStruct[]>([head, ...tail]: [head: Head, ...tail: Tail]): Struct<Infer<Head> | InferStructTuple<Tail>[number], [
|
|
39
|
+
head: Head,
|
|
40
|
+
...tail: Tail
|
|
41
|
+
]>;
|
|
42
|
+
/**
|
|
43
|
+
* A wrapper of `superstruct`'s `string` struct that coerces a value to a string
|
|
44
|
+
* and resolves it relative to the current working directory. This is useful
|
|
45
|
+
* for specifying file paths in a configuration file, as it allows the user to
|
|
46
|
+
* use both relative and absolute paths.
|
|
47
|
+
*
|
|
48
|
+
* @returns The `superstruct` struct, which validates that the value is a
|
|
49
|
+
* string, and resolves it relative to the current working directory.
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* const config = struct({
|
|
53
|
+
* file: file(),
|
|
54
|
+
* // ...
|
|
55
|
+
* });
|
|
56
|
+
*
|
|
57
|
+
* const value = create({ file: 'path/to/file' }, config);
|
|
58
|
+
* console.log(value.file); // /process/cwd/path/to/file
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function file(): Struct<string, null>;
|
|
62
|
+
/**
|
|
63
|
+
* Define a struct, and also define the name of the struct as the given name.
|
|
64
|
+
*
|
|
65
|
+
* This is useful for improving the error messages returned by `superstruct`.
|
|
66
|
+
*
|
|
67
|
+
* @param name - The name of the struct.
|
|
68
|
+
* @param struct - The struct.
|
|
69
|
+
* @returns The struct.
|
|
70
|
+
*/
|
|
71
|
+
export declare function named<Type, Schema>(name: string, struct: Struct<Type, Schema>): Struct<Type, Schema>;
|
|
72
|
+
export declare class SnapsStructError<Type, Schema> extends StructError {
|
|
73
|
+
constructor(struct: Struct<Type, Schema>, prefix: string, suffix: string, failure: StructError, failures: () => Generator<Failure>);
|
|
74
|
+
}
|
|
75
|
+
declare type GetErrorOptions<Type, Schema> = {
|
|
76
|
+
struct: Struct<Type, Schema>;
|
|
77
|
+
prefix: string;
|
|
78
|
+
suffix?: string;
|
|
79
|
+
error: StructError;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Converts an array to a generator function that yields the items in the
|
|
83
|
+
* array.
|
|
84
|
+
*
|
|
85
|
+
* @param array - The array.
|
|
86
|
+
* @returns A generator function.
|
|
87
|
+
* @yields The items in the array.
|
|
88
|
+
*/
|
|
89
|
+
export declare function arrayToGenerator<Type>(array: Type[]): Generator<Type, void, undefined>;
|
|
90
|
+
/**
|
|
91
|
+
* Returns a `SnapsStructError` with the given prefix and suffix.
|
|
92
|
+
*
|
|
93
|
+
* @param options - The options.
|
|
94
|
+
* @param options.struct - The struct that caused the error.
|
|
95
|
+
* @param options.prefix - The prefix to add to the error message.
|
|
96
|
+
* @param options.suffix - The suffix to add to the error message. Defaults to
|
|
97
|
+
* an empty string.
|
|
98
|
+
* @param options.error - The `superstruct` error to wrap.
|
|
99
|
+
* @returns The `SnapsStructError`.
|
|
100
|
+
*/
|
|
101
|
+
export declare function getError<Type, Schema>({ struct, prefix, suffix, error, }: GetErrorOptions<Type, Schema>): SnapsStructError<Type, Schema>;
|
|
102
|
+
/**
|
|
103
|
+
* A wrapper of `superstruct`'s `create` function that throws a
|
|
104
|
+
* `SnapsStructError` instead of a `StructError`. This is useful for improving
|
|
105
|
+
* the error messages returned by `superstruct`.
|
|
106
|
+
*
|
|
107
|
+
* @param value - The value to validate.
|
|
108
|
+
* @param struct - The `superstruct` struct to validate the value against.
|
|
109
|
+
* @param prefix - The prefix to add to the error message.
|
|
110
|
+
* @param suffix - The suffix to add to the error message. Defaults to an empty
|
|
111
|
+
* string.
|
|
112
|
+
* @returns The validated value.
|
|
113
|
+
*/
|
|
114
|
+
export declare function createFromStruct<Type, Schema>(value: unknown, struct: Struct<Type, Schema>, prefix: string, suffix?: string): Type;
|
|
115
|
+
/**
|
|
116
|
+
* Get a struct from a failure path.
|
|
117
|
+
*
|
|
118
|
+
* @param struct - The struct.
|
|
119
|
+
* @param path - The failure path.
|
|
120
|
+
* @returns The struct at the failure path.
|
|
121
|
+
*/
|
|
122
|
+
export declare function getStructFromPath<Type, Schema>(struct: Struct<Type, Schema>, path: string[]): AnyStruct;
|
|
123
|
+
/**
|
|
124
|
+
* Get the union struct names from a struct.
|
|
125
|
+
*
|
|
126
|
+
* @param struct - The struct.
|
|
127
|
+
* @returns The union struct names, or `null` if the struct is not a union
|
|
128
|
+
* struct.
|
|
129
|
+
*/
|
|
130
|
+
export declare function getUnionStructNames<Type, Schema>(struct: Struct<Type, Schema>): string[] | null;
|
|
131
|
+
/**
|
|
132
|
+
* Get a error prefix from a `superstruct` failure. This is useful for
|
|
133
|
+
* formatting the error message returned by `superstruct`.
|
|
134
|
+
*
|
|
135
|
+
* @param failure - The `superstruct` failure.
|
|
136
|
+
* @returns The error prefix.
|
|
137
|
+
*/
|
|
138
|
+
export declare function getStructErrorPrefix(failure: Failure): string;
|
|
139
|
+
/**
|
|
140
|
+
* Get a string describing the failure. This is similar to the `message`
|
|
141
|
+
* property of `superstruct`'s `Failure` type, but formats the value in a more
|
|
142
|
+
* readable way.
|
|
143
|
+
*
|
|
144
|
+
* @param struct - The struct that caused the failure.
|
|
145
|
+
* @param failure - The `superstruct` failure.
|
|
146
|
+
* @returns A string describing the failure.
|
|
147
|
+
*/
|
|
148
|
+
export declare function getStructFailureMessage<Type, Schema>(struct: Struct<Type, Schema>, failure: Failure): string;
|
|
149
|
+
/**
|
|
150
|
+
* Get a string describing the errors. This formats all the errors in a
|
|
151
|
+
* human-readable way.
|
|
152
|
+
*
|
|
153
|
+
* @param struct - The struct that caused the failures.
|
|
154
|
+
* @param failures - The `superstruct` failures.
|
|
155
|
+
* @returns A string describing the errors.
|
|
156
|
+
*/
|
|
157
|
+
export declare function getStructErrorMessage<Type, Schema>(struct: Struct<Type, Schema>, failures: Failure[]): string;
|
|
158
|
+
export {};
|
package/dist/types/types.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { Json } from '@metamask/utils';
|
|
|
2
2
|
import type { Infer, Struct } from 'superstruct';
|
|
3
3
|
import type { SnapCaveatType } from './caveats';
|
|
4
4
|
import type { SnapFunctionExports } from './handlers';
|
|
5
|
+
import { HandlerType } from './handlers';
|
|
5
6
|
import type { SnapManifest } from './manifest';
|
|
6
7
|
import type { VirtualFile } from './virtual-file';
|
|
7
8
|
export declare enum NpmSnapFileNames {
|
|
@@ -85,11 +86,6 @@ export declare enum SNAP_STREAM_NAMES {
|
|
|
85
86
|
JSON_RPC = "jsonRpc",
|
|
86
87
|
COMMAND = "command"
|
|
87
88
|
}
|
|
88
|
-
export declare enum HandlerType {
|
|
89
|
-
OnRpcRequest = "onRpcRequest",
|
|
90
|
-
OnTransaction = "onTransaction",
|
|
91
|
-
OnCronjob = "onCronjob"
|
|
92
|
-
}
|
|
93
89
|
export declare const SNAP_EXPORT_NAMES: HandlerType[];
|
|
94
90
|
export declare type SnapRpcHookArgs = {
|
|
95
91
|
origin: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/snaps-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.38.0-flask.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/MetaMask/snaps.git"
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"lint:misc": "prettier --no-error-on-unmatched-pattern --loglevel warn \"**/*.json\" \"**/*.md\" \"**/*.html\" \"!CHANGELOG.md\" --ignore-path ../../.gitignore",
|
|
53
53
|
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn lint:changelog",
|
|
54
54
|
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
|
|
55
|
-
"lint:changelog": "
|
|
55
|
+
"lint:changelog": "../../scripts/validate-changelog.sh @metamask/snaps-utils",
|
|
56
56
|
"build": "yarn build:source && yarn build:types",
|
|
57
57
|
"build:source": "yarn build:esm && yarn build:cjs",
|
|
58
58
|
"build:types": "tsc --project tsconfig.build.json",
|
|
@@ -72,17 +72,18 @@
|
|
|
72
72
|
"@metamask/permission-controller": "^4.0.0",
|
|
73
73
|
"@metamask/providers": "^11.0.0",
|
|
74
74
|
"@metamask/snaps-registry": "^1.2.1",
|
|
75
|
-
"@metamask/snaps-ui": "^0.37.
|
|
75
|
+
"@metamask/snaps-ui": "^0.37.3-flask.1",
|
|
76
76
|
"@metamask/utils": "^6.0.1",
|
|
77
77
|
"@noble/hashes": "^1.3.1",
|
|
78
78
|
"@scure/base": "^1.1.1",
|
|
79
|
+
"chalk": "^4.1.2",
|
|
79
80
|
"cron-parser": "^4.5.0",
|
|
80
81
|
"eth-rpc-errors": "^4.0.3",
|
|
81
82
|
"fast-deep-equal": "^3.1.3",
|
|
82
83
|
"fast-json-stable-stringify": "^2.1.0",
|
|
83
84
|
"is-svg": "^4.4.0",
|
|
84
85
|
"rfdc": "^1.3.0",
|
|
85
|
-
"semver": "^7.
|
|
86
|
+
"semver": "^7.5.4",
|
|
86
87
|
"ses": "^0.18.1",
|
|
87
88
|
"superstruct": "^1.0.3",
|
|
88
89
|
"validate-npm-package-name": "^5.0.0"
|
|
@@ -103,7 +104,7 @@
|
|
|
103
104
|
"@types/jest": "^27.5.1",
|
|
104
105
|
"@types/mocha": "^10.0.1",
|
|
105
106
|
"@types/node": "^20.3.1",
|
|
106
|
-
"@types/semver": "^7.
|
|
107
|
+
"@types/semver": "^7.5.0",
|
|
107
108
|
"@types/validate-npm-package-name": "^4.0.0",
|
|
108
109
|
"@typescript-eslint/eslint-plugin": "^5.42.1",
|
|
109
110
|
"@typescript-eslint/parser": "^5.42.1",
|