@twin.org/core 0.0.3-next.4 → 0.0.3-next.40
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/README.md +1 -9
- package/dist/es/encoding/base32.js +1 -1
- package/dist/es/encoding/base32.js.map +1 -1
- package/dist/es/encoding/base64.js +1 -1
- package/dist/es/encoding/base64.js.map +1 -1
- package/dist/es/errors/alreadyExistsError.js +1 -1
- package/dist/es/errors/alreadyExistsError.js.map +1 -1
- package/dist/es/errors/baseError.js +1 -1
- package/dist/es/errors/baseError.js.map +1 -1
- package/dist/es/errors/conflictError.js +1 -1
- package/dist/es/errors/conflictError.js.map +1 -1
- package/dist/es/errors/generalError.js +1 -1
- package/dist/es/errors/generalError.js.map +1 -1
- package/dist/es/errors/guardError.js +1 -1
- package/dist/es/errors/guardError.js.map +1 -1
- package/dist/es/errors/notFoundError.js +1 -1
- package/dist/es/errors/notFoundError.js.map +1 -1
- package/dist/es/errors/notSupportedError.js +1 -1
- package/dist/es/errors/notSupportedError.js.map +1 -1
- package/dist/es/errors/unauthorizedError.js +2 -2
- package/dist/es/errors/unauthorizedError.js.map +1 -1
- package/dist/es/errors/unprocessableError.js +1 -1
- package/dist/es/errors/unprocessableError.js.map +1 -1
- package/dist/es/factories/factory.js +41 -0
- package/dist/es/factories/factory.js.map +1 -1
- package/dist/es/helpers/arrayHelper.js +2 -0
- package/dist/es/helpers/arrayHelper.js.map +1 -1
- package/dist/es/helpers/jsonHelper.js.map +1 -1
- package/dist/es/helpers/objectHelper.js +12 -36
- package/dist/es/helpers/objectHelper.js.map +1 -1
- package/dist/es/helpers/randomHelper.js +50 -2
- package/dist/es/helpers/randomHelper.js.map +1 -1
- package/dist/es/helpers/stringHelper.js +11 -0
- package/dist/es/helpers/stringHelper.js.map +1 -1
- package/dist/es/index.js +4 -1
- package/dist/es/index.js.map +1 -1
- package/dist/es/models/IComponent.js +0 -2
- package/dist/es/models/IComponent.js.map +1 -1
- package/dist/es/models/IError.js.map +1 -1
- package/dist/es/models/IHealth.js +2 -0
- package/dist/es/models/IHealth.js.map +1 -0
- package/dist/es/models/healthStatus.js +21 -0
- package/dist/es/models/healthStatus.js.map +1 -0
- package/dist/es/types/objectOrArray.js.map +1 -0
- package/dist/es/types/singleOccurrenceArray.js +2 -0
- package/dist/es/types/singleOccurrenceArray.js.map +1 -0
- package/dist/es/types/urn.js +1 -2
- package/dist/es/types/urn.js.map +1 -1
- package/dist/es/utils/asyncCache.js +102 -75
- package/dist/es/utils/asyncCache.js.map +1 -1
- package/dist/es/utils/guards.js +16 -0
- package/dist/es/utils/guards.js.map +1 -1
- package/dist/es/utils/i18n.js.map +1 -1
- package/dist/es/utils/is.js +16 -0
- package/dist/es/utils/is.js.map +1 -1
- package/dist/types/encoding/base32.d.ts +1 -1
- package/dist/types/errors/alreadyExistsError.d.ts +1 -1
- package/dist/types/errors/baseError.d.ts +1 -1
- package/dist/types/errors/conflictError.d.ts +1 -1
- package/dist/types/errors/generalError.d.ts +1 -1
- package/dist/types/errors/guardError.d.ts +1 -1
- package/dist/types/errors/notFoundError.d.ts +1 -1
- package/dist/types/errors/notSupportedError.d.ts +1 -1
- package/dist/types/errors/unauthorizedError.d.ts +2 -2
- package/dist/types/errors/unprocessableError.d.ts +1 -1
- package/dist/types/factories/factory.d.ts +23 -1
- package/dist/types/helpers/arrayHelper.d.ts +1 -8
- package/dist/types/helpers/objectHelper.d.ts +6 -16
- package/dist/types/helpers/randomHelper.d.ts +16 -0
- package/dist/types/helpers/stringHelper.d.ts +6 -0
- package/dist/types/index.d.ts +4 -1
- package/dist/types/models/IComponent.d.ts +12 -0
- package/dist/types/models/IError.d.ts +1 -1
- package/dist/types/models/IHealth.d.ts +32 -0
- package/dist/types/models/healthStatus.d.ts +21 -0
- package/dist/types/types/singleOccurrenceArray.d.ts +9 -0
- package/dist/types/utils/asyncCache.d.ts +7 -0
- package/dist/types/utils/guards.d.ts +10 -1
- package/dist/types/utils/is.d.ts +7 -0
- package/docs/changelog.md +870 -184
- package/docs/examples.md +308 -1
- package/docs/reference/classes/AlreadyExistsError.md +36 -36
- package/docs/reference/classes/ArrayHelper.md +10 -44
- package/docs/reference/classes/AsyncCache.md +8 -8
- package/docs/reference/classes/Base32.md +4 -4
- package/docs/reference/classes/Base58.md +3 -3
- package/docs/reference/classes/Base64.md +4 -4
- package/docs/reference/classes/Base64Url.md +3 -3
- package/docs/reference/classes/BaseError.md +35 -35
- package/docs/reference/classes/BitString.md +6 -6
- package/docs/reference/classes/Coerce.md +11 -11
- package/docs/reference/classes/Compression.md +3 -3
- package/docs/reference/classes/ConflictError.md +36 -36
- package/docs/reference/classes/Converter.md +18 -18
- package/docs/reference/classes/EnvHelper.md +1 -1
- package/docs/reference/classes/ErrorHelper.md +3 -3
- package/docs/reference/classes/Factory.md +112 -18
- package/docs/reference/classes/FilenameHelper.md +1 -1
- package/docs/reference/classes/GeneralError.md +36 -36
- package/docs/reference/classes/GuardError.md +36 -36
- package/docs/reference/classes/Guards.md +72 -30
- package/docs/reference/classes/HexHelper.md +6 -6
- package/docs/reference/classes/I18n.md +14 -14
- package/docs/reference/classes/Is.md +67 -39
- package/docs/reference/classes/JsonHelper.md +10 -10
- package/docs/reference/classes/NotFoundError.md +36 -36
- package/docs/reference/classes/NotImplementedError.md +35 -35
- package/docs/reference/classes/NotSupportedError.md +36 -36
- package/docs/reference/classes/NumberHelper.md +2 -2
- package/docs/reference/classes/ObjectHelper.md +49 -83
- package/docs/reference/classes/RandomHelper.md +53 -1
- package/docs/reference/classes/SharedStore.md +3 -3
- package/docs/reference/classes/StringHelper.md +45 -23
- package/docs/reference/classes/Uint8ArrayHelper.md +1 -1
- package/docs/reference/classes/UnauthorizedError.md +37 -37
- package/docs/reference/classes/UnprocessableError.md +36 -36
- package/docs/reference/classes/Url.md +8 -8
- package/docs/reference/classes/Urn.md +24 -24
- package/docs/reference/classes/Validation.md +25 -25
- package/docs/reference/classes/ValidationError.md +35 -35
- package/docs/reference/index.md +5 -0
- package/docs/reference/interfaces/IComponent.md +40 -4
- package/docs/reference/interfaces/IError.md +11 -11
- package/docs/reference/interfaces/IHealth.md +55 -0
- package/docs/reference/interfaces/II18nShared.md +4 -4
- package/docs/reference/interfaces/IKeyValue.md +2 -2
- package/docs/reference/interfaces/ILabelledValue.md +2 -2
- package/docs/reference/interfaces/ILocale.md +2 -2
- package/docs/reference/interfaces/ILocaleDictionary.md +1 -1
- package/docs/reference/interfaces/ILocalesIndex.md +1 -1
- package/docs/reference/interfaces/IPatchOperation.md +6 -6
- package/docs/reference/interfaces/IUrlParts.md +9 -9
- package/docs/reference/interfaces/IValidationFailure.md +4 -4
- package/docs/reference/type-aliases/HealthStatus.md +5 -0
- package/docs/reference/type-aliases/SingleOccurrenceArray.md +15 -0
- package/docs/reference/type-aliases/SingleOccurrenceArrayDepthHelper.md +19 -0
- package/docs/reference/variables/CoerceType.md +10 -10
- package/docs/reference/variables/CompressionType.md +2 -2
- package/docs/reference/variables/HealthStatus.md +25 -0
- package/locales/en.json +5 -2
- package/package.json +6 -6
- package/dist/es/models/objectOrArray.js.map +0 -1
- /package/dist/es/{models → types}/objectOrArray.js +0 -0
- /package/dist/types/{models → types}/objectOrArray.d.ts +0 -0
package/README.md
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
# TWIN Framework Core
|
|
2
2
|
|
|
3
|
-
This package
|
|
4
|
-
|
|
5
|
-
- Guards
|
|
6
|
-
- Type Checking
|
|
7
|
-
- Error Handling
|
|
8
|
-
- Validation
|
|
9
|
-
- Encoding
|
|
10
|
-
- Conversion
|
|
11
|
-
- Localisation
|
|
3
|
+
This package is part of the framework workspace and provides helper methods/classes for data type checking/validation/guarding/error handling to support consistent development workflows across the ecosystem.
|
|
12
4
|
|
|
13
5
|
## Installation
|
|
14
6
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base32.js","sourceRoot":"","sources":["../../../src/encoding/base32.ts"],"names":[],"mappings":"AAAA,gCAAgC;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.\n// SPDX-License-Identifier: Apache-2.0.\n/* eslint-disable no-bitwise */\n\nimport { nameof } from \"@twin.org/nameof\";\nimport { GeneralError } from \"../errors/generalError.js\";\nimport { Guards } from \"../utils/guards.js\";\n\n/**\n * Class to help with
|
|
1
|
+
{"version":3,"file":"base32.js","sourceRoot":"","sources":["../../../src/encoding/base32.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,+BAA+B;AAG/B,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;OAEG;IACI,MAAM,CAAU,UAAU,YAA4B;IAE7D;;;OAGG;IACK,MAAM,CAAU,SAAS,GAAW,kCAAkC,CAAC;IAE/E;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,MAAc;QAClC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,YAAkB,MAAM,CAAC,CAAC;QAEzD,IAAI,IAAI,GAAG,CAAC,CAAC;QACb,IAAI,KAAK,GAAG,CAAC,CAAC;QAEd,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QAEnC,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAEnE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACxC,MAAM,GAAG,GAAG,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;YAEhD,IAAI,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC;gBAChB,MAAM,IAAI,Y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AI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;QACxD,CAAC;QAED,OAAO,MAAM,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;YAChC,MAAM,IAAI,GAAG,CAAC;QACf,CAAC;QAED,OAAO,MAAM,CAAC;IACf,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/* eslint-disable no-bitwise */\n\nimport { nameof } from \"@twin.org/nameof\";\nimport { GeneralError } from \"../errors/generalError.js\";\nimport { Guards } from \"../utils/guards.js\";\n\n/**\n * Class to help with base32 Encoding/Decoding.\n */\nexport class Base32 {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Base32>();\n\n\t/**\n\t * Alphabet table for encoding.\n\t * @internal\n\t */\n\tprivate static readonly _ALPHABET: string = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567\";\n\n\t/**\n\t * Convert the base 32 string to a byte array.\n\t * @param base32 The base32 string to convert.\n\t * @returns The byte array.\n\t * @throws If the input string contains a character not in the Base32 alphabet.\n\t */\n\tpublic static decode(base32: string): Uint8Array {\n\t\tGuards.string(Base32.CLASS_NAME, nameof(base32), base32);\n\n\t\tlet bits = 0;\n\t\tlet value = 0;\n\n\t\tbase32 = base32.replace(/=+$/, \"\");\n\n\t\tlet index = 0;\n\t\tconst output = new Uint8Array(Math.trunc((base32.length * 5) / 8));\n\n\t\tfor (let i = 0; i < base32.length; i++) {\n\t\t\tconst idx = Base32._ALPHABET.indexOf(base32[i]);\n\n\t\t\tif (idx === -1) {\n\t\t\t\tthrow new GeneralError(Base32.CLASS_NAME, \"invalidCharacter\", {\n\t\t\t\t\tinvalidCharacter: base32[i]\n\t\t\t\t});\n\t\t\t}\n\t\t\tvalue = (value << 5) | idx;\n\t\t\tbits += 5;\n\n\t\t\tif (bits >= 8) {\n\t\t\t\toutput[index++] = (value >>> (bits - 8)) & 255;\n\t\t\t\tbits -= 8;\n\t\t\t}\n\t\t}\n\n\t\treturn output;\n\t}\n\n\t/**\n\t * Convert a byte array to base 32.\n\t * @param bytes The byte array to convert.\n\t * @returns The data as base32 string.\n\t */\n\tpublic static encode(bytes: Uint8Array): string {\n\t\tGuards.uint8Array(Base32.CLASS_NAME, nameof(bytes), bytes);\n\n\t\tlet bits = 0;\n\t\tlet value = 0;\n\t\tlet output = \"\";\n\n\t\tfor (let i = 0; i < bytes.byteLength; i++) {\n\t\t\tvalue = (value << 8) | bytes[i];\n\t\t\tbits += 8;\n\n\t\t\twhile (bits >= 5) {\n\t\t\t\toutput += Base32._ALPHABET[(value >>> (bits - 5)) & 31];\n\t\t\t\tbits -= 5;\n\t\t\t}\n\t\t}\n\n\t\tif (bits > 0) {\n\t\t\toutput += Base32._ALPHABET[(value << (5 - bits)) & 31];\n\t\t}\n\n\t\twhile (output.length % 8 !== 0) {\n\t\t\toutput += \"=\";\n\t\t}\n\n\t\treturn output;\n\t}\n}\n"]}
|
|
@@ -137,7 +137,7 @@ export class Base64 {
|
|
|
137
137
|
(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 4) |
|
|
138
138
|
(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] >> 2);
|
|
139
139
|
arr[curByte++] = (tmp >> 8) & 0xff;
|
|
140
|
-
arr[curByte
|
|
140
|
+
arr[curByte] = tmp & 0xff;
|
|
141
141
|
}
|
|
142
142
|
return arr;
|
|
143
143
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base64.js","sourceRoot":"","sources":["../../../src/encoding/base64.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,+BAA+B;AAC/B,uCAAuC;AAGvC,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;;GAGG;AACH,MAAM,OAAO,MAAM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,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC;QAC1C,CAAC;QACD,OAAO,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACxB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/* eslint-disable no-bitwise */\n/* eslint-disable no-mixed-operators */\n\nimport { nameof } from \"@twin.org/nameof\";\nimport { GeneralError } from \"../errors/generalError.js\";\nimport { Guards } from \"../utils/guards.js\";\n\n/**\n * Class to help with base64 Encoding/Decoding.\n * Sourced from https://github.com/beatgammit/base64-js.\n */\nexport class Base64 {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Base64>();\n\n\t/**\n\t * Alphabet table for encoding.\n\t * @internal\n\t */\n\tprivate static readonly _LOOKUP: string =\n\t\t\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\";\n\n\t/**\n\t * Alphabet table for decoding.\n\t * @internal\n\t */\n\tprivate static readonly _REVERSE_LOOKUP: { [id: number]: number } = {\n\t\t\"43\": 62,\n\t\t\"45\": 62,\n\t\t\"47\": 63,\n\t\t\"48\": 52,\n\t\t\"49\": 53,\n\t\t\"50\": 54,\n\t\t\"51\": 55,\n\t\t\"52\": 56,\n\t\t\"53\": 57,\n\t\t\"54\": 58,\n\t\t\"55\": 59,\n\t\t\"56\": 60,\n\t\t\"57\": 61,\n\t\t\"65\": 0,\n\t\t\"66\": 1,\n\t\t\"67\": 2,\n\t\t\"68\": 3,\n\t\t\"69\": 4,\n\t\t\"70\": 5,\n\t\t\"71\": 6,\n\t\t\"72\": 7,\n\t\t\"73\": 8,\n\t\t\"74\": 9,\n\t\t\"75\": 10,\n\t\t\"76\": 11,\n\t\t\"77\": 12,\n\t\t\"78\": 13,\n\t\t\"79\": 14,\n\t\t\"80\": 15,\n\t\t\"81\": 16,\n\t\t\"82\": 17,\n\t\t\"83\": 18,\n\t\t\"84\": 19,\n\t\t\"85\": 20,\n\t\t\"86\": 21,\n\t\t\"87\": 22,\n\t\t\"88\": 23,\n\t\t\"89\": 24,\n\t\t\"90\": 25,\n\t\t\"95\": 63,\n\t\t\"97\": 26,\n\t\t\"98\": 27,\n\t\t\"99\": 28,\n\t\t\"100\": 29,\n\t\t\"101\": 30,\n\t\t\"102\": 31,\n\t\t\"103\": 32,\n\t\t\"104\": 33,\n\t\t\"105\": 34,\n\t\t\"106\": 35,\n\t\t\"107\": 36,\n\t\t\"108\": 37,\n\t\t\"109\": 38,\n\t\t\"110\": 39,\n\t\t\"111\": 40,\n\t\t\"112\": 41,\n\t\t\"113\": 42,\n\t\t\"114\": 43,\n\t\t\"115\": 44,\n\t\t\"116\": 45,\n\t\t\"117\": 46,\n\t\t\"118\": 47,\n\t\t\"119\": 48,\n\t\t\"120\": 49,\n\t\t\"121\": 50,\n\t\t\"122\": 51\n\t};\n\n\t/**\n\t * Get the byte length of the data.\n\t * @param base64 The base64 string.\n\t * @returns The byte length of the data.\n\t */\n\tpublic static byteLength(base64: string): number {\n\t\tconst lens = Base64.getLengths(base64);\n\t\treturn Base64.calcByteLength(lens[0], lens[1]);\n\t}\n\n\t/**\n\t * Convert the base 64 string to a byte array.\n\t * @param base64 The base64 string to convert.\n\t * @returns The byte array.\n\t */\n\tpublic static decode(base64: string): Uint8Array {\n\t\tGuards.string(Base64.CLASS_NAME, nameof(base64), base64);\n\n\t\tlet tmp;\n\t\tconst lens = Base64.getLengths(base64);\n\t\tconst validLen = lens[0];\n\t\tconst placeHoldersLen = lens[1];\n\n\t\tconst arr = new Uint8Array(Base64.calcByteLength(validLen, placeHoldersLen));\n\n\t\tlet curByte = 0;\n\n\t\t// if there are placeholders, only get up to the last complete 4 chars\n\t\tconst len = placeHoldersLen > 0 ? validLen - 4 : validLen;\n\n\t\tlet i;\n\t\tfor (i = 0; i < len; i += 4) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 18) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 12) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] << 6) |\n\t\t\t\tBase64._REVERSE_LOOKUP[base64.charCodeAt(i + 3)];\n\t\t\tarr[curByte++] = (tmp >> 16) & 0xff;\n\t\t\tarr[curByte++] = (tmp >> 8) & 0xff;\n\t\t\tarr[curByte++] = tmp & 0xff;\n\t\t}\n\n\t\tif (placeHoldersLen === 2) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 2) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] >> 4);\n\t\t\tarr[curByte++] = tmp & 0xff;\n\t\t}\n\n\t\tif (placeHoldersLen === 1) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 10) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 4) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] >> 2);\n\t\t\tarr[curByte++] = (tmp >> 8) & 0xff;\n\t\t\tarr[curByte++] = tmp & 0xff;\n\t\t}\n\n\t\treturn arr;\n\t}\n\n\t/**\n\t * Convert a byte array to base 64.\n\t * @param bytes The byte array to convert.\n\t * @returns The data as base64 string.\n\t */\n\tpublic static encode(bytes: Uint8Array): string {\n\t\tGuards.uint8Array(Base64.CLASS_NAME, nameof(bytes), bytes);\n\n\t\tlet tmp;\n\t\tconst len = bytes.length;\n\t\tconst extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes\n\t\tconst parts = [];\n\t\tconst maxChunkLength = 16383; // must be multiple of 3\n\n\t\t// go through the array every three bytes, we'll deal with trailing stuff later\n\t\tfor (let i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {\n\t\t\tparts.push(Base64.encodeChunk(bytes, i, Math.min(i + maxChunkLength, len2)));\n\t\t}\n\n\t\t// pad the end with zeros, but make sure to not forget the extra bytes\n\t\tif (extraBytes === 1) {\n\t\t\ttmp = bytes[len - 1];\n\t\t\tparts.push(`${Base64._LOOKUP[tmp >> 2] + Base64._LOOKUP[(tmp << 4) & 0x3f]}==`);\n\t\t} else if (extraBytes === 2) {\n\t\t\ttmp = (bytes[len - 2] << 8) + bytes[len - 1];\n\t\t\tparts.push(\n\t\t\t\t`${Base64._LOOKUP[tmp >> 10] + Base64._LOOKUP[(tmp >> 4) & 0x3f] + Base64._LOOKUP[(tmp << 2) & 0x3f]}=`\n\t\t\t);\n\t\t}\n\n\t\treturn parts.join(\"\");\n\t}\n\n\t/**\n\t * Calculate the byte length.\n\t * @param validLen The valid length.\n\t * @param placeHoldersLen The placeholder length.\n\t * @returns The length.\n\t * @internal\n\t */\n\tprivate static calcByteLength(validLen: number, placeHoldersLen: number): number {\n\t\treturn ((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen;\n\t}\n\n\t/**\n\t * Get the valid and placeholder lengths from a bas64 string.\n\t * @param base64 The base64 string.\n\t * @returns The lengths.\n\t * @internal\n\t */\n\tprivate static getLengths(base64: string): number[] {\n\t\tconst len = base64.length;\n\n\t\tif (len % 4 > 0) {\n\t\t\tthrow new GeneralError(Base64.CLASS_NAME, \"length4Multiple\", { value: len });\n\t\t}\n\n\t\t// Trim off extra bytes after placeholder bytes are found\n\t\t// See: https://github.com/beatgammit/base64-js/issues/42\n\t\tlet validLen = base64.indexOf(\"=\");\n\t\tif (validLen === -1) {\n\t\t\tvalidLen = len;\n\t\t}\n\n\t\tconst placeHoldersLen = validLen === len ? 0 : 4 - (validLen % 4);\n\n\t\treturn [validLen, placeHoldersLen];\n\t}\n\n\t/**\n\t * Convert the triplet to base 64.\n\t * @param num The number to convert.\n\t * @returns The base64 encoding.\n\t * @internal\n\t */\n\tprivate static tripletToBase64(num: number): string {\n\t\treturn (\n\t\t\tBase64._LOOKUP[(num >> 18) & 0x3f] +\n\t\t\tBase64._LOOKUP[(num >> 12) & 0x3f] +\n\t\t\tBase64._LOOKUP[(num >> 6) & 0x3f] +\n\t\t\tBase64._LOOKUP[num & 0x3f]\n\t\t);\n\t}\n\n\t/**\n\t * Encode a chunk.\n\t * @param bytes The byte array.\n\t * @param start The start index in the buffer.\n\t * @param end The end index in the buffer.\n\t * @returns The encoded chunk.\n\t * @internal\n\t */\n\tprivate static encodeChunk(bytes: Uint8Array, start: number, end: number): string {\n\t\tlet tmp;\n\t\tconst output = [];\n\t\tfor (let i = start; i < end; i += 3) {\n\t\t\ttmp = ((bytes[i] << 16) & 0xff0000) + ((bytes[i + 1] << 8) & 0xff00) + (bytes[i + 2] & 0xff);\n\t\t\toutput.push(Base64.tripletToBase64(tmp));\n\t\t}\n\t\treturn output.join(\"\");\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"base64.js","sourceRoot":"","sources":["../../../src/encoding/base64.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,+BAA+B;AAC/B,uCAAuC;AAGvC,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;;GAGG;AACH,MAAM,OAAO,MAAM;IAClB;;OAEG;IACI,MAAM,CAAU,UAAU,YAA4B;IAE7D;;;OAGG;IACK,MAAM,CAAU,OAAO,GAC9B,kEAAkE,CAAC;IAEpE;;;OAGG;IACK,MAAM,CAAU,eAAe,GAA6B;QACnE,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;Q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AAW;QACzC,OAAO,CACN,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;YAClC,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;YAClC,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC;YACjC,MAAM,CAAC,OAAO,CAAC,GAAG,GAAG,IAAI,CAAC,CAC1B,CAAC;IACH,CAAC;IAED;;;;;;;OAOG;IACK,MAAM,CAAC,WAAW,CAAC,KAAiB,EAAE,KAAa,EAAE,GAAW;QACvE,IAAI,GAAG,CAAC;QACR,MAAM,MAAM,GAAG,EAAE,CAAC;QAClB,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;YACrC,GAAG,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;YAC7F,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC;QAC1C,CAAC;QACD,OAAO,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACxB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/* eslint-disable no-bitwise */\n/* eslint-disable no-mixed-operators */\n\nimport { nameof } from \"@twin.org/nameof\";\nimport { GeneralError } from \"../errors/generalError.js\";\nimport { Guards } from \"../utils/guards.js\";\n\n/**\n * Class to help with base64 Encoding/Decoding.\n * Sourced from https://github.com/beatgammit/base64-js.\n */\nexport class Base64 {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Base64>();\n\n\t/**\n\t * Alphabet table for encoding.\n\t * @internal\n\t */\n\tprivate static readonly _LOOKUP: string =\n\t\t\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\";\n\n\t/**\n\t * Alphabet table for decoding.\n\t * @internal\n\t */\n\tprivate static readonly _REVERSE_LOOKUP: { [id: number]: number } = {\n\t\t\"43\": 62,\n\t\t\"45\": 62,\n\t\t\"47\": 63,\n\t\t\"48\": 52,\n\t\t\"49\": 53,\n\t\t\"50\": 54,\n\t\t\"51\": 55,\n\t\t\"52\": 56,\n\t\t\"53\": 57,\n\t\t\"54\": 58,\n\t\t\"55\": 59,\n\t\t\"56\": 60,\n\t\t\"57\": 61,\n\t\t\"65\": 0,\n\t\t\"66\": 1,\n\t\t\"67\": 2,\n\t\t\"68\": 3,\n\t\t\"69\": 4,\n\t\t\"70\": 5,\n\t\t\"71\": 6,\n\t\t\"72\": 7,\n\t\t\"73\": 8,\n\t\t\"74\": 9,\n\t\t\"75\": 10,\n\t\t\"76\": 11,\n\t\t\"77\": 12,\n\t\t\"78\": 13,\n\t\t\"79\": 14,\n\t\t\"80\": 15,\n\t\t\"81\": 16,\n\t\t\"82\": 17,\n\t\t\"83\": 18,\n\t\t\"84\": 19,\n\t\t\"85\": 20,\n\t\t\"86\": 21,\n\t\t\"87\": 22,\n\t\t\"88\": 23,\n\t\t\"89\": 24,\n\t\t\"90\": 25,\n\t\t\"95\": 63,\n\t\t\"97\": 26,\n\t\t\"98\": 27,\n\t\t\"99\": 28,\n\t\t\"100\": 29,\n\t\t\"101\": 30,\n\t\t\"102\": 31,\n\t\t\"103\": 32,\n\t\t\"104\": 33,\n\t\t\"105\": 34,\n\t\t\"106\": 35,\n\t\t\"107\": 36,\n\t\t\"108\": 37,\n\t\t\"109\": 38,\n\t\t\"110\": 39,\n\t\t\"111\": 40,\n\t\t\"112\": 41,\n\t\t\"113\": 42,\n\t\t\"114\": 43,\n\t\t\"115\": 44,\n\t\t\"116\": 45,\n\t\t\"117\": 46,\n\t\t\"118\": 47,\n\t\t\"119\": 48,\n\t\t\"120\": 49,\n\t\t\"121\": 50,\n\t\t\"122\": 51\n\t};\n\n\t/**\n\t * Get the byte length of the data.\n\t * @param base64 The base64 string.\n\t * @returns The byte length of the data.\n\t */\n\tpublic static byteLength(base64: string): number {\n\t\tconst lens = Base64.getLengths(base64);\n\t\treturn Base64.calcByteLength(lens[0], lens[1]);\n\t}\n\n\t/**\n\t * Convert the base 64 string to a byte array.\n\t * @param base64 The base64 string to convert.\n\t * @returns The byte array.\n\t */\n\tpublic static decode(base64: string): Uint8Array {\n\t\tGuards.string(Base64.CLASS_NAME, nameof(base64), base64);\n\n\t\tlet tmp;\n\t\tconst lens = Base64.getLengths(base64);\n\t\tconst validLen = lens[0];\n\t\tconst placeHoldersLen = lens[1];\n\n\t\tconst arr = new Uint8Array(Base64.calcByteLength(validLen, placeHoldersLen));\n\n\t\tlet curByte = 0;\n\n\t\t// if there are placeholders, only get up to the last complete 4 chars\n\t\tconst len = placeHoldersLen > 0 ? validLen - 4 : validLen;\n\n\t\tlet i;\n\t\tfor (i = 0; i < len; i += 4) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 18) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 12) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] << 6) |\n\t\t\t\tBase64._REVERSE_LOOKUP[base64.charCodeAt(i + 3)];\n\t\t\tarr[curByte++] = (tmp >> 16) & 0xff;\n\t\t\tarr[curByte++] = (tmp >> 8) & 0xff;\n\t\t\tarr[curByte++] = tmp & 0xff;\n\t\t}\n\n\t\tif (placeHoldersLen === 2) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 2) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] >> 4);\n\t\t\tarr[curByte++] = tmp & 0xff;\n\t\t}\n\n\t\tif (placeHoldersLen === 1) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 10) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 4) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] >> 2);\n\t\t\tarr[curByte++] = (tmp >> 8) & 0xff;\n\t\t\tarr[curByte] = tmp & 0xff;\n\t\t}\n\n\t\treturn arr;\n\t}\n\n\t/**\n\t * Convert a byte array to base 64.\n\t * @param bytes The byte array to convert.\n\t * @returns The data as base64 string.\n\t */\n\tpublic static encode(bytes: Uint8Array): string {\n\t\tGuards.uint8Array(Base64.CLASS_NAME, nameof(bytes), bytes);\n\n\t\tlet tmp;\n\t\tconst len = bytes.length;\n\t\tconst extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes\n\t\tconst parts = [];\n\t\tconst maxChunkLength = 16383; // must be multiple of 3\n\n\t\t// go through the array every three bytes, we'll deal with trailing stuff later\n\t\tfor (let i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {\n\t\t\tparts.push(Base64.encodeChunk(bytes, i, Math.min(i + maxChunkLength, len2)));\n\t\t}\n\n\t\t// pad the end with zeros, but make sure to not forget the extra bytes\n\t\tif (extraBytes === 1) {\n\t\t\ttmp = bytes[len - 1];\n\t\t\tparts.push(`${Base64._LOOKUP[tmp >> 2] + Base64._LOOKUP[(tmp << 4) & 0x3f]}==`);\n\t\t} else if (extraBytes === 2) {\n\t\t\ttmp = (bytes[len - 2] << 8) + bytes[len - 1];\n\t\t\tparts.push(\n\t\t\t\t`${Base64._LOOKUP[tmp >> 10] + Base64._LOOKUP[(tmp >> 4) & 0x3f] + Base64._LOOKUP[(tmp << 2) & 0x3f]}=`\n\t\t\t);\n\t\t}\n\n\t\treturn parts.join(\"\");\n\t}\n\n\t/**\n\t * Calculate the byte length.\n\t * @param validLen The valid length.\n\t * @param placeHoldersLen The placeholder length.\n\t * @returns The length.\n\t * @internal\n\t */\n\tprivate static calcByteLength(validLen: number, placeHoldersLen: number): number {\n\t\treturn ((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen;\n\t}\n\n\t/**\n\t * Get the valid and placeholder lengths from a bas64 string.\n\t * @param base64 The base64 string.\n\t * @returns The lengths.\n\t * @internal\n\t */\n\tprivate static getLengths(base64: string): number[] {\n\t\tconst len = base64.length;\n\n\t\tif (len % 4 > 0) {\n\t\t\tthrow new GeneralError(Base64.CLASS_NAME, \"length4Multiple\", { value: len });\n\t\t}\n\n\t\t// Trim off extra bytes after placeholder bytes are found\n\t\t// See: https://github.com/beatgammit/base64-js/issues/42\n\t\tlet validLen = base64.indexOf(\"=\");\n\t\tif (validLen === -1) {\n\t\t\tvalidLen = len;\n\t\t}\n\n\t\tconst placeHoldersLen = validLen === len ? 0 : 4 - (validLen % 4);\n\n\t\treturn [validLen, placeHoldersLen];\n\t}\n\n\t/**\n\t * Convert the triplet to base 64.\n\t * @param num The number to convert.\n\t * @returns The base64 encoding.\n\t * @internal\n\t */\n\tprivate static tripletToBase64(num: number): string {\n\t\treturn (\n\t\t\tBase64._LOOKUP[(num >> 18) & 0x3f] +\n\t\t\tBase64._LOOKUP[(num >> 12) & 0x3f] +\n\t\t\tBase64._LOOKUP[(num >> 6) & 0x3f] +\n\t\t\tBase64._LOOKUP[num & 0x3f]\n\t\t);\n\t}\n\n\t/**\n\t * Encode a chunk.\n\t * @param bytes The byte array.\n\t * @param start The start index in the buffer.\n\t * @param end The end index in the buffer.\n\t * @returns The encoded chunk.\n\t * @internal\n\t */\n\tprivate static encodeChunk(bytes: Uint8Array, start: number, end: number): string {\n\t\tlet tmp;\n\t\tconst output = [];\n\t\tfor (let i = start; i < end; i += 3) {\n\t\t\ttmp = ((bytes[i] << 16) & 0xff0000) + ((bytes[i + 1] << 8) & 0xff00) + (bytes[i + 2] & 0xff);\n\t\t\toutput.push(Base64.tripletToBase64(tmp));\n\t\t}\n\t\treturn output.join(\"\");\n\t}\n}\n"]}
|
|
@@ -10,7 +10,7 @@ export class AlreadyExistsError extends BaseError {
|
|
|
10
10
|
/**
|
|
11
11
|
* Create a new instance of AlreadyExistsError.
|
|
12
12
|
* @param source The source of the error.
|
|
13
|
-
* @param message The message as
|
|
13
|
+
* @param message The message as an i18n key.
|
|
14
14
|
* @param existingId The id for the item.
|
|
15
15
|
* @param properties Any additional information for the error.
|
|
16
16
|
* @param cause The cause of the error if we have wrapped another error.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"alreadyExistsError.js","sourceRoot":"","sources":["../../../src/errors/alreadyExistsError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,kBAAmB,SAAQ,SAAS;IAChD;;OAEG;IACI,MAAM,CAAU,UAAU,wBAAwC;IAEzE;;;;;;;OAOG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAmB,EACnB,UAAsC,EACtC,KAAe;QAEf,KAAK,CAAC,kBAAkB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,UAAU,EAAE,GAAG,UAAU,EAAE,EAAE,KAAK,CAAC,CAAC;IAC7F,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors which are triggered by data already existing.\n */\nexport class AlreadyExistsError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<AlreadyExistsError>();\n\n\t/**\n\t * Create a new instance of AlreadyExistsError.\n\t * @param source The source of the error.\n\t * @param message The message as
|
|
1
|
+
{"version":3,"file":"alreadyExistsError.js","sourceRoot":"","sources":["../../../src/errors/alreadyExistsError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,kBAAmB,SAAQ,SAAS;IAChD;;OAEG;IACI,MAAM,CAAU,UAAU,wBAAwC;IAEzE;;;;;;;OAOG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAmB,EACnB,UAAsC,EACtC,KAAe;QAEf,KAAK,CAAC,kBAAkB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,UAAU,EAAE,GAAG,UAAU,EAAE,EAAE,KAAK,CAAC,CAAC;IAC7F,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors which are triggered by data already existing.\n */\nexport class AlreadyExistsError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<AlreadyExistsError>();\n\n\t/**\n\t * Create a new instance of AlreadyExistsError.\n\t * @param source The source of the error.\n\t * @param message The message as an i18n key.\n\t * @param existingId The id for the item.\n\t * @param properties Any additional information for the error.\n\t * @param cause The cause of the error if we have wrapped another error.\n\t */\n\tconstructor(\n\t\tsource: string,\n\t\tmessage: string,\n\t\texistingId?: string,\n\t\tproperties?: { [id: string]: unknown },\n\t\tcause?: unknown\n\t) {\n\t\tsuper(AlreadyExistsError.CLASS_NAME, source, message, { existingId, ...properties }, cause);\n\t}\n}\n"]}
|
|
@@ -22,7 +22,7 @@ export class BaseError extends Error {
|
|
|
22
22
|
* Create a new instance of BaseError.
|
|
23
23
|
* @param name The name of the error.
|
|
24
24
|
* @param source The source of the error.
|
|
25
|
-
* @param message The message as
|
|
25
|
+
* @param message The message as an i18n key.
|
|
26
26
|
* @param properties Any additional information for the error.
|
|
27
27
|
* @param cause The cause of error if we have wrapped another error.
|
|
28
28
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"baseError.js","sourceRoot":"","sources":["../../../src/errors/baseError.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAE1D,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAEpC;;GAEG;AACH,MAAM,OAAO,SAAU,SAAQ,KAAK;IACnC;;OAEG;IACI,MAAM,CAAU;IAEvB;;OAEG;IACI,UAAU,CAA6B;IAE9C;;OAEG;IACI,KAAK,CAAU;IAEtB;;;;;;;OAOG;IACH,YACC,IAAY,EACZ,MAAc,EACd,OAAe,EACf,UAAsC,EACtC,KAAe;QAEf,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QAC5F,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAE7B,mEAAmE;QACnE,sCAAsC;QACtC,IACC,EAAE,CAAC,WAAW,CAAC,MAAM,CAAC;YACtB,EAAE,CAAC,WAAW,CAAC,OAAO,CAAC;YACvB,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC;YACtB,kEAAkE;YAClE,6CAA6C;YAC7C,YAAY,CAAC,SAAS,CAAC,OAAO,CAAC,KAAK,OAAO,EAC1C,CAAC;YACF,IAAI,CAAC,OAAO,GAAG,GAAG,YAAY,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,OAAO,EAAE,CAAC;QAC/D,CAAC;IACF,CA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AAI,CAAC,iBAAiB,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YAChE,GAAG,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QACxB,CAAC;QACD,IAAI,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YAC7B,GAAG,CAAC,KAAK,GAAG,SAAS,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,YAAY,CAAC,iBAAiB,CAAC,CAAC;QAC7E,CAAC;QACD,OAAO,GAAa,CAAC;IACtB,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { StringHelper } from \"../helpers/stringHelper.js\";\nimport type { IError } from \"../models/IError.js\";\nimport { Is } from \"../utils/is.js\";\n\n/**\n * Class to handle errors.\n */\nexport class BaseError extends Error implements IError {\n\t/**\n\t * The source of the error.\n\t */\n\tpublic source?: string;\n\n\t/**\n\t * Any additional information for the error.\n\t */\n\tpublic properties?: { [id: string]: unknown };\n\n\t/**\n\t * The cause of the error.\n\t */\n\tpublic cause?: IError;\n\n\t/**\n\t * Create a new instance of BaseError.\n\t * @param name The name of the error.\n\t * @param source The source of the error.\n\t * @param message The message as a code.\n\t * @param properties Any additional information for the error.\n\t * @param cause The cause of error if we have wrapped another error.\n\t */\n\tconstructor(\n\t\tname: string,\n\t\tsource: string,\n\t\tmessage: string,\n\t\tproperties?: { [id: string]: unknown },\n\t\tcause?: unknown\n\t) {\n\t\tsuper(message);\n\t\tthis.name = name;\n\t\tthis.source = source;\n\t\tthis.cause = Is.notEmpty(cause) ? BaseError.fromError(cause).toJsonObject(true) : undefined;\n\t\tthis.properties = properties;\n\n\t\t// If the message is camel case but has no namespace then prefix it\n\t\t// with the source name in camel case.\n\t\tif (\n\t\t\tIs.stringValue(source) &&\n\t\t\tIs.stringValue(message) &&\n\t\t\t!message.includes(\".\") &&\n\t\t\t// This comparison checks that it is most likely a camel case name\n\t\t\t// and not a free text error with a dot in it\n\t\t\tStringHelper.camelCase(message) === message\n\t\t) {\n\t\t\tthis.message = `${StringHelper.camelCase(source)}.${message}`;\n\t\t}\n\t}\n\n\t/**\n\t * Construct an error from an existing one.\n\t * @param err The existing error.\n\t * @returns The new instance.\n\t */\n\tpublic static fromError(err: unknown): BaseError {\n\t\tlet name = \"Base\";\n\t\tlet message;\n\t\tlet source;\n\t\tlet properties;\n\t\tlet cause;\n\t\tlet stack;\n\n\t\tif (Is.object<{ error: string }>(err) && Is.stringValue(err.error)) {\n\t\t\tmessage = err.error;\n\t\t} else if (Is.object<BaseError>(err)) {\n\t\t\tif (Is.stringValue(err.name)) {\n\t\t\t\tname = err.name;\n\t\t\t}\n\t\t\tif (Is.stringValue(err.source)) {\n\t\t\t\tsource = err.source;\n\t\t\t}\n\t\t\tif (Is.stringValue(err.message)) {\n\t\t\t\tmessage = err.message;\n\t\t\t}\n\t\t\tif (Is.notEmpty(err.properties)) {\n\t\t\t\tproperties = err.properties;\n\t\t\t}\n\t\t\tif (BaseError.isAggregateError(err)) {\n\t\t\t\tproperties ??= {};\n\t\t\t\tproperties.errors = err.errors;\n\t\t\t}\n\t\t\tif (Is.notEmpty(err.cause)) {\n\t\t\t\tcause = err.cause;\n\t\t\t}\n\t\t\tif (Is.notEmpty(err.stack)) {\n\t\t\t\tstack = err.stack;\n\t\t\t}\n\t\t} else if (Is.stringValue(err)) {\n\t\t\tmessage = err;\n\t\t} else {\n\t\t\tmessage = JSON.stringify(err);\n\t\t}\n\n\t\tconst baseError = new BaseError(name, source ?? \"\", message ?? \"\", properties, cause);\n\n\t\tbaseError.stack = stack;\n\n\t\treturn baseError;\n\t}\n\n\t/**\n\t * Flatten an error tree.\n\t * @param err The starting error.\n\t * @returns The list of all internal errors.\n\t */\n\tpublic static flatten(err: unknown): IError[] {\n\t\tconst flattened: IError[] = [];\n\n\t\tlet e: IError | undefined = BaseError.fromError(err).toJsonObject(true);\n\n\t\twhile (e) {\n\t\t\tconst cause: IError | undefined = e.cause;\n\t\t\te.cause = undefined;\n\t\t\tflattened.push(e);\n\t\t\te = cause;\n\t\t}\n\n\t\treturn flattened;\n\t}\n\n\t/**\n\t * Expand an error tree.\n\t * @param errors The list of errors to expand.\n\t * @returns The first level error.\n\t */\n\tpublic static expand(errors: IError[] | undefined): IError | undefined {\n\t\tlet first: IError | undefined;\n\n\t\tif (Is.arrayValue(errors)) {\n\t\t\tfirst = errors[0];\n\t\t\tlet current = first;\n\t\t\tfor (let i = 1; i < errors.length; i++) {\n\t\t\t\tcurrent.cause = errors[i];\n\t\t\t\tcurrent = current.cause;\n\t\t\t}\n\t\t}\n\n\t\treturn first;\n\t}\n\n\t/**\n\t * Test to see if the error has the specified error name.\n\t * @param error The error to test.\n\t * @param name The name to check for.\n\t * @returns True if the error has the name.\n\t */\n\tpublic static isErrorName(error: unknown, name: string | RegExp): error is BaseError {\n\t\treturn (\n\t\t\tIs.object<{ name: string }>(error) &&\n\t\t\t(Is.string(name) ? error.name === name : name.test(error.name))\n\t\t);\n\t}\n\n\t/**\n\t * Test to see if the error has the specified error message.\n\t * @param error The error to test.\n\t * @param message The message to check for.\n\t * @returns True if the error has the name.\n\t */\n\tpublic static isErrorMessage(error: unknown, message: string | RegExp): error is BaseError {\n\t\treturn (\n\t\t\tIs.object<{ message: string }>(error) &&\n\t\t\t(Is.string(message) ? error.message === message : message.test(error.message))\n\t\t);\n\t}\n\n\t/**\n\t * Test to see if the error has the specified error code.\n\t * @param error The error to test.\n\t * @param code The code to check for.\n\t * @returns True if the error has the code.\n\t */\n\tpublic static isErrorCode(error: unknown, code: string | RegExp): boolean {\n\t\treturn (\n\t\t\tIs.object<{ code: string }>(error) &&\n\t\t\t(Is.string(code) ? error.code === code : code.test(error.code))\n\t\t);\n\t}\n\n\t/**\n\t * Test to see if any of the errors or children have the given error name.\n\t * @param error The error to test.\n\t * @param name The name to check for.\n\t * @returns True if the error has the name.\n\t */\n\tpublic static someErrorName(error: unknown, name: string | RegExp): error is BaseError {\n\t\treturn BaseError.flatten(error).some(e => BaseError.isErrorName(e, name));\n\t}\n\n\t/**\n\t * Test to see if any of the errors or children have the given error message.\n\t * @param error The error to test.\n\t * @param message The message to check for.\n\t * @returns True if the error has the name.\n\t */\n\tpublic static someErrorMessage(error: unknown, message: string | RegExp): error is BaseError {\n\t\treturn BaseError.flatten(error).some(e => BaseError.isErrorMessage(e, message));\n\t}\n\n\t/**\n\t * Test to see if any of the errors or children are from a specific class.\n\t * @param error The error to test.\n\t * @param cls The class to check for.\n\t * @returns True if the error has the specific class.\n\t */\n\tpublic static someErrorClass(error: unknown, cls: string): error is BaseError {\n\t\tconst errorClass = StringHelper.camelCase(cls);\n\t\tconst regExp = new RegExp(`^${errorClass}\\\\.`);\n\t\treturn BaseError.flatten(error).some(e => BaseError.isErrorMessage(e, regExp));\n\t}\n\n\t/**\n\t * Test to see if any of the errors or children have the given error code.\n\t * @param error The error to test.\n\t * @param code The code to check for.\n\t * @returns True if the error has the name.\n\t */\n\tpublic static someErrorCode(error: unknown, code: string | RegExp): error is BaseError {\n\t\treturn BaseError.flatten(error).some(e => BaseError.isErrorCode(e, code));\n\t}\n\n\t/**\n\t * Is the error empty, i.e. does it have no message, source, properties, or cause?\n\t * @param err The error to check for being empty.\n\t * @returns True if the error is empty.\n\t */\n\tpublic static isEmpty(err: IError): boolean {\n\t\treturn (\n\t\t\t!Is.stringValue(err.message) &&\n\t\t\t!Is.stringValue(err.source) &&\n\t\t\t!Is.objectValue(err.properties) &&\n\t\t\tIs.empty(err.cause)\n\t\t);\n\t}\n\n\t/**\n\t * Is the error an aggregate error.\n\t * @param err The error to check for being an aggregate error.\n\t * @returns True if the error is an aggregate error.\n\t */\n\tpublic static isAggregateError(err: unknown): err is AggregateError {\n\t\t// This is the only way we can reliably check for AggregateError\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn err instanceof AggregateError;\n\t}\n\n\t/**\n\t * Convert the aggregate error to an array of errors.\n\t * @param err The error to convert.\n\t * @param includeStackTrace Whether to include the error stack in the model, defaults to false.\n\t * @returns The array of errors.\n\t */\n\tpublic static fromAggregate(err: unknown, includeStackTrace?: boolean): IError[] {\n\t\tif (BaseError.isAggregateError(err)) {\n\t\t\treturn err.errors.map(e => BaseError.fromError(e).toJsonObject(includeStackTrace));\n\t\t}\n\t\treturn [BaseError.fromError(err).toJsonObject(includeStackTrace)];\n\t}\n\n\t/**\n\t * Serialize the error to the error model.\n\t * @param includeStackTrace Whether to include the error stack in the model, defaults to false.\n\t * @returns The error model.\n\t */\n\tpublic toJsonObject(includeStackTrace?: boolean): IError {\n\t\tconst err: Partial<IError> = {};\n\t\tif (Is.stringValue(this.name)) {\n\t\t\terr.name = this.name;\n\t\t}\n\t\tif (Is.stringValue(this.source)) {\n\t\t\terr.source = this.source;\n\t\t}\n\t\tif (Is.stringValue(this.message)) {\n\t\t\terr.message = this.message;\n\t\t}\n\t\tif (Is.object(this.properties)) {\n\t\t\terr.properties = this.properties;\n\t\t}\n\t\tif ((includeStackTrace ?? false) && Is.stringValue(this.stack)) {\n\t\t\terr.stack = this.stack;\n\t\t}\n\t\tif (Is.notEmpty(this.cause)) {\n\t\t\terr.cause = BaseError.fromError(this.cause).toJsonObject(includeStackTrace);\n\t\t}\n\t\treturn err as IError;\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"baseError.js","sourceRoot":"","sources":["../../../src/errors/baseError.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAE1D,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAEpC;;GAEG;AACH,MAAM,OAAO,SAAU,SAAQ,KAAK;IACnC;;OAEG;IACI,MAAM,CAAU;IAEvB;;OAEG;IACI,UAAU,CAA6B;IAE9C;;OAEG;IACI,KAAK,CAAU;IAEtB;;;;;;;OAOG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ung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { StringHelper } from \"../helpers/stringHelper.js\";\nimport type { IError } from \"../models/IError.js\";\nimport { Is } from \"../utils/is.js\";\n\n/**\n * Class to handle errors.\n */\nexport class BaseError extends Error implements IError {\n\t/**\n\t * The source of the error.\n\t */\n\tpublic source?: string;\n\n\t/**\n\t * Any additional information for the error.\n\t */\n\tpublic properties?: { [id: string]: unknown };\n\n\t/**\n\t * The cause of the error.\n\t */\n\tpublic cause?: IError;\n\n\t/**\n\t * Create a new instance of BaseError.\n\t * @param name The name of the error.\n\t * @param source The source of the error.\n\t * @param message The message as an i18n key.\n\t * @param properties Any additional information for the error.\n\t * @param cause The cause of error if we have wrapped another error.\n\t */\n\tconstructor(\n\t\tname: string,\n\t\tsource: string,\n\t\tmessage: string,\n\t\tproperties?: { [id: string]: unknown },\n\t\tcause?: unknown\n\t) {\n\t\tsuper(message);\n\t\tthis.name = name;\n\t\tthis.source = source;\n\t\tthis.cause = Is.notEmpty(cause) ? BaseError.fromError(cause).toJsonObject(true) : undefined;\n\t\tthis.properties = properties;\n\n\t\t// If the message is camel case but has no namespace then prefix it\n\t\t// with the source name in camel case.\n\t\tif (\n\t\t\tIs.stringValue(source) &&\n\t\t\tIs.stringValue(message) &&\n\t\t\t!message.includes(\".\") &&\n\t\t\t// This comparison checks that it is most likely a camel case name\n\t\t\t// and not a free text error with a dot in it\n\t\t\tStringHelper.camelCase(message) === message\n\t\t) {\n\t\t\tthis.message = `${StringHelper.camelCase(source)}.${message}`;\n\t\t}\n\t}\n\n\t/**\n\t * Construct an error from an existing one.\n\t * @param err The existing error.\n\t * @returns The new instance.\n\t */\n\tpublic static fromError(err: unknown): BaseError {\n\t\tlet name = \"Base\";\n\t\tlet message;\n\t\tlet source;\n\t\tlet properties;\n\t\tlet cause;\n\t\tlet stack;\n\n\t\tif (Is.object<{ error: string }>(err) && Is.stringValue(err.error)) {\n\t\t\tmessage = err.error;\n\t\t} else if (Is.object<BaseError>(err)) {\n\t\t\tif (Is.stringValue(err.name)) {\n\t\t\t\tname = err.name;\n\t\t\t}\n\t\t\tif (Is.stringValue(err.source)) {\n\t\t\t\tsource = err.source;\n\t\t\t}\n\t\t\tif (Is.stringValue(err.message)) {\n\t\t\t\tmessage = err.message;\n\t\t\t}\n\t\t\tif (Is.notEmpty(err.properties)) {\n\t\t\t\tproperties = err.properties;\n\t\t\t}\n\t\t\tif (BaseError.isAggregateError(err)) {\n\t\t\t\tproperties ??= {};\n\t\t\t\tproperties.errors = err.errors;\n\t\t\t}\n\t\t\tif (Is.notEmpty(err.cause)) {\n\t\t\t\tcause = err.cause;\n\t\t\t}\n\t\t\tif (Is.notEmpty(err.stack)) {\n\t\t\t\tstack = err.stack;\n\t\t\t}\n\t\t} else if (Is.stringValue(err)) {\n\t\t\tmessage = err;\n\t\t} else {\n\t\t\tmessage = JSON.stringify(err);\n\t\t}\n\n\t\tconst baseError = new BaseError(name, source ?? \"\", message ?? \"\", properties, cause);\n\n\t\tbaseError.stack = stack;\n\n\t\treturn baseError;\n\t}\n\n\t/**\n\t * Flatten an error tree.\n\t * @param err The starting error.\n\t * @returns The list of all internal errors.\n\t */\n\tpublic static flatten(err: unknown): IError[] {\n\t\tconst flattened: IError[] = [];\n\n\t\tlet e: IError | undefined = BaseError.fromError(err).toJsonObject(true);\n\n\t\twhile (e) {\n\t\t\tconst cause: IError | undefined = e.cause;\n\t\t\te.cause = undefined;\n\t\t\tflattened.push(e);\n\t\t\te = cause;\n\t\t}\n\n\t\treturn flattened;\n\t}\n\n\t/**\n\t * Expand an error tree.\n\t * @param errors The list of errors to expand.\n\t * @returns The first level error.\n\t */\n\tpublic static expand(errors: IError[] | undefined): IError | undefined {\n\t\tlet first: IError | undefined;\n\n\t\tif (Is.arrayValue(errors)) {\n\t\t\tfirst = errors[0];\n\t\t\tlet current = first;\n\t\t\tfor (let i = 1; i < errors.length; i++) {\n\t\t\t\tcurrent.cause = errors[i];\n\t\t\t\tcurrent = current.cause;\n\t\t\t}\n\t\t}\n\n\t\treturn first;\n\t}\n\n\t/**\n\t * Test to see if the error has the specified error name.\n\t * @param error The error to test.\n\t * @param name The name to check for.\n\t * @returns True if the error has the name.\n\t */\n\tpublic static isErrorName(error: unknown, name: string | RegExp): error is BaseError {\n\t\treturn (\n\t\t\tIs.object<{ name: string }>(error) &&\n\t\t\t(Is.string(name) ? error.name === name : name.test(error.name))\n\t\t);\n\t}\n\n\t/**\n\t * Test to see if the error has the specified error message.\n\t * @param error The error to test.\n\t * @param message The message to check for.\n\t * @returns True if the error has the name.\n\t */\n\tpublic static isErrorMessage(error: unknown, message: string | RegExp): error is BaseError {\n\t\treturn (\n\t\t\tIs.object<{ message: string }>(error) &&\n\t\t\t(Is.string(message) ? error.message === message : message.test(error.message))\n\t\t);\n\t}\n\n\t/**\n\t * Test to see if the error has the specified error code.\n\t * @param error The error to test.\n\t * @param code The code to check for.\n\t * @returns True if the error has the code.\n\t */\n\tpublic static isErrorCode(error: unknown, code: string | RegExp): boolean {\n\t\treturn (\n\t\t\tIs.object<{ code: string }>(error) &&\n\t\t\t(Is.string(code) ? error.code === code : code.test(error.code))\n\t\t);\n\t}\n\n\t/**\n\t * Test to see if any of the errors or children have the given error name.\n\t * @param error The error to test.\n\t * @param name The name to check for.\n\t * @returns True if the error has the name.\n\t */\n\tpublic static someErrorName(error: unknown, name: string | RegExp): error is BaseError {\n\t\treturn BaseError.flatten(error).some(e => BaseError.isErrorName(e, name));\n\t}\n\n\t/**\n\t * Test to see if any of the errors or children have the given error message.\n\t * @param error The error to test.\n\t * @param message The message to check for.\n\t * @returns True if the error has the name.\n\t */\n\tpublic static someErrorMessage(error: unknown, message: string | RegExp): error is BaseError {\n\t\treturn BaseError.flatten(error).some(e => BaseError.isErrorMessage(e, message));\n\t}\n\n\t/**\n\t * Test to see if any of the errors or children are from a specific class.\n\t * @param error The error to test.\n\t * @param cls The class to check for.\n\t * @returns True if the error has the specific class.\n\t */\n\tpublic static someErrorClass(error: unknown, cls: string): error is BaseError {\n\t\tconst errorClass = StringHelper.camelCase(cls);\n\t\tconst regExp = new RegExp(`^${errorClass}\\\\.`);\n\t\treturn BaseError.flatten(error).some(e => BaseError.isErrorMessage(e, regExp));\n\t}\n\n\t/**\n\t * Test to see if any of the errors or children have the given error code.\n\t * @param error The error to test.\n\t * @param code The code to check for.\n\t * @returns True if the error has the name.\n\t */\n\tpublic static someErrorCode(error: unknown, code: string | RegExp): error is BaseError {\n\t\treturn BaseError.flatten(error).some(e => BaseError.isErrorCode(e, code));\n\t}\n\n\t/**\n\t * Is the error empty, i.e. does it have no message, source, properties, or cause?\n\t * @param err The error to check for being empty.\n\t * @returns True if the error is empty.\n\t */\n\tpublic static isEmpty(err: IError): boolean {\n\t\treturn (\n\t\t\t!Is.stringValue(err.message) &&\n\t\t\t!Is.stringValue(err.source) &&\n\t\t\t!Is.objectValue(err.properties) &&\n\t\t\tIs.empty(err.cause)\n\t\t);\n\t}\n\n\t/**\n\t * Is the error an aggregate error.\n\t * @param err The error to check for being an aggregate error.\n\t * @returns True if the error is an aggregate error.\n\t */\n\tpublic static isAggregateError(err: unknown): err is AggregateError {\n\t\t// This is the only way we can reliably check for AggregateError\n\t\t// eslint-disable-next-line no-restricted-syntax\n\t\treturn err instanceof AggregateError;\n\t}\n\n\t/**\n\t * Convert the aggregate error to an array of errors.\n\t * @param err The error to convert.\n\t * @param includeStackTrace Whether to include the error stack in the model, defaults to false.\n\t * @returns The array of errors.\n\t */\n\tpublic static fromAggregate(err: unknown, includeStackTrace?: boolean): IError[] {\n\t\tif (BaseError.isAggregateError(err)) {\n\t\t\treturn err.errors.map(e => BaseError.fromError(e).toJsonObject(includeStackTrace));\n\t\t}\n\t\treturn [BaseError.fromError(err).toJsonObject(includeStackTrace)];\n\t}\n\n\t/**\n\t * Serialize the error to the error model.\n\t * @param includeStackTrace Whether to include the error stack in the model, defaults to false.\n\t * @returns The error model.\n\t */\n\tpublic toJsonObject(includeStackTrace?: boolean): IError {\n\t\tconst err: Partial<IError> = {};\n\t\tif (Is.stringValue(this.name)) {\n\t\t\terr.name = this.name;\n\t\t}\n\t\tif (Is.stringValue(this.source)) {\n\t\t\terr.source = this.source;\n\t\t}\n\t\tif (Is.stringValue(this.message)) {\n\t\t\terr.message = this.message;\n\t\t}\n\t\tif (Is.object(this.properties)) {\n\t\t\terr.properties = this.properties;\n\t\t}\n\t\tif ((includeStackTrace ?? false) && Is.stringValue(this.stack)) {\n\t\t\terr.stack = this.stack;\n\t\t}\n\t\tif (Is.notEmpty(this.cause)) {\n\t\t\terr.cause = BaseError.fromError(this.cause).toJsonObject(includeStackTrace);\n\t\t}\n\t\treturn err as IError;\n\t}\n}\n"]}
|
|
@@ -10,7 +10,7 @@ export class ConflictError extends BaseError {
|
|
|
10
10
|
/**
|
|
11
11
|
* Create a new instance of ConflictError.
|
|
12
12
|
* @param source The source of the error.
|
|
13
|
-
* @param message The message as
|
|
13
|
+
* @param message The message as an i18n key.
|
|
14
14
|
* @param conflictId The id that has conflicts.
|
|
15
15
|
* @param conflicts The conflicts that occurred.
|
|
16
16
|
* @param properties Any additional information for the error.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conflictError.js","sourceRoot":"","sources":["../../../src/errors/conflictError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,SAAS;IAC3C;;OAEG;IACI,MAAM,CAAU,UAAU,mBAAmC;IAEpE;;;;;;;;OAQG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAmB,EACnB,SAAoB,EACpB,UAAsC,EACtC,KAAe;QAEf,KAAK,CACJ,aAAa,CAAC,UAAU,EACxB,MAAM,EACN,OAAO,EACP,EAAE,UAAU,EAAE,SAAS,EAAE,GAAG,UAAU,EAAE,EACxC,KAAK,CACL,CAAC;IACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors which are triggered by conflicting data.\n */\nexport class ConflictError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<ConflictError>();\n\n\t/**\n\t * Create a new instance of ConflictError.\n\t * @param source The source of the error.\n\t * @param message The message as
|
|
1
|
+
{"version":3,"file":"conflictError.js","sourceRoot":"","sources":["../../../src/errors/conflictError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,SAAS;IAC3C;;OAEG;IACI,MAAM,CAAU,UAAU,mBAAmC;IAEpE;;;;;;;;OAQG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAmB,EACnB,SAAoB,EACpB,UAAsC,EACtC,KAAe;QAEf,KAAK,CACJ,aAAa,CAAC,UAAU,EACxB,MAAM,EACN,OAAO,EACP,EAAE,UAAU,EAAE,SAAS,EAAE,GAAG,UAAU,EAAE,EACxC,KAAK,CACL,CAAC;IACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors which are triggered by conflicting data.\n */\nexport class ConflictError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<ConflictError>();\n\n\t/**\n\t * Create a new instance of ConflictError.\n\t * @param source The source of the error.\n\t * @param message The message as an i18n key.\n\t * @param conflictId The id that has conflicts.\n\t * @param conflicts The conflicts that occurred.\n\t * @param properties Any additional information for the error.\n\t * @param cause The cause or the error if we have wrapped another error.\n\t */\n\tconstructor(\n\t\tsource: string,\n\t\tmessage: string,\n\t\tconflictId?: string,\n\t\tconflicts?: string[],\n\t\tproperties?: { [id: string]: unknown },\n\t\tcause?: unknown\n\t) {\n\t\tsuper(\n\t\t\tConflictError.CLASS_NAME,\n\t\t\tsource,\n\t\t\tmessage,\n\t\t\t{ conflictId, conflicts, ...properties },\n\t\t\tcause\n\t\t);\n\t}\n}\n"]}
|
|
@@ -10,7 +10,7 @@ export class GeneralError extends BaseError {
|
|
|
10
10
|
/**
|
|
11
11
|
* Create a new instance of GeneralError.
|
|
12
12
|
* @param source The source of the error.
|
|
13
|
-
* @param message The message as
|
|
13
|
+
* @param message The message as an i18n key.
|
|
14
14
|
* @param properties Any additional information for the error.
|
|
15
15
|
* @param cause The cause of the error if we have wrapped another error.
|
|
16
16
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generalError.js","sourceRoot":"","sources":["../../../src/errors/generalError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,YAAa,SAAQ,SAAS;IAC1C;;OAEG;IACI,MAAM,CAAU,UAAU,kBAAkC;IAEnE;;;;;;OAMG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAsC,EACtC,KAAe;QAEf,KAAK,CAAC,YAAY,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;IACpE,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors.\n */\nexport class GeneralError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<GeneralError>();\n\n\t/**\n\t * Create a new instance of GeneralError.\n\t * @param source The source of the error.\n\t * @param message The message as
|
|
1
|
+
{"version":3,"file":"generalError.js","sourceRoot":"","sources":["../../../src/errors/generalError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,YAAa,SAAQ,SAAS;IAC1C;;OAEG;IACI,MAAM,CAAU,UAAU,kBAAkC;IAEnE;;;;;;OAMG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAsC,EACtC,KAAe;QAEf,KAAK,CAAC,YAAY,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;IACpE,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors.\n */\nexport class GeneralError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<GeneralError>();\n\n\t/**\n\t * Create a new instance of GeneralError.\n\t * @param source The source of the error.\n\t * @param message The message as an i18n key.\n\t * @param properties Any additional information for the error.\n\t * @param cause The cause of the error if we have wrapped another error.\n\t */\n\tconstructor(\n\t\tsource: string,\n\t\tmessage: string,\n\t\tproperties?: { [id: string]: unknown },\n\t\tcause?: unknown\n\t) {\n\t\tsuper(GeneralError.CLASS_NAME, source, message, properties, cause);\n\t}\n}\n"]}
|
|
@@ -11,7 +11,7 @@ export class GuardError extends BaseError {
|
|
|
11
11
|
/**
|
|
12
12
|
* Create a new instance of GuardError.
|
|
13
13
|
* @param source The source of the error.
|
|
14
|
-
* @param message The message as
|
|
14
|
+
* @param message The message as an i18n key.
|
|
15
15
|
* @param propertyName The property which triggered the guard error for the item.
|
|
16
16
|
* @param propertyValue The property value which triggered the guard error for the item.
|
|
17
17
|
* @param propertyOptions The property options which might be allowed.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guardError.js","sourceRoot":"","sources":["../../../src/errors/guardError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAEpC;;GAEG;AACH,MAAM,OAAO,UAAW,SAAQ,SAAS;IACxC;;OAEG;IACI,MAAM,CAAU,UAAU,gBAAgC;IAEjE;;;;;;;OAOG;IACH,YACC,MAAc,EACd,OAAe,EACf,YAAoB,EACpB,aAAsB,EACtB,eAAwB;QAExB,KAAK,CAAC,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE;YAC7C,QAAQ,EAAE,YAAY,IAAI,UAAU;YACpC,KAAK,EAAE,EAAE,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,aAAa;YAChE,OAAO,EAAE,eAAe;SACxB,CAAC,CAAC;IACJ,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\nimport { Is } from \"../utils/is.js\";\n\n/**\n * Class to handle errors which are triggered by data guards.\n */\nexport class GuardError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<GuardError>();\n\n\t/**\n\t * Create a new instance of GuardError.\n\t * @param source The source of the error.\n\t * @param message The message as
|
|
1
|
+
{"version":3,"file":"guardError.js","sourceRoot":"","sources":["../../../src/errors/guardError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAEpC;;GAEG;AACH,MAAM,OAAO,UAAW,SAAQ,SAAS;IACxC;;OAEG;IACI,MAAM,CAAU,UAAU,gBAAgC;IAEjE;;;;;;;OAOG;IACH,YACC,MAAc,EACd,OAAe,EACf,YAAoB,EACpB,aAAsB,EACtB,eAAwB;QAExB,KAAK,CAAC,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE;YAC7C,QAAQ,EAAE,YAAY,IAAI,UAAU;YACpC,KAAK,EAAE,EAAE,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,aAAa;YAChE,OAAO,EAAE,eAAe;SACxB,CAAC,CAAC;IACJ,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\nimport { Is } from \"../utils/is.js\";\n\n/**\n * Class to handle errors which are triggered by data guards.\n */\nexport class GuardError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<GuardError>();\n\n\t/**\n\t * Create a new instance of GuardError.\n\t * @param source The source of the error.\n\t * @param message The message as an i18n key.\n\t * @param propertyName The property which triggered the guard error for the item.\n\t * @param propertyValue The property value which triggered the guard error for the item.\n\t * @param propertyOptions The property options which might be allowed.\n\t */\n\tconstructor(\n\t\tsource: string,\n\t\tmessage: string,\n\t\tpropertyName: string,\n\t\tpropertyValue: unknown,\n\t\tpropertyOptions?: string\n\t) {\n\t\tsuper(GuardError.CLASS_NAME, source, message, {\n\t\t\tproperty: propertyName ?? \"property\",\n\t\t\tvalue: Is.undefined(propertyValue) ? \"undefined\" : propertyValue,\n\t\t\toptions: propertyOptions\n\t\t});\n\t}\n}\n"]}
|
|
@@ -10,7 +10,7 @@ export class NotFoundError extends BaseError {
|
|
|
10
10
|
/**
|
|
11
11
|
* Create a new instance of NotFoundError.
|
|
12
12
|
* @param source The source of the error.
|
|
13
|
-
* @param message The message as
|
|
13
|
+
* @param message The message as an i18n key.
|
|
14
14
|
* @param notFoundId The id for the item.
|
|
15
15
|
* @param properties Any additional information for the error.
|
|
16
16
|
* @param cause The cause of the error if we have wrapped another error.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"notFoundError.js","sourceRoot":"","sources":["../../../src/errors/notFoundError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,SAAS;IAC3C;;OAEG;IACI,MAAM,CAAU,UAAU,mBAAmC;IAEpE;;;;;;;OAOG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAmB,EACnB,UAAsC,EACtC,KAAe;QAEf,KAAK,CAAC,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,UAAU,EAAE,GAAG,UAAU,EAAE,EAAE,KAAK,CAAC,CAAC;IACxF,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors which are triggered by data not being found.\n */\nexport class NotFoundError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<NotFoundError>();\n\n\t/**\n\t * Create a new instance of NotFoundError.\n\t * @param source The source of the error.\n\t * @param message The message as
|
|
1
|
+
{"version":3,"file":"notFoundError.js","sourceRoot":"","sources":["../../../src/errors/notFoundError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,SAAS;IAC3C;;OAEG;IACI,MAAM,CAAU,UAAU,mBAAmC;IAEpE;;;;;;;OAOG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAmB,EACnB,UAAsC,EACtC,KAAe;QAEf,KAAK,CAAC,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,UAAU,EAAE,GAAG,UAAU,EAAE,EAAE,KAAK,CAAC,CAAC;IACxF,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors which are triggered by data not being found.\n */\nexport class NotFoundError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<NotFoundError>();\n\n\t/**\n\t * Create a new instance of NotFoundError.\n\t * @param source The source of the error.\n\t * @param message The message as an i18n key.\n\t * @param notFoundId The id for the item.\n\t * @param properties Any additional information for the error.\n\t * @param cause The cause of the error if we have wrapped another error.\n\t */\n\tconstructor(\n\t\tsource: string,\n\t\tmessage: string,\n\t\tnotFoundId?: string,\n\t\tproperties?: { [id: string]: unknown },\n\t\tcause?: unknown\n\t) {\n\t\tsuper(NotFoundError.CLASS_NAME, source, message, { notFoundId, ...properties }, cause);\n\t}\n}\n"]}
|
|
@@ -10,7 +10,7 @@ export class NotSupportedError extends BaseError {
|
|
|
10
10
|
/**
|
|
11
11
|
* Create a new instance of NotSupportedError.
|
|
12
12
|
* @param source The source of the error.
|
|
13
|
-
* @param message The message as
|
|
13
|
+
* @param message The message as an i18n key.
|
|
14
14
|
* @param properties Any additional information for the error.
|
|
15
15
|
* @param cause The cause of the error if we have wrapped another error.
|
|
16
16
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"notSupportedError.js","sourceRoot":"","sources":["../../../src/errors/notSupportedError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,iBAAkB,SAAQ,SAAS;IAC/C;;OAEG;IACI,MAAM,CAAU,UAAU,uBAAuC;IAExE;;;;;;OAMG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAsC,EACtC,KAAe;QAEf,KAAK,CAAC,iBAAiB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;IACzE,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors when a feature is unsupported.\n */\nexport class NotSupportedError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<NotSupportedError>();\n\n\t/**\n\t * Create a new instance of NotSupportedError.\n\t * @param source The source of the error.\n\t * @param message The message as
|
|
1
|
+
{"version":3,"file":"notSupportedError.js","sourceRoot":"","sources":["../../../src/errors/notSupportedError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,iBAAkB,SAAQ,SAAS;IAC/C;;OAEG;IACI,MAAM,CAAU,UAAU,uBAAuC;IAExE;;;;;;OAMG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAsC,EACtC,KAAe;QAEf,KAAK,CAAC,iBAAiB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;IACzE,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors when a feature is unsupported.\n */\nexport class NotSupportedError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<NotSupportedError>();\n\n\t/**\n\t * Create a new instance of NotSupportedError.\n\t * @param source The source of the error.\n\t * @param message The message as an i18n key.\n\t * @param properties Any additional information for the error.\n\t * @param cause The cause of the error if we have wrapped another error.\n\t */\n\tconstructor(\n\t\tsource: string,\n\t\tmessage: string,\n\t\tproperties?: { [id: string]: unknown },\n\t\tcause?: unknown\n\t) {\n\t\tsuper(NotSupportedError.CLASS_NAME, source, message, properties, cause);\n\t}\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseError } from "./baseError.js";
|
|
2
2
|
/**
|
|
3
|
-
* Class to handle errors which are triggered by access not being
|
|
3
|
+
* Class to handle errors which are triggered by access not being authorized.
|
|
4
4
|
*/
|
|
5
5
|
export class UnauthorizedError extends BaseError {
|
|
6
6
|
/**
|
|
@@ -10,7 +10,7 @@ export class UnauthorizedError extends BaseError {
|
|
|
10
10
|
/**
|
|
11
11
|
* Create a new instance of UnauthorizedError.
|
|
12
12
|
* @param source The source of the error.
|
|
13
|
-
* @param message The message as
|
|
13
|
+
* @param message The message as an i18n key.
|
|
14
14
|
* @param properties Any additional information for the error.
|
|
15
15
|
* @param cause The cause of the error if we have wrapped another error.
|
|
16
16
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unauthorizedError.js","sourceRoot":"","sources":["../../../src/errors/unauthorizedError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,iBAAkB,SAAQ,SAAS;IAC/C;;OAEG;IACI,MAAM,CAAU,UAAU,uBAAuC;IAExE;;;;;;OAMG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAsC,EACtC,KAAe;QAEf,KAAK,CAAC,iBAAiB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;IACzE,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors which are triggered by access not being
|
|
1
|
+
{"version":3,"file":"unauthorizedError.js","sourceRoot":"","sources":["../../../src/errors/unauthorizedError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,iBAAkB,SAAQ,SAAS;IAC/C;;OAEG;IACI,MAAM,CAAU,UAAU,uBAAuC;IAExE;;;;;;OAMG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAsC,EACtC,KAAe;QAEf,KAAK,CAAC,iBAAiB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;IACzE,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors which are triggered by access not being authorized.\n */\nexport class UnauthorizedError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<UnauthorizedError>();\n\n\t/**\n\t * Create a new instance of UnauthorizedError.\n\t * @param source The source of the error.\n\t * @param message The message as an i18n key.\n\t * @param properties Any additional information for the error.\n\t * @param cause The cause of the error if we have wrapped another error.\n\t */\n\tconstructor(\n\t\tsource: string,\n\t\tmessage: string,\n\t\tproperties?: { [id: string]: unknown },\n\t\tcause?: unknown\n\t) {\n\t\tsuper(UnauthorizedError.CLASS_NAME, source, message, properties, cause);\n\t}\n}\n"]}
|
|
@@ -10,7 +10,7 @@ export class UnprocessableError extends BaseError {
|
|
|
10
10
|
/**
|
|
11
11
|
* Create a new instance of UnprocessableError.
|
|
12
12
|
* @param source The source of the error.
|
|
13
|
-
* @param message The message as
|
|
13
|
+
* @param message The message as an i18n key.
|
|
14
14
|
* @param properties Any additional information for the error.
|
|
15
15
|
* @param cause The cause of the error if we have wrapped another error.
|
|
16
16
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unprocessableError.js","sourceRoot":"","sources":["../../../src/errors/unprocessableError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,kBAAmB,SAAQ,SAAS;IAChD;;OAEG;IACI,MAAM,CAAU,UAAU,wBAAwC;IAEzE;;;;;;OAMG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAsC,EACtC,KAAe;QAEf,KAAK,CAAC,kBAAkB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;IAC1E,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors when some data can not be processed.\n */\nexport class UnprocessableError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<UnprocessableError>();\n\n\t/**\n\t * Create a new instance of UnprocessableError.\n\t * @param source The source of the error.\n\t * @param message The message as
|
|
1
|
+
{"version":3,"file":"unprocessableError.js","sourceRoot":"","sources":["../../../src/errors/unprocessableError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,kBAAmB,SAAQ,SAAS;IAChD;;OAEG;IACI,MAAM,CAAU,UAAU,wBAAwC;IAEzE;;;;;;OAMG;IACH,YACC,MAAc,EACd,OAAe,EACf,UAAsC,EACtC,KAAe;QAEf,KAAK,CAAC,kBAAkB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;IAC1E,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { BaseError } from \"./baseError.js\";\n\n/**\n * Class to handle errors when some data can not be processed.\n */\nexport class UnprocessableError extends BaseError {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<UnprocessableError>();\n\n\t/**\n\t * Create a new instance of UnprocessableError.\n\t * @param source The source of the error.\n\t * @param message The message as an i18n key.\n\t * @param properties Any additional information for the error.\n\t * @param cause The cause of the error if we have wrapped another error.\n\t */\n\tconstructor(\n\t\tsource: string,\n\t\tmessage: string,\n\t\tproperties?: { [id: string]: unknown },\n\t\tcause?: unknown\n\t) {\n\t\tsuper(UnprocessableError.CLASS_NAME, source, message, properties, cause);\n\t}\n}\n"]}
|
|
@@ -99,6 +99,13 @@ export class Factory {
|
|
|
99
99
|
factories[typeName].clear();
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Get the type name of the factory.
|
|
104
|
+
* @returns The type name of the factory.
|
|
105
|
+
*/
|
|
106
|
+
typeName() {
|
|
107
|
+
return this._typeName;
|
|
108
|
+
}
|
|
102
109
|
/**
|
|
103
110
|
* Register a new generator.
|
|
104
111
|
* @param name The name of the generator.
|
|
@@ -173,6 +180,40 @@ export class Factory {
|
|
|
173
180
|
}
|
|
174
181
|
}
|
|
175
182
|
}
|
|
183
|
+
/**
|
|
184
|
+
* Create a new instance without caching it.
|
|
185
|
+
* @param name The name of the instance to generate.
|
|
186
|
+
* @param args The arguments to pass to the generator.
|
|
187
|
+
* @returns A new instance of the item.
|
|
188
|
+
* @throws GuardError if the parameters are invalid.
|
|
189
|
+
* @throws GeneralError if no item exists to create.
|
|
190
|
+
*/
|
|
191
|
+
create(name, args) {
|
|
192
|
+
Guards.stringValue(Factory.CLASS_NAME, "name", name);
|
|
193
|
+
const instance = this.createIfExists(name, args);
|
|
194
|
+
if (!instance) {
|
|
195
|
+
throw new GeneralError(Factory.CLASS_NAME, "noCreate", {
|
|
196
|
+
typeName: this._typeName,
|
|
197
|
+
name,
|
|
198
|
+
args: Is.undefined(args) ? "" : JSON.stringify(args)
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
return instance;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Create a new instance without caching it if it exists.
|
|
205
|
+
* @param name The name of the instance to generate.
|
|
206
|
+
* @param args The arguments to pass to the generator.
|
|
207
|
+
* @returns A new instance of the item if it exists.
|
|
208
|
+
* @throws GuardError if the parameters are invalid.
|
|
209
|
+
*/
|
|
210
|
+
createIfExists(name, args) {
|
|
211
|
+
Guards.stringValue(Factory.CLASS_NAME, "name", name);
|
|
212
|
+
const matchName = this._matcher(Object.keys(this._generators), name);
|
|
213
|
+
if (Is.stringValue(matchName) && this._generators[matchName]) {
|
|
214
|
+
return this._generators[matchName].generator(args);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
176
217
|
/**
|
|
177
218
|
* Remove all the instances and leave the generators intact.
|
|
178
219
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../../src/factories/factory.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEtD;;GAEG;AACH,MAAM,OAAO,OAAO;IACnB;;OAEG;IACI,MAAM,CAAU,UAAU,aAAsC;IAEvE;;;OAGG;IACc,SAAS,CAAS;IAEnC;;;OAGG;IACK,WAAW,CAKjB;IAEF;;;OAGG;IACK,UAAU,CAAwB;IAE1C;;;OAGG;IACK,aAAa,CAAS;IAE9B;;;OAGG;IACc,aAAa,CAAU;IAExC;;;OAGG;IACc,QAAQ,CAAwD;IAEjF;;;;;;OAMG;IACH,YACC,QAAgB,EAChB,eAAwB,KAAK,EAC7B,OAA+D;QAE/D,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;QACtB,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;QACrB,IAAI,CAAC,aAAa,GAAG,CAAC,CAAC;QACvB,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;QAClC,IA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EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;IAC3E,CAAC;IAED;;;;OAIG;IACK,cAAc,CAAC,IAAY;QAClC,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;IAED;;;;;;OAMG;IACK,cAAc,CAAC,KAAe,EAAE,IAAY;QACnD,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;IAClD,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { GeneralError } from \"../errors/generalError.js\";\nimport { Guards } from \"../utils/guards.js\";\nimport { Is } from \"../utils/is.js\";\nimport { SharedStore } from \"../utils/sharedStore.js\";\n\n/**\n * Factory for creating implementation of generic types.\n */\nexport class Factory<T> {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Factory<unknown>>();\n\n\t/**\n\t * Type name for the instances.\n\t * @internal\n\t */\n\tprivate readonly _typeName: string;\n\n\t/**\n\t * Store the generators.\n\t * @internal\n\t */\n\tprivate _generators: {\n\t\t[name: string]: {\n\t\t\tgenerator: () => T;\n\t\t\torder: number;\n\t\t};\n\t};\n\n\t/**\n\t * Store the created instances.\n\t * @internal\n\t */\n\tprivate _instances: { [name: string]: T };\n\n\t/**\n\t * Counter for the ordering.\n\t * @internal\n\t */\n\tprivate _orderCounter: number;\n\n\t/**\n\t * Automatically created an instance when registered.\n\t * @internal\n\t */\n\tprivate readonly _autoInstance: boolean;\n\n\t/**\n\t * Match the name of the instance.\n\t * @internal\n\t */\n\tprivate readonly _matcher: (names: string[], name: string) => string | undefined;\n\n\t/**\n\t * Create a new instance of Factory, private use createFactory.\n\t * @param typeName The type name for the instances.\n\t * @param autoInstance Automatically create an instance when registered.\n\t * @param matcher Match the name of the instance.\n\t * @internal\n\t */\n\tprivate constructor(\n\t\ttypeName: string,\n\t\tautoInstance: boolean = false,\n\t\tmatcher?: (names: string[], name: string) => string | undefined\n\t) {\n\t\tthis._typeName = typeName;\n\t\tthis._generators = {};\n\t\tthis._instances = {};\n\t\tthis._orderCounter = 0;\n\t\tthis._autoInstance = autoInstance;\n\t\tthis._matcher = matcher ?? this.defaultMatcher.bind(this);\n\t}\n\n\t/**\n\t * Create a new factory, which is shared throughout all library instances.\n\t * @param typeName The type name for the instances.\n\t * @param autoInstance Automatically create an instance when registered.\n\t * @param matcher Match the name of the instance.\n\t * @returns The factory instance.\n\t */\n\tpublic static createFactory<U>(\n\t\ttypeName: string,\n\t\tautoInstance: boolean = false,\n\t\tmatcher?: (names: string[], name: string) => string | undefined\n\t): Factory<U> {\n\t\tconst factories = Factory.getFactories();\n\n\t\tif (Is.undefined(factories[typeName])) {\n\t\t\tfactories[typeName] = new Factory<U>(typeName, autoInstance, matcher);\n\t\t}\n\t\treturn factories[typeName] as Factory<U>;\n\t}\n\n\t/**\n\t * Get all the factories.\n\t * @returns All the factories.\n\t */\n\tpublic static getFactories(): { [typeName: string]: Factory<unknown> } {\n\t\tlet factories = SharedStore.get<{\n\t\t\t[typeName: string]: Factory<unknown>;\n\t\t}>(\"factories\");\n\n\t\tif (Is.undefined(factories)) {\n\t\t\tfactories = {};\n\t\t\tSharedStore.set(\"factories\", factories);\n\t\t}\n\n\t\treturn factories;\n\t}\n\n\t/**\n\t * Reset all the factories, which removes any created instances, but not the registrations.\n\t */\n\tpublic static resetFactories(): void {\n\t\tconst factories = Factory.getFactories();\n\n\t\tfor (const typeName in factories) {\n\t\t\tfactories[typeName].reset();\n\t\t}\n\t}\n\n\t/**\n\t * Clear all the factories, which removes anything registered with the factories.\n\t */\n\tpublic static clearFactories(): void {\n\t\tconst factories = Factory.getFactories();\n\n\t\tfor (const typeName in factories) {\n\t\t\tfactories[typeName].clear();\n\t\t}\n\t}\n\n\t/**\n\t * Register a new generator.\n\t * @param name The name of the generator.\n\t * @param generator The function to create an instance.\n\t */\n\tpublic register<U extends T>(name: string, generator: () => U): void {\n\t\tGuards.stringValue(Factory.CLASS_NAME, nameof(name), name);\n\t\tGuards.function(Factory.CLASS_NAME, nameof(generator), generator);\n\t\tthis._generators[name] = {\n\t\t\tgenerator,\n\t\t\torder: this._orderCounter++\n\t\t};\n\t\t// Remove any existing instance\n\t\tthis.removeInstance(name);\n\t\tif (this._autoInstance) {\n\t\t\tthis._instances[name] = generator();\n\t\t}\n\t}\n\n\t/**\n\t * Unregister a generator.\n\t * @param name The name of the generator to unregister.\n\t * @throws GuardError if the parameters are invalid.\n\t * @throws GeneralError if no generator exists.\n\t */\n\tpublic unregister(name: string): void {\n\t\tGuards.stringValue(Factory.CLASS_NAME, nameof(name), name);\n\t\tif (!this._generators[name]) {\n\t\t\tthrow new GeneralError(Factory.CLASS_NAME, \"noUnregister\", {\n\t\t\t\ttypeName: this._typeName,\n\t\t\t\tname\n\t\t\t});\n\t\t}\n\t\tdelete this._generators[name];\n\t\t// Remove any existing instance\n\t\tthis.removeInstance(name);\n\t}\n\n\t/**\n\t * Get a generator instance.\n\t * @param name The name of the instance to generate.\n\t * @returns An instance of the item.\n\t * @throws GuardError if the parameters are invalid.\n\t * @throws GeneralError if no item exists to get.\n\t */\n\tpublic get<U extends T>(name: string): U {\n\t\tGuards.stringValue(Factory.CLASS_NAME, nameof(name), name);\n\t\tconst instance = this.getIfExists(name);\n\t\tif (!instance) {\n\t\t\tthrow new GeneralError(Factory.CLASS_NAME, \"noGet\", {\n\t\t\t\ttypeName: this._typeName,\n\t\t\t\tname\n\t\t\t});\n\t\t}\n\t\treturn instance as U;\n\t}\n\n\t/**\n\t * Get a generator instance with no exceptions.\n\t * @param name The name of the instance to generate.\n\t * @returns An instance of the item or undefined if it does not exist.\n\t */\n\tpublic getIfExists<U extends T>(name?: string): U | undefined {\n\t\tif (Is.empty(name)) {\n\t\t\treturn;\n\t\t}\n\t\tGuards.stringValue(Factory.CLASS_NAME, nameof(name), name);\n\n\t\tconst matchName = this._matcher(Object.keys(this._generators), name);\n\n\t\tif (Is.stringValue(matchName) && this._generators[matchName]) {\n\t\t\tif (!this._instances[matchName]) {\n\t\t\t\tthis._instances[matchName] = this._generators[matchName].generator();\n\t\t\t}\n\t\t\tif (this._instances[matchName]) {\n\t\t\t\treturn this._instances[matchName] as U;\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Remove all the instances and leave the generators intact.\n\t */\n\tpublic reset(): void {\n\t\tfor (const name in this._generators) {\n\t\t\tthis.removeInstance(name);\n\t\t}\n\t\tthis._instances = {};\n\t}\n\n\t/**\n\t * Remove all the instances and the generators.\n\t */\n\tpublic clear(): void {\n\t\tthis._instances = {};\n\t\tthis._generators = {};\n\t\tthis._orderCounter = 0;\n\t}\n\n\t/**\n\t * Get all the instances as a map.\n\t * @returns The instances as a map.\n\t */\n\tpublic instancesMap(): { [name: string]: T } {\n\t\treturn this._instances;\n\t}\n\n\t/**\n\t * Get all the instances as a list in the order they were registered.\n\t * @returns The instances as a list in the order they were registered.\n\t */\n\tpublic instancesList(): T[] {\n\t\tconst orderedInstances: { instance: T; order: number }[] = [];\n\t\tfor (const instanceName in this._instances) {\n\t\t\torderedInstances.push({\n\t\t\t\tinstance: this._instances[instanceName],\n\t\t\t\torder: this._generators[instanceName].order\n\t\t\t});\n\t\t}\n\t\treturn orderedInstances.sort((a, b) => a.order - b.order).map(o => o.instance);\n\t}\n\n\t/**\n\t * Get all the generator names in the order they were registered.\n\t * @returns The ordered generator names.\n\t */\n\tpublic names(): string[] {\n\t\tconst orderedNames: { name: string; order: number }[] = [];\n\t\tfor (const generator in this._generators) {\n\t\t\torderedNames.push({\n\t\t\t\tname: generator,\n\t\t\t\torder: this._generators[generator].order\n\t\t\t});\n\t\t}\n\t\treturn orderedNames.sort((a, b) => a.order - b.order).map(o => o.name);\n\t}\n\n\t/**\n\t * Does the factory contain the name.\n\t * @param name The name of the instance to find.\n\t * @returns True if the factory has a matching name.\n\t */\n\tpublic hasName(name: string): boolean {\n\t\tGuards.stringValue(Factory.CLASS_NAME, nameof(name), name);\n\t\treturn Is.stringValue(this._matcher(Object.keys(this._generators), name));\n\t}\n\n\t/**\n\t * Remove any instances of the given name.\n\t * @param name The name of the instances to remove.\n\t * @internal\n\t */\n\tprivate removeInstance(name: string): void {\n\t\tdelete this._instances[name];\n\t}\n\n\t/**\n\t * Match the requested name to the generator name.\n\t * @param names The list of names for all the generators.\n\t * @param name The name to match.\n\t * @returns The matched name or undefined if no match.\n\t * @internal\n\t */\n\tprivate defaultMatcher(names: string[], name: string): string | undefined {\n\t\treturn this._generators[name] ? name : undefined;\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../../src/factories/factory.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEtD;;GAEG;AACH,MAAM,OAAO,OAAO;IACnB;;OAEG;IACI,MAAM,CAAU,UAAU,aAAsC;IAEvE;;;OAGG;IACc,SAAS,CAAS;IAEnC;;;OAGG;IACK,WAAW,CAKjB;IAEF;;;OAGG;IACK,UAAU,CAAwB;IAE1C;;;OAGG;IACK,aAAa,CAAS;IAE9B;;;OAGG;IACc,aAAa,CAAU;IAExC;;;OAGG;IACc,QAAQ,CAAwD;IAEjF;;;;;;OAMG;IACH,YACC,QAAgB,EAChB,eAAwB,KAAK,EAC7B,OAA+D;QAE/D,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;QACtB,IAAI,CA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AAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;IAC3E,CAAC;IAED;;;;OAIG;IACK,cAAc,CAAC,IAAY;QAClC,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;IAED;;;;;;OAMG;IACK,cAAc,CAAC,KAAe,EAAE,IAAY;QACnD,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;IAClD,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { GeneralError } from \"../errors/generalError.js\";\nimport { Guards } from \"../utils/guards.js\";\nimport { Is } from \"../utils/is.js\";\nimport { SharedStore } from \"../utils/sharedStore.js\";\n\n/**\n * Factory for creating implementation of generic types.\n */\nexport class Factory<T> {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Factory<unknown>>();\n\n\t/**\n\t * Type name for the instances.\n\t * @internal\n\t */\n\tprivate readonly _typeName: string;\n\n\t/**\n\t * Store the generators.\n\t * @internal\n\t */\n\tprivate _generators: {\n\t\t[name: string]: {\n\t\t\tgenerator: (args?: unknown) => T;\n\t\t\torder: number;\n\t\t};\n\t};\n\n\t/**\n\t * Store the created instances.\n\t * @internal\n\t */\n\tprivate _instances: { [name: string]: T };\n\n\t/**\n\t * Counter for the ordering.\n\t * @internal\n\t */\n\tprivate _orderCounter: number;\n\n\t/**\n\t * Automatically created an instance when registered.\n\t * @internal\n\t */\n\tprivate readonly _autoInstance: boolean;\n\n\t/**\n\t * Match the name of the instance.\n\t * @internal\n\t */\n\tprivate readonly _matcher: (names: string[], name: string) => string | undefined;\n\n\t/**\n\t * Create a new instance of Factory, private use createFactory.\n\t * @param typeName The type name for the instances.\n\t * @param autoInstance Automatically create an instance when registered.\n\t * @param matcher Match the name of the instance.\n\t * @internal\n\t */\n\tprivate constructor(\n\t\ttypeName: string,\n\t\tautoInstance: boolean = false,\n\t\tmatcher?: (names: string[], name: string) => string | undefined\n\t) {\n\t\tthis._typeName = typeName;\n\t\tthis._generators = {};\n\t\tthis._instances = {};\n\t\tthis._orderCounter = 0;\n\t\tthis._autoInstance = autoInstance;\n\t\tthis._matcher = matcher ?? this.defaultMatcher.bind(this);\n\t}\n\n\t/**\n\t * Create a new factory, which is shared throughout all library instances.\n\t * @param typeName The type name for the instances.\n\t * @param autoInstance Automatically create an instance when registered.\n\t * @param matcher Match the name of the instance.\n\t * @returns The factory instance.\n\t */\n\tpublic static createFactory<U>(\n\t\ttypeName: string,\n\t\tautoInstance: boolean = false,\n\t\tmatcher?: (names: string[], name: string) => string | undefined\n\t): Factory<U> {\n\t\tconst factories = Factory.getFactories();\n\n\t\tif (Is.undefined(factories[typeName])) {\n\t\t\tfactories[typeName] = new Factory<U>(typeName, autoInstance, matcher);\n\t\t}\n\t\treturn factories[typeName] as Factory<U>;\n\t}\n\n\t/**\n\t * Get all the factories.\n\t * @returns All the factories.\n\t */\n\tpublic static getFactories(): { [typeName: string]: Factory<unknown> } {\n\t\tlet factories = SharedStore.get<{\n\t\t\t[typeName: string]: Factory<unknown>;\n\t\t}>(\"factories\");\n\n\t\tif (Is.undefined(factories)) {\n\t\t\tfactories = {};\n\t\t\tSharedStore.set(\"factories\", factories);\n\t\t}\n\n\t\treturn factories;\n\t}\n\n\t/**\n\t * Reset all the factories, which removes any created instances, but not the registrations.\n\t */\n\tpublic static resetFactories(): void {\n\t\tconst factories = Factory.getFactories();\n\n\t\tfor (const typeName in factories) {\n\t\t\tfactories[typeName].reset();\n\t\t}\n\t}\n\n\t/**\n\t * Clear all the factories, which removes anything registered with the factories.\n\t */\n\tpublic static clearFactories(): void {\n\t\tconst factories = Factory.getFactories();\n\n\t\tfor (const typeName in factories) {\n\t\t\tfactories[typeName].clear();\n\t\t}\n\t}\n\n\t/**\n\t * Get the type name of the factory.\n\t * @returns The type name of the factory.\n\t */\n\tpublic typeName(): string {\n\t\treturn this._typeName;\n\t}\n\n\t/**\n\t * Register a new generator.\n\t * @param name The name of the generator.\n\t * @param generator The function to create an instance.\n\t */\n\tpublic register<U extends T>(name: string, generator: (args?: unknown) => U): void {\n\t\tGuards.stringValue(Factory.CLASS_NAME, nameof(name), name);\n\t\tGuards.function(Factory.CLASS_NAME, nameof(generator), generator);\n\t\tthis._generators[name] = {\n\t\t\tgenerator,\n\t\t\torder: this._orderCounter++\n\t\t};\n\n\t\t// Remove any existing instance\n\t\tthis.removeInstance(name);\n\t\tif (this._autoInstance) {\n\t\t\tthis._instances[name] = generator();\n\t\t}\n\t}\n\n\t/**\n\t * Unregister a generator.\n\t * @param name The name of the generator to unregister.\n\t * @throws GuardError if the parameters are invalid.\n\t * @throws GeneralError if no generator exists.\n\t */\n\tpublic unregister(name: string): void {\n\t\tGuards.stringValue(Factory.CLASS_NAME, nameof(name), name);\n\t\tif (!this._generators[name]) {\n\t\t\tthrow new GeneralError(Factory.CLASS_NAME, \"noUnregister\", {\n\t\t\t\ttypeName: this._typeName,\n\t\t\t\tname\n\t\t\t});\n\t\t}\n\t\tdelete this._generators[name];\n\t\t// Remove any existing instance\n\t\tthis.removeInstance(name);\n\t}\n\n\t/**\n\t * Get a generator instance.\n\t * @param name The name of the instance to generate.\n\t * @returns An instance of the item.\n\t * @throws GuardError if the parameters are invalid.\n\t * @throws GeneralError if no item exists to get.\n\t */\n\tpublic get<U extends T>(name: string): U {\n\t\tGuards.stringValue(Factory.CLASS_NAME, nameof(name), name);\n\t\tconst instance = this.getIfExists(name);\n\t\tif (!instance) {\n\t\t\tthrow new GeneralError(Factory.CLASS_NAME, \"noGet\", {\n\t\t\t\ttypeName: this._typeName,\n\t\t\t\tname\n\t\t\t});\n\t\t}\n\t\treturn instance as U;\n\t}\n\n\t/**\n\t * Get a generator instance with no exceptions.\n\t * @param name The name of the instance to generate.\n\t * @returns An instance of the item or undefined if it does not exist.\n\t */\n\tpublic getIfExists<U extends T>(name?: string): U | undefined {\n\t\tif (Is.empty(name)) {\n\t\t\treturn;\n\t\t}\n\t\tGuards.stringValue(Factory.CLASS_NAME, nameof(name), name);\n\n\t\tconst matchName = this._matcher(Object.keys(this._generators), name);\n\n\t\tif (Is.stringValue(matchName) && this._generators[matchName]) {\n\t\t\tif (!this._instances[matchName]) {\n\t\t\t\tthis._instances[matchName] = this._generators[matchName].generator();\n\t\t\t}\n\t\t\tif (this._instances[matchName]) {\n\t\t\t\treturn this._instances[matchName] as U;\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Create a new instance without caching it.\n\t * @param name The name of the instance to generate.\n\t * @param args The arguments to pass to the generator.\n\t * @returns A new instance of the item.\n\t * @throws GuardError if the parameters are invalid.\n\t * @throws GeneralError if no item exists to create.\n\t */\n\tpublic create<U extends T>(name: string, args?: unknown): U {\n\t\tGuards.stringValue(Factory.CLASS_NAME, nameof(name), name);\n\t\tconst instance = this.createIfExists(name, args);\n\t\tif (!instance) {\n\t\t\tthrow new GeneralError(Factory.CLASS_NAME, \"noCreate\", {\n\t\t\t\ttypeName: this._typeName,\n\t\t\t\tname,\n\t\t\t\targs: Is.undefined(args) ? \"\" : JSON.stringify(args)\n\t\t\t});\n\t\t}\n\t\treturn instance as U;\n\t}\n\n\t/**\n\t * Create a new instance without caching it if it exists.\n\t * @param name The name of the instance to generate.\n\t * @param args The arguments to pass to the generator.\n\t * @returns A new instance of the item if it exists.\n\t * @throws GuardError if the parameters are invalid.\n\t */\n\tpublic createIfExists<U extends T>(name: string, args?: unknown): U | undefined {\n\t\tGuards.stringValue(Factory.CLASS_NAME, nameof(name), name);\n\t\tconst matchName = this._matcher(Object.keys(this._generators), name);\n\n\t\tif (Is.stringValue(matchName) && this._generators[matchName]) {\n\t\t\treturn this._generators[matchName].generator(args) as U;\n\t\t}\n\t}\n\n\t/**\n\t * Remove all the instances and leave the generators intact.\n\t */\n\tpublic reset(): void {\n\t\tfor (const name in this._generators) {\n\t\t\tthis.removeInstance(name);\n\t\t}\n\t\tthis._instances = {};\n\t}\n\n\t/**\n\t * Remove all the instances and the generators.\n\t */\n\tpublic clear(): void {\n\t\tthis._instances = {};\n\t\tthis._generators = {};\n\t\tthis._orderCounter = 0;\n\t}\n\n\t/**\n\t * Get all the instances as a map.\n\t * @returns The instances as a map.\n\t */\n\tpublic instancesMap(): { [name: string]: T } {\n\t\treturn this._instances;\n\t}\n\n\t/**\n\t * Get all the instances as a list in the order they were registered.\n\t * @returns The instances as a list in the order they were registered.\n\t */\n\tpublic instancesList(): T[] {\n\t\tconst orderedInstances: { instance: T; order: number }[] = [];\n\t\tfor (const instanceName in this._instances) {\n\t\t\torderedInstances.push({\n\t\t\t\tinstance: this._instances[instanceName],\n\t\t\t\torder: this._generators[instanceName].order\n\t\t\t});\n\t\t}\n\t\treturn orderedInstances.sort((a, b) => a.order - b.order).map(o => o.instance);\n\t}\n\n\t/**\n\t * Get all the generator names in the order they were registered.\n\t * @returns The ordered generator names.\n\t */\n\tpublic names(): string[] {\n\t\tconst orderedNames: { name: string; order: number }[] = [];\n\t\tfor (const generator in this._generators) {\n\t\t\torderedNames.push({\n\t\t\t\tname: generator,\n\t\t\t\torder: this._generators[generator].order\n\t\t\t});\n\t\t}\n\t\treturn orderedNames.sort((a, b) => a.order - b.order).map(o => o.name);\n\t}\n\n\t/**\n\t * Does the factory contain the name.\n\t * @param name The name of the instance to find.\n\t * @returns True if the factory has a matching name.\n\t */\n\tpublic hasName(name: string): boolean {\n\t\tGuards.stringValue(Factory.CLASS_NAME, nameof(name), name);\n\t\treturn Is.stringValue(this._matcher(Object.keys(this._generators), name));\n\t}\n\n\t/**\n\t * Remove any instances of the given name.\n\t * @param name The name of the instances to remove.\n\t * @internal\n\t */\n\tprivate removeInstance(name: string): void {\n\t\tdelete this._instances[name];\n\t}\n\n\t/**\n\t * Match the requested name to the generator name.\n\t * @param names The list of names for all the generators.\n\t * @param name The name to match.\n\t * @returns The matched name or undefined if no match.\n\t * @internal\n\t */\n\tprivate defaultMatcher(names: string[], name: string): string | undefined {\n\t\treturn this._generators[name] ? name : undefined;\n\t}\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"arrayHelper.js","sourceRoot":"","sources":["../../../src/helpers/arrayHelper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"arrayHelper.js","sourceRoot":"","sources":["../../../src/helpers/arrayHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAEpC;;GAEG;AACH,MAAM,OAAO,WAAW;IACvB;;;;;OAKG;IACI,MAAM,CAAC,OAAO,CAAC,IAAa,EAAE,IAAa;QACjD,IAAI,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;YACtC,OAAO,IAAI,CAAC;QACb,CAAC;QACD,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;YAC3F,OAAO,KAAK,CAAC;QACd,CAAC;QACD,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,EAAE,CAAC;YACjC,OAAO,KAAK,CAAC;QACd,CAAC;QACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACtC,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;gBACzB,OAAO,KAAK,CAAC;YACd,CAAC;QACF,CAAC;QACD,OAAO,IAAI,CAAC;IACb,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,iBAAiB,CAC9B,KAA0B;QAE1B,IAAI,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YACrB,OAAO,SAAkD,CAAC;QAC3D,CAAC;QACD,IAAI,EAAE,CAAC,KAAK,CAAI,KAAK,CAAC,EAAE,CAAC;YACxB,OAAO,KAA8C,CAAC;QACvD,CAAC;QACD,OAAO,CAAC,KAAK,CAA0C,CAAC;IACzD,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is } from \"../utils/is.js\";\n\n/**\n * Class to help with arrays.\n */\nexport class ArrayHelper {\n\t/**\n\t * Do the two arrays match.\n\t * @param arr1 The first array.\n\t * @param arr2 The second array.\n\t * @returns True if both arrays are empty of have the same values.\n\t */\n\tpublic static matches(arr1: unknown, arr2: unknown): boolean {\n\t\tif (Is.empty(arr1) && Is.empty(arr2)) {\n\t\t\treturn true;\n\t\t}\n\t\tif (!((Is.array(arr1) && Is.array(arr2)) || (Is.typedArray(arr1) && Is.typedArray(arr2)))) {\n\t\t\treturn false;\n\t\t}\n\t\tif (arr1.length !== arr2.length) {\n\t\t\treturn false;\n\t\t}\n\t\tfor (let i = 0; i < arr1.length; i++) {\n\t\t\tif (arr1[i] !== arr2[i]) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\treturn true;\n\t}\n\n\t/**\n\t * Convert an object or array to an array.\n\t * @param value The object or array to convert.\n\t * @returns The array.\n\t */\n\tpublic static fromObjectOrArray<T = unknown>(\n\t\tvalue: T | T[] | undefined\n\t): T extends undefined ? undefined : T[] {\n\t\tif (Is.empty(value)) {\n\t\t\treturn undefined as T extends undefined ? undefined : T[];\n\t\t}\n\t\tif (Is.array<T>(value)) {\n\t\t\treturn value as T extends undefined ? undefined : T[];\n\t\t}\n\t\treturn [value] as T extends undefined ? undefined : T[];\n\t}\n}\n"]}
|