@twin.org/core 0.0.2-next.9 → 0.0.3-next.2

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.
Files changed (187) hide show
  1. package/dist/es/encoding/base32.js +75 -0
  2. package/dist/es/encoding/base32.js.map +1 -0
  3. package/dist/es/encoding/base58.js +124 -0
  4. package/dist/es/encoding/base58.js.map +1 -0
  5. package/dist/es/encoding/base64.js +231 -0
  6. package/dist/es/encoding/base64.js.map +1 -0
  7. package/dist/es/encoding/base64Url.js +42 -0
  8. package/dist/es/encoding/base64Url.js.map +1 -0
  9. package/dist/es/errors/alreadyExistsError.js +22 -0
  10. package/dist/es/errors/alreadyExistsError.js.map +1 -0
  11. package/dist/es/errors/baseError.js +257 -0
  12. package/dist/es/errors/baseError.js.map +1 -0
  13. package/dist/es/errors/conflictError.js +23 -0
  14. package/dist/es/errors/conflictError.js.map +1 -0
  15. package/dist/es/errors/generalError.js +21 -0
  16. package/dist/es/errors/generalError.js.map +1 -0
  17. package/dist/es/errors/guardError.js +27 -0
  18. package/dist/es/errors/guardError.js.map +1 -0
  19. package/dist/es/errors/notFoundError.js +22 -0
  20. package/dist/es/errors/notFoundError.js.map +1 -0
  21. package/dist/es/errors/notImplementedError.js +24 -0
  22. package/dist/es/errors/notImplementedError.js.map +1 -0
  23. package/dist/es/errors/notSupportedError.js +21 -0
  24. package/dist/es/errors/notSupportedError.js.map +1 -0
  25. package/dist/es/errors/unauthorizedError.js +21 -0
  26. package/dist/es/errors/unauthorizedError.js.map +1 -0
  27. package/dist/es/errors/unprocessableError.js +21 -0
  28. package/dist/es/errors/unprocessableError.js.map +1 -0
  29. package/dist/es/errors/validationError.js +23 -0
  30. package/dist/es/errors/validationError.js.map +1 -0
  31. package/dist/es/factories/componentFactory.js +9 -0
  32. package/dist/es/factories/componentFactory.js.map +1 -0
  33. package/dist/es/factories/factory.js +256 -0
  34. package/dist/es/factories/factory.js.map +1 -0
  35. package/dist/es/helpers/arrayHelper.js +44 -0
  36. package/dist/es/helpers/arrayHelper.js.map +1 -0
  37. package/dist/es/helpers/envHelper.js +39 -0
  38. package/dist/es/helpers/envHelper.js.map +1 -0
  39. package/dist/es/helpers/errorHelper.js +100 -0
  40. package/dist/es/helpers/errorHelper.js.map +1 -0
  41. package/dist/es/helpers/filenameHelper.js +32 -0
  42. package/dist/es/helpers/filenameHelper.js.map +1 -0
  43. package/dist/es/helpers/hexHelper.js +45 -0
  44. package/dist/es/helpers/hexHelper.js.map +1 -0
  45. package/dist/es/helpers/jsonHelper.js +163 -0
  46. package/dist/es/helpers/jsonHelper.js.map +1 -0
  47. package/dist/es/helpers/numberHelper.js +30 -0
  48. package/dist/es/helpers/numberHelper.js.map +1 -0
  49. package/dist/es/helpers/objectHelper.js +333 -0
  50. package/dist/es/helpers/objectHelper.js.map +1 -0
  51. package/dist/es/helpers/randomHelper.js +18 -0
  52. package/dist/es/helpers/randomHelper.js.map +1 -0
  53. package/dist/es/helpers/stringHelper.js +241 -0
  54. package/dist/es/helpers/stringHelper.js.map +1 -0
  55. package/dist/es/helpers/uint8ArrayHelper.js +26 -0
  56. package/dist/es/helpers/uint8ArrayHelper.js.map +1 -0
  57. package/dist/es/index.js +57 -0
  58. package/dist/es/index.js.map +1 -0
  59. package/dist/es/models/IComponent.js +4 -0
  60. package/dist/es/models/IComponent.js.map +1 -0
  61. package/dist/es/models/IError.js +4 -0
  62. package/dist/es/models/IError.js.map +1 -0
  63. package/dist/es/models/II18nShared.js +2 -0
  64. package/dist/es/models/II18nShared.js.map +1 -0
  65. package/dist/es/models/IKeyValue.js +2 -0
  66. package/dist/es/models/IKeyValue.js.map +1 -0
  67. package/dist/es/models/ILabelledValue.js +2 -0
  68. package/dist/es/models/ILabelledValue.js.map +1 -0
  69. package/dist/es/models/ILocale.js +2 -0
  70. package/dist/es/models/ILocale.js.map +1 -0
  71. package/dist/es/models/ILocaleDictionary.js +4 -0
  72. package/dist/es/models/ILocaleDictionary.js.map +1 -0
  73. package/dist/es/models/ILocalesIndex.js +2 -0
  74. package/dist/es/models/ILocalesIndex.js.map +1 -0
  75. package/dist/es/models/IPatchOperation.js +4 -0
  76. package/dist/es/models/IPatchOperation.js.map +1 -0
  77. package/dist/es/models/IUrlParts.js +4 -0
  78. package/dist/es/models/IUrlParts.js.map +1 -0
  79. package/dist/es/models/IValidationFailure.js +2 -0
  80. package/dist/es/models/IValidationFailure.js.map +1 -0
  81. package/dist/es/models/coerceType.js +49 -0
  82. package/dist/es/models/coerceType.js.map +1 -0
  83. package/dist/es/models/compressionType.js +17 -0
  84. package/dist/es/models/compressionType.js.map +1 -0
  85. package/dist/es/models/objectOrArray.js +2 -0
  86. package/dist/es/models/objectOrArray.js.map +1 -0
  87. package/dist/es/types/bitString.js +98 -0
  88. package/dist/es/types/bitString.js.map +1 -0
  89. package/dist/es/types/url.js +144 -0
  90. package/dist/es/types/url.js.map +1 -0
  91. package/dist/es/types/urn.js +212 -0
  92. package/dist/es/types/urn.js.map +1 -0
  93. package/dist/es/utils/asyncCache.js +179 -0
  94. package/dist/es/utils/asyncCache.js.map +1 -0
  95. package/dist/es/utils/coerce.js +269 -0
  96. package/dist/es/utils/coerce.js.map +1 -0
  97. package/dist/es/utils/compression.js +49 -0
  98. package/dist/es/utils/compression.js.map +1 -0
  99. package/dist/es/utils/converter.js +256 -0
  100. package/dist/es/utils/converter.js.map +1 -0
  101. package/dist/es/utils/guards.js +369 -0
  102. package/dist/es/utils/guards.js.map +1 -0
  103. package/dist/es/utils/i18n.js +243 -0
  104. package/dist/es/utils/i18n.js.map +1 -0
  105. package/dist/es/utils/is.js +351 -0
  106. package/dist/es/utils/is.js.map +1 -0
  107. package/dist/es/utils/sharedStore.js +48 -0
  108. package/dist/es/utils/sharedStore.js.map +1 -0
  109. package/dist/es/utils/validation.js +774 -0
  110. package/dist/es/utils/validation.js.map +1 -0
  111. package/dist/types/encoding/base32.d.ts +4 -0
  112. package/dist/types/encoding/base58.d.ts +4 -0
  113. package/dist/types/encoding/base64.d.ts +4 -0
  114. package/dist/types/encoding/base64Url.d.ts +4 -0
  115. package/dist/types/errors/alreadyExistsError.d.ts +5 -2
  116. package/dist/types/errors/baseError.d.ts +1 -1
  117. package/dist/types/errors/conflictError.d.ts +5 -2
  118. package/dist/types/errors/generalError.d.ts +1 -1
  119. package/dist/types/errors/guardError.d.ts +1 -1
  120. package/dist/types/errors/notFoundError.d.ts +5 -2
  121. package/dist/types/errors/notImplementedError.d.ts +6 -2
  122. package/dist/types/errors/notSupportedError.d.ts +5 -2
  123. package/dist/types/errors/unauthorizedError.d.ts +5 -2
  124. package/dist/types/errors/unprocessableError.d.ts +1 -1
  125. package/dist/types/errors/validationError.d.ts +2 -2
  126. package/dist/types/factories/componentFactory.d.ts +2 -2
  127. package/dist/types/factories/factory.d.ts +4 -0
  128. package/dist/types/helpers/arrayHelper.d.ts +1 -1
  129. package/dist/types/helpers/errorHelper.d.ts +1 -1
  130. package/dist/types/helpers/jsonHelper.d.ts +5 -1
  131. package/dist/types/helpers/numberHelper.d.ts +17 -0
  132. package/dist/types/helpers/objectHelper.d.ts +14 -0
  133. package/dist/types/helpers/stringHelper.d.ts +6 -6
  134. package/dist/types/index.d.ts +54 -53
  135. package/dist/types/models/IComponent.d.ts +6 -7
  136. package/dist/types/models/ILocalesIndex.d.ts +1 -1
  137. package/dist/types/models/IValidationFailure.d.ts +0 -4
  138. package/dist/types/types/bitString.d.ts +4 -0
  139. package/dist/types/types/url.d.ts +8 -3
  140. package/dist/types/types/urn.d.ts +7 -2
  141. package/dist/types/utils/coerce.d.ts +1 -1
  142. package/dist/types/utils/compression.d.ts +5 -1
  143. package/dist/types/utils/guards.d.ts +2 -3
  144. package/dist/types/utils/i18n.d.ts +16 -1
  145. package/dist/types/utils/is.d.ts +1 -1
  146. package/dist/types/utils/validation.d.ts +1 -1
  147. package/docs/changelog.md +283 -0
  148. package/docs/reference/classes/AlreadyExistsError.md +8 -4
  149. package/docs/reference/classes/AsyncCache.md +5 -5
  150. package/docs/reference/classes/Base32.md +8 -0
  151. package/docs/reference/classes/Base58.md +8 -0
  152. package/docs/reference/classes/Base64.md +8 -0
  153. package/docs/reference/classes/Base64Url.md +8 -0
  154. package/docs/reference/classes/BaseError.md +3 -3
  155. package/docs/reference/classes/BitString.md +8 -0
  156. package/docs/reference/classes/Coerce.md +20 -20
  157. package/docs/reference/classes/Compression.md +8 -0
  158. package/docs/reference/classes/ConflictError.md +8 -4
  159. package/docs/reference/classes/ErrorHelper.md +2 -2
  160. package/docs/reference/classes/Factory.md +11 -3
  161. package/docs/reference/classes/GeneralError.md +3 -3
  162. package/docs/reference/classes/GuardError.md +3 -3
  163. package/docs/reference/classes/Guards.md +8 -4
  164. package/docs/reference/classes/I18n.md +54 -2
  165. package/docs/reference/classes/Is.md +10 -4
  166. package/docs/reference/classes/JsonHelper.md +8 -0
  167. package/docs/reference/classes/NotFoundError.md +8 -4
  168. package/docs/reference/classes/NotImplementedError.md +14 -4
  169. package/docs/reference/classes/NotSupportedError.md +8 -4
  170. package/docs/reference/classes/NumberHelper.md +55 -0
  171. package/docs/reference/classes/ObjectHelper.md +58 -8
  172. package/docs/reference/classes/SharedStore.md +2 -2
  173. package/docs/reference/classes/StringHelper.md +8 -8
  174. package/docs/reference/classes/UnauthorizedError.md +8 -4
  175. package/docs/reference/classes/UnprocessableError.md +3 -3
  176. package/docs/reference/classes/Url.md +17 -3
  177. package/docs/reference/classes/Urn.md +19 -5
  178. package/docs/reference/classes/Validation.md +2 -2
  179. package/docs/reference/classes/ValidationError.md +3 -3
  180. package/docs/reference/index.md +1 -0
  181. package/docs/reference/interfaces/IComponent.md +19 -25
  182. package/docs/reference/interfaces/IValidationFailure.md +0 -8
  183. package/locales/.validate-ignore +1 -0
  184. package/locales/en.json +4 -5
  185. package/package.json +22 -10
  186. package/dist/cjs/index.cjs +0 -5175
  187. package/dist/esm/index.mjs +0 -5133
