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,72 @@
1
+ import * as R from 'ramda';
2
+ import { namespacedModelInternalProps, Model } from './model.mjs';
3
+
4
+ const modelClassTags = {};
5
+ /**
6
+ * Creates a custom class tag
7
+ * @param name Name of the tag
8
+ * @param properties any properties that should be saved
9
+ */
10
+ const createModelClassAnnotationTag = (name, properties = {}) => (constructor) => {
11
+ modelClassTags[name] = {
12
+ properties,
13
+ modelClass: constructor.prototype,
14
+ modelClassName: constructor.name,
15
+ };
16
+ };
17
+ /**
18
+ * Retrieves a list of properties set by one or more custom class tags
19
+ * @param tagNames Array<string>
20
+ * @param ModelClass Class<Model>
21
+ */
22
+ const getModelClassAnnotationTagProperties = (tagNames, ModelClass) => {
23
+ return R.map((tag) => modelClassTags[tag], tagNames).filter((v) => v);
24
+ };
25
+ /**
26
+ * Creates a custom field tag
27
+ * @param name Name of the tag
28
+ * @param properties any properties that should be saved
29
+ * @param onFieldCreated callback executed once property exist on a model (Optional)
30
+ */
31
+ const createModelFieldAnnotationTag = (name, properties = {}, onFieldCreated) => (target, key) => {
32
+ var _a, _b;
33
+ const props = (_a = namespacedModelInternalProps.get(target.constructor.name)) !== null && _a !== void 0 ? _a : {
34
+ fields: new Map(),
35
+ };
36
+ const result = R.find(([k, v]) => v.name === key, Array.from(props.fields.entries()));
37
+ if (!result) {
38
+ throw new Error(`Cannot create annotation tag of type ${name}, field does not exist as yet`);
39
+ }
40
+ props.fields.get(result[0]).customTags = (_b = props.fields.get(result[0]).customTags) !== null && _b !== void 0 ? _b : {};
41
+ props.fields.get(result[0]).customTags[name] = {
42
+ __callback__: onFieldCreated,
43
+ properties,
44
+ };
45
+ props.parent = Object.getPrototypeOf(target).constructor.name;
46
+ namespacedModelInternalProps.set(target.constructor.name, props);
47
+ };
48
+ /**
49
+ * Retrieves a list of properties set by one or more custom field tags
50
+ * @param tagNames Array<String>
51
+ * @param ModelClass Class<Model>
52
+ */
53
+ const getModelFieldAnnotationTagProperties = (tagNames, ModelClass) => {
54
+ const props = Model.buildPropTree(ModelClass);
55
+ return R.map(([k, v]) => {
56
+ if (v.customTags) {
57
+ const tagName = tagNames.find((t) => t in v.customTags);
58
+ return tagName
59
+ ? {
60
+ tagName,
61
+ fieldName: v.name,
62
+ Type: v.Type,
63
+ isArray: v.isArray,
64
+ tagProperties: v.customTags[tagName].properties,
65
+ }
66
+ : null;
67
+ }
68
+ return null;
69
+ }, Array.from(props.fields.entries())).filter((v) => v);
70
+ };
71
+
72
+ export { createModelClassAnnotationTag, createModelFieldAnnotationTag, getModelClassAnnotationTagProperties, getModelFieldAnnotationTagProperties };
@@ -0,0 +1,310 @@
1
+ 'use strict';
2
+
3
+ var R = require('ramda');
4
+ var model = require('./model.cjs');
5
+ var InvalidType = require('../../errors/InvalidType.cjs');
6
+ var InvalidValue = require('../../errors/InvalidValue.cjs');
7
+ var types = require('./types.cjs');
8
+
9
+ function _interopNamespaceDefault(e) {
10
+ var n = Object.create(null);
11
+ if (e) {
12
+ Object.keys(e).forEach(function (k) {
13
+ if (k !== 'default') {
14
+ var d = Object.getOwnPropertyDescriptor(e, k);
15
+ Object.defineProperty(n, k, d.get ? d : {
16
+ enumerable: true,
17
+ get: function () { return e[k]; }
18
+ });
19
+ }
20
+ });
21
+ }
22
+ n.default = e;
23
+ return Object.freeze(n);
24
+ }
25
+
26
+ var R__namespace = /*#__PURE__*/_interopNamespaceDefault(R);
27
+
28
+ /**
29
+ * Creates a decorator that updates a field
30
+ * @param functor updater function
31
+ * @returns a decorator
32
+ */
33
+ const createFieldUpdater = (functor) => (target, key) => {
34
+ var _a;
35
+ const props = (_a = model.namespacedModelInternalProps.get(target.constructor.name)) !== null && _a !== void 0 ? _a : {
36
+ fields: new Map(),
37
+ };
38
+ const field = R__namespace.find((v) => v === key, Array.from(props.fields.keys()));
39
+ if (!field) {
40
+ throw new Error(`No field found that matches ${key}`);
41
+ }
42
+ functor({ field: props.fields.get(field), target, key });
43
+ props.parent = Object.getPrototypeOf(target).constructor.name;
44
+ model.namespacedModelInternalProps.set(target.constructor.name, props);
45
+ };
46
+ /**
47
+ * Creates a validator decorator
48
+ * @param functor validation function
49
+ * @param error error to be thrown when validation fails
50
+ * @param updater optional argument to update the current field
51
+ * @returns a decorator
52
+ */
53
+ const createValidator = (functor, error, updater) => createFieldUpdater(({ field, target, key }) => {
54
+ const symbol = Symbol(key);
55
+ Object.defineProperty(target, key, {
56
+ get: function () {
57
+ return this[symbol];
58
+ },
59
+ set: function (v) {
60
+ if (v instanceof model.Optional) {
61
+ if (v.isPresent()) {
62
+ v = v.get();
63
+ }
64
+ else {
65
+ this[symbol] = v;
66
+ return;
67
+ }
68
+ }
69
+ if (!functor(v))
70
+ throw error;
71
+ this[symbol] = v;
72
+ },
73
+ });
74
+ if (updater)
75
+ updater(field);
76
+ });
77
+ /**
78
+ * A property that exist in the incoming data
79
+ * @param name Name of the property in the incoming data. If not provided, the variable name will be assumed as the
80
+ * property name
81
+ * @param type type of the field. Not required (useful when the field is an Optional)
82
+ * @constructor
83
+ */
84
+ const Field = (name, type = undefined) => (target, key) => {
85
+ var _a, _b, _c, _d;
86
+ const schema = {};
87
+ const props = (_a = model.namespacedModelInternalProps.get(target.constructor.name)) !== null && _a !== void 0 ? _a : {
88
+ fields: new Map(),
89
+ };
90
+ const dtype = Reflect.getMetadata("design:type", target, key);
91
+ const fieldProp = (_b = props.fields.get(key)) !== null && _b !== void 0 ? _b : {};
92
+ if (type && dtype === model.Optional) {
93
+ schema.options = { useOptionalObj: true };
94
+ fieldProp.ignore = Object.assign(Object.assign({}, ((_c = fieldProp.ignore) !== null && _c !== void 0 ? _c : {})), { deserialize: true });
95
+ }
96
+ else if (!type) {
97
+ type = dtype;
98
+ }
99
+ else if (!(dtype instanceof type)) {
100
+ throw new InvalidType.InvalidType(`Type mismatch for property ${key}`);
101
+ }
102
+ if (type instanceof Number) {
103
+ schema.dataType = types.DataType.DOUBLE;
104
+ }
105
+ else if (type instanceof String) {
106
+ schema.dataType = types.DataType.STRING;
107
+ }
108
+ else if (type instanceof Boolean) {
109
+ schema.dataType = types.DataType.BOOLEAN;
110
+ }
111
+ else if (type instanceof model.Model) {
112
+ schema.dataType = types.DataType.RECORD;
113
+ schema.options = Object.assign(Object.assign({}, ((_d = schema.options) !== null && _d !== void 0 ? _d : {})), { recordClass: type });
114
+ }
115
+ fieldProp.name = name !== null && name !== void 0 ? name : key;
116
+ fieldProp.Type = type;
117
+ fieldProp.isArray = false;
118
+ fieldProp.schema = schema;
119
+ props.fields.set(key, fieldProp);
120
+ if (name) {
121
+ if (fieldProp.aliases) {
122
+ fieldProp.aliases.push(name);
123
+ }
124
+ else {
125
+ fieldProp.aliases = [name];
126
+ }
127
+ }
128
+ props.parent = Object.getPrototypeOf(target).constructor.name;
129
+ model.namespacedModelInternalProps.set(target.constructor.name, props);
130
+ };
131
+ /**
132
+ * A property that exists on the incoming data, that contains an array of same data types
133
+ * @param type Type of data in the array
134
+ * @param name Name of the property in the incoming data. If not provided, the variable name will be assumed as the
135
+ * property name
136
+ * @constructor
137
+ */
138
+ const ArrayField = (type, name) => (target, key) => {
139
+ var _a, _b;
140
+ const props = (_a = model.namespacedModelInternalProps.get(target.constructor.name)) !== null && _a !== void 0 ? _a : {
141
+ fields: new Map(),
142
+ };
143
+ const schema = { dataType: types.DataType.ARRAY };
144
+ const dtype = Reflect.getMetadata("design:type", target, key);
145
+ const optional = dtype === model.Optional;
146
+ schema.options = { useOptionalObj: optional, optional };
147
+ if (type instanceof Number) {
148
+ schema.itemType = types.DataType.DOUBLE;
149
+ }
150
+ else if (type instanceof String) {
151
+ schema.itemType = types.DataType.STRING;
152
+ }
153
+ else if (type instanceof Boolean) {
154
+ schema.itemType = types.DataType.BOOLEAN;
155
+ }
156
+ else if (type instanceof model.Model) {
157
+ schema.options.recordClass = type;
158
+ }
159
+ const fieldProp = (_b = props.fields.get(key)) !== null && _b !== void 0 ? _b : {};
160
+ fieldProp.name = key;
161
+ fieldProp.Type = type;
162
+ fieldProp.isArray = true;
163
+ fieldProp.schema = schema;
164
+ props.fields.set(key, fieldProp);
165
+ if (name) {
166
+ if (fieldProp.aliases) {
167
+ fieldProp.aliases.push(name);
168
+ }
169
+ else {
170
+ fieldProp.aliases = [name];
171
+ }
172
+ }
173
+ props.parent = Object.getPrototypeOf(target).constructor.name;
174
+ model.namespacedModelInternalProps.set(target.constructor.name, props);
175
+ };
176
+ /**
177
+ * A property that exists on the incoming data, that contains an object/array that can be represented as a hashmap
178
+ * @param keyType type for the keys in the map
179
+ * @param valueType type for the values in the map
180
+ * @param name optional name of the field
181
+ * @constructor
182
+ */
183
+ const MapField = (keyType, valueType, name) => (target, key) => {
184
+ var _a, _b;
185
+ const props = (_a = model.namespacedModelInternalProps.get(target.constructor.name)) !== null && _a !== void 0 ? _a : {
186
+ fields: new Map(),
187
+ };
188
+ const schema = { dataType: types.DataType.MAP };
189
+ const dtype = Reflect.getMetadata("design:type", target, key);
190
+ const optional = dtype === model.Optional;
191
+ schema.options = { useOptionalObj: optional, optional };
192
+ const fieldProp = (_b = props.fields.get(key)) !== null && _b !== void 0 ? _b : {};
193
+ fieldProp.name = key;
194
+ fieldProp.Type = Map;
195
+ fieldProp.isMap = true;
196
+ fieldProp.KeyType = keyType;
197
+ fieldProp.ValueType = valueType;
198
+ fieldProp.schema = schema;
199
+ props.fields.set(key, fieldProp);
200
+ if (name) {
201
+ props.fields.set(name, fieldProp);
202
+ }
203
+ props.parent = Object.getPrototypeOf(target).constructor.name;
204
+ model.namespacedModelInternalProps.set(target.constructor.name, props);
205
+ };
206
+ /**
207
+ * Alternative name for the associated field
208
+ * @param name
209
+ */
210
+ const Alias = (name) => createFieldUpdater(({ field }) => {
211
+ var _a;
212
+ const aliases = (_a = field.aliases) !== null && _a !== void 0 ? _a : [];
213
+ aliases.push(name);
214
+ field.aliases = aliases;
215
+ });
216
+ /**
217
+ * Makes the given property only readable
218
+ * @constructor
219
+ */
220
+ const Readonly = (target, key) => {
221
+ var _a, _b;
222
+ const props = (_a = model.namespacedModelInternalProps.get(target.constructor.name)) !== null && _a !== void 0 ? _a : {
223
+ fields: new Map(),
224
+ };
225
+ const fieldProps = (_b = props.fields.get(key)) !== null && _b !== void 0 ? _b : {};
226
+ fieldProps.readonly = true;
227
+ props.fields.set(key, fieldProps);
228
+ props.parent = Object.getPrototypeOf(target).constructor.name;
229
+ model.namespacedModelInternalProps.set(target.constructor.name, props);
230
+ };
231
+ /**
232
+ * Validates the value assigned to the associated property
233
+ * @param matcher functor to check if the assigned value is valid
234
+ */
235
+ const Validator = (matcher) => createValidator(matcher instanceof Function ? matcher : matcher instanceof RegExp ? (v) => matcher.test(v) : (v) => v === matcher, new InvalidValue.InvalidValue(`Invalid value assigned`));
236
+ /**
237
+ * Raises an error if the field contains an error
238
+ * @constructor
239
+ */
240
+ const RaiseError = createFieldUpdater(({ field, target, key }) => {
241
+ const symbol = Symbol(key);
242
+ Object.defineProperty(target, key, {
243
+ get: function () {
244
+ return this[symbol];
245
+ },
246
+ set: function (v) {
247
+ if (v) {
248
+ throw v instanceof Error ? v : new Error(v);
249
+ }
250
+ this[symbol] = v;
251
+ },
252
+ });
253
+ });
254
+ /**
255
+ * Ignores the annotated field during deserialization (by default) and/or serialization
256
+ * @param value an object for enabling/disabling which directions should ignore be used
257
+ * @constructor
258
+ */
259
+ const Ignore = (value = { serialize: false, deserialize: true }) => createFieldUpdater(({ field }) => {
260
+ var _a, _b;
261
+ field.ignore = value;
262
+ if (value.serialize)
263
+ delete field.schema;
264
+ else if (field.schema) {
265
+ const options = (_b = (_a = field.schema) === null || _a === void 0 ? void 0 : _a.options) !== null && _b !== void 0 ? _b : {};
266
+ options.optional = true;
267
+ field.schema.options = options;
268
+ }
269
+ });
270
+ /**
271
+ * Registers a schema type for the given field
272
+ * @param dataType
273
+ * @param options
274
+ */
275
+ const SchemaType = (dataType, options) => createFieldUpdater(({ field }) => (field.schema = {
276
+ dataType,
277
+ options,
278
+ }));
279
+ /**
280
+ * Decorator that applies a lower limit validation on a numeric field
281
+ * @param value limit
282
+ * @param inclusive should the limit be included. Default is false
283
+ */
284
+ const LowerBound = (value, inclusive = true) => createValidator(inclusive ? (v) => v >= value : (v) => v > value, new InvalidValue.InvalidValue(`value is lower than the minimum value of ${value}`));
285
+ /**
286
+ * Decorator that applies an upper limit validation on a numeric field
287
+ * @param value limit
288
+ * @param inclusive should the limit be included. Default is false
289
+ */
290
+ const UpperBound = (value, inclusive = false) => createValidator(inclusive ? (v) => v <= value : (v) => v < value, new InvalidValue.InvalidValue(`value is higher than the maximum value of ${value}`));
291
+ /**
292
+ * Decorator that applies a range validation on a numeric field
293
+ * @param lower lower bound
294
+ * @param upper upper bound
295
+ * @param inclusive should the upper bound be included. Default is false
296
+ */
297
+ const Range = (lower, upper, inclusive = false) => createValidator(inclusive ? (v) => lower <= v && v <= upper : (v) => lower <= v && v < upper, new InvalidValue.InvalidValue(`value is not within the range of ${lower} <= v ` + (inclusive ? `<= ${upper}` : `< ${upper}`)));
298
+
299
+ exports.Alias = Alias;
300
+ exports.ArrayField = ArrayField;
301
+ exports.Field = Field;
302
+ exports.Ignore = Ignore;
303
+ exports.LowerBound = LowerBound;
304
+ exports.MapField = MapField;
305
+ exports.RaiseError = RaiseError;
306
+ exports.Range = Range;
307
+ exports.Readonly = Readonly;
308
+ exports.SchemaType = SchemaType;
309
+ exports.UpperBound = UpperBound;
310
+ exports.Validator = Validator;
@@ -0,0 +1,83 @@
1
+ import { IgnoreProps } from "./model";
2
+ import { DataType } from "./types";
3
+ /**
4
+ * A property that exist in the incoming data
5
+ * @param name Name of the property in the incoming data. If not provided, the variable name will be assumed as the
6
+ * property name
7
+ * @param type type of the field. Not required (useful when the field is an Optional)
8
+ * @constructor
9
+ */
10
+ export declare const Field: (name?: string, type?: any) => (target: any, key: string) => void;
11
+ /**
12
+ * A property that exists on the incoming data, that contains an array of same data types
13
+ * @param type Type of data in the array
14
+ * @param name Name of the property in the incoming data. If not provided, the variable name will be assumed as the
15
+ * property name
16
+ * @constructor
17
+ */
18
+ export declare const ArrayField: (type: any, name?: string) => (target: any, key: string) => void;
19
+ /**
20
+ * A property that exists on the incoming data, that contains an object/array that can be represented as a hashmap
21
+ * @param keyType type for the keys in the map
22
+ * @param valueType type for the values in the map
23
+ * @param name optional name of the field
24
+ * @constructor
25
+ */
26
+ export declare const MapField: (keyType: any, valueType: any, name?: string) => (target: any, key: string) => void;
27
+ /**
28
+ * Alternative name for the associated field
29
+ * @param name
30
+ */
31
+ export declare const Alias: (name: string) => (target: any, key: string) => void;
32
+ /**
33
+ * Makes the given property only readable
34
+ * @constructor
35
+ */
36
+ export declare const Readonly: (target: any, key: string) => void;
37
+ /**
38
+ * Validates the value assigned to the associated property
39
+ * @param matcher functor to check if the assigned value is valid
40
+ */
41
+ export declare const Validator: (matcher: string | number | boolean | RegExp | ((v: any) => boolean)) => (target: any, key: string) => void;
42
+ /**
43
+ * Raises an error if the field contains an error
44
+ * @constructor
45
+ */
46
+ export declare const RaiseError: (target: any, key: string) => void;
47
+ /**
48
+ * Ignores the annotated field during deserialization (by default) and/or serialization
49
+ * @param value an object for enabling/disabling which directions should ignore be used
50
+ * @constructor
51
+ */
52
+ export declare const Ignore: (value?: IgnoreProps) => (target: any, key: string) => void;
53
+ /**
54
+ * Registers a schema type for the given field
55
+ * @param dataType
56
+ * @param options
57
+ */
58
+ export declare const SchemaType: (dataType: DataType, options?: {
59
+ values?: string[];
60
+ length?: number;
61
+ itemType?: string;
62
+ recordClass?: any;
63
+ optional?: boolean;
64
+ }) => (target: any, key: string) => void;
65
+ /**
66
+ * Decorator that applies a lower limit validation on a numeric field
67
+ * @param value limit
68
+ * @param inclusive should the limit be included. Default is false
69
+ */
70
+ export declare const LowerBound: (value: number, inclusive?: boolean) => (target: any, key: string) => void;
71
+ /**
72
+ * Decorator that applies an upper limit validation on a numeric field
73
+ * @param value limit
74
+ * @param inclusive should the limit be included. Default is false
75
+ */
76
+ export declare const UpperBound: (value: number, inclusive?: boolean) => (target: any, key: string) => void;
77
+ /**
78
+ * Decorator that applies a range validation on a numeric field
79
+ * @param lower lower bound
80
+ * @param upper upper bound
81
+ * @param inclusive should the upper bound be included. Default is false
82
+ */
83
+ export declare const Range: (lower: number, upper: number, inclusive?: boolean) => (target: any, key: string) => void;