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