@@ -0,0 +1,256 @@
1
+ import { GeneralError } from "../errors/generalError.js";
2
+ import { Guards } from "../utils/guards.js";
3
+ import { Is } from "../utils/is.js";
4
+ import { SharedStore } from "../utils/sharedStore.js";
5
+ /**
6
+ * Factory for creating implementation of generic types.
7
+ */
8
+ export class Factory {
9
+ /**
10
+ * Runtime name for the class.
11
+ */
12
+ static CLASS_NAME = "Factory";
13
+ /**
14
+ * Type name for the instances.
15
+ * @internal
16
+ */
17
+ _typeName;
18
+ /**
19
+ * Store the generators.
20
+ * @internal
21
+ */
22
+ _generators;
23
+ /**
24
+ * Store the created instances.
25
+ * @internal
26
+ */
27
+ _instances;
28
+ /**
29
+ * Counter for the ordering.
30
+ * @internal
31
+ */
32
+ _orderCounter;
33
+ /**
34
+ * Automatically created an instance when registered.
35
+ * @internal
36
+ */
37
+ _autoInstance;
38
+ /**
39
+ * Match the name of the instance.
40
+ * @internal
41
+ */
42
+ _matcher;
43
+ /**
44
+ * Create a new instance of Factory, private use createFactory.
45
+ * @param typeName The type name for the instances.
46
+ * @param autoInstance Automatically create an instance when registered.
47
+ * @param matcher Match the name of the instance.
48
+ * @internal
49
+ */
50
+ constructor(typeName, autoInstance = false, matcher) {
51
+ this._typeName = typeName;
52
+ this._generators = {};
53
+ this._instances = {};
54
+ this._orderCounter = 0;
55
+ this._autoInstance = autoInstance;
56
+ this._matcher = matcher ?? this.defaultMatcher.bind(this);
57
+ }
58
+ /**
59
+ * Create a new factory, which is shared throughout all library instances.
60
+ * @param typeName The type name for the instances.
61
+ * @param autoInstance Automatically create an instance when registered.
62
+ * @param matcher Match the name of the instance.
63
+ * @returns The factory instance.
64
+ */
65
+ static createFactory(typeName, autoInstance = false, matcher) {
66
+ const factories = Factory.getFactories();
67
+ if (Is.undefined(factories[typeName])) {
68
+ factories[typeName] = new Factory(typeName, autoInstance, matcher);
69
+ }
70
+ return factories[typeName];
71
+ }
72
+ /**
73
+ * Get all the factories.
74
+ * @returns All the factories.
75
+ */
76
+ static getFactories() {
77
+ let factories = SharedStore.get("factories");
78
+ if (Is.undefined(factories)) {
79
+ factories = {};
80
+ SharedStore.set("factories", factories);
81
+ }
82
+ return factories;
83
+ }
84
+ /**
85
+ * Reset all the factories, which removes any created instances, but not the registrations.
86
+ */
87
+ static resetFactories() {
88
+ const factories = Factory.getFactories();
89
+ for (const typeName in factories) {
90
+ factories[typeName].reset();
91
+ }
92
+ }
93
+ /**
94
+ * Clear all the factories, which removes anything registered with the factories.
95
+ */
96
+ static clearFactories() {
97
+ const factories = Factory.getFactories();
98
+ for (const typeName in factories) {
99
+ factories[typeName].clear();
100
+ }
101
+ }
102
+ /**
103
+ * Register a new generator.
104
+ * @param name The name of the generator.
105
+ * @param generator The function to create an instance.
106
+ */
107
+ register(name, generator) {
108
+ Guards.stringValue(Factory.CLASS_NAME, "name", name);
109
+ Guards.function(Factory.CLASS_NAME, "generator", generator);
110
+ this._generators[name] = {
111
+ generator,
112
+ order: this._orderCounter++
113
+ };
114
+ // Remove any existing instance
115
+ this.removeInstance(name);
116
+ if (this._autoInstance) {
117
+ this._instances[name] = generator();
118
+ }
119
+ }
120
+ /**
121
+ * Unregister a generator.
122
+ * @param name The name of the generator to unregister.
123
+ * @throws GuardError if the parameters are invalid.
124
+ * @throws GeneralError if no generator exists.
125
+ */
126
+ unregister(name) {
127
+ Guards.stringValue(Factory.CLASS_NAME, "name", name);
128
+ if (!this._generators[name]) {
129
+ throw new GeneralError(Factory.CLASS_NAME, "noUnregister", {
130
+ typeName: this._typeName,
131
+ name
132
+ });
133
+ }
134
+ delete this._generators[name];
135
+ // Remove any existing instance
136
+ this.removeInstance(name);
137
+ }
138
+ /**
139
+ * Get a generator instance.
140
+ * @param name The name of the instance to generate.
141
+ * @returns An instance of the item.
142
+ * @throws GuardError if the parameters are invalid.
143
+ * @throws GeneralError if no item exists to get.
144
+ */
145
+ get(name) {
146
+ Guards.stringValue(Factory.CLASS_NAME, "name", name);
147
+ const instance = this.getIfExists(name);
148
+ if (!instance) {
149
+ throw new GeneralError(Factory.CLASS_NAME, "noGet", {
150
+ typeName: this._typeName,
151
+ name
152
+ });
153
+ }
154
+ return instance;
155
+ }
156
+ /**
157
+ * Get a generator instance with no exceptions.
158
+ * @param name The name of the instance to generate.
159
+ * @returns An instance of the item or undefined if it does not exist.
160
+ */
161
+ getIfExists(name) {
162
+ if (Is.empty(name)) {
163
+ return;
164
+ }
165
+ Guards.stringValue(Factory.CLASS_NAME, "name", name);
166
+ const matchName = this._matcher(Object.keys(this._generators), name);
167
+ if (Is.stringValue(matchName) && this._generators[matchName]) {
168
+ if (!this._instances[matchName]) {
169
+ this._instances[matchName] = this._generators[matchName].generator();
170
+ }
171
+ if (this._instances[matchName]) {
172
+ return this._instances[matchName];
173
+ }
174
+ }
175
+ }
176
+ /**
177
+ * Remove all the instances and leave the generators intact.
178
+ */
179
+ reset() {
180
+ for (const name in this._generators) {
181
+ this.removeInstance(name);
182
+ }
183
+ this._instances = {};
184
+ }
185
+ /**
186
+ * Remove all the instances and the generators.
187
+ */
188
+ clear() {
189
+ this._instances = {};
190
+ this._generators = {};
191
+ this._orderCounter = 0;
192
+ }
193
+ /**
194
+ * Get all the instances as a map.
195
+ * @returns The instances as a map.
196
+ */
197
+ instancesMap() {
198
+ return this._instances;
199
+ }
200
+ /**
201
+ * Get all the instances as a list in the order they were registered.
202
+ * @returns The instances as a list in the order they were registered.
203
+ */
204
+ instancesList() {
205
+ const orderedInstances = [];
206
+ for (const instanceName in this._instances) {
207
+ orderedInstances.push({
208
+ instance: this._instances[instanceName],
209
+ order: this._generators[instanceName].order
210
+ });
211
+ }
212
+ return orderedInstances.sort((a, b) => a.order - b.order).map(o => o.instance);
213
+ }
214
+ /**
215
+ * Get all the generator names in the order they were registered.
216
+ * @returns The ordered generator names.
217
+ */
218
+ names() {
219
+ const orderedNames = [];
220
+ for (const generator in this._generators) {
221
+ orderedNames.push({
222
+ name: generator,
223
+ order: this._generators[generator].order
224
+ });
225
+ }
226
+ return orderedNames.sort((a, b) => a.order - b.order).map(o => o.name);
227
+ }
228
+ /**
229
+ * Does the factory contain the name.
230
+ * @param name The name of the instance to find.
231
+ * @returns True if the factory has a matching name.
232
+ */
233
+ hasName(name) {
234
+ Guards.stringValue(Factory.CLASS_NAME, "name", name);
235
+ return Is.stringValue(this._matcher(Object.keys(this._generators), name));
236
+ }
237
+ /**
238
+ * Remove any instances of the given name.
239
+ * @param name The name of the instances to remove.
240
+ * @internal
241
+ */
242
+ removeInstance(name) {
243
+ delete this._instances[name];
244
+ }
245
+ /**
246
+ * Match the requested name to the generator name.
247
+ * @param names The list of names for all the generators.
248
+ * @param name The name to match.
249
+ * @returns The matched name or undefined if no match.
250
+ * @internal
251
+ */
252
+ defaultMatcher(names, name) {
253
+ return this._generators[name] ? name : undefined;
254
+ }
255
+ }
256
+ //# sourceMappingURL=factory.js.map
@@ -0,0 +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,I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C1B,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,UAAU,UAAgB,IAAI,CAAC,CAAC;QAC3D,OAAO,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"]}
@@ -0,0 +1,44 @@
1
+ import { Is } from "../utils/is.js";
2
+ /**
3
+ * Class to help with arrays.
4
+ */
5
+ export class ArrayHelper {
6
+ /**
7
+ * Do the two arrays match.
8
+ * @param arr1 The first array.
9
+ * @param arr2 The second array.
10
+ * @returns True if both arrays are empty of have the same values.
11
+ */
12
+ static matches(arr1, arr2) {
13
+ if (Is.empty(arr1) && Is.empty(arr2)) {
14
+ return true;
15
+ }
16
+ if (!((Is.array(arr1) && Is.array(arr2)) || (Is.typedArray(arr1) && Is.typedArray(arr2)))) {
17
+ return false;
18
+ }
19
+ if (arr1.length !== arr2.length) {
20
+ return false;
21
+ }
22
+ for (let i = 0; i < arr1.length; i++) {
23
+ if (arr1[i] !== arr2[i]) {
24
+ return false;
25
+ }
26
+ }
27
+ return true;
28
+ }
29
+ /**
30
+ * Convert an object or array to an array.
31
+ * @param value The object or array to convert.
32
+ * @returns The array.
33
+ */
34
+ static fromObjectOrArray(value) {
35
+ if (Is.empty(value)) {
36
+ return undefined;
37
+ }
38
+ if (Is.array(value)) {
39
+ return value;
40
+ }
41
+ return [value];
42
+ }
43
+ }
44
+ //# sourceMappingURL=arrayHelper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arrayHelper.js","sourceRoot":"","sources":["../../../src/helpers/arrayHelper.ts"],"names":[],"mappings":"AAGA,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,KAAmC;QAEnC,IAAI,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YACrB,OAAO,SAAS,CAAC;QAClB,CAAC;QACD,IAAI,EAAE,CAAC,KAAK,CAAI,KAAK,CAAC,EAAE,CAAC;YACxB,OAAO,KAAK,CAAC;QACd,CAAC;QACD,OAAO,CAAC,KAAK,CAAC,CAAC;IAChB,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { ObjectOrArray } from \"../models/objectOrArray.js\";\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\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\tpublic static fromObjectOrArray<T = unknown>(value: undefined): undefined;\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>(value: ObjectOrArray<T>): T[];\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: ObjectOrArray<T> | undefined\n\t): T[] | undefined {\n\t\tif (Is.empty(value)) {\n\t\t\treturn undefined;\n\t\t}\n\t\tif (Is.array<T>(value)) {\n\t\t\treturn value;\n\t\t}\n\t\treturn [value];\n\t}\n}\n"]}
@@ -0,0 +1,39 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ import { ObjectHelper } from "./objectHelper.js";
4
+ import { StringHelper } from "./stringHelper.js";
5
+ import { Is } from "../utils/is.js";
6
+ /**
7
+ * Environment variable helper.
8
+ */
9
+ export class EnvHelper {
10
+ /**
11
+ * Get the environment variable as an object with camel cased names.
12
+ * @param envVars The environment variables.
13
+ * @param prefix The prefix of the environment variables, if not provided gets all.
14
+ * @returns The object with camel cased names.
15
+ */
16
+ static envToJson(envVars, prefix) {
17
+ const result = {};
18
+ if (!Is.empty(envVars)) {
19
+ if (Is.empty(prefix)) {
20
+ for (const envVar in envVars) {
21
+ if (Is.stringValue(envVars[envVar])) {
22
+ const camelCaseName = StringHelper.camelCase(envVar.toLowerCase());
23
+ ObjectHelper.propertySet(result, camelCaseName, envVars[envVar]);
24
+ }
25
+ }
26
+ }
27
+ else {
28
+ for (const envVar in envVars) {
29
+ if (envVar.startsWith(prefix) && Is.stringValue(envVars[envVar])) {
30
+ const camelCaseName = StringHelper.camelCase(envVar.replace(prefix, "").toLowerCase());
31
+ ObjectHelper.propertySet(result, camelCaseName, envVars[envVar]);
32
+ }
33
+ }
34
+ }
35
+ }
36
+ return result;
37
+ }
38
+ }
39
+ //# sourceMappingURL=envHelper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"envHelper.js","sourceRoot":"","sources":["../../../src/helpers/envHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAEpC;;GAEG;AACH,MAAM,OAAO,SAAS;IACrB;;;;;OAKG;IACI,MAAM,CAAC,SAAS,CACtB,OAA6C,EAC7C,MAAe;QAEf,MAAM,MAAM,GAA6B,EAAE,CAAC;QAE5C,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;YACxB,IAAI,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;gBACtB,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;oBAC9B,IAAI,EAAE,CAAC,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC;wBACrC,MAAM,aAAa,GAAG,YAAY,CAAC,SAAS,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC;wBACnE,YAAY,CAAC,WAAW,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;oBAClE,CAAC;gBACF,CAAC;YACF,CAAC;iBAAM,CAAC;gBACP,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;oBAC9B,IAAI,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC;wBAClE,MAAM,aAAa,GAAG,YAAY,CAAC,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC;wBACvF,YAAY,CAAC,WAAW,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;oBAClE,CAAC;gBACF,CAAC;YACF,CAAC;QACF,CAAC;QAED,OAAO,MAAW,CAAC;IACpB,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { ObjectHelper } from \"./objectHelper.js\";\nimport { StringHelper } from \"./stringHelper.js\";\nimport { Is } from \"../utils/is.js\";\n\n/**\n * Environment variable helper.\n */\nexport class EnvHelper {\n\t/**\n\t * Get the environment variable as an object with camel cased names.\n\t * @param envVars The environment variables.\n\t * @param prefix The prefix of the environment variables, if not provided gets all.\n\t * @returns The object with camel cased names.\n\t */\n\tpublic static envToJson<T = { [id: string]: string }>(\n\t\tenvVars: { [id: string]: string | undefined },\n\t\tprefix?: string\n\t): T {\n\t\tconst result: { [id: string]: string } = {};\n\n\t\tif (!Is.empty(envVars)) {\n\t\t\tif (Is.empty(prefix)) {\n\t\t\t\tfor (const envVar in envVars) {\n\t\t\t\t\tif (Is.stringValue(envVars[envVar])) {\n\t\t\t\t\t\tconst camelCaseName = StringHelper.camelCase(envVar.toLowerCase());\n\t\t\t\t\t\tObjectHelper.propertySet(result, camelCaseName, envVars[envVar]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfor (const envVar in envVars) {\n\t\t\t\t\tif (envVar.startsWith(prefix) && Is.stringValue(envVars[envVar])) {\n\t\t\t\t\t\tconst camelCaseName = StringHelper.camelCase(envVar.replace(prefix, \"\").toLowerCase());\n\t\t\t\t\t\tObjectHelper.propertySet(result, camelCaseName, envVars[envVar]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn result as T;\n\t}\n}\n"]}
@@ -0,0 +1,100 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ import { StringHelper } from "./stringHelper.js";
4
+ import { BaseError } from "../errors/baseError.js";
5
+ import { I18n } from "../utils/i18n.js";
6
+ import { Is } from "../utils/is.js";
7
+ /**
8
+ * Error helper functions.
9
+ */
10
+ export class ErrorHelper {
11
+ /**
12
+ * Format Errors and returns just their messages.
13
+ * @param error The error to format.
14
+ * @param includeDetails Whether to include error details, defaults to false.
15
+ * @returns The error formatted including any causes errors.
16
+ */
17
+ static formatErrors(error, includeDetails) {
18
+ const localizedErrors = ErrorHelper.localizeErrors(error);
19
+ if (includeDetails ?? false) {
20
+ const output = [];
21
+ for (const err of localizedErrors) {
22
+ let detailedError = err.message;
23
+ if (Is.stringValue(err.stack)) {
24
+ detailedError += `\n${err.stack}`;
25
+ }
26
+ output.push(detailedError);
27
+ }
28
+ return output;
29
+ }
30
+ return localizedErrors.map(e => e.message);
31
+ }
32
+ /**
33
+ * Localize the content of an error and any causes.
34
+ * @param error The error to format.
35
+ * @returns The localized version of the errors flattened.
36
+ */
37
+ static localizeErrors(error) {
38
+ const formattedErrors = [];
39
+ if (Is.notEmpty(error)) {
40
+ const errors = BaseError.flatten(error);
41
+ for (const err of errors) {
42
+ const errorNameKey = `errorNames.${StringHelper.camelCase(err.name)}`;
43
+ const errorMessageKey = `error.${err.message}`;
44
+ // If there is no error message then it is probably
45
+ // from a 3rd party lib, so don't format it just display
46
+ const hasErrorName = I18n.hasMessage(errorNameKey);
47
+ const hasErrorMessage = I18n.hasMessage(errorMessageKey);
48
+ const localizedError = {
49
+ name: I18n.formatMessage(hasErrorName ? errorNameKey : "errorNames.error"),
50
+ message: hasErrorMessage
51
+ ? I18n.formatMessage(errorMessageKey, err.properties)
52
+ : err.message
53
+ };
54
+ if (Is.stringValue(err.source)) {
55
+ localizedError.source = err.source;
56
+ }
57
+ if (Is.stringValue(err.stack)) {
58
+ // Remove the first line from the stack traces as they
59
+ // just have the error type and message duplicated
60
+ const lines = err.stack.split("\n");
61
+ lines.shift();
62
+ localizedError.stack = lines.join("\n");
63
+ }
64
+ const additional = ErrorHelper.formatValidationErrors(err);
65
+ if (Is.stringValue(additional)) {
66
+ localizedError.additional = additional;
67
+ }
68
+ formattedErrors.push(localizedError);
69
+ }
70
+ }
71
+ return formattedErrors;
72
+ }
73
+ /**
74
+ * Localize the content of an error and any causes.
75
+ * @param error The error to format.
76
+ * @returns The localized version of the errors flattened.
77
+ */
78
+ static formatValidationErrors(error) {
79
+ if (Is.object(error.properties) &&
80
+ Object.keys(error.properties).length > 0 &&
81
+ Is.object(error.properties) &&
82
+ Is.arrayValue(error.properties.validationFailures)) {
83
+ const validationErrors = [];
84
+ for (const validationFailure of error.properties.validationFailures) {
85
+ const errorI18n = `error.${validationFailure.reason}`;
86
+ const errorMessage = I18n.hasMessage(errorI18n)
87
+ ? I18n.formatMessage(errorI18n, validationFailure.properties)
88
+ : errorI18n;
89
+ let v = `${validationFailure.property}: ${errorMessage}`;
90
+ if (Is.object(validationFailure.properties) &&
91
+ Is.notEmpty(validationFailure.properties.value)) {
92
+ v += ` = ${JSON.stringify(validationFailure.properties.value)}`;
93
+ }
94
+ validationErrors.push(v);
95
+ }
96
+ return validationErrors.join("\n");
97
+ }
98
+ }
99
+ }
100
+ //# sourceMappingURL=errorHelper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errorHelper.js","sourceRoot":"","sources":["../../../src/helpers/errorHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAGnD,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAEpC;;GAEG;AACH,MAAM,OAAO,WAAW;IACvB;;;;;OAKG;IACI,MAAM,CAAC,YAAY,CAAC,KAAc,EAAE,cAAwB;QAClE,MAAM,eAAe,GAAG,WAAW,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QAC1D,IAAI,cAAc,IAAI,KAAK,EAAE,CAAC;YAC7B,MAAM,MAAM,GAAa,EAAE,CAAC;YAC5B,KAAK,MAAM,GAAG,IAAI,eAAe,EAAE,CAAC;gBACnC,IAAI,aAAa,GAAG,GAAG,CAAC,OAAO,CAAC;gBAChC,IAAI,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;oBAC/B,aAAa,IAAI,KAAK,GAAG,CAAC,KAAK,EAAE,CAAC;gBACnC,CAAC;gBACD,MAAM,CAAC,IAAI,C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AAC;gBAEb,IAAI,CAAC,GAAG,GAAG,iBAAiB,CAAC,QAAQ,KAAK,YAAY,EAAE,CAAC;gBACzD,IACC,EAAE,CAAC,MAAM,CAAqB,iBAAiB,CAAC,UAAU,CAAC;oBAC3D,EAAE,CAAC,QAAQ,CAAC,iBAAiB,CAAC,UAAU,CAAC,KAAK,CAAC,EAC9C,CAAC;oBACF,CAAC,IAAI,MAAM,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;gBACjE,CAAC;gBACD,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YAC1B,CAAC;YACD,OAAO,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACpC,CAAC;IACF,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { StringHelper } from \"./stringHelper.js\";\nimport { BaseError } from \"../errors/baseError.js\";\nimport type { IError } from \"../models/IError.js\";\nimport type { IValidationFailure } from \"../models/IValidationFailure.js\";\nimport { I18n } from \"../utils/i18n.js\";\nimport { Is } from \"../utils/is.js\";\n\n/**\n * Error helper functions.\n */\nexport class ErrorHelper {\n\t/**\n\t * Format Errors and returns just their messages.\n\t * @param error The error to format.\n\t * @param includeDetails Whether to include error details, defaults to false.\n\t * @returns The error formatted including any causes errors.\n\t */\n\tpublic static formatErrors(error: unknown, includeDetails?: boolean): string[] {\n\t\tconst localizedErrors = ErrorHelper.localizeErrors(error);\n\t\tif (includeDetails ?? false) {\n\t\t\tconst output: string[] = [];\n\t\t\tfor (const err of localizedErrors) {\n\t\t\t\tlet detailedError = err.message;\n\t\t\t\tif (Is.stringValue(err.stack)) {\n\t\t\t\t\tdetailedError += `\\n${err.stack}`;\n\t\t\t\t}\n\t\t\t\toutput.push(detailedError);\n\t\t\t}\n\t\t\treturn output;\n\t\t}\n\t\treturn localizedErrors.map(e => e.message);\n\t}\n\n\t/**\n\t * Localize the content of an error and any causes.\n\t * @param error The error to format.\n\t * @returns The localized version of the errors flattened.\n\t */\n\tpublic static localizeErrors(error: unknown): IError[] {\n\t\tconst formattedErrors: IError[] = [];\n\n\t\tif (Is.notEmpty(error)) {\n\t\t\tconst errors = BaseError.flatten(error);\n\n\t\t\tfor (const err of errors) {\n\t\t\t\tconst errorNameKey = `errorNames.${StringHelper.camelCase(err.name)}`;\n\t\t\t\tconst errorMessageKey = `error.${err.message}`;\n\n\t\t\t\t// If there is no error message then it is probably\n\t\t\t\t// from a 3rd party lib, so don't format it just display\n\t\t\t\tconst hasErrorName = I18n.hasMessage(errorNameKey);\n\t\t\t\tconst hasErrorMessage = I18n.hasMessage(errorMessageKey);\n\n\t\t\t\tconst localizedError: IError & { additional?: string } = {\n\t\t\t\t\tname: I18n.formatMessage(hasErrorName ? errorNameKey : \"errorNames.error\"),\n\t\t\t\t\tmessage: hasErrorMessage\n\t\t\t\t\t\t? I18n.formatMessage(errorMessageKey, err.properties)\n\t\t\t\t\t\t: err.message\n\t\t\t\t};\n\n\t\t\t\tif (Is.stringValue(err.source)) {\n\t\t\t\t\tlocalizedError.source = err.source;\n\t\t\t\t}\n\t\t\t\tif (Is.stringValue(err.stack)) {\n\t\t\t\t\t// Remove the first line from the stack traces as they\n\t\t\t\t\t// just have the error type and message duplicated\n\t\t\t\t\tconst lines = err.stack.split(\"\\n\");\n\t\t\t\t\tlines.shift();\n\t\t\t\t\tlocalizedError.stack = lines.join(\"\\n\");\n\t\t\t\t}\n\n\t\t\t\tconst additional = ErrorHelper.formatValidationErrors(err);\n\t\t\t\tif (Is.stringValue(additional)) {\n\t\t\t\t\tlocalizedError.additional = additional;\n\t\t\t\t}\n\n\t\t\t\tformattedErrors.push(localizedError);\n\t\t\t}\n\t\t}\n\n\t\treturn formattedErrors;\n\t}\n\n\t/**\n\t * Localize the content of an error and any causes.\n\t * @param error The error to format.\n\t * @returns The localized version of the errors flattened.\n\t */\n\tpublic static formatValidationErrors(error: IError): string | undefined {\n\t\tif (\n\t\t\tIs.object(error.properties) &&\n\t\t\tObject.keys(error.properties).length > 0 &&\n\t\t\tIs.object<{ validationFailures: IValidationFailure[] }>(error.properties) &&\n\t\t\tIs.arrayValue(error.properties.validationFailures)\n\t\t) {\n\t\t\tconst validationErrors = [];\n\t\t\tfor (const validationFailure of error.properties.validationFailures) {\n\t\t\t\tconst errorI18n = `error.${validationFailure.reason}`;\n\t\t\t\tconst errorMessage = I18n.hasMessage(errorI18n)\n\t\t\t\t\t? I18n.formatMessage(errorI18n, validationFailure.properties)\n\t\t\t\t\t: errorI18n;\n\n\t\t\t\tlet v = `${validationFailure.property}: ${errorMessage}`;\n\t\t\t\tif (\n\t\t\t\t\tIs.object<{ value: unknown }>(validationFailure.properties) &&\n\t\t\t\t\tIs.notEmpty(validationFailure.properties.value)\n\t\t\t\t) {\n\t\t\t\t\tv += ` = ${JSON.stringify(validationFailure.properties.value)}`;\n\t\t\t\t}\n\t\t\t\tvalidationErrors.push(v);\n\t\t\t}\n\t\t\treturn validationErrors.join(\"\\n\");\n\t\t}\n\t}\n}\n"]}
@@ -0,0 +1,32 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ import { Coerce } from "../utils/coerce.js";
4
+ import { Is } from "../utils/is.js";
5
+ /**
6
+ * Class to help with filenames.
7
+ */
8
+ export class FilenameHelper {
9
+ /**
10
+ * Replaces any unsafe characters in the filename.
11
+ * @param filename The filename to make safe.
12
+ * @returns The safe filename.
13
+ */
14
+ static safeFilename(filename) {
15
+ let safe = Coerce.string(filename);
16
+ if (Is.empty(safe)) {
17
+ return "";
18
+ }
19
+ // Common non filename characters
20
+ safe = safe.replace(/["*/:<>?\\|]/g, "_");
21
+ // Windows non filename characters
22
+ safe = safe.replace(/^(con|prn|aux|nul|com\d|lpt\d)$/i, "_");
23
+ // Control characters
24
+ safe = safe.replace(/[\u0000-\u001F\u0080-\u009F]/g, "_");
25
+ // Relative paths
26
+ safe = safe.replace(/^\.+/, "_");
27
+ // Trailing periods
28
+ safe = safe.replace(/\.+$/, "");
29
+ return safe;
30
+ }
31
+ }
32
+ //# sourceMappingURL=filenameHelper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filenameHelper.js","sourceRoot":"","sources":["../../../src/helpers/filenameHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAEpC;;GAEG;AACH,MAAM,OAAO,cAAc;IAC1B;;;;OAIG;IACI,MAAM,CAAC,YAAY,CAAC,QAAiB;QAC3C,IAAI,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAEnC,IAAI,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;YACpB,OAAO,EAAE,CAAC;QACX,CAAC;QAED,iCAAiC;QACjC,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,eAAe,EAAE,GAAG,CAAC,CAAC;QAC1C,kCAAkC;QAClC,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,kCAAkC,EAAE,GAAG,CAAC,CAAC;QAC7D,qBAAqB;QACrB,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,+BAA+B,EAAE,GAAG,CAAC,CAAC;QAC1D,iBAAiB;QACjB,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QACjC,mBAAmB;QACnB,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAEhC,OAAO,IAAI,CAAC;IACb,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Coerce } from \"../utils/coerce.js\";\nimport { Is } from \"../utils/is.js\";\n\n/**\n * Class to help with filenames.\n */\nexport class FilenameHelper {\n\t/**\n\t * Replaces any unsafe characters in the filename.\n\t * @param filename The filename to make safe.\n\t * @returns The safe filename.\n\t */\n\tpublic static safeFilename(filename: unknown): string {\n\t\tlet safe = Coerce.string(filename);\n\n\t\tif (Is.empty(safe)) {\n\t\t\treturn \"\";\n\t\t}\n\n\t\t// Common non filename characters\n\t\tsafe = safe.replace(/[\"*/:<>?\\\\|]/g, \"_\");\n\t\t// Windows non filename characters\n\t\tsafe = safe.replace(/^(con|prn|aux|nul|com\\d|lpt\\d)$/i, \"_\");\n\t\t// Control characters\n\t\tsafe = safe.replace(/[\\u0000-\\u001F\\u0080-\\u009F]/g, \"_\");\n\t\t// Relative paths\n\t\tsafe = safe.replace(/^\\.+/, \"_\");\n\t\t// Trailing periods\n\t\tsafe = safe.replace(/\\.+$/, \"\");\n\n\t\treturn safe;\n\t}\n}\n"]}
@@ -0,0 +1,45 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ /**
4
+ * Helper methods for hex conversions.
5
+ */
6
+ export class HexHelper {
7
+ /**
8
+ * Strip the 0x prefix if it exists.
9
+ * @param hex The hex value to strip.
10
+ * @returns The stripped hex without the prefix.
11
+ */
12
+ static stripPrefix(hex) {
13
+ return hex.replace(/^0x/, "");
14
+ }
15
+ /**
16
+ * Add the 0x prefix if it does not exist.
17
+ * @param hex The hex value to add the prefix to.
18
+ * @returns The hex with the prefix.
19
+ */
20
+ static addPrefix(hex) {
21
+ return HexHelper.hasPrefix(hex) ? hex : `0x${hex}`;
22
+ }
23
+ /**
24
+ * Does the hex string have the prefix.
25
+ * @param hex The hex value to check for the prefix.
26
+ * @returns True if the hex string has the prefix.
27
+ */
28
+ static hasPrefix(hex) {
29
+ return hex.startsWith("0x");
30
+ }
31
+ /**
32
+ * Is the data hex format.
33
+ * @param value The value to test.
34
+ * @param allowPrefix Allow the hex to have the 0x prefix.
35
+ * @returns True if the string is hex.
36
+ */
37
+ static isHex(value, allowPrefix = false) {
38
+ const localHex = allowPrefix ? HexHelper.stripPrefix(value) : value;
39
+ if (localHex.length % 2 === 1) {
40
+ return false;
41
+ }
42
+ return /^[\da-f]+$/g.test(localHex);
43
+ }
44
+ }
45
+ //# sourceMappingURL=hexHelper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hexHelper.js","sourceRoot":"","sources":["../../../src/helpers/hexHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AAEvC;;GAEG;AACH,MAAM,OAAO,SAAS;IACrB;;;;OAIG;IACI,MAAM,CAAC,WAAW,CAAC,GAAW;QACpC,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAC/B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,SAAS,CAAC,GAAW;QAClC,OAAO,SAAS,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC;IACpD,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,SAAS,CAAC,GAAW;QAClC,OAAO,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAAC,KAAa,EAAE,cAAuB,KAAK;QAC9D,MAAM,QAAQ,GAAG,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;QAEpE,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;YAC/B,OAAO,KAAK,CAAC;QACd,CAAC;QACD,OAAO,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACrC,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Helper methods for hex conversions.\n */\nexport class HexHelper {\n\t/**\n\t * Strip the 0x prefix if it exists.\n\t * @param hex The hex value to strip.\n\t * @returns The stripped hex without the prefix.\n\t */\n\tpublic static stripPrefix(hex: string): string {\n\t\treturn hex.replace(/^0x/, \"\");\n\t}\n\n\t/**\n\t * Add the 0x prefix if it does not exist.\n\t * @param hex The hex value to add the prefix to.\n\t * @returns The hex with the prefix.\n\t */\n\tpublic static addPrefix(hex: string): string {\n\t\treturn HexHelper.hasPrefix(hex) ? hex : `0x${hex}`;\n\t}\n\n\t/**\n\t * Does the hex string have the prefix.\n\t * @param hex The hex value to check for the prefix.\n\t * @returns True if the hex string has the prefix.\n\t */\n\tpublic static hasPrefix(hex: string): boolean {\n\t\treturn hex.startsWith(\"0x\");\n\t}\n\n\t/**\n\t * Is the data hex format.\n\t * @param value The value to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @returns True if the string is hex.\n\t */\n\tpublic static isHex(value: string, allowPrefix: boolean = false): boolean {\n\t\tconst localHex = allowPrefix ? HexHelper.stripPrefix(value) : value;\n\n\t\tif (localHex.length % 2 === 1) {\n\t\t\treturn false;\n\t\t}\n\t\treturn /^[\\da-f]+$/g.test(localHex);\n\t}\n}\n"]}