gingersnap 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (251) hide show
  1. package/_virtual/_tslib.cjs +102 -0
  2. package/_virtual/_tslib.mjs +93 -0
  3. package/data/AtomicValue.d.ts +15 -0
  4. package/data/bus.cjs +179 -0
  5. package/data/bus.d.ts +72 -0
  6. package/data/bus.mjs +177 -0
  7. package/data/decoders/index.d.ts +4 -0
  8. package/data/decoders/json.cjs +12 -0
  9. package/data/decoders/json.d.ts +7 -0
  10. package/data/decoders/json.mjs +10 -0
  11. package/data/decoders/msgpack.cjs +14 -0
  12. package/data/decoders/msgpack.d.ts +7 -0
  13. package/data/decoders/msgpack.mjs +12 -0
  14. package/data/decoders/protobuf.d.ts +13 -0
  15. package/data/decoders/string.cjs +12 -0
  16. package/data/decoders/string.d.ts +7 -0
  17. package/data/decoders/string.mjs +10 -0
  18. package/data/decoders/type.d.ts +7 -0
  19. package/data/decoders.cjs +11 -0
  20. package/data/decoders.mjs +3 -0
  21. package/data/model/credentials.cjs +114 -0
  22. package/data/model/credentials.d.ts +61 -0
  23. package/data/model/credentials.mjs +109 -0
  24. package/data/model/extra.cjs +88 -0
  25. package/data/model/extra.d.ts +5 -0
  26. package/data/model/extra.mjs +66 -0
  27. package/data/model/index.d.ts +6 -0
  28. package/data/model/model.cjs +541 -0
  29. package/data/model/model.d.ts +207 -0
  30. package/data/model/model.mjs +518 -0
  31. package/data/model/plugin.cjs +96 -0
  32. package/data/model/plugin.d.ts +38 -0
  33. package/data/model/plugin.mjs +72 -0
  34. package/data/model/property.cjs +310 -0
  35. package/data/model/property.d.ts +83 -0
  36. package/data/model/property.mjs +278 -0
  37. package/data/model/types.cjs +32 -0
  38. package/data/model/types.d.ts +28 -0
  39. package/data/model/types.mjs +32 -0
  40. package/data/model.cjs +43 -0
  41. package/data/model.mjs +6 -0
  42. package/data/signal.cjs +47 -0
  43. package/data/signal.d.ts +17 -0
  44. package/data/signal.mjs +45 -0
  45. package/data/store/index.d.ts +4 -0
  46. package/data/store/json.cjs +58 -0
  47. package/data/store/json.d.ts +18 -0
  48. package/data/store/json.mjs +56 -0
  49. package/data/store/manager.cjs +206 -0
  50. package/data/store/manager.d.ts +44 -0
  51. package/data/store/manager.mjs +203 -0
  52. package/data/store/web.cjs +92 -0
  53. package/data/store/web.d.ts +25 -0
  54. package/data/store/web.mjs +89 -0
  55. package/data/value.cjs +66 -0
  56. package/data/value.mjs +64 -0
  57. package/data-structures/array/CyclicalList.cjs +57 -0
  58. package/data-structures/array/CyclicalList.d.ts +12 -0
  59. package/data-structures/array/CyclicalList.mjs +55 -0
  60. package/data-structures/array/Pair.cjs +30 -0
  61. package/data-structures/array/Pair.d.ts +17 -0
  62. package/data-structures/array/Pair.mjs +27 -0
  63. package/data-structures/array/Stack.cjs +96 -0
  64. package/data-structures/array/Stack.d.ts +41 -0
  65. package/data-structures/array/Stack.mjs +94 -0
  66. package/data-structures/array/Triple.cjs +34 -0
  67. package/data-structures/array/Triple.d.ts +19 -0
  68. package/data-structures/array/Triple.mjs +31 -0
  69. package/data-structures/array/Tuple.d.ts +8 -0
  70. package/data-structures/array/index.d.ts +5 -0
  71. package/data-structures/array.cjs +15 -0
  72. package/data-structures/array.mjs +4 -0
  73. package/data-structures/object/BufferQueue.cjs +98 -0
  74. package/data-structures/object/BufferQueue.d.ts +21 -0
  75. package/data-structures/object/BufferQueue.mjs +77 -0
  76. package/data-structures/object/CyclicalObject.cjs +155 -0
  77. package/data-structures/object/CyclicalObject.d.ts +74 -0
  78. package/data-structures/object/CyclicalObject.mjs +134 -0
  79. package/data-structures/object/Queue.cjs +178 -0
  80. package/data-structures/object/Queue.d.ts +31 -0
  81. package/data-structures/object/Queue.mjs +176 -0
  82. package/data-structures/object/SimpleQueue.cjs +53 -0
  83. package/data-structures/object/SimpleQueue.d.ts +13 -0
  84. package/data-structures/object/SimpleQueue.mjs +51 -0
  85. package/data-structures/object/TimeableObject.cjs +73 -0
  86. package/data-structures/object/TimeableObject.d.ts +30 -0
  87. package/data-structures/object/TimeableObject.mjs +71 -0
  88. package/data-structures/object/WaitableObject.cjs +61 -0
  89. package/data-structures/object/WaitableObject.d.ts +29 -0
  90. package/data-structures/object/WaitableObject.mjs +59 -0
  91. package/data-structures/object/WatchableObject.cjs +251 -0
  92. package/data-structures/object/WatchableObject.d.ts +88 -0
  93. package/data-structures/object/WatchableObject.mjs +230 -0
  94. package/data-structures/object/index.d.ts +7 -0
  95. package/data-structures/object.cjs +23 -0
  96. package/data-structures/object.mjs +7 -0
  97. package/errors/AbortError.cjs +9 -0
  98. package/errors/AbortError.d.ts +5 -0
  99. package/errors/AbortError.mjs +7 -0
  100. package/errors/CallExecutionError.cjs +13 -0
  101. package/errors/CallExecutionError.d.ts +7 -0
  102. package/errors/CallExecutionError.mjs +11 -0
  103. package/errors/FutureCancelled.cjs +11 -0
  104. package/errors/FutureCancelled.d.ts +6 -0
  105. package/errors/FutureCancelled.mjs +9 -0
  106. package/errors/FutureError.cjs +11 -0
  107. package/errors/FutureError.d.ts +6 -0
  108. package/errors/FutureError.mjs +9 -0
  109. package/errors/IllegalArgumentsError.cjs +13 -0
  110. package/errors/IllegalArgumentsError.d.ts +7 -0
  111. package/errors/IllegalArgumentsError.mjs +11 -0
  112. package/errors/IllegalOperationError.cjs +6 -0
  113. package/errors/IllegalOperationError.d.ts +2 -0
  114. package/errors/IllegalOperationError.mjs +4 -0
  115. package/errors/InvalidType.cjs +6 -0
  116. package/errors/InvalidType.d.ts +2 -0
  117. package/errors/InvalidType.mjs +4 -0
  118. package/errors/InvalidValue.cjs +11 -0
  119. package/errors/InvalidValue.d.ts +4 -0
  120. package/errors/InvalidValue.mjs +9 -0
  121. package/errors/MissingArgumentsError.cjs +13 -0
  122. package/errors/MissingArgumentsError.d.ts +7 -0
  123. package/errors/MissingArgumentsError.mjs +11 -0
  124. package/errors/NetworkError.cjs +13 -0
  125. package/errors/NetworkError.d.ts +8 -0
  126. package/errors/NetworkError.mjs +11 -0
  127. package/errors/NoSuchElement.cjs +6 -0
  128. package/errors/NoSuchElement.d.ts +2 -0
  129. package/errors/NoSuchElement.mjs +4 -0
  130. package/errors/NotImplemented.cjs +9 -0
  131. package/errors/NotImplemented.d.ts +5 -0
  132. package/errors/NotImplemented.mjs +7 -0
  133. package/errors/ParsingError.cjs +10 -0
  134. package/errors/ParsingError.d.ts +5 -0
  135. package/errors/ParsingError.mjs +8 -0
  136. package/errors/QueueEmptyError.cjs +6 -0
  137. package/errors/QueueEmptyError.d.ts +2 -0
  138. package/errors/QueueEmptyError.mjs +4 -0
  139. package/errors/QueueFullError.cjs +6 -0
  140. package/errors/QueueFullError.d.ts +2 -0
  141. package/errors/QueueFullError.mjs +4 -0
  142. package/errors/StackEmptyError.cjs +6 -0
  143. package/errors/StackEmptyError.d.ts +2 -0
  144. package/errors/StackEmptyError.mjs +4 -0
  145. package/errors/StackFullError.cjs +6 -0
  146. package/errors/StackFullError.d.ts +2 -0
  147. package/errors/StackFullError.mjs +4 -0
  148. package/errors/StreamEnded.cjs +6 -0
  149. package/errors/StreamEnded.d.ts +2 -0
  150. package/errors/StreamEnded.mjs +4 -0
  151. package/errors/TimeoutError.cjs +11 -0
  152. package/errors/TimeoutError.d.ts +6 -0
  153. package/errors/TimeoutError.mjs +9 -0
  154. package/errors/ValueDestroyedError.cjs +6 -0
  155. package/errors/ValueDestroyedError.d.ts +2 -0
  156. package/errors/ValueDestroyedError.mjs +4 -0
  157. package/errors/VerificationError.cjs +9 -0
  158. package/errors/VerificationError.d.ts +5 -0
  159. package/errors/VerificationError.mjs +7 -0
  160. package/errors/index.d.ts +18 -0
  161. package/errors.cjs +41 -0
  162. package/errors.mjs +18 -0
  163. package/files.cjs +201 -0
  164. package/files.d.ts +31 -0
  165. package/files.mjs +180 -0
  166. package/functools/index.d.ts +31 -0
  167. package/functools.cjs +371 -0
  168. package/functools.mjs +334 -0
  169. package/future/future.cjs +570 -0
  170. package/future/future.d.ts +208 -0
  171. package/future/future.mjs +548 -0
  172. package/future/group.cjs +180 -0
  173. package/future/group.d.ts +82 -0
  174. package/future/group.mjs +159 -0
  175. package/future/index.d.ts +3 -0
  176. package/future/result.cjs +19 -0
  177. package/future/result.d.ts +18 -0
  178. package/future/result.mjs +17 -0
  179. package/future.cjs +11 -0
  180. package/future.mjs +3 -0
  181. package/managers/context.d.ts +8 -0
  182. package/managers/index.d.ts +1 -0
  183. package/managers.cjs +2 -0
  184. package/managers.mjs +1 -0
  185. package/mocks.cjs +301 -0
  186. package/mocks.d.ts +125 -0
  187. package/mocks.mjs +293 -0
  188. package/networking/EventSourceService.cjs +104 -0
  189. package/networking/EventSourceService.d.ts +11 -0
  190. package/networking/EventSourceService.mjs +83 -0
  191. package/networking/NetworkService.cjs +394 -0
  192. package/networking/NetworkService.d.ts +97 -0
  193. package/networking/NetworkService.mjs +373 -0
  194. package/networking/SocketService.cjs +218 -0
  195. package/networking/SocketService.d.ts +23 -0
  196. package/networking/SocketService.mjs +197 -0
  197. package/networking/decorators/index.d.ts +4 -0
  198. package/networking/decorators/options.cjs +289 -0
  199. package/networking/decorators/options.d.ts +148 -0
  200. package/networking/decorators/options.mjs +242 -0
  201. package/networking/decorators/requests.cjs +80 -0
  202. package/networking/decorators/requests.d.ts +34 -0
  203. package/networking/decorators/requests.mjs +52 -0
  204. package/networking/decorators/socket.cjs +81 -0
  205. package/networking/decorators/socket.d.ts +13 -0
  206. package/networking/decorators/socket.mjs +51 -0
  207. package/networking/http.cjs +69 -0
  208. package/networking/http.d.ts +66 -0
  209. package/networking/http.mjs +69 -0
  210. package/networking/index.d.ts +39 -0
  211. package/networking/request.cjs +18 -0
  212. package/networking/request.d.ts +9 -0
  213. package/networking/request.mjs +16 -0
  214. package/networking/types.cjs +37 -0
  215. package/networking/types.d.ts +132 -0
  216. package/networking/types.mjs +35 -0
  217. package/networking.cjs +91 -0
  218. package/networking.mjs +33 -0
  219. package/package.json +1 -0
  220. package/reflection/injector.cjs +43 -0
  221. package/reflection/injector.d.ts +3 -0
  222. package/reflection/injector.mjs +40 -0
  223. package/socket.cjs +239 -0
  224. package/socket.d.ts +65 -0
  225. package/socket.mjs +237 -0
  226. package/store.cjs +12 -0
  227. package/store.mjs +3 -0
  228. package/stream/call.cjs +157 -0
  229. package/stream/call.d.ts +66 -0
  230. package/stream/call.mjs +154 -0
  231. package/stream/collector.cjs +217 -0
  232. package/stream/collector.d.ts +14 -0
  233. package/stream/collector.mjs +215 -0
  234. package/stream/index.d.ts +257 -0
  235. package/stream/observable.cjs +110 -0
  236. package/stream/observable.d.ts +65 -0
  237. package/stream/observable.mjs +106 -0
  238. package/stream/state.cjs +10 -0
  239. package/stream/state.d.ts +5 -0
  240. package/stream/state.mjs +8 -0
  241. package/stream.cjs +1153 -0
  242. package/stream.mjs +1132 -0
  243. package/synchronize.cjs +106 -0
  244. package/synchronize.d.ts +54 -0
  245. package/synchronize.mjs +103 -0
  246. package/typing/types.d.ts +24 -0
  247. package/typing.cjs +2 -0
  248. package/typing.mjs +1 -0
  249. package/utils/parser.cjs +37 -0
  250. package/utils/parser.d.ts +7 -0
  251. package/utils/parser.mjs +16 -0
