@metamask/utils 3.1.0 → 3.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.
- package/CHANGELOG.md +102 -0
- package/dist/assert.d.ts +26 -2
- package/dist/assert.js +86 -4
- package/dist/assert.js.map +1 -1
- package/dist/bytes.d.ts +31 -1
- package/dist/bytes.js +46 -2
- package/dist/bytes.js.map +1 -1
- package/dist/coercers.d.ts +96 -0
- package/dist/coercers.js +162 -0
- package/dist/coercers.js.map +1 -0
- package/dist/hex.d.ts +21 -1
- package/dist/hex.js +30 -4
- package/dist/hex.js.map +1 -1
- package/dist/hex.test-d.d.ts +1 -0
- package/dist/hex.test-d.js +16 -0
- package/dist/hex.test-d.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/json.d.ts +122 -42
- package/dist/json.js +122 -121
- package/dist/json.js.map +1 -1
- package/dist/json.test-d.d.ts +1 -0
- package/dist/json.test-d.js +62 -0
- package/dist/json.test-d.js.map +1 -0
- package/dist/number.d.ts +72 -0
- package/dist/number.js +102 -0
- package/dist/number.js.map +1 -0
- package/package.json +9 -4
package/dist/coercers.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createHex = exports.createBytes = exports.createBigInt = exports.createNumber = void 0;
|
|
4
|
+
const superstruct_1 = require("superstruct");
|
|
5
|
+
const hex_1 = require("./hex");
|
|
6
|
+
const assert_1 = require("./assert");
|
|
7
|
+
const bytes_1 = require("./bytes");
|
|
8
|
+
const NumberLikeStruct = (0, superstruct_1.union)([(0, superstruct_1.number)(), (0, superstruct_1.bigint)(), (0, superstruct_1.string)(), hex_1.StrictHexStruct]);
|
|
9
|
+
const NumberCoercer = (0, superstruct_1.coerce)((0, superstruct_1.number)(), NumberLikeStruct, Number);
|
|
10
|
+
const BigIntCoercer = (0, superstruct_1.coerce)((0, superstruct_1.bigint)(), NumberLikeStruct, BigInt);
|
|
11
|
+
const BytesLikeStruct = (0, superstruct_1.union)([hex_1.StrictHexStruct, (0, superstruct_1.instance)(Uint8Array)]);
|
|
12
|
+
const BytesCoercer = (0, superstruct_1.coerce)((0, superstruct_1.instance)(Uint8Array), (0, superstruct_1.union)([hex_1.StrictHexStruct]), bytes_1.hexToBytes);
|
|
13
|
+
const HexCoercer = (0, superstruct_1.coerce)(hex_1.StrictHexStruct, (0, superstruct_1.instance)(Uint8Array), bytes_1.bytesToHex);
|
|
14
|
+
/**
|
|
15
|
+
* Create a number from a number-like value.
|
|
16
|
+
*
|
|
17
|
+
* - If the value is a number, it is returned as-is.
|
|
18
|
+
* - If the value is a `bigint`, it is converted to a number.
|
|
19
|
+
* - If the value is a string, it is interpreted as a decimal number.
|
|
20
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is
|
|
21
|
+
* interpreted as a hexadecimal number.
|
|
22
|
+
*
|
|
23
|
+
* This validates that the value is a number-like value, and that the resulting
|
|
24
|
+
* number is not `NaN` or `Infinity`.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const value = createNumber('0x010203');
|
|
29
|
+
* console.log(value); // 66051
|
|
30
|
+
*
|
|
31
|
+
* const otherValue = createNumber(123n);
|
|
32
|
+
* console.log(otherValue); // 123
|
|
33
|
+
* ```
|
|
34
|
+
* @param value - The value to create the number from.
|
|
35
|
+
* @returns The created number.
|
|
36
|
+
* @throws If the value is not a number-like value, or if the resulting number
|
|
37
|
+
* is `NaN` or `Infinity`.
|
|
38
|
+
*/
|
|
39
|
+
function createNumber(value) {
|
|
40
|
+
try {
|
|
41
|
+
const result = (0, superstruct_1.create)(value, NumberCoercer);
|
|
42
|
+
(0, assert_1.assert)(Number.isFinite(result), `Expected a number-like value, got "${value}".`);
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (error instanceof superstruct_1.StructError) {
|
|
47
|
+
throw new Error(`Expected a number-like value, got "${value}".`);
|
|
48
|
+
}
|
|
49
|
+
/* istanbul ignore next */
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.createNumber = createNumber;
|
|
54
|
+
/**
|
|
55
|
+
* Create a `bigint` from a number-like value.
|
|
56
|
+
*
|
|
57
|
+
* - If the value is a number, it is converted to a `bigint`.
|
|
58
|
+
* - If the value is a `bigint`, it is returned as-is.
|
|
59
|
+
* - If the value is a string, it is interpreted as a decimal number and
|
|
60
|
+
* converted to a `bigint`.
|
|
61
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is
|
|
62
|
+
* interpreted as a hexadecimal number and converted to a `bigint`.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* const value = createBigInt('0x010203');
|
|
67
|
+
* console.log(value); // 16909060n
|
|
68
|
+
*
|
|
69
|
+
* const otherValue = createBigInt(123);
|
|
70
|
+
* console.log(otherValue); // 123n
|
|
71
|
+
* ```
|
|
72
|
+
* @param value - The value to create the bigint from.
|
|
73
|
+
* @returns The created bigint.
|
|
74
|
+
* @throws If the value is not a number-like value.
|
|
75
|
+
*/
|
|
76
|
+
function createBigInt(value) {
|
|
77
|
+
try {
|
|
78
|
+
// The `BigInt` constructor throws if the value is not a number-like value.
|
|
79
|
+
// There is no need to validate the value manually.
|
|
80
|
+
return (0, superstruct_1.create)(value, BigIntCoercer);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
if (error instanceof superstruct_1.StructError) {
|
|
84
|
+
throw new Error(`Expected a number-like value, got "${error.value}".`);
|
|
85
|
+
}
|
|
86
|
+
/* istanbul ignore next */
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.createBigInt = createBigInt;
|
|
91
|
+
/**
|
|
92
|
+
* Create a byte array from a bytes-like value.
|
|
93
|
+
*
|
|
94
|
+
* - If the value is a byte array, it is returned as-is.
|
|
95
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is interpreted
|
|
96
|
+
* as a hexadecimal number and converted to a byte array.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const value = createBytes('0x010203');
|
|
101
|
+
* console.log(value); // Uint8Array [ 1, 2, 3 ]
|
|
102
|
+
*
|
|
103
|
+
* const otherValue = createBytes('0x010203');
|
|
104
|
+
* console.log(otherValue); // Uint8Array [ 1, 2, 3 ]
|
|
105
|
+
* ```
|
|
106
|
+
* @param value - The value to create the byte array from.
|
|
107
|
+
* @returns The created byte array.
|
|
108
|
+
* @throws If the value is not a bytes-like value.
|
|
109
|
+
*/
|
|
110
|
+
function createBytes(value) {
|
|
111
|
+
if (typeof value === 'string' && value.toLowerCase() === '0x') {
|
|
112
|
+
return new Uint8Array();
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
return (0, superstruct_1.create)(value, BytesCoercer);
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
if (error instanceof superstruct_1.StructError) {
|
|
119
|
+
throw new Error(`Expected a bytes-like value, got "${error.value}".`);
|
|
120
|
+
}
|
|
121
|
+
/* istanbul ignore next */
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
exports.createBytes = createBytes;
|
|
126
|
+
/**
|
|
127
|
+
* Create a hexadecimal string from a bytes-like value.
|
|
128
|
+
*
|
|
129
|
+
* - If the value is a hex string (i.e., it starts with "0x"), it is returned
|
|
130
|
+
* as-is.
|
|
131
|
+
* - If the value is a `Uint8Array`, it is converted to a hex string.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* const value = createHex(new Uint8Array([1, 2, 3]));
|
|
136
|
+
* console.log(value); // '0x010203'
|
|
137
|
+
*
|
|
138
|
+
* const otherValue = createHex('0x010203');
|
|
139
|
+
* console.log(otherValue); // '0x010203'
|
|
140
|
+
* ```
|
|
141
|
+
* @param value - The value to create the hex string from.
|
|
142
|
+
* @returns The created hex string.
|
|
143
|
+
* @throws If the value is not a bytes-like value.
|
|
144
|
+
*/
|
|
145
|
+
function createHex(value) {
|
|
146
|
+
if ((value instanceof Uint8Array && value.length === 0) ||
|
|
147
|
+
(typeof value === 'string' && value.toLowerCase() === '0x')) {
|
|
148
|
+
return '0x';
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
return (0, superstruct_1.create)(value, HexCoercer);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
if (error instanceof superstruct_1.StructError) {
|
|
155
|
+
throw new Error(`Expected a bytes-like value, got "${error.value}".`);
|
|
156
|
+
}
|
|
157
|
+
/* istanbul ignore next */
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
exports.createHex = createHex;
|
|
162
|
+
//# sourceMappingURL=coercers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coercers.js","sourceRoot":"","sources":["../src/coercers.ts"],"names":[],"mappings":"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","sourcesContent":["import {\n bigint,\n coerce,\n create,\n Infer,\n instance,\n number,\n string,\n StructError,\n union,\n} from 'superstruct';\nimport { Hex, StrictHexStruct } from './hex';\nimport { assert } from './assert';\nimport { bytesToHex, hexToBytes } from './bytes';\n\nconst NumberLikeStruct = union([number(), bigint(), string(), StrictHexStruct]);\nconst NumberCoercer = coerce(number(), NumberLikeStruct, Number);\nconst BigIntCoercer = coerce(bigint(), NumberLikeStruct, BigInt);\n\nconst BytesLikeStruct = union([StrictHexStruct, instance(Uint8Array)]);\nconst BytesCoercer = coerce(\n instance(Uint8Array),\n union([StrictHexStruct]),\n hexToBytes,\n);\n\nconst HexCoercer = coerce(StrictHexStruct, instance(Uint8Array), bytesToHex);\n\nexport type NumberLike = Infer<typeof NumberLikeStruct>;\nexport type BytesLike = Infer<typeof BytesLikeStruct>;\n\n/**\n * Create a number from a number-like value.\n *\n * - If the value is a number, it is returned as-is.\n * - If the value is a `bigint`, it is converted to a number.\n * - If the value is a string, it is interpreted as a decimal number.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is\n * interpreted as a hexadecimal number.\n *\n * This validates that the value is a number-like value, and that the resulting\n * number is not `NaN` or `Infinity`.\n *\n * @example\n * ```typescript\n * const value = createNumber('0x010203');\n * console.log(value); // 66051\n *\n * const otherValue = createNumber(123n);\n * console.log(otherValue); // 123\n * ```\n * @param value - The value to create the number from.\n * @returns The created number.\n * @throws If the value is not a number-like value, or if the resulting number\n * is `NaN` or `Infinity`.\n */\nexport function createNumber(value: NumberLike): number {\n try {\n const result = create(value, NumberCoercer);\n\n assert(\n Number.isFinite(result),\n `Expected a number-like value, got \"${value}\".`,\n );\n\n return result;\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(`Expected a number-like value, got \"${value}\".`);\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a `bigint` from a number-like value.\n *\n * - If the value is a number, it is converted to a `bigint`.\n * - If the value is a `bigint`, it is returned as-is.\n * - If the value is a string, it is interpreted as a decimal number and\n * converted to a `bigint`.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is\n * interpreted as a hexadecimal number and converted to a `bigint`.\n *\n * @example\n * ```typescript\n * const value = createBigInt('0x010203');\n * console.log(value); // 16909060n\n *\n * const otherValue = createBigInt(123);\n * console.log(otherValue); // 123n\n * ```\n * @param value - The value to create the bigint from.\n * @returns The created bigint.\n * @throws If the value is not a number-like value.\n */\nexport function createBigInt(value: NumberLike): bigint {\n try {\n // The `BigInt` constructor throws if the value is not a number-like value.\n // There is no need to validate the value manually.\n return create(value, BigIntCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(`Expected a number-like value, got \"${error.value}\".`);\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a byte array from a bytes-like value.\n *\n * - If the value is a byte array, it is returned as-is.\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is interpreted\n * as a hexadecimal number and converted to a byte array.\n *\n * @example\n * ```typescript\n * const value = createBytes('0x010203');\n * console.log(value); // Uint8Array [ 1, 2, 3 ]\n *\n * const otherValue = createBytes('0x010203');\n * console.log(otherValue); // Uint8Array [ 1, 2, 3 ]\n * ```\n * @param value - The value to create the byte array from.\n * @returns The created byte array.\n * @throws If the value is not a bytes-like value.\n */\nexport function createBytes(value: BytesLike): Uint8Array {\n if (typeof value === 'string' && value.toLowerCase() === '0x') {\n return new Uint8Array();\n }\n\n try {\n return create(value, BytesCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(`Expected a bytes-like value, got \"${error.value}\".`);\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n\n/**\n * Create a hexadecimal string from a bytes-like value.\n *\n * - If the value is a hex string (i.e., it starts with \"0x\"), it is returned\n * as-is.\n * - If the value is a `Uint8Array`, it is converted to a hex string.\n *\n * @example\n * ```typescript\n * const value = createHex(new Uint8Array([1, 2, 3]));\n * console.log(value); // '0x010203'\n *\n * const otherValue = createHex('0x010203');\n * console.log(otherValue); // '0x010203'\n * ```\n * @param value - The value to create the hex string from.\n * @returns The created hex string.\n * @throws If the value is not a bytes-like value.\n */\nexport function createHex(value: BytesLike): Hex {\n if (\n (value instanceof Uint8Array && value.length === 0) ||\n (typeof value === 'string' && value.toLowerCase() === '0x')\n ) {\n return '0x';\n }\n\n try {\n return create(value, HexCoercer);\n } catch (error) {\n if (error instanceof StructError) {\n throw new Error(`Expected a bytes-like value, got \"${error.value}\".`);\n }\n\n /* istanbul ignore next */\n throw error;\n }\n}\n"]}
|
package/dist/hex.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { Struct } from 'superstruct';
|
|
2
|
+
export declare type Hex = `0x${string}`;
|
|
3
|
+
export declare const HexStruct: Struct<string, null>;
|
|
4
|
+
export declare const StrictHexStruct: Struct<`0x${string}`, null>;
|
|
1
5
|
/**
|
|
2
6
|
* Check if a string is a valid hex string.
|
|
3
7
|
*
|
|
@@ -5,6 +9,14 @@
|
|
|
5
9
|
* @returns Whether the value is a valid hex string.
|
|
6
10
|
*/
|
|
7
11
|
export declare function isHexString(value: unknown): value is string;
|
|
12
|
+
/**
|
|
13
|
+
* Strictly check if a string is a valid hex string. A valid hex string must
|
|
14
|
+
* start with the "0x"-prefix.
|
|
15
|
+
*
|
|
16
|
+
* @param value - The value to check.
|
|
17
|
+
* @returns Whether the value is a valid hex string.
|
|
18
|
+
*/
|
|
19
|
+
export declare function isStrictHexString(value: unknown): value is Hex;
|
|
8
20
|
/**
|
|
9
21
|
* Assert that a value is a valid hex string.
|
|
10
22
|
*
|
|
@@ -12,6 +24,14 @@ export declare function isHexString(value: unknown): value is string;
|
|
|
12
24
|
* @throws If the value is not a valid hex string.
|
|
13
25
|
*/
|
|
14
26
|
export declare function assertIsHexString(value: unknown): asserts value is string;
|
|
27
|
+
/**
|
|
28
|
+
* Assert that a value is a valid hex string. A valid hex string must start with
|
|
29
|
+
* the "0x"-prefix.
|
|
30
|
+
*
|
|
31
|
+
* @param value - The value to check.
|
|
32
|
+
* @throws If the value is not a valid hex string.
|
|
33
|
+
*/
|
|
34
|
+
export declare function assertIsStrictHexString(value: unknown): asserts value is Hex;
|
|
15
35
|
/**
|
|
16
36
|
* Add the `0x`-prefix to a hexadecimal string. If the string already has the
|
|
17
37
|
* prefix, it is returned as-is.
|
|
@@ -19,7 +39,7 @@ export declare function assertIsHexString(value: unknown): asserts value is stri
|
|
|
19
39
|
* @param hex - The hexadecimal string to add the prefix to.
|
|
20
40
|
* @returns The prefixed hexadecimal string.
|
|
21
41
|
*/
|
|
22
|
-
export declare function add0x(hex: string):
|
|
42
|
+
export declare function add0x(hex: string): Hex;
|
|
23
43
|
/**
|
|
24
44
|
* Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have
|
|
25
45
|
* the prefix, it is returned as-is.
|
package/dist/hex.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.remove0x = exports.add0x = exports.assertIsHexString = exports.isHexString = void 0;
|
|
3
|
+
exports.remove0x = exports.add0x = exports.assertIsStrictHexString = exports.assertIsHexString = exports.isStrictHexString = exports.isHexString = exports.StrictHexStruct = exports.HexStruct = void 0;
|
|
4
4
|
const superstruct_1 = require("superstruct");
|
|
5
5
|
const assert_1 = require("./assert");
|
|
6
|
-
|
|
6
|
+
exports.HexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^(?:0x)?[0-9a-f]+$/iu);
|
|
7
|
+
exports.StrictHexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[0-9a-f]+$/iu);
|
|
7
8
|
/**
|
|
8
9
|
* Check if a string is a valid hex string.
|
|
9
10
|
*
|
|
@@ -11,9 +12,20 @@ const HexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^(?:0
|
|
|
11
12
|
* @returns Whether the value is a valid hex string.
|
|
12
13
|
*/
|
|
13
14
|
function isHexString(value) {
|
|
14
|
-
return (0, superstruct_1.is)(value, HexStruct);
|
|
15
|
+
return (0, superstruct_1.is)(value, exports.HexStruct);
|
|
15
16
|
}
|
|
16
17
|
exports.isHexString = isHexString;
|
|
18
|
+
/**
|
|
19
|
+
* Strictly check if a string is a valid hex string. A valid hex string must
|
|
20
|
+
* start with the "0x"-prefix.
|
|
21
|
+
*
|
|
22
|
+
* @param value - The value to check.
|
|
23
|
+
* @returns Whether the value is a valid hex string.
|
|
24
|
+
*/
|
|
25
|
+
function isStrictHexString(value) {
|
|
26
|
+
return (0, superstruct_1.is)(value, exports.StrictHexStruct);
|
|
27
|
+
}
|
|
28
|
+
exports.isStrictHexString = isStrictHexString;
|
|
17
29
|
/**
|
|
18
30
|
* Assert that a value is a valid hex string.
|
|
19
31
|
*
|
|
@@ -24,6 +36,17 @@ function assertIsHexString(value) {
|
|
|
24
36
|
(0, assert_1.assert)(isHexString(value), 'Value must be a hexadecimal string.');
|
|
25
37
|
}
|
|
26
38
|
exports.assertIsHexString = assertIsHexString;
|
|
39
|
+
/**
|
|
40
|
+
* Assert that a value is a valid hex string. A valid hex string must start with
|
|
41
|
+
* the "0x"-prefix.
|
|
42
|
+
*
|
|
43
|
+
* @param value - The value to check.
|
|
44
|
+
* @throws If the value is not a valid hex string.
|
|
45
|
+
*/
|
|
46
|
+
function assertIsStrictHexString(value) {
|
|
47
|
+
(0, assert_1.assert)(isStrictHexString(value), 'Value must be a hexadecimal string, starting with "0x".');
|
|
48
|
+
}
|
|
49
|
+
exports.assertIsStrictHexString = assertIsStrictHexString;
|
|
27
50
|
/**
|
|
28
51
|
* Add the `0x`-prefix to a hexadecimal string. If the string already has the
|
|
29
52
|
* prefix, it is returned as-is.
|
|
@@ -32,9 +55,12 @@ exports.assertIsHexString = assertIsHexString;
|
|
|
32
55
|
* @returns The prefixed hexadecimal string.
|
|
33
56
|
*/
|
|
34
57
|
function add0x(hex) {
|
|
35
|
-
if (hex.startsWith('0x')
|
|
58
|
+
if (hex.startsWith('0x')) {
|
|
36
59
|
return hex;
|
|
37
60
|
}
|
|
61
|
+
if (hex.startsWith('0X')) {
|
|
62
|
+
return `0x${hex.substring(2)}`;
|
|
63
|
+
}
|
|
38
64
|
return `0x${hex}`;
|
|
39
65
|
}
|
|
40
66
|
exports.add0x = add0x;
|
package/dist/hex.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hex.js","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"hex.js","sourceRoot":"","sources":["../src/hex.ts"],"names":[],"mappings":";;;AAAA,6CAA0D;AAC1D,qCAAkC;AAIrB,QAAA,SAAS,GAAG,IAAA,qBAAO,EAAC,IAAA,oBAAM,GAAE,EAAE,sBAAsB,CAAC,CAAC;AACtD,QAAA,eAAe,GAAG,IAAA,qBAAO,EAAC,IAAA,oBAAM,GAAE,EAAE,iBAAiB,CAGjE,CAAC;AAEF;;;;;GAKG;AACH,SAAgB,WAAW,CAAC,KAAc;IACxC,OAAO,IAAA,gBAAE,EAAC,KAAK,EAAE,iBAAS,CAAC,CAAC;AAC9B,CAAC;AAFD,kCAEC;AAED;;;;;;GAMG;AACH,SAAgB,iBAAiB,CAAC,KAAc;IAC9C,OAAO,IAAA,gBAAE,EAAC,KAAK,EAAE,uBAAe,CAAC,CAAC;AACpC,CAAC;AAFD,8CAEC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAAC,KAAc;IAC9C,IAAA,eAAM,EAAC,WAAW,CAAC,KAAK,CAAC,EAAE,qCAAqC,CAAC,CAAC;AACpE,CAAC;AAFD,8CAEC;AAED;;;;;;GAMG;AACH,SAAgB,uBAAuB,CAAC,KAAc;IACpD,IAAA,eAAM,EACJ,iBAAiB,CAAC,KAAK,CAAC,EACxB,yDAAyD,CAC1D,CAAC;AACJ,CAAC;AALD,0DAKC;AAED;;;;;;GAMG;AACH,SAAgB,KAAK,CAAC,GAAW;IAC/B,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QACxB,OAAO,GAAU,CAAC;KACnB;IAED,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QACxB,OAAO,KAAK,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;KAChC;IAED,OAAO,KAAK,GAAG,EAAE,CAAC;AACpB,CAAC;AAVD,sBAUC;AAED;;;;;;GAMG;AACH,SAAgB,QAAQ,CAAC,GAAW;IAClC,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QAChD,OAAO,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;KACzB;IAED,OAAO,GAAG,CAAC;AACb,CAAC;AAND,4BAMC","sourcesContent":["import { is, pattern, string, Struct } from 'superstruct';\nimport { assert } from './assert';\n\nexport type Hex = `0x${string}`;\n\nexport const HexStruct = pattern(string(), /^(?:0x)?[0-9a-f]+$/iu);\nexport const StrictHexStruct = pattern(string(), /^0x[0-9a-f]+$/iu) as Struct<\n Hex,\n null\n>;\n\n/**\n * Check if a string is a valid hex string.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isHexString(value: unknown): value is string {\n return is(value, HexStruct);\n}\n\n/**\n * Strictly check if a string is a valid hex string. A valid hex string must\n * start with the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid hex string.\n */\nexport function isStrictHexString(value: unknown): value is Hex {\n return is(value, StrictHexStruct);\n}\n\n/**\n * Assert that a value is a valid hex string.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsHexString(value: unknown): asserts value is string {\n assert(isHexString(value), 'Value must be a hexadecimal string.');\n}\n\n/**\n * Assert that a value is a valid hex string. A valid hex string must start with\n * the \"0x\"-prefix.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid hex string.\n */\nexport function assertIsStrictHexString(value: unknown): asserts value is Hex {\n assert(\n isStrictHexString(value),\n 'Value must be a hexadecimal string, starting with \"0x\".',\n );\n}\n\n/**\n * Add the `0x`-prefix to a hexadecimal string. If the string already has the\n * prefix, it is returned as-is.\n *\n * @param hex - The hexadecimal string to add the prefix to.\n * @returns The prefixed hexadecimal string.\n */\nexport function add0x(hex: string): Hex {\n if (hex.startsWith('0x')) {\n return hex as Hex;\n }\n\n if (hex.startsWith('0X')) {\n return `0x${hex.substring(2)}`;\n }\n\n return `0x${hex}`;\n}\n\n/**\n * Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have\n * the prefix, it is returned as-is.\n *\n * @param hex - The hexadecimal string to remove the prefix from.\n * @returns The un-prefixed hexadecimal string.\n */\nexport function remove0x(hex: string): string {\n if (hex.startsWith('0x') || hex.startsWith('0X')) {\n return hex.substring(2);\n }\n\n return hex;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tsd_1 = require("tsd");
|
|
4
|
+
// Valid hex strings:
|
|
5
|
+
(0, tsd_1.expectAssignable)('0x');
|
|
6
|
+
(0, tsd_1.expectAssignable)('0x0');
|
|
7
|
+
(0, tsd_1.expectAssignable)('0x😀');
|
|
8
|
+
const embeddedString = 'test';
|
|
9
|
+
(0, tsd_1.expectAssignable)(`0x${embeddedString}`);
|
|
10
|
+
// Not valid hex strings:
|
|
11
|
+
(0, tsd_1.expectNotAssignable)(`0X${embeddedString}`);
|
|
12
|
+
(0, tsd_1.expectNotAssignable)(`1x${embeddedString}`);
|
|
13
|
+
(0, tsd_1.expectNotAssignable)(0);
|
|
14
|
+
(0, tsd_1.expectNotAssignable)('0');
|
|
15
|
+
(0, tsd_1.expectNotAssignable)('🙃');
|
|
16
|
+
//# sourceMappingURL=hex.test-d.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hex.test-d.js","sourceRoot":"","sources":["../src/hex.test-d.ts"],"names":[],"mappings":";;AAAA,6BAA4D;AAI5D,qBAAqB;AAErB,IAAA,sBAAgB,EAAM,IAAI,CAAC,CAAC;AAE5B,IAAA,sBAAgB,EAAM,KAAK,CAAC,CAAC;AAE7B,IAAA,sBAAgB,EAAM,MAAM,CAAC,CAAC;AAE9B,MAAM,cAAc,GAAG,MAAM,CAAC;AAC9B,IAAA,sBAAgB,EAAM,KAAK,cAAc,EAAE,CAAC,CAAC;AAE7C,yBAAyB;AAEzB,IAAA,yBAAmB,EAAM,KAAK,cAAc,EAAE,CAAC,CAAC;AAEhD,IAAA,yBAAmB,EAAM,KAAK,cAAc,EAAE,CAAC,CAAC;AAEhD,IAAA,yBAAmB,EAAM,CAAC,CAAC,CAAC;AAE5B,IAAA,yBAAmB,EAAM,GAAG,CAAC,CAAC;AAE9B,IAAA,yBAAmB,EAAM,IAAI,CAAC,CAAC","sourcesContent":["import { expectAssignable, expectNotAssignable } from 'tsd';\n\nimport { Hex } from '.';\n\n// Valid hex strings:\n\nexpectAssignable<Hex>('0x');\n\nexpectAssignable<Hex>('0x0');\n\nexpectAssignable<Hex>('0x😀');\n\nconst embeddedString = 'test';\nexpectAssignable<Hex>(`0x${embeddedString}`);\n\n// Not valid hex strings:\n\nexpectNotAssignable<Hex>(`0X${embeddedString}`);\n\nexpectNotAssignable<Hex>(`1x${embeddedString}`);\n\nexpectNotAssignable<Hex>(0);\n\nexpectNotAssignable<Hex>('0');\n\nexpectNotAssignable<Hex>('🙃');\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
export * from './assert';
|
|
2
2
|
export * from './bytes';
|
|
3
|
+
export * from './coercers';
|
|
3
4
|
export * from './collections';
|
|
4
5
|
export * from './hex';
|
|
5
6
|
export * from './json';
|
|
6
7
|
export * from './logging';
|
|
7
8
|
export * from './misc';
|
|
9
|
+
export * from './number';
|
|
8
10
|
export * from './time';
|
package/dist/index.js
CHANGED
|
@@ -16,10 +16,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./assert"), exports);
|
|
18
18
|
__exportStar(require("./bytes"), exports);
|
|
19
|
+
__exportStar(require("./coercers"), exports);
|
|
19
20
|
__exportStar(require("./collections"), exports);
|
|
20
21
|
__exportStar(require("./hex"), exports);
|
|
21
22
|
__exportStar(require("./json"), exports);
|
|
22
23
|
__exportStar(require("./logging"), exports);
|
|
23
24
|
__exportStar(require("./misc"), exports);
|
|
25
|
+
__exportStar(require("./number"), exports);
|
|
24
26
|
__exportStar(require("./time"), exports);
|
|
25
27
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,0CAAwB;AACxB,gDAA8B;AAC9B,wCAAsB;AACtB,yCAAuB;AACvB,4CAA0B;AAC1B,yCAAuB;AACvB,yCAAuB","sourcesContent":["export * from './assert';\nexport * from './bytes';\nexport * from './collections';\nexport * from './hex';\nexport * from './json';\nexport * from './logging';\nexport * from './misc';\nexport * from './time';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,0CAAwB;AACxB,6CAA2B;AAC3B,gDAA8B;AAC9B,wCAAsB;AACtB,yCAAuB;AACvB,4CAA0B;AAC1B,yCAAuB;AACvB,2CAAyB;AACzB,yCAAuB","sourcesContent":["export * from './assert';\nexport * from './bytes';\nexport * from './coercers';\nexport * from './collections';\nexport * from './hex';\nexport * from './json';\nexport * from './logging';\nexport * from './misc';\nexport * from './number';\nexport * from './time';\n"]}
|
package/dist/json.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Infer, Struct } from 'superstruct';
|
|
2
|
-
|
|
2
|
+
import { AssertionErrorConstructor } from './assert';
|
|
3
|
+
export declare const JsonStruct: Struct<Json, null>;
|
|
3
4
|
/**
|
|
4
5
|
* Any JSON-compatible value.
|
|
5
6
|
*/
|
|
@@ -7,10 +8,11 @@ export declare type Json = null | boolean | number | string | Json[] | {
|
|
|
7
8
|
[prop: string]: Json;
|
|
8
9
|
};
|
|
9
10
|
/**
|
|
10
|
-
*
|
|
11
|
+
* Check if the given value is a valid {@link Json} value, i.e., a value that is
|
|
12
|
+
* serializable to JSON.
|
|
11
13
|
*
|
|
12
14
|
* @param value - The value to check.
|
|
13
|
-
* @returns Whether the value is valid
|
|
15
|
+
* @returns Whether the value is a valid {@link Json} value.
|
|
14
16
|
*/
|
|
15
17
|
export declare function isValidJson(value: unknown): value is Json;
|
|
16
18
|
/**
|
|
@@ -33,13 +35,13 @@ export declare const JsonRpcIdStruct: Struct<string | number | null, null>;
|
|
|
33
35
|
export declare type JsonRpcId = Infer<typeof JsonRpcIdStruct>;
|
|
34
36
|
export declare const JsonRpcErrorStruct: Struct<{
|
|
35
37
|
code: number;
|
|
36
|
-
data: unknown;
|
|
37
38
|
message: string;
|
|
39
|
+
data?: Json | undefined;
|
|
38
40
|
stack?: string | undefined;
|
|
39
41
|
}, {
|
|
40
42
|
code: Struct<number, null>;
|
|
41
43
|
message: Struct<string, null>;
|
|
42
|
-
data: Struct<
|
|
44
|
+
data: Struct<Json | undefined, null>;
|
|
43
45
|
stack: Struct<string | undefined, null>;
|
|
44
46
|
}>;
|
|
45
47
|
/**
|
|
@@ -91,34 +93,70 @@ export declare const JsonRpcNotificationStruct: Struct<{
|
|
|
91
93
|
*/
|
|
92
94
|
export declare type JsonRpcNotification<Params extends JsonRpcParams> = InferWithParams<typeof JsonRpcNotificationStruct, Params>;
|
|
93
95
|
/**
|
|
94
|
-
*
|
|
95
|
-
* notification.
|
|
96
|
+
* Check if the given value is a valid {@link JsonRpcNotification} object.
|
|
96
97
|
*
|
|
97
|
-
* @param
|
|
98
|
-
* @returns Whether the
|
|
98
|
+
* @param value - The value to check.
|
|
99
|
+
* @returns Whether the given value is a valid {@link JsonRpcNotification}
|
|
100
|
+
* object.
|
|
99
101
|
*/
|
|
100
|
-
export declare function isJsonRpcNotification(
|
|
102
|
+
export declare function isJsonRpcNotification(value: unknown): value is JsonRpcNotification<JsonRpcParams>;
|
|
101
103
|
/**
|
|
102
|
-
*
|
|
103
|
-
* notification.
|
|
104
|
+
* Assert that the given value is a valid {@link JsonRpcNotification} object.
|
|
104
105
|
*
|
|
105
|
-
* @param
|
|
106
|
+
* @param value - The value to check.
|
|
107
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
108
|
+
* Defaults to {@link AssertionError}.
|
|
109
|
+
* @throws If the given value is not a valid {@link JsonRpcNotification} object.
|
|
106
110
|
*/
|
|
107
|
-
export declare function assertIsJsonRpcNotification(
|
|
111
|
+
export declare function assertIsJsonRpcNotification(value: unknown, ErrorWrapper?: AssertionErrorConstructor): asserts value is JsonRpcNotification<JsonRpcParams>;
|
|
108
112
|
/**
|
|
109
|
-
*
|
|
113
|
+
* Check if the given value is a valid {@link JsonRpcRequest} object.
|
|
110
114
|
*
|
|
111
|
-
* @param
|
|
112
|
-
* @returns Whether the
|
|
115
|
+
* @param value - The value to check.
|
|
116
|
+
* @returns Whether the given value is a valid {@link JsonRpcRequest} object.
|
|
113
117
|
*/
|
|
114
|
-
export declare function isJsonRpcRequest(
|
|
118
|
+
export declare function isJsonRpcRequest(value: unknown): value is JsonRpcRequest<JsonRpcParams>;
|
|
115
119
|
/**
|
|
116
|
-
*
|
|
117
|
-
* request.
|
|
120
|
+
* Assert that the given value is a valid {@link JsonRpcRequest} object.
|
|
118
121
|
*
|
|
119
|
-
* @param
|
|
122
|
+
* @param value - The JSON-RPC request or notification to check.
|
|
123
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
124
|
+
* Defaults to {@link AssertionError}.
|
|
125
|
+
* @throws If the given value is not a valid {@link JsonRpcRequest} object.
|
|
126
|
+
*/
|
|
127
|
+
export declare function assertIsJsonRpcRequest(value: unknown, ErrorWrapper?: AssertionErrorConstructor): asserts value is JsonRpcRequest<JsonRpcParams>;
|
|
128
|
+
export declare const PendingJsonRpcResponseStruct: Struct<{
|
|
129
|
+
id: string | number | null;
|
|
130
|
+
jsonrpc: "2.0";
|
|
131
|
+
result: unknown;
|
|
132
|
+
error?: {
|
|
133
|
+
code: number;
|
|
134
|
+
message: string;
|
|
135
|
+
data?: Json | undefined;
|
|
136
|
+
stack?: string | undefined;
|
|
137
|
+
} | undefined;
|
|
138
|
+
}, {
|
|
139
|
+
id: Struct<string | number | null, null>;
|
|
140
|
+
jsonrpc: Struct<"2.0", "2.0">;
|
|
141
|
+
result: Struct<unknown, null>;
|
|
142
|
+
error: Struct<{
|
|
143
|
+
code: number;
|
|
144
|
+
message: string;
|
|
145
|
+
data?: Json | undefined;
|
|
146
|
+
stack?: string | undefined;
|
|
147
|
+
} | undefined, {
|
|
148
|
+
code: Struct<number, null>;
|
|
149
|
+
message: Struct<string, null>;
|
|
150
|
+
data: Struct<Json | undefined, null>;
|
|
151
|
+
stack: Struct<string | undefined, null>;
|
|
152
|
+
}>;
|
|
153
|
+
}>;
|
|
154
|
+
/**
|
|
155
|
+
* A JSON-RPC response object that has not yet been resolved.
|
|
120
156
|
*/
|
|
121
|
-
export declare
|
|
157
|
+
export declare type PendingJsonRpcResponse<Result extends Json> = Omit<Infer<typeof PendingJsonRpcResponseStruct>, 'result'> & {
|
|
158
|
+
result?: Result;
|
|
159
|
+
};
|
|
122
160
|
export declare const JsonRpcSuccessStruct: Struct<{
|
|
123
161
|
id: string | number | null;
|
|
124
162
|
jsonrpc: "2.0";
|
|
@@ -126,7 +164,7 @@ export declare const JsonRpcSuccessStruct: Struct<{
|
|
|
126
164
|
}, {
|
|
127
165
|
id: Struct<string | number | null, null>;
|
|
128
166
|
jsonrpc: Struct<"2.0", "2.0">;
|
|
129
|
-
result: Struct<Json,
|
|
167
|
+
result: Struct<Json, null>;
|
|
130
168
|
}>;
|
|
131
169
|
/**
|
|
132
170
|
* A successful JSON-RPC response object.
|
|
@@ -164,45 +202,87 @@ export declare const JsonRpcResponseStruct: Struct<{
|
|
|
164
202
|
*/
|
|
165
203
|
export declare type JsonRpcResponse<Result extends Json> = JsonRpcSuccess<Result> | JsonRpcFailure;
|
|
166
204
|
/**
|
|
167
|
-
* Type guard to check
|
|
205
|
+
* Type guard to check whether specified JSON-RPC response is a
|
|
206
|
+
* {@link PendingJsonRpcResponse}.
|
|
207
|
+
*
|
|
208
|
+
* @param response - The JSON-RPC response to check.
|
|
209
|
+
* @returns Whether the specified JSON-RPC response is pending.
|
|
210
|
+
*/
|
|
211
|
+
export declare function isPendingJsonRpcResponse(response: unknown): response is PendingJsonRpcResponse<Json>;
|
|
212
|
+
/**
|
|
213
|
+
* Assert that the given value is a valid {@link PendingJsonRpcResponse} object.
|
|
214
|
+
*
|
|
215
|
+
* @param response - The JSON-RPC response to check.
|
|
216
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
217
|
+
* Defaults to {@link AssertionError}.
|
|
218
|
+
* @throws If the given value is not a valid {@link PendingJsonRpcResponse}
|
|
219
|
+
* object.
|
|
220
|
+
*/
|
|
221
|
+
export declare function assertIsPendingJsonRpcResponse(response: unknown, ErrorWrapper?: AssertionErrorConstructor): asserts response is PendingJsonRpcResponse<Json>;
|
|
222
|
+
/**
|
|
223
|
+
* Type guard to check if a value is a {@link JsonRpcResponse}.
|
|
168
224
|
*
|
|
169
225
|
* @param response - The object to check.
|
|
170
226
|
* @returns Whether the object is a JsonRpcResponse.
|
|
171
227
|
*/
|
|
172
228
|
export declare function isJsonRpcResponse(response: unknown): response is JsonRpcResponse<Json>;
|
|
173
229
|
/**
|
|
174
|
-
*
|
|
230
|
+
* Assert that the given value is a valid {@link JsonRpcResponse} object.
|
|
175
231
|
*
|
|
176
|
-
* @param
|
|
232
|
+
* @param value - The value to check.
|
|
233
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
234
|
+
* Defaults to {@link AssertionError}.
|
|
235
|
+
* @throws If the given value is not a valid {@link JsonRpcResponse} object.
|
|
177
236
|
*/
|
|
178
|
-
export declare function assertIsJsonRpcResponse(
|
|
237
|
+
export declare function assertIsJsonRpcResponse(value: unknown, ErrorWrapper?: AssertionErrorConstructor): asserts value is JsonRpcResponse<Json>;
|
|
179
238
|
/**
|
|
180
|
-
*
|
|
239
|
+
* Check if the given value is a valid {@link JsonRpcSuccess} object.
|
|
181
240
|
*
|
|
182
|
-
* @param
|
|
183
|
-
* @returns Whether the
|
|
241
|
+
* @param value - The value to check.
|
|
242
|
+
* @returns Whether the given value is a valid {@link JsonRpcSuccess} object.
|
|
184
243
|
*/
|
|
185
|
-
export declare function isJsonRpcSuccess(
|
|
244
|
+
export declare function isJsonRpcSuccess(value: unknown): value is JsonRpcSuccess<Json>;
|
|
186
245
|
/**
|
|
187
|
-
*
|
|
246
|
+
* Assert that the given value is a valid {@link JsonRpcSuccess} object.
|
|
188
247
|
*
|
|
189
|
-
* @param
|
|
248
|
+
* @param value - The value to check.
|
|
249
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
250
|
+
* Defaults to {@link AssertionError}.
|
|
251
|
+
* @throws If the given value is not a valid {@link JsonRpcSuccess} object.
|
|
190
252
|
*/
|
|
191
|
-
export declare function assertIsJsonRpcSuccess(
|
|
253
|
+
export declare function assertIsJsonRpcSuccess(value: unknown, ErrorWrapper?: AssertionErrorConstructor): asserts value is JsonRpcSuccess<Json>;
|
|
192
254
|
/**
|
|
193
|
-
*
|
|
255
|
+
* Check if the given value is a valid {@link JsonRpcFailure} object.
|
|
194
256
|
*
|
|
195
|
-
* @param
|
|
196
|
-
* @returns Whether the
|
|
197
|
-
* property.
|
|
257
|
+
* @param value - The value to check.
|
|
258
|
+
* @returns Whether the given value is a valid {@link JsonRpcFailure} object.
|
|
198
259
|
*/
|
|
199
|
-
export declare function isJsonRpcFailure(
|
|
260
|
+
export declare function isJsonRpcFailure(value: unknown): value is JsonRpcFailure;
|
|
200
261
|
/**
|
|
201
|
-
*
|
|
262
|
+
* Assert that the given value is a valid {@link JsonRpcFailure} object.
|
|
202
263
|
*
|
|
203
|
-
* @param
|
|
264
|
+
* @param value - The value to check.
|
|
265
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
266
|
+
* Defaults to {@link AssertionError}.
|
|
267
|
+
* @throws If the given value is not a valid {@link JsonRpcFailure} object.
|
|
268
|
+
*/
|
|
269
|
+
export declare function assertIsJsonRpcFailure(value: unknown, ErrorWrapper?: AssertionErrorConstructor): asserts value is JsonRpcFailure;
|
|
270
|
+
/**
|
|
271
|
+
* Check if the given value is a valid {@link JsonRpcError} object.
|
|
272
|
+
*
|
|
273
|
+
* @param value - The value to check.
|
|
274
|
+
* @returns Whether the given value is a valid {@link JsonRpcError} object.
|
|
275
|
+
*/
|
|
276
|
+
export declare function isJsonRpcError(value: unknown): value is JsonRpcError;
|
|
277
|
+
/**
|
|
278
|
+
* Assert that the given value is a valid {@link JsonRpcError} object.
|
|
279
|
+
*
|
|
280
|
+
* @param value - The value to check.
|
|
281
|
+
* @param ErrorWrapper - The error class to throw if the assertion fails.
|
|
282
|
+
* Defaults to {@link AssertionError}.
|
|
283
|
+
* @throws If the given value is not a valid {@link JsonRpcError} object.
|
|
204
284
|
*/
|
|
205
|
-
export declare function
|
|
285
|
+
export declare function assertIsJsonRpcError(value: unknown, ErrorWrapper?: AssertionErrorConstructor): asserts value is JsonRpcError;
|
|
206
286
|
declare type JsonRpcValidatorOptions = {
|
|
207
287
|
permitEmptyString?: boolean;
|
|
208
288
|
permitFractions?: boolean;
|