@@ -0,0 +1,518 @@
1
+ import { __awaiter } from '../../_virtual/_tslib.mjs';
2
+ import * as R from 'ramda';
3
+ import { ParsingError } from '../../errors/ParsingError.mjs';
4
+ import { NetworkError } from '../../errors/NetworkError.mjs';
5
+ import { NoSuchElement } from '../../errors/NoSuchElement.mjs';
6
+ import X2JS from 'x2js';
7
+ import 'reflect-metadata';
8
+ import { DataFormat } from './types.mjs';
9
+ import Papa from 'papaparse';
10
+ import { encode, decode } from '@msgpack/msgpack';
11
+ import { parse } from '../../utils/parser.mjs';
12
+
13
+ /** @ignore */
14
+ const namespacedModelInternalProps = new Map();
15
+ /**
16
+ * A container object which may or may not contain a non-null/non-undefined value
17
+ */
18
+ class Optional {
19
+ constructor(value) {
20
+ this.value = value;
21
+ }
22
+ /**
23
+ * Returns an Optional with the specified present non-null value.
24
+ * @param value
25
+ */
26
+ static of(value) {
27
+ return new Optional(value);
28
+ }
29
+ /**
30
+ * Returns an empty Optional instance.
31
+ */
32
+ static empty() {
33
+ return new Optional(undefined);
34
+ }
35
+ /**
36
+ * Return true if there is a value present, otherwise false.
37
+ */
38
+ isPresent() {
39
+ return this.value !== undefined && this.value !== null;
40
+ }
41
+ /**
42
+ * If a value is present in this Optional, returns the value, otherwise throws NoSuchElement.
43
+ */
44
+ get() {
45
+ if (this.isPresent()) {
46
+ return this.value;
47
+ }
48
+ throw new NoSuchElement();
49
+ }
50
+ }
51
+ /**
52
+ * A Data de/serializer class that manages and validates data as JavaScript Objects
53
+ */
54
+ class Model {
55
+ /**
56
+ * Converts arraybuffer to a model
57
+ * @param data arraybuffer
58
+ * @param format data format
59
+ * @param options configurations for deserializing the data
60
+ * @returns new model or an array of models
61
+ */
62
+ static fromBuffer(data, format = DataFormat.JSON, options) {
63
+ switch (format) {
64
+ case DataFormat.MESSAGE_PACK: {
65
+ const decoder = decode;
66
+ const result = decoder(data);
67
+ if ((options === null || options === void 0 ? void 0 : options.array) && !(result instanceof Array || Array.isArray(result)))
68
+ throw new ParsingError([], "expected an array of models");
69
+ if ((!(options === null || options === void 0 ? void 0 : options.array) && result instanceof Array) || Array.isArray(result))
70
+ throw new ParsingError([], "expected only one model");
71
+ if (result instanceof Array || Array.isArray(result))
72
+ return result.map((v) => this.fromJSON(v));
73
+ return this.fromJSON(result);
74
+ }
75
+ case DataFormat.CSV: {
76
+ const text = data instanceof Uint8Array ? new TextDecoder().decode(data.buffer) : data.toString();
77
+ return this.fromString(text, format, options);
78
+ }
79
+ case DataFormat.XML:
80
+ case DataFormat.JSON:
81
+ return this.fromString(data instanceof Uint8Array ? new TextDecoder().decode(data) : data.toString(), format, options);
82
+ }
83
+ }
84
+ /**
85
+ * Downloads data from the given source and deserializes the data to one or more models
86
+ * @param source URL source to fetch the data
87
+ * @param format data format
88
+ * @param options configurations for deserializing and/or retrieving the data
89
+ * @returns one or more models
90
+ */
91
+ static fromURL(source, format = DataFormat.JSON, options) {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ const fetcher = fetch;
94
+ const resp = yield fetcher(source, {
95
+ method: "GET",
96
+ headers: options === null || options === void 0 ? void 0 : options.requestHeaders,
97
+ mode: options === null || options === void 0 ? void 0 : options.mode,
98
+ });
99
+ if (!resp.ok)
100
+ throw new NetworkError(resp.status);
101
+ return yield this.fromBlob(yield resp.blob(), format, options);
102
+ });
103
+ }
104
+ /**
105
+ * Deserializes one or more models from a Blob
106
+ * @param data blob data
107
+ * @param format data format
108
+ * @param options configurations for deserializing the data
109
+ * @returns one or more models
110
+ */
111
+ static fromBlob(data, format = DataFormat.JSON, options) {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ return this.fromBuffer(new Uint8Array(yield data.arrayBuffer()), format);
114
+ });
115
+ }
116
+ /**
117
+ * Deserializes one or more models from given string
118
+ *
119
+ * @remarks
120
+ * If the data format is a binary one, the provided string should be a hexadecimal string.
121
+ * @param data string data source
122
+ * @param format data format
123
+ * @param options
124
+ */
125
+ static fromString(data, format = DataFormat.JSON, options) {
126
+ var _a, _b, _c, _d, _e, _f;
127
+ switch (format) {
128
+ case DataFormat.JSON:
129
+ try {
130
+ data = JSON.parse(data);
131
+ }
132
+ catch (e) {
133
+ throw new ParsingError((_a = e === null || e === void 0 ? void 0 : e.message) !== null && _a !== void 0 ? _a : String(e));
134
+ }
135
+ return this.fromJSON(data);
136
+ case DataFormat.XML:
137
+ return this.fromObject(this.__xmlParser__.xml2js(data), format);
138
+ case DataFormat.CSV: {
139
+ let result;
140
+ let text = data;
141
+ if (options === null || options === void 0 ? void 0 : options.headers)
142
+ text = options.headers.join((_b = options === null || options === void 0 ? void 0 : options.delimiter) !== null && _b !== void 0 ? _b : ",") + ((_c = options.newline) !== null && _c !== void 0 ? _c : "\n") + text;
143
+ try {
144
+ result = Papa.parse(text, {
145
+ header: true,
146
+ skipEmptyLines: true,
147
+ });
148
+ }
149
+ catch (e) {
150
+ throw new ParsingError([], (_d = e === null || e === void 0 ? void 0 : e.message) !== null && _d !== void 0 ? _d : String(e));
151
+ }
152
+ if (!(options === null || options === void 0 ? void 0 : options.ignoreErrors) && result.errors.length > 0)
153
+ throw new ParsingError(result.errors);
154
+ if (!(options === null || options === void 0 ? void 0 : options.array) && result.data.length > 0)
155
+ throw new ParsingError([], "Too many records found");
156
+ if (options === null || options === void 0 ? void 0 : options.array)
157
+ return result.data.map((v) => this.fromObject(v));
158
+ return this.fromObject(result.data[0]);
159
+ }
160
+ default:
161
+ return this.fromBuffer(new Uint8Array((_f = (_e = data.match(/../g)) === null || _e === void 0 ? void 0 : _e.map((h) => parseInt(h, 16))) !== null && _f !== void 0 ? _f : []), format, options);
162
+ }
163
+ }
164
+ /**
165
+ * Converts a JSON object to a model
166
+ * @param data JSON Object
167
+ * @returns new model
168
+ */
169
+ static fromJSON(data) {
170
+ return this.fromObject(data, DataFormat.JSON);
171
+ }
172
+ /**
173
+ * Converts the current model to a JSON object
174
+ */
175
+ object(removeMissingFields = false) {
176
+ var _a, _b;
177
+ const serialize = (value) => {
178
+ if (value instanceof Model) {
179
+ return value.object(removeMissingFields);
180
+ }
181
+ else if (value instanceof Array || Array.isArray(value)) {
182
+ return value.map((v) => serialize(v));
183
+ }
184
+ else if (value instanceof Set) {
185
+ return Array.from(value);
186
+ }
187
+ else if (value instanceof Map) {
188
+ try {
189
+ return Object.fromEntries(value.entries());
190
+ }
191
+ catch (e) {
192
+ // failed to serialize map to object, which means it contains keys that are not serializable to object
193
+ return value;
194
+ }
195
+ }
196
+ else if (value instanceof Function) {
197
+ return null;
198
+ }
199
+ return value;
200
+ };
201
+ return R.reduce((acc, [key, fieldProps]) => {
202
+ var _a;
203
+ if ((_a = fieldProps.ignore) === null || _a === void 0 ? void 0 : _a.serialize)
204
+ return acc;
205
+ const value = R.prop(key, this);
206
+ const serializedValue = serialize(value);
207
+ if (!removeMissingFields || (serializedValue !== null && serializedValue !== undefined)) {
208
+ acc[fieldProps.name] = serializedValue;
209
+ }
210
+ return acc;
211
+ }, {}, Array.from(((_b = (_a = Model.buildPropTree(Object.getPrototypeOf(this))) === null || _a === void 0 ? void 0 : _a.fields) !== null && _b !== void 0 ? _b : new Map()).entries()));
212
+ }
213
+ /**
214
+ * Converts the current model to a JSON string
215
+ */
216
+ json(removeMissingFields = false) {
217
+ return JSON.stringify(this.object(removeMissingFields));
218
+ }
219
+ /**
220
+ * Converts the current model to an arraybuffer
221
+ */
222
+ buffer(removeMissingFields = false) {
223
+ return new TextEncoder().encode(this.json(removeMissingFields));
224
+ }
225
+ /**
226
+ * Converts the current model to a blob
227
+ */
228
+ blob(removeMissingFields = false) {
229
+ return new Blob([this.buffer(removeMissingFields)]);
230
+ }
231
+ /**
232
+ * Converts the current model to an XML string
233
+ */
234
+ xml(removeMissingFields = false) {
235
+ return Model.__xmlParser__.js2xml(this.object(removeMissingFields));
236
+ }
237
+ /**
238
+ * Converts the current model to message pack binary format
239
+ * @returns message pack binary
240
+ */
241
+ messagePack(removeMissingFields = false) {
242
+ return encode(this.object(removeMissingFields)).buffer;
243
+ }
244
+ /**
245
+ * Converts the current model to csv format
246
+ * @param removeMissingFields
247
+ * @param config CSV unparsing configuration
248
+ * @returns csv string
249
+ */
250
+ csv(removeMissingFields = false, config) {
251
+ return Papa.unparse([this.object(removeMissingFields)], config);
252
+ }
253
+ clone() {
254
+ const newModel = new (Object.getPrototypeOf(this))();
255
+ for (const [key, value] of Object.entries(this)) {
256
+ newModel[key] = !R.isNil(value) ? R.clone(value) : value;
257
+ }
258
+ return newModel;
259
+ }
260
+ /**
261
+ * Retrieves the schema of the current model based on the selected format
262
+ * @param format data format
263
+ * @returns the schema for the current model
264
+ */
265
+ schema(format = DataFormat.JSON) {
266
+ return Object.getPrototypeOf(this).schema(format);
267
+ }
268
+ toString() {
269
+ return this.json(true);
270
+ }
271
+ /**
272
+ * Constructs the model properties by traversing the inheritance tree of the current Model being instantiated
273
+ * @param modelPrototype prototype of the model
274
+ * @ignore
275
+ */
276
+ static buildPropTree(modelPrototype) {
277
+ var _a;
278
+ let value;
279
+ let proto = !modelPrototype.name ? modelPrototype.constructor : modelPrototype;
280
+ const tree = { fields: new Map() };
281
+ while (proto && (proto === null || proto === void 0 ? void 0 : proto.name) !== "Function") {
282
+ let space = proto.name;
283
+ tree.fields.clear();
284
+ while (space && (value = namespacedModelInternalProps.get(space))) {
285
+ value.fields.forEach((value, key) => tree.fields.set(key, value));
286
+ space = (_a = value.parent) !== null && _a !== void 0 ? _a : "";
287
+ }
288
+ // could be that subclass didn't use any annotators. check parent
289
+ if (tree.fields.size === 0) {
290
+ proto = Object.getPrototypeOf(proto);
291
+ }
292
+ else {
293
+ break;
294
+ }
295
+ }
296
+ return tree;
297
+ }
298
+ /**
299
+ * Constructs a model from a JSON object
300
+ * @param data JSON object
301
+ * @param format
302
+ * @private
303
+ */
304
+ static fromObject(data, format = DataFormat.JSON) {
305
+ if (data instanceof Array || Array.isArray(data) || typeof data !== "object") {
306
+ throw new ParsingError([], "Invalid data provided. Must be an object");
307
+ }
308
+ const processValue = (fieldProps, value) => {
309
+ var _a, _b;
310
+ if ((_b = (_a = fieldProps.schema) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.useOptionalObj) {
311
+ return Optional.of(value);
312
+ }
313
+ return value;
314
+ };
315
+ const model = new this();
316
+ const props = this.buildPropTree(Object.getPrototypeOf(model));
317
+ R.forEach(([key, fieldProps]) => {
318
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
319
+ let value = data[key];
320
+ if (value === undefined || value === null) {
321
+ for (const alias of (_a = fieldProps.aliases) !== null && _a !== void 0 ? _a : []) {
322
+ value = data[alias];
323
+ if (value !== undefined)
324
+ break;
325
+ }
326
+ }
327
+ if ((value === undefined || value === null) && ((_b = fieldProps.ignore) === null || _b === void 0 ? void 0 : _b.deserialize)) {
328
+ if ((_d = (_c = fieldProps.schema) === null || _c === void 0 ? void 0 : _c.options) === null || _d === void 0 ? void 0 : _d.useOptionalObj) {
329
+ model[key] = Optional.empty();
330
+ }
331
+ return;
332
+ }
333
+ else if ((value === undefined || value === null) && (model[key] === undefined || model[key] === null)) {
334
+ throw new ParsingError([], `Property "${key}" is missing from the data provided. ${JSON.stringify(data)}`);
335
+ }
336
+ else if ((value === undefined || value === null) && !(model[key] === undefined || model[key] === null)) {
337
+ return;
338
+ }
339
+ if ((_e = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(model), key)) === null || _e === void 0 ? void 0 : _e.set) {
340
+ model[key] = processValue(fieldProps, value);
341
+ }
342
+ else if ((_f = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(model), key)) === null || _f === void 0 ? void 0 : _f.get) {
343
+ return;
344
+ }
345
+ else if (model[key] instanceof fieldProps.Type &&
346
+ typeof model[key] === "function" &&
347
+ fieldProps.Type instanceof Function) {
348
+ model[key](value);
349
+ }
350
+ else if (fieldProps.isArray && fieldProps.Type.prototype instanceof Model) {
351
+ const parser = parse({
352
+ string: (v) => fieldProps.Type.fromString(v, format),
353
+ object: (v) => fieldProps.Type.fromObject(v, format),
354
+ default: () => {
355
+ throw new ParsingError([], `value for ${key} is expected to be an Array, instead received ${typeof value}`);
356
+ },
357
+ supportArray: true,
358
+ });
359
+ model[key] = processValue(fieldProps, parser(value));
360
+ }
361
+ else if (fieldProps.Type.prototype instanceof Model) {
362
+ const parser = parse({
363
+ string: (v) => fieldProps.Type.fromString(v, format),
364
+ object: (v) => fieldProps.Type.fromObject(v, format),
365
+ default: () => {
366
+ throw new ParsingError([], `value for ${key} expected to be serializable object`);
367
+ },
368
+ });
369
+ const result = parser(value);
370
+ if (result instanceof Array || Array.isArray(result)) {
371
+ throw new ParsingError([], `value for ${key} expected to be serializable object, not an array`);
372
+ }
373
+ model[key] = processValue(fieldProps, result);
374
+ }
375
+ else if (fieldProps.isMap && fieldProps.ValueType.prototype instanceof Model) {
376
+ const parser = parse({
377
+ string: (v) => fieldProps.ValueType.fromString(v, format),
378
+ object: (v) => fieldProps.ValueType.fromObject(v, format),
379
+ default: () => {
380
+ throw new ParsingError([], `value for ${key} expected to be serializable object`);
381
+ },
382
+ });
383
+ try {
384
+ const data = new Map((!(value instanceof Array || Array.isArray(value)) && typeof value === "object"
385
+ ? Object.entries(value)
386
+ : value).map(([k, v]) => [fieldProps.KeyType(k), v]));
387
+ data.forEach((value, key) => {
388
+ data.set(key, parser(value));
389
+ });
390
+ model[key] = processValue(fieldProps, data);
391
+ }
392
+ catch (e) {
393
+ if (e instanceof ParsingError)
394
+ throw e;
395
+ throw new ParsingError([], (_g = e === null || e === void 0 ? void 0 : e.message) !== null && _g !== void 0 ? _g : String(e));
396
+ }
397
+ }
398
+ else if (fieldProps.isMap) {
399
+ try {
400
+ const data = new Map((!(value instanceof Array || Array.isArray(value)) && typeof value === "object"
401
+ ? Object.entries(value)
402
+ : value).map(([k, v]) => [fieldProps.KeyType(k), v]));
403
+ model[key] = processValue(fieldProps, data);
404
+ data.forEach((value, key) => {
405
+ data.set(key, parse({ default: (v) => new fieldProps.Type(v) }, value));
406
+ });
407
+ }
408
+ catch (e) {
409
+ if (e instanceof ParsingError)
410
+ throw e;
411
+ throw new ParsingError([], (_h = e === null || e === void 0 ? void 0 : e.message) !== null && _h !== void 0 ? _h : String(e));
412
+ }
413
+ }
414
+ else if (fieldProps.isArray) {
415
+ if (typeof value === "string") {
416
+ try {
417
+ value = JSON.parse(value);
418
+ }
419
+ catch (e) {
420
+ throw new ParsingError([], (_j = e === null || e === void 0 ? void 0 : e.message) !== null && _j !== void 0 ? _j : String(e));
421
+ }
422
+ }
423
+ if (!(value instanceof Array || Array.isArray(value))) {
424
+ throw new ParsingError([], `value for ${key} is expected to be an Array, instead received ${typeof value}`);
425
+ }
426
+ model[key] = processValue(fieldProps, value.map((v) => {
427
+ var _a;
428
+ switch (typeof v) {
429
+ case "boolean":
430
+ case "number":
431
+ case "bigint":
432
+ case "undefined":
433
+ case "string":
434
+ return v;
435
+ default: {
436
+ let val;
437
+ try {
438
+ val = new fieldProps.Type(v);
439
+ }
440
+ catch (e) {
441
+ if (e instanceof ParsingError) {
442
+ throw e;
443
+ }
444
+ throw new ParsingError([], (_a = e === null || e === void 0 ? void 0 : e.message) !== null && _a !== void 0 ? _a : String(e));
445
+ }
446
+ if (isNaN(val.getTime())) {
447
+ throw new ParsingError([], "Failed to convert to annotated date type");
448
+ }
449
+ return val;
450
+ }
451
+ }
452
+ }));
453
+ }
454
+ else if (((_k = fieldProps.Type) === null || _k === void 0 ? void 0 : _k.name) === "String") {
455
+ model[key] = processValue(fieldProps, String(value));
456
+ }
457
+ else if (((_l = fieldProps.Type) === null || _l === void 0 ? void 0 : _l.name) === "Boolean") {
458
+ model[key] = processValue(fieldProps, Boolean(value));
459
+ }
460
+ else if (((_m = fieldProps.Type) === null || _m === void 0 ? void 0 : _m.name) === "Number") {
461
+ model[key] = processValue(fieldProps, Number(value));
462
+ }
463
+ else if (((_o = fieldProps.Type) === null || _o === void 0 ? void 0 : _o.name) === "Map") {
464
+ const valueType = typeof value;
465
+ switch (valueType) {
466
+ case "object": {
467
+ try {
468
+ model[key] = processValue(fieldProps, value instanceof Array || Array.isArray(value) ? new Map(value) : new Map(Object.entries(value)));
469
+ }
470
+ catch (e) {
471
+ throw new ParsingError([], (_p = e === null || e === void 0 ? void 0 : e.message) !== null && _p !== void 0 ? _p : String(e));
472
+ }
473
+ break;
474
+ }
475
+ case "string":
476
+ try {
477
+ value = JSON.parse(value);
478
+ model[key] = processValue(fieldProps, value instanceof Array || Array.isArray(value) ? new Map(value) : new Map(Object.entries(value)));
479
+ }
480
+ catch (e) {
481
+ throw new ParsingError([], (_q = e === null || e === void 0 ? void 0 : e.message) !== null && _q !== void 0 ? _q : String(e));
482
+ }
483
+ break;
484
+ default:
485
+ throw new ParsingError([value], `value for ${key} cannot be converted to a Map`);
486
+ }
487
+ }
488
+ else if (((_r = fieldProps.Type) === null || _r === void 0 ? void 0 : _r.name) === "Set") {
489
+ if (value instanceof Array || Array.isArray(value)) {
490
+ model[key] = processValue(fieldProps, new Set(value));
491
+ }
492
+ else if (typeof value === "string") {
493
+ model[key] = processValue(fieldProps, new Set(JSON.parse(value)));
494
+ }
495
+ throw new ParsingError([value], `value for ${key} cannot be converted to a Set`);
496
+ }
497
+ else {
498
+ model[key] = processValue(fieldProps, new fieldProps.Type(value));
499
+ }
500
+ if (fieldProps.readonly) {
501
+ Object.defineProperty(model, key, {
502
+ value: model[key],
503
+ writable: false,
504
+ enumerable: true,
505
+ });
506
+ }
507
+ R.forEach(([k, v]) => {
508
+ if (v.__callback__)
509
+ v.__callback__(k, v.properties, model, key);
510
+ }, R.toPairs((_s = fieldProps.customTags) !== null && _s !== void 0 ? _s : {}));
511
+ }, Array.from(props.fields.entries()));
512
+ return Object.seal(model);
513
+ }
514
+ }
515
+ /** @ignore */
516
+ Model.__xmlParser__ = new X2JS();
517
+
518
+ export { Model, Optional, namespacedModelInternalProps };
@@ -0,0 +1,96 @@
1
+ 'use strict';
2
+
3
+ var R = require('ramda');
4
+ var model = require('./model.cjs');
5
+
6
+ function _interopNamespaceDefault(e) {
7
+ var n = Object.create(null);
8
+ if (e) {
9
+ Object.keys(e).forEach(function (k) {
10
+ if (k !== 'default') {
11
+ var d = Object.getOwnPropertyDescriptor(e, k);
12
+ Object.defineProperty(n, k, d.get ? d : {
13
+ enumerable: true,
14
+ get: function () { return e[k]; }
15
+ });
16
+ }
17
+ });
18
+ }
19
+ n.default = e;
20
+ return Object.freeze(n);
21
+ }
22
+
23
+ var R__namespace = /*#__PURE__*/_interopNamespaceDefault(R);
24
+
25
+ const modelClassTags = {};
26
+ /**
27
+ * Creates a custom class tag
28
+ * @param name Name of the tag
29
+ * @param properties any properties that should be saved
30
+ */
31
+ const createModelClassAnnotationTag = (name, properties = {}) => (constructor) => {
32
+ modelClassTags[name] = {
33
+ properties,
34
+ modelClass: constructor.prototype,
35
+ modelClassName: constructor.name,
36
+ };
37
+ };
38
+ /**
39
+ * Retrieves a list of properties set by one or more custom class tags
40
+ * @param tagNames Array<string>
41
+ * @param ModelClass Class<Model>
42
+ */
43
+ const getModelClassAnnotationTagProperties = (tagNames, ModelClass) => {
44
+ return R__namespace.map((tag) => modelClassTags[tag], tagNames).filter((v) => v);
45
+ };
46
+ /**
47
+ * Creates a custom field tag
48
+ * @param name Name of the tag
49
+ * @param properties any properties that should be saved
50
+ * @param onFieldCreated callback executed once property exist on a model (Optional)
51
+ */
52
+ const createModelFieldAnnotationTag = (name, properties = {}, onFieldCreated) => (target, key) => {
53
+ var _a, _b;
54
+ const props = (_a = model.namespacedModelInternalProps.get(target.constructor.name)) !== null && _a !== void 0 ? _a : {
55
+ fields: new Map(),
56
+ };
57
+ const result = R__namespace.find(([k, v]) => v.name === key, Array.from(props.fields.entries()));
58
+ if (!result) {
59
+ throw new Error(`Cannot create annotation tag of type ${name}, field does not exist as yet`);
60
+ }
61
+ props.fields.get(result[0]).customTags = (_b = props.fields.get(result[0]).customTags) !== null && _b !== void 0 ? _b : {};
62
+ props.fields.get(result[0]).customTags[name] = {
63
+ __callback__: onFieldCreated,
64
+ properties,
65
+ };
66
+ props.parent = Object.getPrototypeOf(target).constructor.name;
67
+ model.namespacedModelInternalProps.set(target.constructor.name, props);
68
+ };
69
+ /**
70
+ * Retrieves a list of properties set by one or more custom field tags
71
+ * @param tagNames Array<String>
72
+ * @param ModelClass Class<Model>
73
+ */
74
+ const getModelFieldAnnotationTagProperties = (tagNames, ModelClass) => {
75
+ const props = model.Model.buildPropTree(ModelClass);
76
+ return R__namespace.map(([k, v]) => {
77
+ if (v.customTags) {
78
+ const tagName = tagNames.find((t) => t in v.customTags);
79
+ return tagName
80
+ ? {
81
+ tagName,
82
+ fieldName: v.name,
83
+ Type: v.Type,
84
+ isArray: v.isArray,
85
+ tagProperties: v.customTags[tagName].properties,
86
+ }
87
+ : null;
88
+ }
89
+ return null;
90
+ }, Array.from(props.fields.entries())).filter((v) => v);
91
+ };
92
+
93
+ exports.createModelClassAnnotationTag = createModelClassAnnotationTag;
94
+ exports.createModelFieldAnnotationTag = createModelFieldAnnotationTag;
95
+ exports.getModelClassAnnotationTagProperties = getModelClassAnnotationTagProperties;
96
+ exports.getModelFieldAnnotationTagProperties = getModelFieldAnnotationTagProperties;
@@ -0,0 +1,38 @@
1
+ import { Model } from "./model";
2
+ export interface FieldTagPropertyDescription {
3
+ Type: any;
4
+ fieldName: string;
5
+ isArray: boolean;
6
+ tagName: string;
7
+ tagProperties: Object;
8
+ }
9
+ export interface ModelTagPropertyDescription {
10
+ properties: Object;
11
+ modelClassName: string;
12
+ modelClass: any;
13
+ }
14
+ /**
15
+ * Creates a custom class tag
16
+ * @param name Name of the tag
17
+ * @param properties any properties that should be saved
18
+ */
19
+ export declare const createModelClassAnnotationTag: (name: string, properties?: Object) => (constructor: any) => void;
20
+ /**
21
+ * Retrieves a list of properties set by one or more custom class tags
22
+ * @param tagNames Array<string>
23
+ * @param ModelClass Class<Model>
24
+ */
25
+ export declare const getModelClassAnnotationTagProperties: (tagNames: string[], ModelClass: any) => ModelTagPropertyDescription[];
26
+ /**
27
+ * Creates a custom field tag
28
+ * @param name Name of the tag
29
+ * @param properties any properties that should be saved
30
+ * @param onFieldCreated callback executed once property exist on a model (Optional)
31
+ */
32
+ export declare const createModelFieldAnnotationTag: (name: string, properties?: Object, onFieldCreated?: (tagName: string, properties: Object, target: Model, fieldName: string) => void) => (target: any, key: string) => void;
33
+ /**
34
+ * Retrieves a list of properties set by one or more custom field tags
35
+ * @param tagNames Array<String>
36
+ * @param ModelClass Class<Model>
37
+ */
38
+ export declare const getModelFieldAnnotationTagProperties: (tagNames: string[], ModelClass: any) => FieldTagPropertyDescription[];