@dipscope/type-manager 5.0.0 → 6.0.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 (751) hide show
  1. package/CHANGELOG.md +247 -224
  2. package/CONTRIBUTING.md +58 -0
  3. package/LICENSE.md +201 -201
  4. package/README.md +1887 -1857
  5. package/dist/es2015/alias.js +2 -0
  6. package/dist/es2015/alias.js.map +1 -0
  7. package/dist/es2015/any.js +3 -0
  8. package/dist/es2015/any.js.map +1 -0
  9. package/dist/es2015/custom-data.js +2 -0
  10. package/dist/es2015/custom-data.js.map +1 -0
  11. package/dist/es2015/default-value-resolver.js +2 -0
  12. package/dist/es2015/default-value-resolver.js.map +1 -0
  13. package/dist/es2015/default-value.js +2 -0
  14. package/dist/es2015/default-value.js.map +1 -0
  15. package/dist/es2015/discriminant.js +2 -0
  16. package/dist/es2015/discriminant.js.map +1 -0
  17. package/dist/es2015/discriminator.js +2 -0
  18. package/dist/es2015/discriminator.js.map +1 -0
  19. package/dist/es2015/factories/index.js +2 -0
  20. package/dist/es2015/factories/index.js.map +1 -0
  21. package/dist/es2015/factories/type-factory.js +34 -0
  22. package/dist/es2015/factories/type-factory.js.map +1 -0
  23. package/dist/es2015/factory.js +2 -0
  24. package/dist/es2015/factory.js.map +1 -0
  25. package/dist/es2015/functions/get-own-reflect-metadata.js +8 -0
  26. package/dist/es2015/functions/get-own-reflect-metadata.js.map +1 -0
  27. package/dist/es2015/functions/get-reflect-metadata.js +8 -0
  28. package/dist/es2015/functions/get-reflect-metadata.js.map +1 -0
  29. package/dist/es2015/functions/get-words.js +5 -0
  30. package/dist/es2015/functions/get-words.js.map +1 -0
  31. package/{functions/index.d.ts → dist/es2015/functions/index.js} +19 -16
  32. package/dist/es2015/functions/index.js.map +1 -0
  33. package/dist/es2015/functions/is-arrow-function.js +5 -0
  34. package/dist/es2015/functions/is-arrow-function.js.map +1 -0
  35. package/dist/es2015/functions/is-ctor-function.js +5 -0
  36. package/dist/es2015/functions/is-ctor-function.js.map +1 -0
  37. package/dist/es2015/functions/is-data-view.js +4 -0
  38. package/dist/es2015/functions/is-data-view.js.map +1 -0
  39. package/dist/es2015/functions/is-float-32-array.js +4 -0
  40. package/dist/es2015/functions/is-float-32-array.js.map +1 -0
  41. package/dist/es2015/functions/is-float-64-array.js +4 -0
  42. package/dist/es2015/functions/is-float-64-array.js.map +1 -0
  43. package/dist/es2015/functions/is-int-16-array.js +4 -0
  44. package/dist/es2015/functions/is-int-16-array.js.map +1 -0
  45. package/dist/es2015/functions/is-int-32-array.js +4 -0
  46. package/dist/es2015/functions/is-int-32-array.js.map +1 -0
  47. package/dist/es2015/functions/is-int-8-array.js +4 -0
  48. package/dist/es2015/functions/is-int-8-array.js.map +1 -0
  49. package/dist/es2015/functions/is-uint-16-array.js +4 -0
  50. package/dist/es2015/functions/is-uint-16-array.js.map +1 -0
  51. package/dist/es2015/functions/is-uint-32-array.js +4 -0
  52. package/dist/es2015/functions/is-uint-32-array.js.map +1 -0
  53. package/dist/es2015/functions/is-uint-8-array.js +4 -0
  54. package/dist/es2015/functions/is-uint-8-array.js.map +1 -0
  55. package/dist/es2015/functions/is-uint-8-clamped-array.js +4 -0
  56. package/dist/es2015/functions/is-uint-8-clamped-array.js.map +1 -0
  57. package/dist/es2015/functions/json-parse.js +4 -0
  58. package/dist/es2015/functions/json-parse.js.map +1 -0
  59. package/dist/es2015/functions/json-stringify.js +67 -0
  60. package/dist/es2015/functions/json-stringify.js.map +1 -0
  61. package/dist/es2015/functions/name-of.js +8 -0
  62. package/dist/es2015/functions/name-of.js.map +1 -0
  63. package/dist/es2015/generic-argument.js +2 -0
  64. package/dist/es2015/generic-argument.js.map +1 -0
  65. package/dist/es2015/generic-metadata-resolver.js +2 -0
  66. package/dist/es2015/generic-metadata-resolver.js.map +1 -0
  67. package/dist/es2015/generic-metadata.js +2 -0
  68. package/dist/es2015/generic-metadata.js.map +1 -0
  69. package/dist/es2015/generic-structure.js +2 -0
  70. package/dist/es2015/generic-structure.js.map +1 -0
  71. package/{index.d.ts → dist/es2015/index.js} +67 -54
  72. package/dist/es2015/index.js.map +1 -0
  73. package/dist/es2015/inject-index.js +2 -0
  74. package/dist/es2015/inject-index.js.map +1 -0
  75. package/dist/es2015/inject-internals.js +2 -0
  76. package/dist/es2015/inject-internals.js.map +1 -0
  77. package/dist/es2015/inject-metadata.js +63 -0
  78. package/dist/es2015/inject-metadata.js.map +1 -0
  79. package/dist/es2015/inject-options.js +2 -0
  80. package/dist/es2015/inject-options.js.map +1 -0
  81. package/dist/es2015/inject-sorter.js +2 -0
  82. package/dist/es2015/inject-sorter.js.map +1 -0
  83. package/dist/es2015/inject-sorters/asc-inject-sorter.js +14 -0
  84. package/dist/es2015/inject-sorters/asc-inject-sorter.js.map +1 -0
  85. package/dist/es2015/inject-sorters/desc-inject-sorter.js +14 -0
  86. package/dist/es2015/inject-sorters/desc-inject-sorter.js.map +1 -0
  87. package/dist/es2015/inject-sorters/index.js +3 -0
  88. package/dist/es2015/inject-sorters/index.js.map +1 -0
  89. package/dist/es2015/inject.js +24 -0
  90. package/dist/es2015/inject.js.map +1 -0
  91. package/dist/es2015/injector.js +2 -0
  92. package/dist/es2015/injector.js.map +1 -0
  93. package/{injectors/index.d.ts → dist/es2015/injectors/index.js} +2 -1
  94. package/dist/es2015/injectors/index.js.map +1 -0
  95. package/dist/es2015/injectors/singleton-injector.js +31 -0
  96. package/dist/es2015/injectors/singleton-injector.js.map +1 -0
  97. package/dist/es2015/log-level.js +8 -0
  98. package/dist/es2015/log-level.js.map +1 -0
  99. package/dist/es2015/log.js +50 -0
  100. package/dist/es2015/log.js.map +1 -0
  101. package/dist/es2015/metadata.js +81 -0
  102. package/dist/es2015/metadata.js.map +1 -0
  103. package/dist/es2015/naming-convention.js +2 -0
  104. package/dist/es2015/naming-convention.js.map +1 -0
  105. package/dist/es2015/naming-conventions/camel-case-naming-convention.js +17 -0
  106. package/dist/es2015/naming-conventions/camel-case-naming-convention.js.map +1 -0
  107. package/dist/es2015/naming-conventions/flat-case-naming-convention.js +13 -0
  108. package/dist/es2015/naming-conventions/flat-case-naming-convention.js.map +1 -0
  109. package/dist/es2015/naming-conventions/flat-upper-case-naming-convention.js +13 -0
  110. package/dist/es2015/naming-conventions/flat-upper-case-naming-convention.js.map +1 -0
  111. package/{naming-conventions/index.d.ts → dist/es2015/naming-conventions/index.js} +9 -8
  112. package/dist/es2015/naming-conventions/index.js.map +1 -0
  113. package/dist/es2015/naming-conventions/kebab-case-naming-convention.js +17 -0
  114. package/dist/es2015/naming-conventions/kebab-case-naming-convention.js.map +1 -0
  115. package/dist/es2015/naming-conventions/kebab-upper-case-naming-convention.js +17 -0
  116. package/dist/es2015/naming-conventions/kebab-upper-case-naming-convention.js.map +1 -0
  117. package/dist/es2015/naming-conventions/pascal-case-naming-convention.js +18 -0
  118. package/dist/es2015/naming-conventions/pascal-case-naming-convention.js.map +1 -0
  119. package/dist/es2015/naming-conventions/snake-case-naming-convention.js +17 -0
  120. package/dist/es2015/naming-conventions/snake-case-naming-convention.js.map +1 -0
  121. package/dist/es2015/naming-conventions/snake-upper-case-naming-convention.js +17 -0
  122. package/dist/es2015/naming-conventions/snake-upper-case-naming-convention.js.map +1 -0
  123. package/dist/es2015/property-internals.js +2 -0
  124. package/dist/es2015/property-internals.js.map +1 -0
  125. package/dist/es2015/property-metadata.js +259 -0
  126. package/dist/es2015/property-metadata.js.map +1 -0
  127. package/dist/es2015/property-name.js +2 -0
  128. package/dist/es2015/property-name.js.map +1 -0
  129. package/dist/es2015/property-options.js +2 -0
  130. package/dist/es2015/property-options.js.map +1 -0
  131. package/dist/es2015/property-sorter.js +2 -0
  132. package/dist/es2015/property-sorter.js.map +1 -0
  133. package/dist/es2015/property-sorters/asc-property-sorter.js +14 -0
  134. package/dist/es2015/property-sorters/asc-property-sorter.js.map +1 -0
  135. package/dist/es2015/property-sorters/desc-property-sorter.js +14 -0
  136. package/dist/es2015/property-sorters/desc-property-sorter.js.map +1 -0
  137. package/dist/es2015/property-sorters/index.js +3 -0
  138. package/dist/es2015/property-sorters/index.js.map +1 -0
  139. package/dist/es2015/property.js +39 -0
  140. package/dist/es2015/property.js.map +1 -0
  141. package/dist/es2015/reference-callback.js +2 -0
  142. package/dist/es2015/reference-callback.js.map +1 -0
  143. package/dist/es2015/reference-handler.js +2 -0
  144. package/dist/es2015/reference-handler.js.map +1 -0
  145. package/dist/es2015/reference-handlers/circular-reference-handler.js +36 -0
  146. package/dist/es2015/reference-handlers/circular-reference-handler.js.map +1 -0
  147. package/dist/es2015/reference-handlers/index.js +4 -0
  148. package/dist/es2015/reference-handlers/index.js.map +1 -0
  149. package/dist/es2015/reference-handlers/json-path-reference-handler.js +69 -0
  150. package/dist/es2015/reference-handlers/json-path-reference-handler.js.map +1 -0
  151. package/dist/es2015/reference-handlers/plain-reference-handler.js +32 -0
  152. package/dist/es2015/reference-handlers/plain-reference-handler.js.map +1 -0
  153. package/dist/es2015/reference-key.js +2 -0
  154. package/dist/es2015/reference-key.js.map +1 -0
  155. package/dist/es2015/reference-value-getter.js +2 -0
  156. package/dist/es2015/reference-value-getter.js.map +1 -0
  157. package/dist/es2015/reference-value-setter.js +2 -0
  158. package/dist/es2015/reference-value-setter.js.map +1 -0
  159. package/dist/es2015/reference-value.js +2 -0
  160. package/dist/es2015/reference-value.js.map +1 -0
  161. package/dist/es2015/serializer-context-options.js +2 -0
  162. package/dist/es2015/serializer-context-options.js.map +1 -0
  163. package/dist/es2015/serializer-context.js +295 -0
  164. package/dist/es2015/serializer-context.js.map +1 -0
  165. package/dist/es2015/serializer.js +2 -0
  166. package/dist/es2015/serializer.js.map +1 -0
  167. package/dist/es2015/serializers/any-serializer.js +22 -0
  168. package/dist/es2015/serializers/any-serializer.js.map +1 -0
  169. package/dist/es2015/serializers/array-buffer-serializer.js +41 -0
  170. package/dist/es2015/serializers/array-buffer-serializer.js.map +1 -0
  171. package/dist/es2015/serializers/array-serializer.js +60 -0
  172. package/dist/es2015/serializers/array-serializer.js.map +1 -0
  173. package/dist/es2015/serializers/boolean-serializer.js +55 -0
  174. package/dist/es2015/serializers/boolean-serializer.js.map +1 -0
  175. package/dist/es2015/serializers/data-view-serializer.js +42 -0
  176. package/dist/es2015/serializers/data-view-serializer.js.map +1 -0
  177. package/dist/es2015/serializers/date-serializer.js +34 -0
  178. package/dist/es2015/serializers/date-serializer.js.map +1 -0
  179. package/dist/es2015/serializers/float-32-array-serializer.js +35 -0
  180. package/dist/es2015/serializers/float-32-array-serializer.js.map +1 -0
  181. package/dist/es2015/serializers/float-64-array-serializer.js +35 -0
  182. package/dist/es2015/serializers/float-64-array-serializer.js.map +1 -0
  183. package/{serializers/index.d.ts → dist/es2015/serializers/index.js} +22 -19
  184. package/dist/es2015/serializers/index.js.map +1 -0
  185. package/dist/es2015/serializers/int-16-array-serializer.js +35 -0
  186. package/dist/es2015/serializers/int-16-array-serializer.js.map +1 -0
  187. package/dist/es2015/serializers/int-32-array-serializer.js +35 -0
  188. package/dist/es2015/serializers/int-32-array-serializer.js.map +1 -0
  189. package/dist/es2015/serializers/int-8-array-serializer.js +35 -0
  190. package/dist/es2015/serializers/int-8-array-serializer.js.map +1 -0
  191. package/dist/es2015/serializers/map-serializer.js +96 -0
  192. package/dist/es2015/serializers/map-serializer.js.map +1 -0
  193. package/dist/es2015/serializers/number-serializer.js +49 -0
  194. package/dist/es2015/serializers/number-serializer.js.map +1 -0
  195. package/dist/es2015/serializers/set-serializer.js +62 -0
  196. package/dist/es2015/serializers/set-serializer.js.map +1 -0
  197. package/dist/es2015/serializers/string-serializer.js +55 -0
  198. package/dist/es2015/serializers/string-serializer.js.map +1 -0
  199. package/dist/es2015/serializers/type-serializer.js +121 -0
  200. package/dist/es2015/serializers/type-serializer.js.map +1 -0
  201. package/dist/es2015/serializers/uint-16-array-serializer.js +35 -0
  202. package/dist/es2015/serializers/uint-16-array-serializer.js.map +1 -0
  203. package/dist/es2015/serializers/uint-32-array-serializer.js +35 -0
  204. package/dist/es2015/serializers/uint-32-array-serializer.js.map +1 -0
  205. package/dist/es2015/serializers/uint-8-array-serializer.js +35 -0
  206. package/dist/es2015/serializers/uint-8-array-serializer.js.map +1 -0
  207. package/dist/es2015/serializers/uint-8-clamped-array-serializer.js +35 -0
  208. package/dist/es2015/serializers/uint-8-clamped-array-serializer.js.map +1 -0
  209. package/dist/es2015/serializers/unknown-serializer.js +22 -0
  210. package/dist/es2015/serializers/unknown-serializer.js.map +1 -0
  211. package/dist/es2015/type-abstraction.js +2 -0
  212. package/dist/es2015/type-abstraction.js.map +1 -0
  213. package/dist/es2015/type-argument.js +2 -0
  214. package/dist/es2015/type-argument.js.map +1 -0
  215. package/dist/es2015/type-configuration.js +2 -0
  216. package/dist/es2015/type-configuration.js.map +1 -0
  217. package/dist/es2015/type-context-entry.js +9 -0
  218. package/dist/es2015/type-context-entry.js.map +1 -0
  219. package/dist/es2015/type-context.js +25 -0
  220. package/dist/es2015/type-context.js.map +1 -0
  221. package/dist/es2015/type-ctor.js +2 -0
  222. package/dist/es2015/type-ctor.js.map +1 -0
  223. package/dist/es2015/type-fn.js +2 -0
  224. package/dist/es2015/type-fn.js.map +1 -0
  225. package/dist/es2015/type-like.js +2 -0
  226. package/dist/es2015/type-like.js.map +1 -0
  227. package/dist/es2015/type-manager-options.js +2 -0
  228. package/dist/es2015/type-manager-options.js.map +1 -0
  229. package/dist/es2015/type-manager.js +314 -0
  230. package/dist/es2015/type-manager.js.map +1 -0
  231. package/dist/es2015/type-metadata-extractor.js +2 -0
  232. package/dist/es2015/type-metadata-extractor.js.map +1 -0
  233. package/dist/es2015/type-metadata-resolver.js +2 -0
  234. package/dist/es2015/type-metadata-resolver.js.map +1 -0
  235. package/dist/es2015/type-metadata-symbol.js +2 -0
  236. package/dist/es2015/type-metadata-symbol.js.map +1 -0
  237. package/dist/es2015/type-metadata.js +399 -0
  238. package/dist/es2015/type-metadata.js.map +1 -0
  239. package/dist/es2015/type-name.js +2 -0
  240. package/dist/es2015/type-name.js.map +1 -0
  241. package/dist/es2015/type-options-base.js +2 -0
  242. package/dist/es2015/type-options-base.js.map +1 -0
  243. package/dist/es2015/type-options.js +2 -0
  244. package/dist/es2015/type-options.js.map +1 -0
  245. package/dist/es2015/type-resolver.js +2 -0
  246. package/dist/es2015/type-resolver.js.map +1 -0
  247. package/dist/es2015/type.js +8 -0
  248. package/dist/es2015/type.js.map +1 -0
  249. package/dist/es2015/unknown.js +3 -0
  250. package/dist/es2015/unknown.js.map +1 -0
  251. package/dist/es5/alias.js +2 -0
  252. package/dist/es5/alias.js.map +1 -0
  253. package/dist/es5/any.js +7 -0
  254. package/dist/es5/any.js.map +1 -0
  255. package/dist/es5/custom-data.js +2 -0
  256. package/dist/es5/custom-data.js.map +1 -0
  257. package/dist/es5/default-value-resolver.js +2 -0
  258. package/dist/es5/default-value-resolver.js.map +1 -0
  259. package/dist/es5/default-value.js +2 -0
  260. package/dist/es5/default-value.js.map +1 -0
  261. package/dist/es5/discriminant.js +2 -0
  262. package/dist/es5/discriminant.js.map +1 -0
  263. package/dist/es5/discriminator.js +2 -0
  264. package/dist/es5/discriminator.js.map +1 -0
  265. package/dist/es5/factories/index.js +2 -0
  266. package/dist/es5/factories/index.js.map +1 -0
  267. package/dist/es5/factories/type-factory.js +60 -0
  268. package/dist/es5/factories/type-factory.js.map +1 -0
  269. package/dist/es5/factory.js +2 -0
  270. package/dist/es5/factory.js.map +1 -0
  271. package/dist/es5/functions/get-own-reflect-metadata.js +8 -0
  272. package/dist/es5/functions/get-own-reflect-metadata.js.map +1 -0
  273. package/dist/es5/functions/get-reflect-metadata.js +8 -0
  274. package/dist/es5/functions/get-reflect-metadata.js.map +1 -0
  275. package/dist/es5/functions/get-words.js +5 -0
  276. package/dist/es5/functions/get-words.js.map +1 -0
  277. package/dist/es5/functions/index.js +19 -0
  278. package/dist/es5/functions/index.js.map +1 -0
  279. package/dist/es5/functions/is-arrow-function.js +5 -0
  280. package/dist/es5/functions/is-arrow-function.js.map +1 -0
  281. package/dist/es5/functions/is-ctor-function.js +5 -0
  282. package/dist/es5/functions/is-ctor-function.js.map +1 -0
  283. package/dist/es5/functions/is-data-view.js +4 -0
  284. package/dist/es5/functions/is-data-view.js.map +1 -0
  285. package/dist/es5/functions/is-float-32-array.js +4 -0
  286. package/dist/es5/functions/is-float-32-array.js.map +1 -0
  287. package/dist/es5/functions/is-float-64-array.js +4 -0
  288. package/dist/es5/functions/is-float-64-array.js.map +1 -0
  289. package/dist/es5/functions/is-int-16-array.js +4 -0
  290. package/dist/es5/functions/is-int-16-array.js.map +1 -0
  291. package/dist/es5/functions/is-int-32-array.js +4 -0
  292. package/dist/es5/functions/is-int-32-array.js.map +1 -0
  293. package/dist/es5/functions/is-int-8-array.js +4 -0
  294. package/dist/es5/functions/is-int-8-array.js.map +1 -0
  295. package/dist/es5/functions/is-uint-16-array.js +4 -0
  296. package/dist/es5/functions/is-uint-16-array.js.map +1 -0
  297. package/dist/es5/functions/is-uint-32-array.js +4 -0
  298. package/dist/es5/functions/is-uint-32-array.js.map +1 -0
  299. package/dist/es5/functions/is-uint-8-array.js +4 -0
  300. package/dist/es5/functions/is-uint-8-array.js.map +1 -0
  301. package/dist/es5/functions/is-uint-8-clamped-array.js +4 -0
  302. package/dist/es5/functions/is-uint-8-clamped-array.js.map +1 -0
  303. package/dist/es5/functions/json-parse.js +4 -0
  304. package/dist/es5/functions/json-parse.js.map +1 -0
  305. package/dist/es5/functions/json-stringify.js +67 -0
  306. package/dist/es5/functions/json-stringify.js.map +1 -0
  307. package/dist/es5/functions/name-of.js +8 -0
  308. package/dist/es5/functions/name-of.js.map +1 -0
  309. package/dist/es5/generic-argument.js +2 -0
  310. package/dist/es5/generic-argument.js.map +1 -0
  311. package/dist/es5/generic-metadata-resolver.js +2 -0
  312. package/dist/es5/generic-metadata-resolver.js.map +1 -0
  313. package/dist/es5/generic-metadata.js +2 -0
  314. package/dist/es5/generic-metadata.js.map +1 -0
  315. package/dist/es5/generic-structure.js +2 -0
  316. package/dist/es5/generic-structure.js.map +1 -0
  317. package/dist/es5/index.js +67 -0
  318. package/dist/es5/index.js.map +1 -0
  319. package/dist/es5/inject-index.js +2 -0
  320. package/dist/es5/inject-index.js.map +1 -0
  321. package/dist/es5/inject-internals.js +2 -0
  322. package/dist/es5/inject-internals.js.map +1 -0
  323. package/dist/es5/inject-metadata.js +84 -0
  324. package/dist/es5/inject-metadata.js.map +1 -0
  325. package/dist/es5/inject-options.js +2 -0
  326. package/dist/es5/inject-options.js.map +1 -0
  327. package/dist/es5/inject-sorter.js +2 -0
  328. package/dist/es5/inject-sorter.js.map +1 -0
  329. package/dist/es5/inject-sorters/asc-inject-sorter.js +18 -0
  330. package/dist/es5/inject-sorters/asc-inject-sorter.js.map +1 -0
  331. package/dist/es5/inject-sorters/desc-inject-sorter.js +18 -0
  332. package/dist/es5/inject-sorters/desc-inject-sorter.js.map +1 -0
  333. package/dist/es5/inject-sorters/index.js +3 -0
  334. package/dist/es5/inject-sorters/index.js.map +1 -0
  335. package/dist/es5/inject.js +24 -0
  336. package/dist/es5/inject.js.map +1 -0
  337. package/dist/es5/injector.js +2 -0
  338. package/dist/es5/injector.js.map +1 -0
  339. package/dist/es5/injectors/index.js +2 -0
  340. package/dist/es5/injectors/index.js.map +1 -0
  341. package/dist/es5/injectors/singleton-injector.js +45 -0
  342. package/dist/es5/injectors/singleton-injector.js.map +1 -0
  343. package/dist/es5/log-level.js +8 -0
  344. package/dist/es5/log-level.js.map +1 -0
  345. package/dist/es5/log.js +65 -0
  346. package/dist/es5/log.js.map +1 -0
  347. package/dist/es5/metadata.js +106 -0
  348. package/dist/es5/metadata.js.map +1 -0
  349. package/dist/es5/naming-convention.js +2 -0
  350. package/dist/es5/naming-convention.js.map +1 -0
  351. package/dist/es5/naming-conventions/camel-case-naming-convention.js +21 -0
  352. package/dist/es5/naming-conventions/camel-case-naming-convention.js.map +1 -0
  353. package/dist/es5/naming-conventions/flat-case-naming-convention.js +17 -0
  354. package/dist/es5/naming-conventions/flat-case-naming-convention.js.map +1 -0
  355. package/dist/es5/naming-conventions/flat-upper-case-naming-convention.js +17 -0
  356. package/dist/es5/naming-conventions/flat-upper-case-naming-convention.js.map +1 -0
  357. package/dist/es5/naming-conventions/index.js +9 -0
  358. package/dist/es5/naming-conventions/index.js.map +1 -0
  359. package/dist/es5/naming-conventions/kebab-case-naming-convention.js +21 -0
  360. package/dist/es5/naming-conventions/kebab-case-naming-convention.js.map +1 -0
  361. package/dist/es5/naming-conventions/kebab-upper-case-naming-convention.js +21 -0
  362. package/dist/es5/naming-conventions/kebab-upper-case-naming-convention.js.map +1 -0
  363. package/dist/es5/naming-conventions/pascal-case-naming-convention.js +22 -0
  364. package/dist/es5/naming-conventions/pascal-case-naming-convention.js.map +1 -0
  365. package/dist/es5/naming-conventions/snake-case-naming-convention.js +21 -0
  366. package/dist/es5/naming-conventions/snake-case-naming-convention.js.map +1 -0
  367. package/dist/es5/naming-conventions/snake-upper-case-naming-convention.js +21 -0
  368. package/dist/es5/naming-conventions/snake-upper-case-naming-convention.js.map +1 -0
  369. package/dist/es5/property-internals.js +2 -0
  370. package/dist/es5/property-internals.js.map +1 -0
  371. package/dist/es5/property-metadata.js +364 -0
  372. package/dist/es5/property-metadata.js.map +1 -0
  373. package/dist/es5/property-name.js +2 -0
  374. package/dist/es5/property-name.js.map +1 -0
  375. package/dist/es5/property-options.js +2 -0
  376. package/dist/es5/property-options.js.map +1 -0
  377. package/dist/es5/property-sorter.js +2 -0
  378. package/dist/es5/property-sorter.js.map +1 -0
  379. package/dist/es5/property-sorters/asc-property-sorter.js +18 -0
  380. package/dist/es5/property-sorters/asc-property-sorter.js.map +1 -0
  381. package/dist/es5/property-sorters/desc-property-sorter.js +18 -0
  382. package/dist/es5/property-sorters/desc-property-sorter.js.map +1 -0
  383. package/dist/es5/property-sorters/index.js +3 -0
  384. package/dist/es5/property-sorters/index.js.map +1 -0
  385. package/dist/es5/property.js +39 -0
  386. package/dist/es5/property.js.map +1 -0
  387. package/dist/es5/reference-callback.js +2 -0
  388. package/dist/es5/reference-callback.js.map +1 -0
  389. package/dist/es5/reference-handler.js +2 -0
  390. package/dist/es5/reference-handler.js.map +1 -0
  391. package/dist/es5/reference-handlers/circular-reference-handler.js +40 -0
  392. package/dist/es5/reference-handlers/circular-reference-handler.js.map +1 -0
  393. package/dist/es5/reference-handlers/index.js +4 -0
  394. package/dist/es5/reference-handlers/index.js.map +1 -0
  395. package/dist/es5/reference-handlers/json-path-reference-handler.js +93 -0
  396. package/dist/es5/reference-handlers/json-path-reference-handler.js.map +1 -0
  397. package/dist/es5/reference-handlers/plain-reference-handler.js +36 -0
  398. package/dist/es5/reference-handlers/plain-reference-handler.js.map +1 -0
  399. package/dist/es5/reference-key.js +2 -0
  400. package/dist/es5/reference-key.js.map +1 -0
  401. package/dist/es5/reference-value-getter.js +2 -0
  402. package/dist/es5/reference-value-getter.js.map +1 -0
  403. package/dist/es5/reference-value-setter.js +2 -0
  404. package/dist/es5/reference-value-setter.js.map +1 -0
  405. package/dist/es5/reference-value.js +2 -0
  406. package/dist/es5/reference-value.js.map +1 -0
  407. package/dist/es5/serializer-context-options.js +2 -0
  408. package/dist/es5/serializer-context-options.js.map +1 -0
  409. package/dist/es5/serializer-context.js +434 -0
  410. package/dist/es5/serializer-context.js.map +1 -0
  411. package/dist/es5/serializer.js +2 -0
  412. package/dist/es5/serializer.js.map +1 -0
  413. package/dist/es5/serializers/any-serializer.js +26 -0
  414. package/dist/es5/serializers/any-serializer.js.map +1 -0
  415. package/dist/es5/serializers/array-buffer-serializer.js +45 -0
  416. package/dist/es5/serializers/array-buffer-serializer.js.map +1 -0
  417. package/dist/es5/serializers/array-serializer.js +70 -0
  418. package/dist/es5/serializers/array-serializer.js.map +1 -0
  419. package/dist/es5/serializers/boolean-serializer.js +59 -0
  420. package/dist/es5/serializers/boolean-serializer.js.map +1 -0
  421. package/dist/es5/serializers/data-view-serializer.js +46 -0
  422. package/dist/es5/serializers/data-view-serializer.js.map +1 -0
  423. package/dist/es5/serializers/date-serializer.js +38 -0
  424. package/dist/es5/serializers/date-serializer.js.map +1 -0
  425. package/dist/es5/serializers/float-32-array-serializer.js +39 -0
  426. package/dist/es5/serializers/float-32-array-serializer.js.map +1 -0
  427. package/dist/es5/serializers/float-64-array-serializer.js +39 -0
  428. package/dist/es5/serializers/float-64-array-serializer.js.map +1 -0
  429. package/dist/es5/serializers/index.js +22 -0
  430. package/dist/es5/serializers/index.js.map +1 -0
  431. package/dist/es5/serializers/int-16-array-serializer.js +39 -0
  432. package/dist/es5/serializers/int-16-array-serializer.js.map +1 -0
  433. package/dist/es5/serializers/int-32-array-serializer.js +39 -0
  434. package/dist/es5/serializers/int-32-array-serializer.js.map +1 -0
  435. package/dist/es5/serializers/int-8-array-serializer.js +39 -0
  436. package/dist/es5/serializers/int-8-array-serializer.js.map +1 -0
  437. package/dist/es5/serializers/map-serializer.js +115 -0
  438. package/dist/es5/serializers/map-serializer.js.map +1 -0
  439. package/dist/es5/serializers/number-serializer.js +53 -0
  440. package/dist/es5/serializers/number-serializer.js.map +1 -0
  441. package/dist/es5/serializers/set-serializer.js +78 -0
  442. package/dist/es5/serializers/set-serializer.js.map +1 -0
  443. package/dist/es5/serializers/string-serializer.js +59 -0
  444. package/dist/es5/serializers/string-serializer.js.map +1 -0
  445. package/dist/es5/serializers/type-serializer.js +154 -0
  446. package/dist/es5/serializers/type-serializer.js.map +1 -0
  447. package/dist/es5/serializers/uint-16-array-serializer.js +39 -0
  448. package/dist/es5/serializers/uint-16-array-serializer.js.map +1 -0
  449. package/dist/es5/serializers/uint-32-array-serializer.js +39 -0
  450. package/dist/es5/serializers/uint-32-array-serializer.js.map +1 -0
  451. package/dist/es5/serializers/uint-8-array-serializer.js +39 -0
  452. package/dist/es5/serializers/uint-8-array-serializer.js.map +1 -0
  453. package/dist/es5/serializers/uint-8-clamped-array-serializer.js +39 -0
  454. package/dist/es5/serializers/uint-8-clamped-array-serializer.js.map +1 -0
  455. package/dist/es5/serializers/unknown-serializer.js +26 -0
  456. package/dist/es5/serializers/unknown-serializer.js.map +1 -0
  457. package/dist/es5/type-abstraction.js +2 -0
  458. package/dist/es5/type-abstraction.js.map +1 -0
  459. package/dist/es5/type-argument.js +2 -0
  460. package/dist/es5/type-argument.js.map +1 -0
  461. package/dist/es5/type-configuration.js +2 -0
  462. package/dist/es5/type-configuration.js.map +1 -0
  463. package/dist/es5/type-context-entry.js +11 -0
  464. package/dist/es5/type-context-entry.js.map +1 -0
  465. package/dist/es5/type-context.js +27 -0
  466. package/dist/es5/type-context.js.map +1 -0
  467. package/dist/es5/type-ctor.js +2 -0
  468. package/dist/es5/type-ctor.js.map +1 -0
  469. package/dist/es5/type-fn.js +2 -0
  470. package/dist/es5/type-fn.js.map +1 -0
  471. package/dist/es5/type-like.js +2 -0
  472. package/dist/es5/type-like.js.map +1 -0
  473. package/dist/es5/type-manager-options.js +2 -0
  474. package/dist/es5/type-manager-options.js.map +1 -0
  475. package/dist/es5/type-manager.js +407 -0
  476. package/dist/es5/type-manager.js.map +1 -0
  477. package/dist/es5/type-metadata-extractor.js +2 -0
  478. package/dist/es5/type-metadata-extractor.js.map +1 -0
  479. package/dist/es5/type-metadata-resolver.js +2 -0
  480. package/dist/es5/type-metadata-resolver.js.map +1 -0
  481. package/dist/es5/type-metadata-symbol.js +2 -0
  482. package/dist/es5/type-metadata-symbol.js.map +1 -0
  483. package/dist/es5/type-metadata.js +545 -0
  484. package/dist/es5/type-metadata.js.map +1 -0
  485. package/dist/es5/type-name.js +2 -0
  486. package/dist/es5/type-name.js.map +1 -0
  487. package/dist/es5/type-options-base.js +2 -0
  488. package/dist/es5/type-options-base.js.map +1 -0
  489. package/dist/es5/type-options.js +2 -0
  490. package/dist/es5/type-options.js.map +1 -0
  491. package/dist/es5/type-resolver.js +2 -0
  492. package/dist/es5/type-resolver.js.map +1 -0
  493. package/dist/es5/type.js +8 -0
  494. package/dist/es5/type.js.map +1 -0
  495. package/dist/es5/unknown.js +7 -0
  496. package/dist/es5/unknown.js.map +1 -0
  497. package/{alias.d.ts → dist/types/alias.d.ts} +7 -6
  498. package/dist/types/alias.d.ts.map +1 -0
  499. package/dist/types/any.d.ts +10 -0
  500. package/dist/types/any.d.ts.map +1 -0
  501. package/{custom-data.d.ts → dist/types/custom-data.d.ts} +7 -6
  502. package/dist/types/custom-data.d.ts.map +1 -0
  503. package/{default-value-resolver.d.ts → dist/types/default-value-resolver.d.ts} +8 -7
  504. package/dist/types/default-value-resolver.d.ts.map +1 -0
  505. package/{default-value.d.ts → dist/types/default-value.d.ts} +8 -7
  506. package/dist/types/default-value.d.ts.map +1 -0
  507. package/{discriminant.d.ts → dist/types/discriminant.d.ts} +9 -8
  508. package/dist/types/discriminant.d.ts.map +1 -0
  509. package/{discriminator.d.ts → dist/types/discriminator.d.ts} +7 -6
  510. package/dist/types/discriminator.d.ts.map +1 -0
  511. package/dist/types/factories/index.d.ts +2 -0
  512. package/dist/types/factories/index.d.ts.map +1 -0
  513. package/{factories → dist/types/factories}/type-factory.d.ts +20 -19
  514. package/dist/types/factories/type-factory.d.ts.map +1 -0
  515. package/{factory.d.ts → dist/types/factory.d.ts} +19 -18
  516. package/dist/types/factory.d.ts.map +1 -0
  517. package/{functions → dist/types/functions}/get-own-reflect-metadata.d.ts +10 -9
  518. package/dist/types/functions/get-own-reflect-metadata.d.ts.map +1 -0
  519. package/{functions → dist/types/functions}/get-reflect-metadata.d.ts +11 -10
  520. package/dist/types/functions/get-reflect-metadata.d.ts.map +1 -0
  521. package/{functions → dist/types/functions}/get-words.d.ts +9 -8
  522. package/dist/types/functions/get-words.d.ts.map +1 -0
  523. package/dist/types/functions/index.d.ts +19 -0
  524. package/dist/types/functions/index.d.ts.map +1 -0
  525. package/{functions → dist/types/functions}/is-arrow-function.d.ts +9 -8
  526. package/dist/types/functions/is-arrow-function.d.ts.map +1 -0
  527. package/{functions → dist/types/functions}/is-ctor-function.d.ts +9 -8
  528. package/dist/types/functions/is-ctor-function.d.ts.map +1 -0
  529. package/{functions → dist/types/functions}/is-data-view.d.ts +9 -8
  530. package/dist/types/functions/is-data-view.d.ts.map +1 -0
  531. package/{functions → dist/types/functions}/is-float-32-array.d.ts +9 -8
  532. package/dist/types/functions/is-float-32-array.d.ts.map +1 -0
  533. package/{functions → dist/types/functions}/is-float-64-array.d.ts +9 -8
  534. package/dist/types/functions/is-float-64-array.d.ts.map +1 -0
  535. package/{functions → dist/types/functions}/is-int-16-array.d.ts +9 -8
  536. package/dist/types/functions/is-int-16-array.d.ts.map +1 -0
  537. package/{functions → dist/types/functions}/is-int-32-array.d.ts +9 -8
  538. package/dist/types/functions/is-int-32-array.d.ts.map +1 -0
  539. package/{functions → dist/types/functions}/is-int-8-array.d.ts +9 -8
  540. package/dist/types/functions/is-int-8-array.d.ts.map +1 -0
  541. package/{functions → dist/types/functions}/is-uint-16-array.d.ts +9 -8
  542. package/dist/types/functions/is-uint-16-array.d.ts.map +1 -0
  543. package/{functions → dist/types/functions}/is-uint-32-array.d.ts +9 -8
  544. package/dist/types/functions/is-uint-32-array.d.ts.map +1 -0
  545. package/{functions → dist/types/functions}/is-uint-8-array.d.ts +9 -8
  546. package/dist/types/functions/is-uint-8-array.d.ts.map +1 -0
  547. package/{functions → dist/types/functions}/is-uint-8-clamped-array.d.ts +9 -8
  548. package/dist/types/functions/is-uint-8-clamped-array.d.ts.map +1 -0
  549. package/dist/types/functions/json-parse.d.ts +10 -0
  550. package/dist/types/functions/json-parse.d.ts.map +1 -0
  551. package/dist/types/functions/json-stringify.d.ts +11 -0
  552. package/dist/types/functions/json-stringify.d.ts.map +1 -0
  553. package/{functions → dist/types/functions}/name-of.d.ts +9 -8
  554. package/dist/types/functions/name-of.d.ts.map +1 -0
  555. package/{generic-argument.d.ts → dist/types/generic-argument.d.ts} +8 -7
  556. package/dist/types/generic-argument.d.ts.map +1 -0
  557. package/dist/types/generic-metadata-resolver.d.ts +8 -0
  558. package/dist/types/generic-metadata-resolver.d.ts.map +1 -0
  559. package/{generic-metadata.d.ts → dist/types/generic-metadata.d.ts} +8 -7
  560. package/dist/types/generic-metadata.d.ts.map +1 -0
  561. package/dist/types/generic-structure.d.ts +8 -0
  562. package/dist/types/generic-structure.d.ts.map +1 -0
  563. package/dist/types/index.d.ts +67 -0
  564. package/dist/types/index.d.ts.map +1 -0
  565. package/{inject-index.d.ts → dist/types/inject-index.d.ts} +7 -6
  566. package/dist/types/inject-index.d.ts.map +1 -0
  567. package/dist/types/inject-internals.d.ts +15 -0
  568. package/dist/types/inject-internals.d.ts.map +1 -0
  569. package/{inject-metadata.d.ts → dist/types/inject-metadata.d.ts} +118 -88
  570. package/dist/types/inject-metadata.d.ts.map +1 -0
  571. package/{inject-options.d.ts → dist/types/inject-options.d.ts} +23 -22
  572. package/dist/types/inject-options.d.ts.map +1 -0
  573. package/dist/types/inject-sorter.d.ts +20 -0
  574. package/dist/types/inject-sorter.d.ts.map +1 -0
  575. package/dist/types/inject-sorters/asc-inject-sorter.d.ts +21 -0
  576. package/dist/types/inject-sorters/asc-inject-sorter.d.ts.map +1 -0
  577. package/dist/types/inject-sorters/desc-inject-sorter.d.ts +21 -0
  578. package/dist/types/inject-sorters/desc-inject-sorter.d.ts.map +1 -0
  579. package/dist/types/inject-sorters/index.d.ts +3 -0
  580. package/dist/types/inject-sorters/index.d.ts.map +1 -0
  581. package/{inject.d.ts → dist/types/inject.d.ts} +11 -10
  582. package/dist/types/inject.d.ts.map +1 -0
  583. package/{injector.d.ts → dist/types/injector.d.ts} +17 -16
  584. package/dist/types/injector.d.ts.map +1 -0
  585. package/dist/types/injectors/index.d.ts +2 -0
  586. package/dist/types/injectors/index.d.ts.map +1 -0
  587. package/{injectors → dist/types/injectors}/singleton-injector.d.ts +32 -31
  588. package/dist/types/injectors/singleton-injector.d.ts.map +1 -0
  589. package/{log-level.d.ts → dist/types/log-level.d.ts} +12 -11
  590. package/dist/types/log-level.d.ts.map +1 -0
  591. package/{log.d.ts → dist/types/log.d.ts} +96 -95
  592. package/dist/types/log.d.ts.map +1 -0
  593. package/dist/types/metadata.d.ts +101 -0
  594. package/dist/types/metadata.d.ts.map +1 -0
  595. package/{naming-convention.d.ts → dist/types/naming-convention.d.ts} +16 -15
  596. package/dist/types/naming-convention.d.ts.map +1 -0
  597. package/{naming-conventions → dist/types/naming-conventions}/camel-case-naming-convention.d.ts +27 -26
  598. package/dist/types/naming-conventions/camel-case-naming-convention.d.ts.map +1 -0
  599. package/{naming-conventions → dist/types/naming-conventions}/flat-case-naming-convention.d.ts +26 -25
  600. package/dist/types/naming-conventions/flat-case-naming-convention.d.ts.map +1 -0
  601. package/{naming-conventions → dist/types/naming-conventions}/flat-upper-case-naming-convention.d.ts +26 -25
  602. package/dist/types/naming-conventions/flat-upper-case-naming-convention.d.ts.map +1 -0
  603. package/dist/types/naming-conventions/index.d.ts +9 -0
  604. package/dist/types/naming-conventions/index.d.ts.map +1 -0
  605. package/{naming-conventions → dist/types/naming-conventions}/kebab-case-naming-convention.d.ts +27 -26
  606. package/dist/types/naming-conventions/kebab-case-naming-convention.d.ts.map +1 -0
  607. package/{naming-conventions → dist/types/naming-conventions}/kebab-upper-case-naming-convention.d.ts +27 -26
  608. package/dist/types/naming-conventions/kebab-upper-case-naming-convention.d.ts.map +1 -0
  609. package/{naming-conventions → dist/types/naming-conventions}/pascal-case-naming-convention.d.ts +27 -26
  610. package/dist/types/naming-conventions/pascal-case-naming-convention.d.ts.map +1 -0
  611. package/{naming-conventions → dist/types/naming-conventions}/snake-case-naming-convention.d.ts +27 -26
  612. package/dist/types/naming-conventions/snake-case-naming-convention.d.ts.map +1 -0
  613. package/{naming-conventions → dist/types/naming-conventions}/snake-upper-case-naming-convention.d.ts +27 -26
  614. package/dist/types/naming-conventions/snake-upper-case-naming-convention.d.ts.map +1 -0
  615. package/dist/types/property-internals.d.ts +29 -0
  616. package/dist/types/property-internals.d.ts.map +1 -0
  617. package/{property-metadata.d.ts → dist/types/property-metadata.d.ts} +353 -309
  618. package/dist/types/property-metadata.d.ts.map +1 -0
  619. package/{property-name.d.ts → dist/types/property-name.d.ts} +7 -6
  620. package/dist/types/property-name.d.ts.map +1 -0
  621. package/{property-options.d.ts → dist/types/property-options.d.ts} +117 -116
  622. package/dist/types/property-options.d.ts.map +1 -0
  623. package/dist/types/property-sorter.d.ts +20 -0
  624. package/dist/types/property-sorter.d.ts.map +1 -0
  625. package/dist/types/property-sorters/asc-property-sorter.d.ts +21 -0
  626. package/dist/types/property-sorters/asc-property-sorter.d.ts.map +1 -0
  627. package/dist/types/property-sorters/desc-property-sorter.d.ts +21 -0
  628. package/dist/types/property-sorters/desc-property-sorter.d.ts.map +1 -0
  629. package/dist/types/property-sorters/index.d.ts +3 -0
  630. package/dist/types/property-sorters/index.d.ts.map +1 -0
  631. package/{property.d.ts → dist/types/property.d.ts} +14 -13
  632. package/dist/types/property.d.ts.map +1 -0
  633. package/{reference-callback.d.ts → dist/types/reference-callback.d.ts} +8 -7
  634. package/dist/types/reference-callback.d.ts.map +1 -0
  635. package/{reference-handler.d.ts → dist/types/reference-handler.d.ts} +32 -31
  636. package/dist/types/reference-handler.d.ts.map +1 -0
  637. package/{reference-handlers/direct-reference-handler.d.ts → dist/types/reference-handlers/circular-reference-handler.d.ts} +33 -32
  638. package/dist/types/reference-handlers/circular-reference-handler.d.ts.map +1 -0
  639. package/dist/types/reference-handlers/index.d.ts +4 -0
  640. package/dist/types/reference-handlers/index.d.ts.map +1 -0
  641. package/{reference-handlers/path-reference-handler.d.ts → dist/types/reference-handlers/json-path-reference-handler.d.ts} +56 -55
  642. package/dist/types/reference-handlers/json-path-reference-handler.d.ts.map +1 -0
  643. package/{reference-handlers/lead-reference-handler.d.ts → dist/types/reference-handlers/plain-reference-handler.d.ts} +34 -33
  644. package/dist/types/reference-handlers/plain-reference-handler.d.ts.map +1 -0
  645. package/{reference-key.d.ts → dist/types/reference-key.d.ts} +7 -6
  646. package/dist/types/reference-key.d.ts.map +1 -0
  647. package/{reference-value-getter.d.ts → dist/types/reference-value-getter.d.ts} +8 -7
  648. package/dist/types/reference-value-getter.d.ts.map +1 -0
  649. package/{reference-value-setter.d.ts → dist/types/reference-value-setter.d.ts} +8 -7
  650. package/dist/types/reference-value-setter.d.ts.map +1 -0
  651. package/{reference-value.d.ts → dist/types/reference-value.d.ts} +7 -6
  652. package/dist/types/reference-value.d.ts.map +1 -0
  653. package/{serializer-context-options.d.ts → dist/types/serializer-context-options.d.ts} +51 -43
  654. package/dist/types/serializer-context-options.d.ts.map +1 -0
  655. package/{serializer-context.d.ts → dist/types/serializer-context.d.ts} +426 -410
  656. package/dist/types/serializer-context.d.ts.map +1 -0
  657. package/{serializer.d.ts → dist/types/serializer.d.ts} +28 -27
  658. package/dist/types/serializer.d.ts.map +1 -0
  659. package/dist/types/serializers/any-serializer.d.ts +29 -0
  660. package/dist/types/serializers/any-serializer.d.ts.map +1 -0
  661. package/{serializers → dist/types/serializers}/array-buffer-serializer.d.ts +29 -28
  662. package/dist/types/serializers/array-buffer-serializer.d.ts.map +1 -0
  663. package/{serializers → dist/types/serializers}/array-serializer.d.ts +29 -28
  664. package/dist/types/serializers/array-serializer.d.ts.map +1 -0
  665. package/{serializers → dist/types/serializers}/boolean-serializer.d.ts +38 -37
  666. package/dist/types/serializers/boolean-serializer.d.ts.map +1 -0
  667. package/{serializers → dist/types/serializers}/data-view-serializer.d.ts +29 -28
  668. package/dist/types/serializers/data-view-serializer.d.ts.map +1 -0
  669. package/{serializers → dist/types/serializers}/date-serializer.d.ts +29 -28
  670. package/dist/types/serializers/date-serializer.d.ts.map +1 -0
  671. package/{serializers → dist/types/serializers}/float-32-array-serializer.d.ts +29 -28
  672. package/dist/types/serializers/float-32-array-serializer.d.ts.map +1 -0
  673. package/{serializers → dist/types/serializers}/float-64-array-serializer.d.ts +29 -28
  674. package/dist/types/serializers/float-64-array-serializer.d.ts.map +1 -0
  675. package/dist/types/serializers/index.d.ts +22 -0
  676. package/dist/types/serializers/index.d.ts.map +1 -0
  677. package/{serializers → dist/types/serializers}/int-16-array-serializer.d.ts +29 -28
  678. package/dist/types/serializers/int-16-array-serializer.d.ts.map +1 -0
  679. package/{serializers → dist/types/serializers}/int-32-array-serializer.d.ts +29 -28
  680. package/dist/types/serializers/int-32-array-serializer.d.ts.map +1 -0
  681. package/{serializers → dist/types/serializers}/int-8-array-serializer.d.ts +29 -28
  682. package/dist/types/serializers/int-8-array-serializer.d.ts.map +1 -0
  683. package/{serializers → dist/types/serializers}/map-serializer.d.ts +29 -28
  684. package/dist/types/serializers/map-serializer.d.ts.map +1 -0
  685. package/{serializers → dist/types/serializers}/number-serializer.d.ts +38 -37
  686. package/dist/types/serializers/number-serializer.d.ts.map +1 -0
  687. package/{serializers → dist/types/serializers}/set-serializer.d.ts +29 -28
  688. package/dist/types/serializers/set-serializer.d.ts.map +1 -0
  689. package/{serializers → dist/types/serializers}/string-serializer.d.ts +38 -37
  690. package/dist/types/serializers/string-serializer.d.ts.map +1 -0
  691. package/{serializers → dist/types/serializers}/type-serializer.d.ts +29 -28
  692. package/dist/types/serializers/type-serializer.d.ts.map +1 -0
  693. package/{serializers → dist/types/serializers}/uint-16-array-serializer.d.ts +29 -28
  694. package/dist/types/serializers/uint-16-array-serializer.d.ts.map +1 -0
  695. package/{serializers → dist/types/serializers}/uint-32-array-serializer.d.ts +29 -28
  696. package/dist/types/serializers/uint-32-array-serializer.d.ts.map +1 -0
  697. package/{serializers → dist/types/serializers}/uint-8-array-serializer.d.ts +29 -28
  698. package/dist/types/serializers/uint-8-array-serializer.d.ts.map +1 -0
  699. package/{serializers → dist/types/serializers}/uint-8-clamped-array-serializer.d.ts +29 -28
  700. package/dist/types/serializers/uint-8-clamped-array-serializer.d.ts.map +1 -0
  701. package/dist/types/serializers/unknown-serializer.d.ts +29 -0
  702. package/dist/types/serializers/unknown-serializer.d.ts.map +1 -0
  703. package/dist/types/type-abstraction.d.ts +7 -0
  704. package/dist/types/type-abstraction.d.ts.map +1 -0
  705. package/{type-argument.d.ts → dist/types/type-argument.d.ts} +10 -9
  706. package/dist/types/type-argument.d.ts.map +1 -0
  707. package/dist/types/type-configuration.d.ts +17 -0
  708. package/dist/types/type-configuration.d.ts.map +1 -0
  709. package/{type-context-entry.d.ts → dist/types/type-context-entry.d.ts} +37 -36
  710. package/dist/types/type-context-entry.d.ts.map +1 -0
  711. package/{type-context.d.ts → dist/types/type-context.d.ts} +59 -58
  712. package/dist/types/type-context.d.ts.map +1 -0
  713. package/{type-ctor.d.ts → dist/types/type-ctor.d.ts} +7 -6
  714. package/dist/types/type-ctor.d.ts.map +1 -0
  715. package/{type-fn.d.ts → dist/types/type-fn.d.ts} +9 -8
  716. package/dist/types/type-fn.d.ts.map +1 -0
  717. package/{type-like.d.ts → dist/types/type-like.d.ts} +8 -7
  718. package/dist/types/type-like.d.ts.map +1 -0
  719. package/{type-manager-options.d.ts → dist/types/type-manager-options.d.ts} +34 -24
  720. package/dist/types/type-manager-options.d.ts.map +1 -0
  721. package/dist/types/type-manager.d.ts +398 -0
  722. package/dist/types/type-manager.d.ts.map +1 -0
  723. package/dist/types/type-metadata-extractor.d.ts +9 -0
  724. package/dist/types/type-metadata-extractor.d.ts.map +1 -0
  725. package/{type-metadata-resolver.d.ts → dist/types/type-metadata-resolver.d.ts} +9 -8
  726. package/dist/types/type-metadata-resolver.d.ts.map +1 -0
  727. package/{type-metadata-symbol.d.ts → dist/types/type-metadata-symbol.d.ts} +7 -6
  728. package/dist/types/type-metadata-symbol.d.ts.map +1 -0
  729. package/{type-metadata.d.ts → dist/types/type-metadata.d.ts} +496 -472
  730. package/dist/types/type-metadata.d.ts.map +1 -0
  731. package/{type-name.d.ts → dist/types/type-name.d.ts} +7 -6
  732. package/dist/types/type-name.d.ts.map +1 -0
  733. package/{type-options-base.d.ts → dist/types/type-options-base.d.ts} +107 -90
  734. package/dist/types/type-options-base.d.ts.map +1 -0
  735. package/{type-options.d.ts → dist/types/type-options.d.ts} +73 -79
  736. package/dist/types/type-options.d.ts.map +1 -0
  737. package/{type-resolver.d.ts → dist/types/type-resolver.d.ts} +8 -7
  738. package/dist/types/type-resolver.d.ts.map +1 -0
  739. package/{type.d.ts → dist/types/type.d.ts} +10 -9
  740. package/dist/types/type.d.ts.map +1 -0
  741. package/dist/types/unknown.d.ts +10 -0
  742. package/dist/types/unknown.d.ts.map +1 -0
  743. package/dist/umd/index.js +2 -0
  744. package/dist/umd/index.js.map +1 -0
  745. package/package.json +63 -55
  746. package/factories/index.d.ts +0 -1
  747. package/index.js +0 -1
  748. package/metadata.d.ts +0 -42
  749. package/reference-handlers/index.d.ts +0 -3
  750. package/type-abstraction.d.ts +0 -8
  751. package/type-manager.d.ts +0 -331
package/README.md CHANGED
@@ -1,1857 +1,1887 @@
1
- # TypeManager.TS
2
-
3
- ![GitHub](https://img.shields.io/github/license/dipscope/TypeManager.TS) ![NPM](https://img.shields.io/npm/v/@dipscope/type-manager) ![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)
4
-
5
- Type manager is a parsing package for `TypeScript` which will help you to transform JSON strings or plain objects into `JavaScript` object instances. It supports [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) or declarative configuration and allows you to configure parsing of your or 3rd party classes easily.
6
-
7
- We recommend to use our [official website](https://dipscope.com/type-manager/what-issues-it-solves) to navigate through available features. You can also use the latest documentation described below.
8
-
9
- ## Give a star :star:
10
-
11
- If you like or are using this project please give it a star. Thanks!
12
-
13
- ## Table of contents
14
-
15
- * [What issues it solves?](#what-issues-it-solves)
16
- * [Installation](#installation)
17
- * [How it works?](#how-it-works)
18
- * [Defining decorators](#defining-decorators)
19
- * [Type decorator](#type-decorator)
20
- * [Property decorator](#property-decorator)
21
- * [Inject decorator](#inject-decorator)
22
- * [Defining decorator options](#defining-decorator-options)
23
- * [Alias option](#alias-option)
24
- * [Custom data option](#custom-data-option)
25
- * [Default value option](#default-value-option)
26
- * [Deserializable option](#deserializable-option)
27
- * [Discriminant option](#discriminant-option)
28
- * [Discriminator option](#discriminator-option)
29
- * [Factory option](#factory-option)
30
- * [Injectable option](#injectable-option)
31
- * [Injector option](#injector-option)
32
- * [Naming convention option](#naming-convention-option)
33
- * [Preserve discriminator option](#preserve-discriminator-option)
34
- * [Preserve null option](#preserve-null-option)
35
- * [Reference handler option](#reference-handler-option)
36
- * [Serializable option](#serializable-option)
37
- * [Serializer option](#serializer-option)
38
- * [Use default value option](#use-default-value-option)
39
- * [Use implicit conversion option](#use-implicit-conversion-option)
40
- * [Defining configuration manually](#defining-configuration-manually)
41
- * [Configuring global options](#configuring-global-options)
42
- * [Configuring options per type](#configuring-options-per-type)
43
- * [Configuring usage of polymorphic types](#configuring-usage-of-polymorphic-types)
44
- * [Configuring naming convention](#configuring-naming-convention)
45
- * [Configuring reference handler](#configuring-reference-handler)
46
- * [Advanced usage](#advanced-usage)
47
- * [Defining custom data](#defining-custom-data)
48
- * [Defining custom serializer](#defining-custom-serializer)
49
- * [Defining custom injector](#defining-custom-injector)
50
- * [Defining custom factory](#defining-custom-factory)
51
- * [Defining custom naming convention](#defining-custom-naming-convention)
52
- * [Use cases](#use-cases)
53
- * [Built in serializers](#built-in-serializers)
54
- * [Circular object references](#circular-object-references)
55
- * [Dependency injection and immutable types](#dependency-injection-and-immutable-types)
56
- * [Different case usage in class and JSON](#different-case-usage-in-class-and-json)
57
- * [Enum types](#enum-types)
58
- * [Generic types](#generic-types)
59
- * [Integration with Angular](#integration-with-angular)
60
- * [Polymorphic types](#polymorphic-types)
61
- * [Versioning](#versioning)
62
- * [Contributing](#contributing)
63
- * [Authors](#authors)
64
- * [Notes](#notes)
65
- * [License](#license)
66
-
67
- ## What issues it solves?
68
-
69
- In `JavaScript` there are two types of objects:
70
-
71
- * Plain objects which are created using `{}` notation;
72
- * Constructor based objects which are created using `new Class()` notation.
73
-
74
- Sometimes we want to transform plain objects to the classes we have. Let's assume we are loading a users JSON data from our backend API or the other datasource:
75
-
76
- ```json
77
- [
78
- {
79
- "id": 1,
80
- "name": "Dmitry",
81
- "deletedAt": "2021-02-22T20:15:00.000Z"
82
- },
83
- {
84
- "id": 2,
85
- "name": "Alex",
86
- "deletedAt": null
87
- },
88
- {
89
- "id": 3,
90
- "name": "Anna",
91
- "deletedAt": null
92
- }
93
- ]
94
- ```
95
-
96
- We have a `User` class:
97
-
98
- ```typescript
99
- export class User
100
- {
101
- public id: number;
102
- public name: string;
103
- public deletedAt: Date;
104
-
105
- public constructor(id: number, name: string)
106
- {
107
- this.id = id;
108
- this.name = name;
109
-
110
- return;
111
- }
112
-
113
- public isDeleted(): boolean
114
- {
115
- return this.deletedAt !== null;
116
- }
117
- }
118
- ```
119
-
120
- Somewhere in code we are checking if user is deleted before performing some action.
121
-
122
- ```typescript
123
- const users: User[] = JSON.parse(usersJson);
124
-
125
- for (const user of users)
126
- {
127
- if (user.isDeleted())
128
- {
129
- // Do some action...
130
- }
131
- }
132
- ```
133
-
134
- Do you see the problem in this piece of code?
135
-
136
- `JSON.parse` function returns plain objects and we actually lied to compiler when said that it is a `User` class array. In this code we can successfully use `user.id` or `user.name`. However we cannot use `user.isDeleted()` because user is not an instance of a `User` class.
137
-
138
- So what to do? How to get `User` array from our JSON?
139
-
140
- Solution is to create new instances of `User` class from plain objects returned by `JSON.parse` function. But things may go wrong once you have a more complex object hierarchy. Besides `deletedAt` property is represented as `String` in JSON but `User` class declares it as a `Date` so we also have to perform appropriate conversion when assigning this property.
141
-
142
- There exists much more simple way. Let's use our `TypeManager` for getting instances of `User` class from JSON:
143
-
144
- ```typescript
145
- import { TypeManager } from '@dipscope/type-manager';
146
-
147
- const users: User[] = TypeManager.parse(User, usersJson);
148
-
149
- for (const user of users)
150
- {
151
- if (user.isDeleted())
152
- {
153
- // Do some action...
154
- }
155
- }
156
- ```
157
-
158
- Now we can use all power provided by `JavaScript` class instances.
159
-
160
- Furthermore `TypeManager.TS` provides you:
161
-
162
- * Reflection abilities at runtime;
163
- * Support for generic types;
164
- * Handling of cyclic object references and different ways of serialization when they appear;
165
- * Ability to configure serialization of 3rd party classes;
166
- * Support for polymorphic types;
167
- * Great alternative for similar packages like [class-transformer](https://github.com/typestack/class-transformer), [TypedJSON](https://github.com/JohnWeisz/TypedJSON) and [jackson-js](https://github.com/pichillilorenzo/jackson-js);
168
-
169
- Want to know more? Let's dive into the details.
170
-
171
- ## Installation
172
-
173
- `TypeManager.TS` is available from NPM, both for browser (e.g. using webpack) and NodeJS:
174
-
175
- ```
176
- npm i @dipscope/type-manager
177
- ```
178
-
179
- TypeScript needs to run with the `experimentalDecorators` and `emitDecoratorMetadata` options enabled when using decorator annotations. So make sure you have properly configured your `tsconfig.json` file.
180
-
181
- _If you want additional type-safety and reduced syntax you may wish to install [reflect-metadata](https://github.com/rbuckton/reflect-metadata). This step is on your choice and fully optional. When installed it must be available globally to work. This can usually be done with `import 'reflect-metadata';` in your main index file._
182
-
183
- ## How it works?
184
-
185
- It defines configuration for each object which you are going to serialize or deserialize and uses this configuration to process data of your choice. There are two possible ways to define a configuration:
186
-
187
- * Using decorator annotations;
188
- * Using declarative style;
189
-
190
- The first one is the easiest and can be used for any class you control. If you want to configure serialization of 3rd party classes, global options or you don't like decorators it is better to go with the second. There are no restrictions to use one or another. You can combine two ways of configuration depending on which one fits better.
191
-
192
- Let's have a look at the most simple example of configuration using decorators.
193
-
194
- ```typescript
195
- import { Type, Property } from '@dipscope/type-manager';
196
-
197
- @Type()
198
- export class User
199
- {
200
- @Property(String) public name: string;
201
- @Property(String) public email: string;
202
- }
203
- ```
204
-
205
- Here we have a `User` class with `Type` and `Property` decorators assigned to it. `Type` decorator declares a type. `Property` decorator describes available properties for that type.
206
-
207
- The same configuration can be rewritten using declarative style.
208
-
209
- ```typescript
210
- import { TypeManager, PropertyName, PropertyOptions } from '@dipscope/type-manager';
211
-
212
- export class User
213
- {
214
- public name: string;
215
- public email: string;
216
- }
217
-
218
- TypeManager.configureTypeOptions(User, {
219
- propertyOptionsMap: new Map<PropertyName, PropertyOptions<any>>([
220
- ['name', { typeArgument: String }],
221
- ['email', { typeArgument: String }],
222
- ])
223
- });
224
- ```
225
-
226
- As you can see now our `User` class defined without decorators. Instead you have to call `TypeManager` configure method and provide `TypeOptions` related to `User` type.
227
-
228
- No matter what style of configuration you have chosen the next step is to call serialize methods of `TypeManager` with providing a type and data you want to process.
229
-
230
- ```typescript
231
- import { TypeManager } from '@dipscope/type-manager';
232
-
233
- const userObject = TypeManager.serialize(User, new User());
234
- const user = TypeManager.deserialize(User, userObject);
235
-
236
- user instanceof User; // True.
237
- ```
238
-
239
- Calling serialize creates a plain object and deserialize creates an instance of `User` class. During deserialize you can provide any object. It's not nesassary that object was produced by `TypeManager`. If object is an `Array` you will get array of types in return. Objects are parsed based on general type configuration defined by developer. It is also possible to stringify and parse JSON.
240
-
241
- ```typescript
242
- import { TypeManager } from '@dipscope/type-manager';
243
-
244
- const userJson = TypeManager.stringify(User, new User());
245
- const user = TypeManager.parse(User, userJson);
246
-
247
- user instanceof User; // True.
248
- ```
249
-
250
- Stringify and parse functions are wrappers over native JSON class functions. In addition they add serialize and deserialize support under the hood.
251
-
252
- Static functions are not the only way to work with a `TypeManager`. You can also work on instance based manner.
253
-
254
- ```typescript
255
- import { TypeManager } from '@dipscope/type-manager';
256
-
257
- const userManager = new TypeManager(User);
258
- const userObject = userManager.serialize(new User());
259
- const user = userManager.deserialize(userObject);
260
-
261
- user instanceof User; // True.
262
- ```
263
-
264
- At first glance, it may seems that there is no difference but creating an instance of `TypeManager` preserves a configuration state. You can work with different configurations at the same time by providing type manager options as a second optional constructor argument. If it is not required then it is better to use static methods. They are not invoking state control which results in better performance.
265
-
266
- ## Defining decorators
267
-
268
- There are few decorators which controls the main flow. This are `Type`, `Property` and `Inject` decorators. Let's go through each of them.
269
-
270
- ### Type decorator
271
-
272
- Type decorator defines a type and should be declared right before a class.
273
-
274
- ```typescript
275
- import { Type } from '@dipscope/type-manager';
276
-
277
- @Type()
278
- export class User
279
- {
280
- ...
281
- }
282
- ```
283
-
284
- This will register a new type with default type serializer assigned to it. You can define how each class should be treated by providing optional configure options as a first argument.
285
-
286
- ```typescript
287
- import { Type } from '@dipscope/type-manager';
288
-
289
- @Type({
290
- alias: 'User',
291
- serializer: new UserSerializer()
292
- })
293
- export class User
294
- {
295
- ...
296
- }
297
- ```
298
-
299
- This call defines a type alias which can be later used to resolve a type for a property at runtime. We will talk about details in the property decorator section. Also we defined custom serializer for a type which is an implementation of `Serializer` interface. This serializer will be used later to serialize and deserialize a type including all custom logic of your choice. You can read more about [creating a custom serializer](#defining-custom-serializer) in a separate section.
300
-
301
- There are more options can be provided for a type, so check `TypeOptions` definition or section with [decorator options](#defining-decorator-options) below.
302
-
303
- ### Property decorator
304
-
305
- Property decorator defines per property configuration within a type and should be declared right before a property or accessor definition.
306
-
307
- ```typescript
308
- import 'reflect-metadata';
309
- import { Type, Property } from '@dipscope/type-manager';
310
-
311
- @Type()
312
- export class User
313
- {
314
- @Property() public name: string;
315
- }
316
- ```
317
-
318
- This will register a `name` property for a `User`. Each property has a type associated with it. In our case this is a `String`. By default if no configure options are provided decorator will try to resolve a property type using [reflect-metadata](https://github.com/rbuckton/reflect-metadata). If you are not using reflect metadata then such configuration will result a property type to be `unknown` and you will get an error during serialization. For such a case you have to explicitly define a property type.
319
-
320
- ```typescript
321
- import { Type, Property } from '@dipscope/type-manager';
322
-
323
- @Type()
324
- export class User
325
- {
326
- @Property(String) public name: string;
327
- }
328
- ```
329
-
330
- Depending on your use case there are two possible ways to configure additional property options.
331
-
332
- If you are using [reflect-metadata](https://github.com/rbuckton/reflect-metadata) then provide options as a first argument.
333
-
334
- ```typescript
335
- import 'reflect-metadata';
336
- import { Type, Property } from '@dipscope/type-manager';
337
-
338
- @Type()
339
- export class User
340
- {
341
- @Property({ alias: 'username' }) public name: string;
342
- }
343
- ```
344
-
345
- If types defined explicitly then provide options as a second argument.
346
-
347
- ```typescript
348
- import { Type, Property } from '@dipscope/type-manager';
349
-
350
- @Type()
351
- export class User
352
- {
353
- @Property(String, { alias: 'username' }) public name: string;
354
- }
355
- ```
356
-
357
- This option configures an alias so `username` property will be used instead of `name` when deserializing from object. There are plenty of configure options, so check `PropertyOptions` definition or section with [decorator options](#defining-decorator-options) below. For example you can make some properties serializable only or define custom property serialization.
358
-
359
- Now let's have a look at more complex definitions with generic types. This are `Array<TType>`, `Map<TKey, TValue>` and others. To declare one of this you have to use extra argument available for `Property` decorator. Generic arguments are always passed as array to exactly see them within a source code.
360
-
361
- If you are using [reflect-metadata](https://github.com/rbuckton/reflect-metadata) then provide generics as a first argument so configure options will become the second.
362
-
363
- ```typescript
364
- import 'reflect-metadata';
365
- import { Type, Property } from '@dipscope/type-manager';
366
-
367
- @Type()
368
- export class User
369
- {
370
- @Property([String, Number], { alias: 'myMap' }) public map: Map<string, number>;
371
- }
372
- ```
373
-
374
- If types defined explicitly then provide generics as a second argument so configure options will become the third.
375
-
376
- ```typescript
377
- import { Type, Property } from '@dipscope/type-manager';
378
-
379
- @Type()
380
- export class User
381
- {
382
- @Property(Map, [String, Number], { alias: 'myMap' }) public map: Map<string, number>;
383
- }
384
- ```
385
-
386
- This is a full set of arguments available for the property. Basically when using [reflect-metadata](https://github.com/rbuckton/reflect-metadata) you have just to omit the first argument.
387
-
388
- We try to simplify declarations as much as possible so you are free to use only configure options if you want and don't ever think about `Property` decorator arguments.
389
-
390
- ```typescript
391
- import { Type, Property } from '@dipscope/type-manager';
392
-
393
- @Type()
394
- export class User
395
- {
396
- @Property({
397
- typeArgument: Map,
398
- genericArguments: [String, Number],
399
- alias: 'myMap'
400
- })
401
- public map: Map<string, number>;
402
- }
403
- ```
404
-
405
- Which syntax to use is completely on your choice. `Property` decorator is smart enough to setup everything based on usage.
406
-
407
- Now let's talk a bit about relation types. They are not differ from built in types, so if you are using [reflect-metadata](https://github.com/rbuckton/reflect-metadata) the definition can be the following.
408
-
409
- ```typescript
410
- import 'reflect-metadata';
411
- import { Type, Property } from '@dipscope/type-manager';
412
-
413
- @Type()
414
- export class UserStatus
415
- {
416
- @Property() public title: string;
417
- }
418
-
419
- @Type()
420
- export class User
421
- {
422
- @Property() public userStatus: UserStatus;
423
- }
424
- ```
425
-
426
- With array of relations you have to use generics.
427
-
428
- ```typescript
429
- import 'reflect-metadata';
430
- import { Type, Property } from '@dipscope/type-manager';
431
-
432
- @Type()
433
- export class UserStatus
434
- {
435
- @Property() public title: string;
436
- }
437
-
438
- @Type()
439
- export class User
440
- {
441
- @Property([UserStatus]) public userStatuses: UserStatus[];
442
- }
443
- ```
444
-
445
- If types defined explicitly then definition will be the following.
446
-
447
- ```typescript
448
- import { Type, Property } from '@dipscope/type-manager';
449
-
450
- @Type()
451
- export class UserStatus
452
- {
453
- @Property(String) public title: string;
454
- }
455
-
456
- @Type()
457
- export class User
458
- {
459
- @Property(UserStatus) public userStatus: UserStatus;
460
- }
461
- ```
462
-
463
- Then for array of relations it will be the following.
464
-
465
- ```typescript
466
- import { Type, Property } from '@dipscope/type-manager';
467
-
468
- @Type()
469
- export class UserStatus
470
- {
471
- @Property(String) public title: string;
472
- }
473
-
474
- @Type()
475
- export class User
476
- {
477
- @Property(Array, [UserStatus]) public userStatuses: UserStatus[];
478
- }
479
- ```
480
-
481
- If any type defines an alias - it can be used as a type resolver.
482
-
483
- ```typescript
484
- import { Type, Property } from '@dipscope/type-manager';
485
-
486
- @Type({
487
- alias: 'UserStatus'
488
- })
489
- export class UserStatus
490
- {
491
- @Property(String) public title: string;
492
- }
493
-
494
- @Type()
495
- export class User
496
- {
497
- @Property('UserStatus') public userStatus: UserStatus;
498
- }
499
- ```
500
-
501
- If you have circular reference or your type declared later an extended syntax can be used to lazily define a type.
502
-
503
- ```typescript
504
- import { Type, Property } from '@dipscope/type-manager';
505
-
506
- @Type()
507
- export class User
508
- {
509
- @Property(() => UserStatus) public userStatus: UserStatus;
510
- }
511
-
512
- @Type()
513
- export class UserStatus
514
- {
515
- @Property(() => String) public title: string;
516
- }
517
- ```
518
-
519
- One great thing to know about arguments for property type and generics is that you can pass lazy function, type directly or type alias. Which definition to use is completely on your choice and dependent from certain use cases.
520
-
521
- While property type arguments exactly match to `TypeScript` types there is a one exception for this rule. This is `Enum`. You have to provide `String` type for a string based `Enum` and `Number` type for a number based `Enum`. This is because of how `Enum` is represented after compiling it to `JavaScript`. You can read more about this [here](https://www.typescriptlang.org/docs/handbook/enums.html).
522
-
523
- If you are using [reflect-metadata](https://github.com/rbuckton/reflect-metadata) this will be done automatically so no additional steps are required from your side.
524
-
525
- ```typescript
526
- import 'reflect-metadata';
527
- import { Type, Property } from '@dipscope/type-manager';
528
-
529
- export enum UserPriorityNumeric
530
- {
531
- Low,
532
- Medium,
533
- High
534
- }
535
-
536
- export enum UserPriorityTextual
537
- {
538
- Low = 'Low',
539
- Medium = 'Medium',
540
- High = 'High'
541
- }
542
-
543
- @Type()
544
- export class User
545
- {
546
- @Property() public userPriorityNumeric: UserPriorityNumeric;
547
- @Property() public userPriorityTextual: UserPriorityTextual;
548
- }
549
- ```
550
-
551
- If types defined explicitly then definition will be the following.
552
-
553
- ```typescript
554
- import { Type, Property } from '@dipscope/type-manager';
555
-
556
- export enum UserPriorityNumeric
557
- {
558
- Low,
559
- Medium,
560
- High
561
- }
562
-
563
- export enum UserPriorityTextual
564
- {
565
- Low = 'Low',
566
- Medium = 'Medium',
567
- High = 'High'
568
- }
569
-
570
- @Type()
571
- export class User
572
- {
573
- @Property(Number) public userPriorityNumeric: UserPriorityNumeric;
574
- @Property(String) public userPriorityTextual: UserPriorityTextual;
575
- }
576
- ```
577
-
578
- One should remember this when explicitly defining types for enums.
579
-
580
- ### Inject decorator
581
-
582
- Inject decorator controls your type dependency and declared right before a constructor parameter.
583
-
584
- ```typescript
585
- import { Type, Inject } from '@dipscope/type-manager';
586
-
587
- @Type()
588
- export class User
589
- {
590
- public constructor(@Inject('name') name: string, @Inject(UserService) userService: UserService)
591
- {
592
- this.name = name;
593
-
594
- // Any action with UserService...
595
-
596
- return;
597
- }
598
- }
599
- ```
600
-
601
- It accepts two possible inputs as its argument.
602
-
603
- * String key from JSON context;
604
- * Certain type registered as injectable;
605
-
606
- When a string key is provided then a certain value will be resolved from JSON context for you when creating an instance. If any property declares the same key you will recieve deserialized value. If not then original value will be provided instead. Injecting context values is a use case of immutable types.
607
-
608
- When a certain type is provided it will be resolved from the dependency injection container. If you are going to use internal type injector then you should register injectable types as the following. By default singleton injector is used to resolve such services.
609
-
610
- ```typescript
611
- import { Type } from '@dipscope/type-manager';
612
-
613
- @Type({
614
- injectable: true
615
- })
616
- export class UserService
617
- {
618
- public property: string;
619
- }
620
- ```
621
-
622
- In most cases you will work in environment where dependency injection system is already setted up. In this case you have to implement custom `Injector` to be used instead of our default one. Besides you should follow the steps to register injectable services specified by the vendor. You can read more about [creating a custom injector](#defining-custom-injector) in a separate section.
623
-
624
- If you are using [reflect-metadata](https://github.com/rbuckton/reflect-metadata) the injection of services can be simplified.
625
-
626
- ```typescript
627
- import { Type, Inject } from '@dipscope/type-manager';
628
-
629
- @Type()
630
- export class User
631
- {
632
- public constructor(@Inject('name') name: string, userService: UserService)
633
- {
634
- this.name = name;
635
-
636
- // Any action with UserService...
637
-
638
- return;
639
- }
640
- }
641
- ```
642
-
643
- Note that now you don't have to specify injection for types explicitly. However injection of values by key from JSON context still present. It's because argument names cannot be resolved using reflection.
644
-
645
- ## Defining decorator options
646
-
647
- `Type` and `Property` decorators provide full configuration for your classes using configure options. In this section we will go through each of them.
648
-
649
- ### Alias option
650
-
651
- This option can be used both on type and property to define an alias.
652
-
653
- ```typescript
654
- import { Type, Property } from '@dipscope/type-manager';
655
-
656
- @Type({
657
- alias: 'User'
658
- })
659
- export class User
660
- {
661
- @Property(String, { alias: 'username' }) public name: string;
662
- }
663
- ```
664
-
665
- Alias defined for a class can be used later for resolving property types. Note that it should be unique within application to work properly.
666
-
667
- Alias defined for a property declares that property name differs from one specified in JSON. In our case `username` will be used instead of `name` during JSON serialization and deserialization.
668
-
669
- ### Custom data option
670
-
671
- This option can be used to provide any custom data for type or property.
672
-
673
- ```typescript
674
- import { Type, Property } from '@dipscope/type-manager';
675
-
676
- @Type({
677
- customData: { rank: 1 }
678
- })
679
- export class User
680
- {
681
- @Property(String, { customData: { order: 1 } }) public name: string;
682
- }
683
- ```
684
-
685
- This custom data later can be accessed in serializers, factories, injectors or your code and used accordingly. Read more about [defining custom data](#defining-custom-data) in a separate section.
686
-
687
- ### Default value option
688
-
689
- This option is used to define a default value when one is undefined. It can be used on type or property.
690
-
691
- ```typescript
692
- import { Type, Property } from '@dipscope/type-manager';
693
-
694
- @Type({
695
- defaultValue: () => new User()
696
- })
697
- export class User
698
- {
699
- @Property(String, { defaultValue: 'BestName' }) public name: string;
700
- }
701
- ```
702
-
703
- This will affect both serialized and deserialized default value. This will fit perfectly for most types. You can also specify serialized and deserialized default value explicitly for complex types by using two other options.
704
-
705
- ```typescript
706
- import { Type, Property } from '@dipscope/type-manager';
707
-
708
- @Type({
709
- serializedDefaultValue: () => new User(),
710
- deserializedDefaultValue: () => new User()
711
- })
712
- export class User
713
- {
714
- @Property(String, { serializedDefaultValue: 'SerializedName', deserializedDefaultValue: 'DeserializedName' }) public name: string;
715
- }
716
- ```
717
-
718
- As you can see it accepts an arrow function or a certain value. Both are valid for type and property. Using default values is turned off by default. You can enable them using `useDefaultValue` option per type and property or enable globally using `TypeManager` configure method.
719
-
720
- ### Deserializable option
721
-
722
- This option is used to enable or disable deserialization for a certain property.
723
-
724
- ```typescript
725
- import { Type, Property } from '@dipscope/type-manager';
726
-
727
- @Type()
728
- export class User
729
- {
730
- @Property(String, { deserializable: true }) public name: string;
731
- }
732
- ```
733
-
734
- By default all properties are deserializable.
735
-
736
- ### Discriminant option
737
-
738
- This option is used to define a custom discriminant for a type which is later used during serialization and deserialization of polymorphic types.
739
-
740
- ```typescript
741
- import { Type, Property } from '@dipscope/type-manager';
742
-
743
- @Type({
744
- discriminant: 'Company.Api.Entities.User'
745
- })
746
- export class User
747
- {
748
- @Property(String) public name: string;
749
- }
750
- ```
751
-
752
- You can read more about handling of polymorphic types in this [section](#configuring-usage-of-polymorphic-types).
753
-
754
- ### Discriminator option
755
-
756
- This option can be used to define a custom property which stores discriminant of polymorphic type.
757
-
758
- ```typescript
759
- import { Type, Property } from '@dipscope/type-manager';
760
-
761
- @Type({
762
- discriminator: '__typename__'
763
- })
764
- export class User
765
- {
766
- @Property(String) public name: string;
767
- }
768
- ```
769
-
770
- In common use cases discriminator should be set globally using `TypeManager` configure method. Using this option on a type level makes sense only if discriminator differs from the global one. You can read more about handling of polymorphic types in this [section](#configuring-usage-of-polymorphic-types).
771
-
772
- ### Factory option
773
-
774
- This option can be used to register a handler which should be used for constructing a type instead of default one.
775
-
776
- ```typescript
777
- import { Type, Property } from '@dipscope/type-manager';
778
-
779
- @Type({
780
- factory: new UserFactory()
781
- })
782
- export class User
783
- {
784
- @Property(String) public name: string;
785
- }
786
- ```
787
-
788
- This may be useful in cases when you want to init some special application specific properties. Read more about [defining custom factory](#defining-custom-factory) in a separate section.
789
-
790
- ### Injectable option
791
-
792
- This option is used to register a type in dependency injection container.
793
-
794
- ```typescript
795
- import { Type } from '@dipscope/type-manager';
796
-
797
- @Type({
798
- injectable: true
799
- })
800
- export class UserService
801
- {
802
- public property: string;
803
- }
804
- ```
805
-
806
- Injectable type later can be provided as a dependency.
807
-
808
- ```typescript
809
- import { Type, Property, Inject } from '@dipscope/type-manager';
810
-
811
- @Type()
812
- export class User
813
- {
814
- @Property(String) public name: string;
815
-
816
- public constructor(@Inject(UserService) userService: UserService)
817
- {
818
- // Any action with UserService...
819
-
820
- return;
821
- }
822
- }
823
- ```
824
-
825
- In most cases you will work in environment where dependency injection system is already setted up. In this case you have to implement custom `Injector` to be used instead of our default one. Besides you should follow the steps to register injectable services specified by the vendor. You can read more about [creating a custom injector](#defining-custom-injector) in a separate section.
826
-
827
- ### Injector option
828
-
829
- This option can be used to define a custom injector implementation which should be used in a type scope.
830
-
831
- ```typescript
832
- import { Type, Property } from '@dipscope/type-manager';
833
-
834
- @Type({
835
- injector: new UserInjector()
836
- })
837
- export class User
838
- {
839
- @Property(String) public name: string;
840
- }
841
- ```
842
-
843
- In most cases this is not required and the common use case is to specify injector globally instead. You can read more about [defining custom injector](#defining-custom-injector) in a separate section.
844
-
845
- ### Naming convention option
846
-
847
- This option can be used both on type and property to provide custom naming strategy.
848
-
849
- ```typescript
850
- import { Type, Property, CamelCaseNamingConvention, SnakeCaseNamingConvention } from '@dipscope/type-manager';
851
-
852
- @Type({
853
- namingConvention: new CamelCaseNamingConvention()
854
- })
855
- export class User
856
- {
857
- @Property(String, { namingConvention: new SnakeCaseNamingConvention() }) public name: string;
858
- }
859
- ```
860
-
861
- In most cases this is not required and the common use case is to specify naming strategy globally instead. You can read more about [configuring naming convention](#configuring-naming-convention) in a separate section.
862
-
863
- ### Preserve discriminator option
864
-
865
- This option defines if discriminator should be preserved in objects during serialization and deserialization.
866
-
867
- ```typescript
868
- import { Type, Property } from '@dipscope/type-manager';
869
-
870
- @Type({
871
- preserveDiscriminator: true
872
- })
873
- export class User
874
- {
875
- @Property(String) public name: string;
876
- }
877
- ```
878
-
879
- By default discriminator is not preserved and only used during deserialization of polymorphic types. You can read more about handling of polymorphic types in this [section](#configuring-usage-of-polymorphic-types).
880
-
881
- ### Preserve null option
882
-
883
- This option defines if null values should be preserved during serialization and deserialization.
884
-
885
- ```typescript
886
- import { Type, Property } from '@dipscope/type-manager';
887
-
888
- @Type({
889
- preserveNull: true
890
- })
891
- export class User
892
- {
893
- @Property(String, { preserveNull: false }) public name: string;
894
- }
895
- ```
896
-
897
- By default null values are preserved. You can set it to `false` per type, property or globally using `TypeManager` configure method. This will result in treating null values as undefined so you will get all related behaviours like setting default values.
898
-
899
- ### Reference handler option
900
-
901
- This option can be used both on type and property to specify how references to the same objects should be handled during serialization and deserialization.
902
-
903
- ```typescript
904
- import { Type, Property, DirectReferenceHandler, LeadReferenceHandler } from '@dipscope/type-manager';
905
-
906
- @Type({
907
- referenceHandler: new DirectReferenceHandler()
908
- })
909
- export class User
910
- {
911
- @Property(String, { referenceHandler: new LeadReferenceHandler() }) public name: string;
912
- }
913
- ```
914
-
915
- In most cases this is not required and the common use case is to specify reference handler globally instead. You can read more about [configuring reference handler](#configuring-reference-handler) in a separate section.
916
-
917
- ### Serializable option
918
-
919
- This option is used to enable or disable serialization for a certain property.
920
-
921
- ```typescript
922
- import { Type, Property } from '@dipscope/type-manager';
923
-
924
- @Type()
925
- export class User
926
- {
927
- @Property(String, { serializable: true }) public name: string;
928
- }
929
- ```
930
-
931
- By default all properties are serializable.
932
-
933
- ### Serializer option
934
-
935
- This option is used to define custom serializer for a type or property.
936
-
937
- ```typescript
938
- import { Type, Property } from '@dipscope/type-manager';
939
-
940
- @Type({
941
- serializer: new UserSerializer()
942
- })
943
- export class User
944
- {
945
- @Property(String, { serializer: new UserNameSerializer() }) public name: string;
946
- }
947
- ```
948
-
949
- Custom serializer should be an implementation of `Serializer` interface. You can read more about [creating a custom serializer](#defining-custom-serializer) in a separate section.
950
-
951
- ### Use default value option
952
-
953
- This option enables or disables using default value per type or property.
954
-
955
- ```typescript
956
- import { Type, Property } from '@dipscope/type-manager';
957
-
958
- @Type({
959
- useDefaultValue: true
960
- })
961
- export class User
962
- {
963
- @Property(String, { useDefaultValue: false }) public name: string;
964
- }
965
- ```
966
-
967
- Using default values is turned off by default. You can enable them globally using `TypeManager` configure method.
968
-
969
- ### Use implicit conversion option
970
-
971
- By default if declared type will not match provided during serialization or deserialization an error will be logged and result value will be undefined. This means that for example assigning `Number` to `String` will not work as `StringSerializer` expects `String`. However `Number` and other types can be converted to `String` for you when implicit conversion is enabled.
972
-
973
- ```typescript
974
- import { Type, Property } from '@dipscope/type-manager';
975
-
976
- @Type()
977
- export class User
978
- {
979
- @Property(String, { useImplicitConversion: true }) public name: string;
980
- }
981
- ```
982
-
983
- With this any value which can be converted to `String` will be converted properly. Such behaviour works for other built in serializers and supported for custom ones. By default implicit conversion is turned off. You can enable it using `useImplicitConversion` option per type and property or enable globally using `TypeManager` configure method.
984
-
985
- ## Defining configuration manually
986
-
987
- There are circumstances when decorators cannot be used or you don't want to. For example you are using a 3rd party package and cannot decorate classes from it. Another use case when you want to configure some options globally. In such scenarios you can define the complete configuration through special static configure methods.
988
-
989
- We have separate methods to configure each type manager option, so the provided examples can be simplified to avoid creating additional object. It is useful when you need to configure only one option. In our examples we are always use the main one to give you a general overview.
990
-
991
- ### Configuring global options
992
-
993
- There are several options which can be configured globally. For example let's override using of default value option so we don't have to specify it explicitly per type or property.
994
-
995
- ```typescript
996
- import { TypeManagerOptions, TypeOptionsBase } from '@dipscope/type-manager';
997
-
998
- const typeOptionsBase: TypeOptionsBase<any> = {
999
- useDefaultValue: true
1000
- };
1001
-
1002
- const typeManagerOptions: TypeManagerOptions = {
1003
- typeOptionsBase: typeOptionsBase
1004
- };
1005
-
1006
- TypeManager.configure(typeManagerOptions);
1007
- ```
1008
-
1009
- For the full list of available global options check `TypeOptionsBase` definition or follow the documentation as we are going to touch them while we proceed.
1010
-
1011
- ### Configuring options per type
1012
-
1013
- Here is an example of declarative configuration which can be used for 3rd party or your own classes in addition to decorators.
1014
-
1015
- ```typescript
1016
- import { DateTime } from '@external-library';
1017
- import { TypeManagerOptions, TypeFn, TypeOptions, PropertyName, PropertyOptions } from '@dipscope/type-manager';
1018
-
1019
- const dateTimeOptions: TypeOptions<DateTime> = {
1020
- alias: 'DateTime',
1021
- serializer: new DateTimeSerializer()
1022
- };
1023
-
1024
- const userOptions: TypeOptions<User> = {
1025
- alias: 'User',
1026
- propertyOptionsMap: new Map<PropertyName, PropertyOptions<any>>(
1027
- ['name', { typeArgument: String, serializable: true, alias: 'username' }],
1028
- ['createdAt', { typeArgument: DateTime }]
1029
- )
1030
- };
1031
-
1032
- const typeManagerOptions: TypeManagerOptions = {
1033
- typeOptionsMap: new Map<TypeFn<any>, TypeOptions<any>>(
1034
- [DateTime, dateTimeOptions],
1035
- [User, userOptions]
1036
- )
1037
- };
1038
-
1039
- TypeManager.configure(typeManagerOptions);
1040
- ```
1041
-
1042
- There is a well defined order to how type options are applied when using configure methods on one type. One should remember this when combining and overriding options in different places.
1043
-
1044
- 1. Base type options are applied;
1045
- 2. Decorator type options are applied;
1046
- 3. Declarative type options are applied;
1047
- 4. Property type options are applied;
1048
-
1049
- Declarative configuration supports the same options as decorators. With such a configuration you can declare types like the following.
1050
-
1051
- ```typescript
1052
- export class User
1053
- {
1054
- public name: string;
1055
- public createdAt: DateTime;
1056
- }
1057
- ```
1058
-
1059
- Also if you are declaring only 3rd party classes the use case can be the following with [reflect-metadata](https://github.com/rbuckton/reflect-metadata).
1060
-
1061
- ```typescript
1062
- import 'reflect-metadata';
1063
- import { Type, Property } from '@dipscope/type-manager';
1064
-
1065
- @Type()
1066
- export class User
1067
- {
1068
- @Property() public name: string;
1069
- @Property() public createdAt: DateTime;
1070
- }
1071
- ```
1072
-
1073
- And without [reflect-metadata](https://github.com/rbuckton/reflect-metadata).
1074
-
1075
- ```typescript
1076
- import { Type, Property } from '@dipscope/type-manager';
1077
-
1078
- @Type()
1079
- export class User
1080
- {
1081
- @Property(String) public name: string;
1082
- @Property(DateTime) public createdAt: DateTime;
1083
- }
1084
- ```
1085
-
1086
- ### Configuring usage of polymorphic types
1087
-
1088
- Let's assume we are working with a shapes. To describe different types of shape we have to create an abstract class with several descendants.
1089
-
1090
- ```typescript
1091
- import { Type, Property } from '@dipscope/type-manager';
1092
-
1093
- @Type()
1094
- export abstract class Shape
1095
- {
1096
- @Property(String) public title: string;
1097
- }
1098
-
1099
- @Type()
1100
- export class Rectangle extends Shape
1101
- {
1102
- @Property(Number) public width: number;
1103
- @Property(Number) public height: number;
1104
- }
1105
-
1106
- @Type()
1107
- export class Square extends Shape
1108
- {
1109
- @Property(Number) public width: number;
1110
- }
1111
-
1112
- @Type()
1113
- export class Circle extends Shape
1114
- {
1115
- @Property(Number) public radius: number;
1116
- }
1117
- ```
1118
-
1119
- Some other class declares a `shapes` property in it.
1120
-
1121
- ```typescript
1122
- import { Type, Property } from '@dipscope/type-manager';
1123
-
1124
- @Type()
1125
- export class Plot
1126
- {
1127
- @Property(Array, [Shape]) public shapes: Shape[];
1128
- }
1129
- ```
1130
-
1131
- From the perspective of declaration everything looks ok but from the point of serialization some things may become complicated. Shapes property can store `Rectangle`, `Square` or `Circle`. Each of this classes have different properties. Here is an example of JSON.
1132
-
1133
- ```json
1134
- {
1135
- "shapes": [
1136
- {
1137
- "title": "Cool rectangle",
1138
- "width": 10,
1139
- "height": 10
1140
- },
1141
- {
1142
- "title": "Perfect square",
1143
- "width": 10
1144
- },
1145
- {
1146
- "title": "Simple circle",
1147
- "radius": 6
1148
- }
1149
- ]
1150
- }
1151
- ```
1152
-
1153
- During deserialization of this JSON to a `Plot` class we only aware that all plain objects inside an `Array` are somehow related to a `Shape` type. So any options to handle this?
1154
-
1155
- Luckily we have a `TypeManager`. When you declaring you types using decorators or declarative style it builds inheritance graph between them which can be used during serialization and deserialization.
1156
-
1157
- It uses 2 special configurable type options:
1158
-
1159
- * `Discriminator` which defines a property inside an object which should be used to define a type.
1160
- * `Discriminant` which represents a certain `Discriminator` value.
1161
-
1162
- This options have default values if you have not configured them explicitly.
1163
-
1164
- * Default value of discriminator is a `$type`. During deserialization `TypeManager` expects such property to be present inside a polymorphic object.
1165
- * Default value of discriminant is a `ClassName` which determined based on the type function.
1166
-
1167
- For proper deserialization of polymorphic types you have to provide such information inside your JSON.
1168
-
1169
- ```json
1170
- {
1171
- "shapes": [
1172
- {
1173
- "$type": "Rectangle",
1174
- "title": "Cool rectangle",
1175
- "width": 10,
1176
- "height": 10
1177
- },
1178
- {
1179
- "$type": "Square",
1180
- "title": "Perfect square",
1181
- "width": 10
1182
- },
1183
- {
1184
- "$type": "Circle",
1185
- "title": "Simple circle",
1186
- "radius": 6
1187
- }
1188
- ]
1189
- }
1190
- ```
1191
-
1192
- Now your JSON will be handled properly and you will get `Rectangle`, `Square` and `Circle` class instances in return.
1193
-
1194
- In some cases your `Discriminator` or `Discriminant` values will not match to our default ones. For example library like [Json.NET](https://www.newtonsoft.com/json) can be used on the backend side to send a response from your API. It uses `$type` property as `Discriminator` and full name of class as `Discriminant`. In such scenario our JSON may look like this.
1195
-
1196
- ```json
1197
- {
1198
- "shapes": [
1199
- {
1200
- "$type": "Company.Api.Entities.Rectangle",
1201
- "title": "Cool rectangle",
1202
- "width": 10,
1203
- "height": 10
1204
- },
1205
- {
1206
- "$type": "Company.Api.Entities.Square",
1207
- "title": "Perfect square",
1208
- "width": 10
1209
- },
1210
- {
1211
- "$type": "Company.Api.Entities.Circle",
1212
- "title": "Simple circle",
1213
- "radius": 6
1214
- }
1215
- ]
1216
- }
1217
- ```
1218
-
1219
- To change `Discriminator` globally you have to use `TypeManager` configure method.
1220
-
1221
- ```typescript
1222
- import { TypeManagerOptions, TypeOptionsBase } from '@dipscope/type-manager';
1223
-
1224
- const typeOptionsBase: TypeOptionsBase<any> = {
1225
- discriminator: '$customType'
1226
- };
1227
-
1228
- const typeManagerOptions: TypeManagerOptions = {
1229
- typeOptionsBase: typeOptionsBase
1230
- };
1231
-
1232
- TypeManager.configure(typeManagerOptions);
1233
- ```
1234
-
1235
- To change `Discriminant` you have to use per type configuration.
1236
-
1237
- ```typescript
1238
- import { TypeManagerOptions, TypeFn, TypeOptions, PropertyName, PropertyOptions } from '@dipscope/type-manager';
1239
-
1240
- const rectangleOptions: TypeOptions<Rectangle> = {
1241
- discriminant: 'Company.Api.Entities.Rectangle'
1242
- };
1243
-
1244
- const squareOptions: TypeOptions<Square> = {
1245
- discriminant: 'Company.Api.Entities.Square'
1246
- };
1247
-
1248
- const circleOptions: TypeOptions<Circle> = {
1249
- discriminant: 'Company.Api.Entities.Circle'
1250
- };
1251
-
1252
- const typeManagerOptions: TypeManagerOptions = {
1253
- typeOptionsMap: new Map<TypeFn<any>, TypeOptions<any>>(
1254
- [Rectangle, rectangleOptions],
1255
- [Square, squareOptions],
1256
- [Circle, circleOptions]
1257
- )
1258
- };
1259
-
1260
- TypeManager.configure(typeManagerOptions);
1261
- ```
1262
-
1263
- As an alternative you can change `Discriminant` as the following using decorators.
1264
-
1265
- ```typescript
1266
- import { Type, Property } from '@dipscope/type-manager';
1267
-
1268
- @Type({
1269
- discriminant: 'Company.Api.Entities.Shape'
1270
- })
1271
- export abstract class Shape
1272
- {
1273
- @Property(String) public title: string;
1274
- }
1275
-
1276
- @Type({
1277
- discriminant: 'Company.Api.Entities.Rectangle'
1278
- })
1279
- export class Rectangle extends Shape
1280
- {
1281
- @Property(Number) public width: number;
1282
- @Property(Number) public height: number;
1283
- }
1284
-
1285
- @Type({
1286
- discriminant: 'Company.Api.Entities.Square'
1287
- })
1288
- export class Square extends Shape
1289
- {
1290
- @Property(Number) public width: number;
1291
- }
1292
-
1293
- @Type({
1294
- discriminant: 'Company.Api.Entities.Circle'
1295
- })
1296
- export class Circle extends Shape
1297
- {
1298
- @Property(Number) public radius: number;
1299
- }
1300
- ```
1301
-
1302
- By default `Discriminator` is not preserved inside objects and only used during deserialization. You can change this behavior by enabling preserving of discriminator globally or per type.
1303
-
1304
- ```typescript
1305
- import { TypeManagerOptions, TypeOptionsBase } from '@dipscope/type-manager';
1306
-
1307
- const typeOptionsBase: TypeOptionsBase<any> = {
1308
- preserveDiscriminator: true
1309
- };
1310
-
1311
- const typeManagerOptions: TypeManagerOptions = {
1312
- typeOptionsBase: typeOptionsBase
1313
- };
1314
-
1315
- TypeManager.configure(typeManagerOptions);
1316
- ```
1317
-
1318
- With this option enabled discriminator will be present in output data.
1319
-
1320
- ### Configuring naming convention
1321
-
1322
- Naming convention specifies how each declared property of a type should be treated when reading it from JSON. By default names are read as is. Let's assume we have a `User` class in camel case naming convention for properties.
1323
-
1324
- ```typescript
1325
- import { Type, Property } from '@dipscope/type-manager';
1326
-
1327
- @Type()
1328
- export class User
1329
- {
1330
- @Property(String) public name: string;
1331
- @Property(Number) public loginCount: number;
1332
- @Property(DateTime) public createdAt: DateTime;
1333
- }
1334
- ```
1335
-
1336
- Our JSON should match the same naming convention to work properly.
1337
-
1338
- ```json
1339
- [
1340
- {
1341
- "name": "Dmitry",
1342
- "loginCount": 10,
1343
- "createdAt": "2021-02-22T20:15:00.000Z"
1344
- },
1345
- {
1346
- "name": "Alex",
1347
- "loginCount": 25,
1348
- "createdAt": "2021-02-22T21:15:00.000Z"
1349
- },
1350
- {
1351
- "name": "Anna",
1352
- "loginCount": 3,
1353
- "createdAt": "2021-02-22T21:15:23.000Z"
1354
- }
1355
- ]
1356
- ```
1357
-
1358
- But what to do if we don't control the JSON naming convention so it comes to us in a snake case?
1359
-
1360
- ```json
1361
- [
1362
- {
1363
- "name": "Dmitry",
1364
- "login_count": 10,
1365
- "created_at": "2021-02-22T20:15:00.000Z"
1366
- },
1367
- {
1368
- "name": "Alex",
1369
- "login_count": 25,
1370
- "created_at": "2021-02-22T21:15:00.000Z"
1371
- },
1372
- {
1373
- "name": "Anna",
1374
- "login_count": 3,
1375
- "created_at": "2021-02-22T21:15:23.000Z"
1376
- }
1377
- ]
1378
- ```
1379
-
1380
- We can still parse such a JSON by specifying an alias for each property but this will become a pain in a while.
1381
-
1382
- ```typescript
1383
- import { Type, Property } from '@dipscope/type-manager';
1384
-
1385
- @Type()
1386
- export class User
1387
- {
1388
- @Property(String) public name: string;
1389
- @Property(Number, { alias: 'login_count' }) public loginCount: number;
1390
- @Property(DateTime, { alias: 'created_at' }) public createdAt: DateTime;
1391
- }
1392
- ```
1393
-
1394
- `TypeManager` supports several naming conventions we can use to avoid dealing with aliases.
1395
-
1396
- * Camel case [camelCase];
1397
- * Flat case [flatcase];
1398
- * Flat upper case [FLATCASE];
1399
- * Kebab case [kebab-kase];
1400
- * Kebab upper case [KEBAB-CASE];
1401
- * Pascal case [PascalCase];
1402
- * Snake case [snake_case];
1403
- * Snake upper case [SNAKE_CASE];
1404
-
1405
- To set one we have to configure global options.
1406
-
1407
- ```typescript
1408
- import { TypeManagerOptions, TypeOptionsBase, SnakeCaseNamingConvention } from '@dipscope/type-manager';
1409
-
1410
- const typeOptionsBase: TypeOptionsBase<any> = {
1411
- namingConvention: new SnakeCaseNamingConvention()
1412
- };
1413
-
1414
- const typeManagerOptions: TypeManagerOptions = {
1415
- typeOptionsBase: typeOptionsBase
1416
- };
1417
-
1418
- TypeManager.configure(typeManagerOptions);
1419
- ```
1420
-
1421
- Now all property names will be converted to snake case while reading them from JSON. If you have not found suitable naming convention you can easily implement your own. Read more about [creating a custom naming convention](#defining-custom-naming-convention) in a separate section.
1422
-
1423
- ### Configuring reference handler
1424
-
1425
- Reference handler defines how references to the same objects including a circular one should be treated. We have several reference handlers: direct, lead and path. Each of them can be used globally or per type.
1426
-
1427
- * Direct reference handler preserves object references without making any special changes;
1428
- * Path reference handler preserves object references using JSONPath notation;
1429
- * Lead reference handler preserves object references excluding a circular one. When circular reference is detected it will be set to undefined;
1430
-
1431
- There is nothing better to show the difference than code. For example we have two classes which reference each other:
1432
-
1433
- ```typescript
1434
- import { Type, Property } from '@dipscope/type-manager';
1435
-
1436
- @Type()
1437
- class User
1438
- {
1439
- @Property(() => Company) public company: Company;
1440
- }
1441
-
1442
- @Type()
1443
- class Company
1444
- {
1445
- @Property(() => User) public user: User;
1446
- }
1447
- ```
1448
-
1449
- Somewhere in code you have such a logic:
1450
-
1451
- ```typescript
1452
- import { TypeManager } from '@dipscope/type-manager';
1453
-
1454
- const user = new User();
1455
- const company = new Company();
1456
-
1457
- user.company = company;
1458
- company.user = user;
1459
-
1460
- const result = TypeManager.serialize(User, user);
1461
- ```
1462
-
1463
- Here are results returned by different reference handlers:
1464
-
1465
- ```typescript
1466
- // Direct reference handler...
1467
- { company: { user: result } };
1468
-
1469
- // Path reference handler...
1470
- { company: { user: { $ref: '$' } } };
1471
-
1472
- // Lead reference handler...
1473
- { company: { user: undefined };
1474
- ```
1475
-
1476
- As you can see `DirectReferenceHandler` does not make changes to your data and completely fine until you have to convert circular reference structure to a string. `JSON.stringify` method which we are using under the hood does not support such conversions so you will encounter an error. In this case you can select another reference handler. For example `PathReferenceHandler` which produces JSON string using JSONPath format for circular references supported by many libraries. Or you can simply ignore circular reference when it should be converted to a string and use `LeadReferenceHandler`. To change default reference handler you have to use `TypeManager` configure methods.
1477
-
1478
- ```typescript
1479
- import { TypeManagerOptions, TypeOptionsBase, PathReferenceHandler } from '@dipscope/type-manager';
1480
-
1481
- const typeOptionsBase: TypeOptionsBase<any> = {
1482
- referenceHandler: new PathReferenceHandler()
1483
- };
1484
-
1485
- const typeManagerOptions: TypeManagerOptions = {
1486
- typeOptionsBase: typeOptionsBase
1487
- };
1488
-
1489
- TypeManager.configure(typeManagerOptions);
1490
- ```
1491
-
1492
- With such configuration any reference will be handled using JSONPath so you are completely free from errors during conversion to a string.
1493
-
1494
- ## Advanced usage
1495
-
1496
- Our goal is to cover as much use cases as possible without making you to write additional code but there always be an application specific case. With that in mind we allow you to customize and extend each part of our pipeline.
1497
-
1498
- ### Defining custom data
1499
-
1500
- You can attach you custom metadata to our decorators using `customData` option available on `Type` and `Property`.
1501
-
1502
- ```typescript
1503
- import { Type, Property } from '@dipscope/type-manager';
1504
-
1505
- @Type({
1506
- customData: { rank: 1 }
1507
- })
1508
- class User
1509
- {
1510
- @Property(String, { customData: { priority: 10 } }) public name: string;
1511
- }
1512
- ```
1513
-
1514
- This allows you to get it later in serializers, factories, injectors or your code and perform specific actions. Besides pipeline you can get this data in any place you want using `TypeManager`.
1515
-
1516
- ```typescript
1517
- import { TypeManager } from '@dipscope/type-manager';
1518
-
1519
- const userMetadata = TypeManager.extractTypeMetadata(User);
1520
- const customData = userMetadata.customData;
1521
-
1522
- // Do something with type custom data...
1523
-
1524
- for (const propertyMetadata of userMetadata.propertyMetadataMap.values())
1525
- {
1526
- const propertyCustomData = propertyMetadata.customData;
1527
-
1528
- // Do something with property custom data...
1529
- }
1530
- ```
1531
-
1532
- ### Defining custom serializer
1533
-
1534
- You can create your own serializer or replace built in one. First you have to implement `Serializer` interface. It declares `serialize` and `deserialize` methods. Serialize method is called during conversion of `JavaScript` object instance into a plain object. Deserialize method is called during backward conversion. Here is an example of possible definition for custom `DateTime` class.
1535
-
1536
- ```typescript
1537
- import { Serializer, TypeLike, SerializerContext } from '@dipscope/type-manager';
1538
- import { Fn } from '@app/module';
1539
-
1540
- export class DateTimeSerializer implements Serializer<DateTime>
1541
- {
1542
- public serialize(x: TypeLike<DateTime>, serializerContext: SerializerContext<DateTime>): TypeLike<string>
1543
- {
1544
- if (Fn.isUndefined(x))
1545
- {
1546
- return serializerContext.serializedDefaultValue;
1547
- }
1548
-
1549
- if (Fn.isNull(x))
1550
- {
1551
- return x;
1552
- }
1553
-
1554
- if (Fn.isDateTime(x))
1555
- {
1556
- return x.toIsoString();
1557
- }
1558
-
1559
- if (serializerContext.log.errorEnabled)
1560
- {
1561
- serializerContext.log.error(`${serializerContext.path}: cannot serialize value as date time.`, x);
1562
- }
1563
-
1564
- return undefined;
1565
- }
1566
-
1567
- public deserialize(x: TypeLike<string>, serializerContext: SerializerContext<DateTime>): TypeLike<DateTime>
1568
- {
1569
- if (Fn.isUndefined(x))
1570
- {
1571
- return serializerContext.deserializedDefaultValue;
1572
- }
1573
-
1574
- if (Fn.isNull(x))
1575
- {
1576
- return x;
1577
- }
1578
-
1579
- if (Fn.isString(x))
1580
- {
1581
- return DateTime.fromIsoString(x);
1582
- }
1583
-
1584
- if (serializerContext.log.errorEnabled)
1585
- {
1586
- serializerContext.log.error(`${serializerContext.path}: cannot deserialize value as date time.`, x);
1587
- }
1588
-
1589
- return undefined;
1590
- }
1591
- }
1592
- ```
1593
-
1594
- This example follows internal conventions and gives you a picture of how real serializer may look like. `TypeManager` does not perform any checks and just passes values directly to serializer. Thats why input values can be undefined, null or others depending from what is stored inside certain property. `TypeLike` is an internal type to declare this behaviour.
1595
-
1596
- Serializer implementation is fully responsible for return result. You can get default values, custom data and other options specified in configuration from current serializer context and react accordingly. For example you can check if implicit conversion is enabled and convert any value to the target one.
1597
-
1598
- When you are finished with definitions there are two possible ways to register a serializer. You can use decorators.
1599
-
1600
- ```typescript
1601
- import { Type, Serializer } from '@dipscope/type-manager';
1602
-
1603
- @Type({
1604
- serializer: new DateTimeSerializer()
1605
- })
1606
- export class DateTime
1607
- {
1608
- ...
1609
- }
1610
- ```
1611
-
1612
- Or declarative configuration.
1613
-
1614
- ```typescript
1615
- import { TypeManager } from '@dipscope/type-manager';
1616
-
1617
- TypeManager.configureTypeOptions(DateTime, {
1618
- alias: 'DateTime',
1619
- serializer: new DateTimeSerializer()
1620
- });
1621
- ```
1622
-
1623
- With declarative configuration it is possible to override built in serializers if it's behaviour not suitable for your use cases. Also you can register serializers for types we not yet cover.
1624
-
1625
- ### Defining custom injector
1626
-
1627
- In modern world we are always use some kind of framework to build our application. It is definitely already have a configured dependency injection container so let's configure `TypeManager` for using it instead of build in one. You have to implement `Injector` interface with only one method. Here how it may look like in `Angular`.
1628
-
1629
- ```typescript
1630
- import { Injector, TypeMetadata } from '@dipscope/type-manager';
1631
- import { Injector as AngularInjector } from '@angular/core';
1632
-
1633
- export class CustomInjector implements Injector
1634
- {
1635
- private readonly angularInjector: AngularInjector;
1636
-
1637
- public constructor(angularInjector: AngularInjector)
1638
- {
1639
- this.angularInjector = angularInjector;
1640
-
1641
- return;
1642
- }
1643
-
1644
- public get<TType>(typeMetadata: TypeMetadata<TType>): TType | undefined
1645
- {
1646
- return this.angularInjector.get(typeMetadata.typeFn);
1647
- }
1648
- }
1649
- ```
1650
-
1651
- In general you have to get framework injector or create it manually. Then call method to get and instance by type when one is requested. Implementations can differ but we hope idea is clear. When you are finished with definitions you have to register custom injector for a `TypeManager`.
1652
-
1653
- ```typescript
1654
- import { TypeManager } from '@dipscope/type-manager';
1655
- import { Injector as AngularInjector } from '@angular/core';
1656
-
1657
- const angularInjector: AngularInjector = ...; // Get framework injector in core module for example.
1658
-
1659
- TypeManager.configureTypeOptionsBase({
1660
- injector: new CustomInjector(angularInjector)
1661
- });
1662
- ```
1663
-
1664
- Now types will be resolved using framework injector.
1665
-
1666
- ### Defining custom factory
1667
-
1668
- When you want to apply additional logic to how types are constructed you can specify custom factory globally or per type. Let's say you want to init some properties based on your custom data specified for a type. You can do this by extending default `TypeFactory`.
1669
-
1670
- ```typescript
1671
- import { TypeContext, Injector, TypeFactory } from '@dipscope/type-manager';
1672
-
1673
- export class CustomTypeFactory extends TypeFactory
1674
- {
1675
- public build<TType>(typeContext: TypeContext<TType>, injector: Injector): TType
1676
- {
1677
- // Build any type.
1678
- const type = super.build(typeContext, injector) as any;
1679
-
1680
- // Resolve custom data.
1681
- const typeMetadata = typeContext.typeMetadata;
1682
- const customData = typeMetadata.customData;
1683
-
1684
- // Process custom data.
1685
- for (const propertyName in customData)
1686
- {
1687
- type[propertyName] = customData[propertyName];
1688
- }
1689
-
1690
- return type;
1691
- }
1692
- }
1693
- ```
1694
-
1695
- When you are finished with definitions there are two possible ways to register a factory. You can use decorators.
1696
-
1697
- ```typescript
1698
- import { Type, Factory } from '@dipscope/type-manager';
1699
-
1700
- @Type({
1701
- customData: { rank: 1 },
1702
- factory: new CustomTypeFactory()
1703
- })
1704
- export class User
1705
- {
1706
- ...
1707
- }
1708
- ```
1709
-
1710
- Or declarative configuration.
1711
-
1712
- ```typescript
1713
- import { TypeManager } from '@dipscope/type-manager';
1714
-
1715
- // Overriding only for user type.
1716
- TypeManager.configureTypeOptions(User, {
1717
- customData: { rank: 1 },
1718
- factory: new CustomTypeFactory()
1719
- });
1720
-
1721
- // Overriding for any type.
1722
- TypeManager.configureTypeOptionsBase({
1723
- factory: new CustomTypeFactory()
1724
- });
1725
- ```
1726
-
1727
- ### Defining custom naming convention
1728
-
1729
- To define custom naming convention you have to implement `NamingConvention` interface with only one `convert` method. Here is an example implementation of camel case naming convention.
1730
-
1731
- ```typescript
1732
- import { NamingConvention, getWords } from '@dipscope/type-manager';
1733
-
1734
- export class CamelCaseNamingConvention implements NamingConvention
1735
- {
1736
- public convert(name: string): string
1737
- {
1738
- return getWords(name).reduce(this.reduce, '');
1739
- }
1740
-
1741
- private reduce(result: string, word: string, index: number): string
1742
- {
1743
- if (word.length === 0)
1744
- {
1745
- return result;
1746
- }
1747
-
1748
- const wordLowerCased = word.toLowerCase();
1749
-
1750
- if (index === 0)
1751
- {
1752
- return wordLowerCased;
1753
- }
1754
-
1755
- return `${result}${wordLowerCased[0].toUpperCase()}${wordLowerCased.slice(1)}`;
1756
- }
1757
- }
1758
- ```
1759
-
1760
- Public `convert` method receives a property name as it declared in a class. You have to call internal `getWords` function on it which will split property name into array of the words. In the `reduce` function you can combine this words to whatever string you want. When you are finished with definitions you have to register custom naming convention for a `TypeManager`.
1761
-
1762
- ```typescript
1763
- import { TypeManager } from '@dipscope/type-manager';
1764
-
1765
- TypeManager.configureTypeOptionsBase({
1766
- namingConvention: new CamelCaseNamingConvention()
1767
- });
1768
- ```
1769
-
1770
- Now property names will be resolved using your custom naming convention.
1771
-
1772
- ## Use cases
1773
-
1774
- This section describes certain use cases to separate them from the main documentation part. We will point to a concrete place you should read to setup what is necessary.
1775
-
1776
- ### Built in serializers
1777
-
1778
- Here is a list of types with built in serializers.
1779
-
1780
- * Array;
1781
- * ArrayBuffer;
1782
- * Boolean;
1783
- * DataView;
1784
- * Date;
1785
- * Float32Array;
1786
- * Float64Array;
1787
- * Int8Array;
1788
- * Int16Array;
1789
- * Int32Array;
1790
- * Map;
1791
- * Number;
1792
- * Set;
1793
- * String;
1794
- * Uint8Array;
1795
- * Uint8ClampedArray;
1796
- * Uint16Array;
1797
- * Uint32Array;
1798
-
1799
- For these you don't have to do anything to make them work. You are free to [create a custom serializer](#defining-custom-serializer) or override built in one if it does not cover your use case.
1800
-
1801
- ### Circular object references
1802
-
1803
- We have a great support for circular references with different behaviour when they are detected. Read [configuring reference handler](#configuring-reference-handler) section for more info.
1804
-
1805
- ### Dependency injection and immutable types
1806
-
1807
- Follow the steps described in [inject decorator](#inject-decorator) section. To integrate our injection system with you yours check [defining custom injector](#defining-custom-injector) section.
1808
-
1809
- ### Different case usage in class and JSON
1810
-
1811
- If your cases differs between class and JSON. For example `camelCase` vs `snake_case`. You can setup naming convention to use during serialization and deserialization. Follow the steps described in [configuring naming convention](#configuring-naming-convention) section.
1812
-
1813
- ### Enum types
1814
-
1815
- Enum types are supported but require special handling. This is because of how enums are represented after compiling them to `JavaScript`. You can know more about how to define them in a [property decorator](#property-decorator) section. If you are interested about the `Enum` compilation details you can check the official [documentation](https://www.typescriptlang.org/docs/handbook/enums.html).
1816
-
1817
- ### Generic types
1818
-
1819
- Generic types are supported and you can define them. Read [property decorator](#property-decorator) section for more info.
1820
-
1821
- ### Integration with Angular
1822
-
1823
- With `Angular` you do not need to install [reflect-metadata](https://github.com/rbuckton/reflect-metadata) as it is already included in `core-js`. However, you still need to instruct CLI to include it in the build. This can be achieved simply by adding `import 'reflect-metadata';` to you `main.ts` file.
1824
-
1825
- To make `Angular` injector work for you a custom `Injector` needs to be implemented. Check [defining custom injector](#defining-custom-injector) section for more info.
1826
-
1827
- ### Polymorphic types
1828
-
1829
- Polymorphic types are supported. In most cases additional configuration is required. Check [configuring usage of polymorphic types](#configuring-usage-of-polymorphic-types) section for more info.
1830
-
1831
- ## Versioning
1832
-
1833
- We use [SemVer](http://semver.org/) for versioning. For the versions available, see the versions section on [NPM project page](https://www.npmjs.com/package/@dipscope/type-manager).
1834
-
1835
- See information about breaking changes, release notes and migration steps between versions in [CHANGELOG.md](https://github.com/dipscope/TypeManager.TS/blob/master/CHANGELOG.md) file.
1836
-
1837
- ## Contributing
1838
-
1839
- Please read [CONTRIBUTING.md](https://github.com/dipscope/TypeManager.TS/blob/master/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
1840
-
1841
- ## Authors
1842
-
1843
- * **Dmitry Pimonov** - *Initial work* - [dpimonov](https://github.com/dpimonov)
1844
-
1845
- See also the list of [contributors](https://github.com/dipscope/TypeManager.TS/contributors) who participated in this project.
1846
-
1847
- ## Notes
1848
-
1849
- Thanks for checking this package.
1850
-
1851
- Feel free to create an issue if you find any mistakes in documentation or have any improvements in mind.
1852
-
1853
- We wish you good luck and happy coding!
1854
-
1855
- ## License
1856
-
1857
- This project is licensed under the Apache 2.0 License - see the [LICENSE.md](https://github.com/dipscope/TypeManager.TS/blob/master/LICENSE.md) file for details.
1
+ # TypeManager.TS
2
+
3
+ ![GitHub](https://img.shields.io/github/license/dipscope/TypeManager.TS) ![NPM](https://img.shields.io/npm/v/@dipscope/type-manager) ![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)
4
+
5
+ Type manager is a parsing package for `TypeScript` which will help you to transform JSON strings or plain objects into `JavaScript` object instances. It supports [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) or declarative configuration and allows you to configure parsing of your or 3rd party classes easily.
6
+
7
+ We recommend to use our [official website](https://dipscope.com/type-manager/what-issues-it-solves) to navigate through available features. You can also use the latest documentation described below.
8
+
9
+ ## Give a star :star:
10
+
11
+ If you like or are using this project please give it a star. Thanks!
12
+
13
+ ## Table of contents
14
+
15
+ * [What issues it solves?](#what-issues-it-solves)
16
+ * [Installation](#installation)
17
+ * [How it works?](#how-it-works)
18
+ * [Defining decorators](#defining-decorators)
19
+ * [Type decorator](#type-decorator)
20
+ * [Property decorator](#property-decorator)
21
+ * [Inject decorator](#inject-decorator)
22
+ * [Defining decorator options](#defining-decorator-options)
23
+ * [Alias option](#alias-option)
24
+ * [Custom data option](#custom-data-option)
25
+ * [Default value option](#default-value-option)
26
+ * [Deserializable option](#deserializable-option)
27
+ * [Discriminant option](#discriminant-option)
28
+ * [Discriminator option](#discriminator-option)
29
+ * [Factory option](#factory-option)
30
+ * [Injectable option](#injectable-option)
31
+ * [Injector option](#injector-option)
32
+ * [Naming convention option](#naming-convention-option)
33
+ * [Preserve discriminator option](#preserve-discriminator-option)
34
+ * [Preserve null option](#preserve-null-option)
35
+ * [Reference handler option](#reference-handler-option)
36
+ * [Serializable option](#serializable-option)
37
+ * [Serializer option](#serializer-option)
38
+ * [Use default value option](#use-default-value-option)
39
+ * [Use implicit conversion option](#use-implicit-conversion-option)
40
+ * [Defining configuration manually](#defining-configuration-manually)
41
+ * [Configuring global options](#configuring-global-options)
42
+ * [Configuring options per type](#configuring-options-per-type)
43
+ * [Configuring usage of polymorphic types](#configuring-usage-of-polymorphic-types)
44
+ * [Configuring naming convention](#configuring-naming-convention)
45
+ * [Configuring reference handler](#configuring-reference-handler)
46
+ * [Advanced usage](#advanced-usage)
47
+ * [Defining custom data](#defining-custom-data)
48
+ * [Defining custom serializer](#defining-custom-serializer)
49
+ * [Defining custom injector](#defining-custom-injector)
50
+ * [Defining custom factory](#defining-custom-factory)
51
+ * [Defining custom naming convention](#defining-custom-naming-convention)
52
+ * [Use cases](#use-cases)
53
+ * [Built in serializers](#built-in-serializers)
54
+ * [Circular object references](#circular-object-references)
55
+ * [Dependency injection and immutable types](#dependency-injection-and-immutable-types)
56
+ * [Different case usage in class and JSON](#different-case-usage-in-class-and-json)
57
+ * [Enum types](#enum-types)
58
+ * [Generic types](#generic-types)
59
+ * [Integration with Angular](#integration-with-angular)
60
+ * [Polymorphic types](#polymorphic-types)
61
+ * [Versioning](#versioning)
62
+ * [Contributing](#contributing)
63
+ * [Authors](#authors)
64
+ * [Notes](#notes)
65
+ * [License](#license)
66
+
67
+ ## What issues it solves?
68
+
69
+ In `JavaScript` there are two types of objects:
70
+
71
+ * Plain objects which are created using `{}` notation;
72
+ * Constructor based objects which are created using `new Class()` notation.
73
+
74
+ Sometimes we want to transform plain objects to the classes we have. Let's assume we are loading a users JSON data from our backend API or the other datasource:
75
+
76
+ ```json
77
+ [
78
+ {
79
+ "id": 1,
80
+ "name": "Dmitry",
81
+ "deletedAt": "2021-02-22T20:15:00.000Z"
82
+ },
83
+ {
84
+ "id": 2,
85
+ "name": "Alex",
86
+ "deletedAt": null
87
+ },
88
+ {
89
+ "id": 3,
90
+ "name": "Anna",
91
+ "deletedAt": null
92
+ }
93
+ ]
94
+ ```
95
+
96
+ We have a `User` class:
97
+
98
+ ```typescript
99
+ export class User
100
+ {
101
+ public id: number;
102
+ public name: string;
103
+ public deletedAt: Date;
104
+
105
+ public constructor(id: number, name: string)
106
+ {
107
+ this.id = id;
108
+ this.name = name;
109
+
110
+ return;
111
+ }
112
+
113
+ public isDeleted(): boolean
114
+ {
115
+ return this.deletedAt !== null;
116
+ }
117
+ }
118
+ ```
119
+
120
+ Somewhere in code we are checking if user is deleted before performing some action.
121
+
122
+ ```typescript
123
+ const users: User[] = JSON.parse(usersJson);
124
+
125
+ for (const user of users)
126
+ {
127
+ if (user.isDeleted())
128
+ {
129
+ // Do some action...
130
+ }
131
+ }
132
+ ```
133
+
134
+ Do you see the problem in this piece of code?
135
+
136
+ `JSON.parse` function returns plain objects and we actually lied to compiler when said that it is a `User` class array. In this code we can successfully use `user.id` or `user.name`. However we cannot use `user.isDeleted()` because user is not an instance of a `User` class.
137
+
138
+ So what to do? How to get `User` array from our JSON?
139
+
140
+ Solution is to create new instances of `User` class from plain objects returned by `JSON.parse` function. But things may go wrong once you have a more complex object hierarchy. Besides `deletedAt` property is represented as `String` in JSON but `User` class declares it as a `Date` so we also have to perform appropriate conversion when assigning this property.
141
+
142
+ There exists much more simple way. Let's use our `TypeManager` for getting instances of `User` class from JSON:
143
+
144
+ ```typescript
145
+ import { TypeManager } from '@dipscope/type-manager';
146
+
147
+ const users: User[] = TypeManager.parse(User, usersJson);
148
+
149
+ for (const user of users)
150
+ {
151
+ if (user.isDeleted())
152
+ {
153
+ // Do some action...
154
+ }
155
+ }
156
+ ```
157
+
158
+ Now we can use all power provided by `JavaScript` class instances.
159
+
160
+ Furthermore `TypeManager.TS` provides you:
161
+
162
+ * Reflection abilities at runtime;
163
+ * Support for generic types;
164
+ * Handling of cyclic object references and different ways of serialization when they appear;
165
+ * Ability to configure serialization of 3rd party classes;
166
+ * Support for polymorphic types;
167
+ * Great alternative for similar packages like [class-transformer](https://github.com/typestack/class-transformer), [TypedJSON](https://github.com/JohnWeisz/TypedJSON) and [jackson-js](https://github.com/pichillilorenzo/jackson-js);
168
+
169
+ Want to know more? Let's dive into the details.
170
+
171
+ ## Installation
172
+
173
+ `TypeManager.TS` is available from NPM, both for browser (e.g. using webpack) and NodeJS:
174
+
175
+ ```
176
+ npm i @dipscope/type-manager
177
+ ```
178
+
179
+ TypeScript needs to run with the `experimentalDecorators` and `emitDecoratorMetadata` options enabled when using decorator annotations. So make sure you have properly configured your `tsconfig.json` file.
180
+
181
+ _If you want additional type-safety and reduced syntax you may wish to install [reflect-metadata](https://github.com/rbuckton/reflect-metadata). This step is on your choice and fully optional. When installed it must be available globally to work. This can usually be done with `import 'reflect-metadata';` in your main index file._
182
+
183
+ ## How it works?
184
+
185
+ It defines configuration for each object which you are going to serialize or deserialize and uses this configuration to process data of your choice. There are two possible ways to define a configuration:
186
+
187
+ * Using decorator annotations;
188
+ * Using declarative style;
189
+
190
+ The first one is the easiest and can be used for any class you control. If you want to configure serialization of 3rd party classes, global options or you don't like decorators it is better to go with the second. There are no restrictions to use one or another. You can combine two ways of configuration depending on which one fits better.
191
+
192
+ Let's have a look at the most simple example of configuration using decorators.
193
+
194
+ ```typescript
195
+ import { Type, Property } from '@dipscope/type-manager';
196
+
197
+ @Type()
198
+ export class User
199
+ {
200
+ @Property(String) public name: string;
201
+ @Property(String) public email: string;
202
+ }
203
+ ```
204
+
205
+ Here we have a `User` class with `Type` and `Property` decorators assigned to it. `Type` decorator declares a type. `Property` decorator describes available properties for that type.
206
+
207
+ The same configuration can be rewritten using declarative style.
208
+
209
+ ```typescript
210
+ import { TypeManager, TypeMetadata, TypeConfiguration } from '@dipscope/type-manager';
211
+
212
+ export class User
213
+ {
214
+ public name: string;
215
+ public email: string;
216
+ }
217
+
218
+ export class UserConfiguration implements TypeConfiguration<User>
219
+ {
220
+ public configure(typeMetadata: TypeMetadata<User>): void
221
+ {
222
+ typeMetadata.configurePropertyMetadata('name')
223
+ .hasTypeArgument(String);
224
+
225
+ typeMetadata.configurePropertyMetadata('email')
226
+ .hasTypeArgument(String);
227
+
228
+ return;
229
+ }
230
+ }
231
+
232
+ TypeManager.applyTypeConfiguration(User, new UserConfiguration());
233
+ ```
234
+
235
+ As you can see now our `User` class defined without decorators. Instead you have to call `TypeManager` method and provide `TypeConfiguration` related to `User` type.
236
+
237
+ No matter what style of configuration you have chosen the next step is to call serialize methods of `TypeManager` with providing a type and data you want to process.
238
+
239
+ ```typescript
240
+ import { TypeManager } from '@dipscope/type-manager';
241
+
242
+ const userObject = TypeManager.serialize(User, new User());
243
+ const user = TypeManager.deserialize(User, userObject);
244
+
245
+ user instanceof User; // True.
246
+ ```
247
+
248
+ Calling serialize creates a plain object and deserialize creates an instance of `User` class. During deserialize you can provide any object. It's not nesassary that object was produced by `TypeManager`. If object is an `Array` you will get array of types in return. Objects are parsed based on general type configuration defined by developer. It is also possible to stringify and parse JSON.
249
+
250
+ ```typescript
251
+ import { TypeManager } from '@dipscope/type-manager';
252
+
253
+ const userJson = TypeManager.stringify(User, new User());
254
+ const user = TypeManager.parse(User, userJson);
255
+
256
+ user instanceof User; // True.
257
+ ```
258
+
259
+ Stringify and parse functions are wrappers over native JSON class functions. In addition they add serialize and deserialize support under the hood.
260
+
261
+ Static functions are not the only way to work with a `TypeManager`. You can also work on instance based manner.
262
+
263
+ ```typescript
264
+ import { TypeManager } from '@dipscope/type-manager';
265
+
266
+ const typeManager = new TypeManager();
267
+ const userObject = userManager.serialize(User, new User());
268
+ const user = userManager.deserialize(User, userObject);
269
+
270
+ user instanceof User; // True.
271
+ ```
272
+
273
+ At first glance, it may seems that there is no difference but creating an instance of `TypeManager` preserves a configuration state. You can work with different configurations at the same time and have different serialization groups. By default all decorator based configurations and static calls are applied to the singleton type manager instance which is automatically created under the hood.
274
+
275
+ ## Defining decorators
276
+
277
+ There are few decorators which controls the main flow. This are `Type`, `Property` and `Inject` decorators. Let's go through each of them.
278
+
279
+ ### Type decorator
280
+
281
+ Type decorator defines a type and should be declared right before a class.
282
+
283
+ ```typescript
284
+ import { Type } from '@dipscope/type-manager';
285
+
286
+ @Type()
287
+ export class User
288
+ {
289
+ ...
290
+ }
291
+ ```
292
+
293
+ This will register a new type with default type serializer assigned to it. You can define how each class should be treated by providing optional configure options as a first argument.
294
+
295
+ ```typescript
296
+ import { Type } from '@dipscope/type-manager';
297
+
298
+ @Type({
299
+ alias: 'User',
300
+ serializer: new UserSerializer()
301
+ })
302
+ export class User
303
+ {
304
+ ...
305
+ }
306
+ ```
307
+
308
+ This call defines a type alias which can be later used to resolve a type for a property at runtime. We will talk about details in the property decorator section. Also we defined custom serializer for a type which is an implementation of `Serializer` interface. This serializer will be used later to serialize and deserialize a type including all custom logic of your choice. You can read more about [creating a custom serializer](#defining-custom-serializer) in a separate section.
309
+
310
+ There are more options can be provided for a type, so check `TypeOptions` definition or section with [decorator options](#defining-decorator-options) below.
311
+
312
+ ### Property decorator
313
+
314
+ Property decorator defines per property configuration within a type and should be declared right before a property or accessor definition.
315
+
316
+ ```typescript
317
+ import 'reflect-metadata';
318
+ import { Type, Property } from '@dipscope/type-manager';
319
+
320
+ @Type()
321
+ export class User
322
+ {
323
+ @Property() public name: string;
324
+ }
325
+ ```
326
+
327
+ This will register a `name` property for a `User`. Each property has a type associated with it. In our case this is a `String`. By default if no configure options are provided decorator will try to resolve a property type using [reflect-metadata](https://github.com/rbuckton/reflect-metadata). If you are not using reflect metadata then such configuration will result a property type to be `unknown` and it will result in direct serialization. For such a case you have to explicitly define a property type.
328
+
329
+ ```typescript
330
+ import { Type, Property } from '@dipscope/type-manager';
331
+
332
+ @Type()
333
+ export class User
334
+ {
335
+ @Property(String) public name: string;
336
+ }
337
+ ```
338
+
339
+ Depending on your use case there are two possible ways to configure additional property options.
340
+
341
+ If you are using [reflect-metadata](https://github.com/rbuckton/reflect-metadata) then provide options as a first argument.
342
+
343
+ ```typescript
344
+ import 'reflect-metadata';
345
+ import { Type, Property } from '@dipscope/type-manager';
346
+
347
+ @Type()
348
+ export class User
349
+ {
350
+ @Property({ alias: 'username' }) public name: string;
351
+ }
352
+ ```
353
+
354
+ If types defined explicitly then provide options as a second argument.
355
+
356
+ ```typescript
357
+ import { Type, Property } from '@dipscope/type-manager';
358
+
359
+ @Type()
360
+ export class User
361
+ {
362
+ @Property(String, { alias: 'username' }) public name: string;
363
+ }
364
+ ```
365
+
366
+ This option configures an alias so `username` property will be used instead of `name` when deserializing from object. There are plenty of configure options, so check `PropertyOptions` definition or section with [decorator options](#defining-decorator-options) below. For example you can make some properties serializable only or define custom property serialization.
367
+
368
+ Now let's have a look at more complex definitions with generic types. This are `Array<TType>`, `Map<TKey, TValue>` and others. To declare one of this you have to use extra argument available for `Property` decorator. Generic arguments are always passed as array to exactly see them within a source code.
369
+
370
+ If you are using [reflect-metadata](https://github.com/rbuckton/reflect-metadata) then provide generics as a first argument so configure options will become the second.
371
+
372
+ ```typescript
373
+ import 'reflect-metadata';
374
+ import { Type, Property } from '@dipscope/type-manager';
375
+
376
+ @Type()
377
+ export class User
378
+ {
379
+ @Property([String, Number], { alias: 'myMap' }) public map: Map<string, number>;
380
+ }
381
+ ```
382
+
383
+ If types defined explicitly then provide generics as a second argument so configure options will become the third.
384
+
385
+ ```typescript
386
+ import { Type, Property } from '@dipscope/type-manager';
387
+
388
+ @Type()
389
+ export class User
390
+ {
391
+ @Property(Map, [String, Number], { alias: 'myMap' }) public map: Map<string, number>;
392
+ }
393
+ ```
394
+
395
+ This is a full set of arguments available for the property. Basically when using [reflect-metadata](https://github.com/rbuckton/reflect-metadata) you have just to omit the first argument.
396
+
397
+ We try to simplify declarations as much as possible so you are free to use only configure options if you want and don't ever think about `Property` decorator arguments.
398
+
399
+ ```typescript
400
+ import { Type, Property } from '@dipscope/type-manager';
401
+
402
+ @Type()
403
+ export class User
404
+ {
405
+ @Property({
406
+ typeArgument: Map,
407
+ genericArguments: [String, Number],
408
+ alias: 'myMap'
409
+ })
410
+ public map: Map<string, number>;
411
+ }
412
+ ```
413
+
414
+ Which syntax to use is completely on your choice. `Property` decorator is smart enough to setup everything based on usage.
415
+
416
+ Now let's talk a bit about relation types. They are not differ from built in types, so if you are using [reflect-metadata](https://github.com/rbuckton/reflect-metadata) the definition can be the following.
417
+
418
+ ```typescript
419
+ import 'reflect-metadata';
420
+ import { Type, Property } from '@dipscope/type-manager';
421
+
422
+ @Type()
423
+ export class UserStatus
424
+ {
425
+ @Property() public title: string;
426
+ }
427
+
428
+ @Type()
429
+ export class User
430
+ {
431
+ @Property() public userStatus: UserStatus;
432
+ }
433
+ ```
434
+
435
+ With array of relations you have to use generics.
436
+
437
+ ```typescript
438
+ import 'reflect-metadata';
439
+ import { Type, Property } from '@dipscope/type-manager';
440
+
441
+ @Type()
442
+ export class UserStatus
443
+ {
444
+ @Property() public title: string;
445
+ }
446
+
447
+ @Type()
448
+ export class User
449
+ {
450
+ @Property([UserStatus]) public userStatuses: Array<UserStatus>;
451
+ }
452
+ ```
453
+
454
+ If types defined explicitly then definition will be the following.
455
+
456
+ ```typescript
457
+ import { Type, Property } from '@dipscope/type-manager';
458
+
459
+ @Type()
460
+ export class UserStatus
461
+ {
462
+ @Property(String) public title: string;
463
+ }
464
+
465
+ @Type()
466
+ export class User
467
+ {
468
+ @Property(UserStatus) public userStatus: UserStatus;
469
+ }
470
+ ```
471
+
472
+ Then for array of relations it will be the following.
473
+
474
+ ```typescript
475
+ import { Type, Property } from '@dipscope/type-manager';
476
+
477
+ @Type()
478
+ export class UserStatus
479
+ {
480
+ @Property(String) public title: string;
481
+ }
482
+
483
+ @Type()
484
+ export class User
485
+ {
486
+ @Property(Array, [UserStatus]) public userStatuses: Array<UserStatus>;
487
+ }
488
+ ```
489
+
490
+ If any type defines an alias - it can be used as a type resolver.
491
+
492
+ ```typescript
493
+ import { Type, Property } from '@dipscope/type-manager';
494
+
495
+ @Type({
496
+ alias: 'UserStatus'
497
+ })
498
+ export class UserStatus
499
+ {
500
+ @Property(String) public title: string;
501
+ }
502
+
503
+ @Type()
504
+ export class User
505
+ {
506
+ @Property('UserStatus') public userStatus: UserStatus;
507
+ }
508
+ ```
509
+
510
+ If you have circular reference or your type declared later an extended syntax can be used to lazily define a type.
511
+
512
+ ```typescript
513
+ import { Type, Property } from '@dipscope/type-manager';
514
+
515
+ @Type()
516
+ export class User
517
+ {
518
+ @Property(() => UserStatus) public userStatus: UserStatus;
519
+ }
520
+
521
+ @Type()
522
+ export class UserStatus
523
+ {
524
+ @Property(() => String) public title: string;
525
+ }
526
+ ```
527
+
528
+ One great thing to know about arguments for property type and generics is that you can pass lazy function, type directly or type alias. Which definition to use is completely on your choice and dependent from certain use cases.
529
+
530
+ While property type arguments exactly match to `TypeScript` types there is a one exception for this rule. This is `Enum`. You have to provide `String` type for a string based `Enum` and `Number` type for a number based `Enum`. This is because of how `Enum` is represented after compiling it to `JavaScript`. You can read more about this [here](https://www.typescriptlang.org/docs/handbook/enums.html).
531
+
532
+ If you are using [reflect-metadata](https://github.com/rbuckton/reflect-metadata) this will be done automatically so no additional steps are required from your side.
533
+
534
+ ```typescript
535
+ import 'reflect-metadata';
536
+ import { Type, Property } from '@dipscope/type-manager';
537
+
538
+ export enum UserPriorityNumeric
539
+ {
540
+ Low,
541
+ Medium,
542
+ High
543
+ }
544
+
545
+ export enum UserPriorityTextual
546
+ {
547
+ Low = 'Low',
548
+ Medium = 'Medium',
549
+ High = 'High'
550
+ }
551
+
552
+ @Type()
553
+ export class User
554
+ {
555
+ @Property() public userPriorityNumeric: UserPriorityNumeric;
556
+ @Property() public userPriorityTextual: UserPriorityTextual;
557
+ }
558
+ ```
559
+
560
+ If types defined explicitly then definition will be the following.
561
+
562
+ ```typescript
563
+ import { Type, Property } from '@dipscope/type-manager';
564
+
565
+ export enum UserPriorityNumeric
566
+ {
567
+ Low,
568
+ Medium,
569
+ High
570
+ }
571
+
572
+ export enum UserPriorityTextual
573
+ {
574
+ Low = 'Low',
575
+ Medium = 'Medium',
576
+ High = 'High'
577
+ }
578
+
579
+ @Type()
580
+ export class User
581
+ {
582
+ @Property(Number) public userPriorityNumeric: UserPriorityNumeric;
583
+ @Property(String) public userPriorityTextual: UserPriorityTextual;
584
+ }
585
+ ```
586
+
587
+ One should remember this when explicitly defining types for enums.
588
+
589
+ ### Inject decorator
590
+
591
+ Inject decorator controls your type dependency and declared right before a constructor parameter.
592
+
593
+ ```typescript
594
+ import { Type, Inject } from '@dipscope/type-manager';
595
+
596
+ @Type()
597
+ export class User
598
+ {
599
+ public constructor(@Inject('name') name: string, @Inject(UserService) userService: UserService)
600
+ {
601
+ this.name = name;
602
+
603
+ // Any action with UserService...
604
+
605
+ return;
606
+ }
607
+ }
608
+ ```
609
+
610
+ It accepts two possible inputs as its argument.
611
+
612
+ * String key from JSON context;
613
+ * Certain type registered as injectable;
614
+
615
+ When a string key is provided then a certain value will be resolved from JSON context for you when creating an instance. If any property declares the same key you will recieve deserialized value. If not then original value will be provided instead. Injecting context values is a use case of immutable types.
616
+
617
+ When a certain type is provided it will be resolved from the dependency injection container. If you are going to use internal type injector then you should register injectable types as the following. By default singleton injector is used to resolve such services.
618
+
619
+ ```typescript
620
+ import { Type } from '@dipscope/type-manager';
621
+
622
+ @Type({
623
+ injectable: true
624
+ })
625
+ export class UserService
626
+ {
627
+ public property: string;
628
+ }
629
+ ```
630
+
631
+ In most cases you will work in environment where dependency injection system is already setted up. In this case you have to implement custom `Injector` to be used instead of our default one. Besides you should follow the steps to register injectable services specified by the vendor. You can read more about [creating a custom injector](#defining-custom-injector) in a separate section.
632
+
633
+ If you are using [reflect-metadata](https://github.com/rbuckton/reflect-metadata) the injection of services can be simplified.
634
+
635
+ ```typescript
636
+ import { Type, Inject } from '@dipscope/type-manager';
637
+
638
+ @Type()
639
+ export class User
640
+ {
641
+ public constructor(@Inject('name') name: string, userService: UserService)
642
+ {
643
+ this.name = name;
644
+
645
+ // Any action with UserService...
646
+
647
+ return;
648
+ }
649
+ }
650
+ ```
651
+
652
+ Note that now you don't have to specify injection for types explicitly. However injection of values by key from JSON context still present. It's because argument names cannot be resolved using reflection.
653
+
654
+ ## Defining decorator options
655
+
656
+ `Type` and `Property` decorators provide full configuration for your classes using configure options. In this section we will go through each of them.
657
+
658
+ ### Alias option
659
+
660
+ This option can be used both on type and property to define an alias.
661
+
662
+ ```typescript
663
+ import { Type, Property } from '@dipscope/type-manager';
664
+
665
+ @Type({
666
+ alias: 'User'
667
+ })
668
+ export class User
669
+ {
670
+ @Property(String, { alias: 'username' }) public name: string;
671
+ }
672
+ ```
673
+
674
+ Alias defined for a class can be used later for resolving property types. Note that it should be unique within application to work properly.
675
+
676
+ Alias defined for a property declares that property name differs from one specified in JSON. In our case `username` will be used instead of `name` during JSON serialization and deserialization.
677
+
678
+ ### Custom data option
679
+
680
+ This option can be used to provide any custom data for type or property.
681
+
682
+ ```typescript
683
+ import { Type, Property } from '@dipscope/type-manager';
684
+
685
+ @Type({
686
+ customData: { rank: 1 }
687
+ })
688
+ export class User
689
+ {
690
+ @Property(String, { customData: { order: 1 } }) public name: string;
691
+ }
692
+ ```
693
+
694
+ This custom data later can be accessed in serializers, factories, injectors or your code and used accordingly. Read more about [defining custom data](#defining-custom-data) in a separate section.
695
+
696
+ ### Default value option
697
+
698
+ This option is used to define a default value when one is undefined. It can be used on type or property.
699
+
700
+ ```typescript
701
+ import { Type, Property } from '@dipscope/type-manager';
702
+
703
+ @Type({
704
+ defaultValue: () => new User()
705
+ })
706
+ export class User
707
+ {
708
+ @Property(String, { defaultValue: 'BestName' }) public name: string;
709
+ }
710
+ ```
711
+
712
+ This will affect both serialized and deserialized default value. This will fit perfectly for most types. You can also specify serialized and deserialized default value explicitly for complex types by using two other options.
713
+
714
+ ```typescript
715
+ import { Type, Property } from '@dipscope/type-manager';
716
+
717
+ @Type({
718
+ serializedDefaultValue: () => {},
719
+ deserializedDefaultValue: () => new User()
720
+ })
721
+ export class User
722
+ {
723
+ @Property(String, { serializedDefaultValue: 'SerializedName', deserializedDefaultValue: 'DeserializedName' }) public name: string;
724
+ }
725
+ ```
726
+
727
+ As you can see it accepts an arrow function or a certain value. Both are valid for type and property. Using default values is turned off by default. You can enable them using `useDefaultValue` option per type and property or enable globally using `TypeManager` configure method.
728
+
729
+ ### Deserializable option
730
+
731
+ This option is used to enable or disable deserialization for a certain property.
732
+
733
+ ```typescript
734
+ import { Type, Property } from '@dipscope/type-manager';
735
+
736
+ @Type()
737
+ export class User
738
+ {
739
+ @Property(String, { deserializable: true }) public name: string;
740
+ }
741
+ ```
742
+
743
+ By default all properties are deserializable.
744
+
745
+ ### Discriminant option
746
+
747
+ This option is used to define a custom discriminant for a type which is later used during serialization and deserialization of polymorphic types.
748
+
749
+ ```typescript
750
+ import { Type, Property } from '@dipscope/type-manager';
751
+
752
+ @Type({
753
+ discriminant: 'Company.Api.Entities.User'
754
+ })
755
+ export class User
756
+ {
757
+ @Property(String) public name: string;
758
+ }
759
+ ```
760
+
761
+ You can read more about handling of polymorphic types in this [section](#configuring-usage-of-polymorphic-types).
762
+
763
+ ### Discriminator option
764
+
765
+ This option can be used to define a custom property which stores discriminant of polymorphic type.
766
+
767
+ ```typescript
768
+ import { Type, Property } from '@dipscope/type-manager';
769
+
770
+ @Type({
771
+ discriminator: '__typename__'
772
+ })
773
+ export class User
774
+ {
775
+ @Property(String) public name: string;
776
+ }
777
+ ```
778
+
779
+ In common use cases discriminator should be set globally using `TypeManager` configure method. Using this option on a type level makes sense only if discriminator differs from the global one. You can read more about handling of polymorphic types in this [section](#configuring-usage-of-polymorphic-types).
780
+
781
+ ### Factory option
782
+
783
+ This option can be used to register a handler which should be used for constructing a type instead of default one.
784
+
785
+ ```typescript
786
+ import { Type, Property } from '@dipscope/type-manager';
787
+
788
+ @Type({
789
+ factory: new UserFactory()
790
+ })
791
+ export class User
792
+ {
793
+ @Property(String) public name: string;
794
+ }
795
+ ```
796
+
797
+ This may be useful in cases when you want to init some special application specific properties. Read more about [defining custom factory](#defining-custom-factory) in a separate section.
798
+
799
+ ### Injectable option
800
+
801
+ This option is used to register a type in dependency injection container.
802
+
803
+ ```typescript
804
+ import { Type } from '@dipscope/type-manager';
805
+
806
+ @Type({
807
+ injectable: true
808
+ })
809
+ export class UserService
810
+ {
811
+ public property: string;
812
+ }
813
+ ```
814
+
815
+ Injectable type later can be provided as a dependency.
816
+
817
+ ```typescript
818
+ import { Type, Property, Inject } from '@dipscope/type-manager';
819
+
820
+ @Type()
821
+ export class User
822
+ {
823
+ @Property(String) public name: string;
824
+
825
+ public constructor(@Inject(UserService) userService: UserService)
826
+ {
827
+ // Any action with UserService...
828
+
829
+ return;
830
+ }
831
+ }
832
+ ```
833
+
834
+ In most cases you will work in environment where dependency injection system is already setted up. In this case you have to implement custom `Injector` to be used instead of our default one. Besides you should follow the steps to register injectable services specified by the vendor. You can read more about [creating a custom injector](#defining-custom-injector) in a separate section.
835
+
836
+ ### Injector option
837
+
838
+ This option can be used to define a custom injector implementation which should be used in a type scope.
839
+
840
+ ```typescript
841
+ import { Type, Property } from '@dipscope/type-manager';
842
+
843
+ @Type({
844
+ injector: new UserInjector()
845
+ })
846
+ export class User
847
+ {
848
+ @Property(String) public name: string;
849
+ }
850
+ ```
851
+
852
+ In most cases this is not required and the common use case is to specify injector globally instead. You can read more about [defining custom injector](#defining-custom-injector) in a separate section.
853
+
854
+ ### Naming convention option
855
+
856
+ This option can be used both on type and property to provide custom naming strategy.
857
+
858
+ ```typescript
859
+ import { Type, Property, CamelCaseNamingConvention, SnakeCaseNamingConvention } from '@dipscope/type-manager';
860
+
861
+ @Type({
862
+ namingConvention: new CamelCaseNamingConvention()
863
+ })
864
+ export class User
865
+ {
866
+ @Property(String, { namingConvention: new SnakeCaseNamingConvention() }) public name: string;
867
+ }
868
+ ```
869
+
870
+ In most cases this is not required and the common use case is to specify naming strategy globally instead. You can read more about [configuring naming convention](#configuring-naming-convention) in a separate section.
871
+
872
+ ### Preserve discriminator option
873
+
874
+ This option defines if discriminator should be preserved in objects during serialization and deserialization.
875
+
876
+ ```typescript
877
+ import { Type, Property } from '@dipscope/type-manager';
878
+
879
+ @Type({
880
+ preserveDiscriminator: true
881
+ })
882
+ export class User
883
+ {
884
+ @Property(String) public name: string;
885
+ }
886
+ ```
887
+
888
+ By default discriminator is not preserved and only used during deserialization of polymorphic types. You can read more about handling of polymorphic types in this [section](#configuring-usage-of-polymorphic-types).
889
+
890
+ ### Preserve null option
891
+
892
+ This option defines if null values should be preserved during serialization and deserialization.
893
+
894
+ ```typescript
895
+ import { Type, Property } from '@dipscope/type-manager';
896
+
897
+ @Type({
898
+ preserveNull: true
899
+ })
900
+ export class User
901
+ {
902
+ @Property(String, { preserveNull: false }) public name: string;
903
+ }
904
+ ```
905
+
906
+ By default null values are preserved. You can set it to `false` per type, property or globally using `TypeManager` configure method. This will result in treating null values as undefined so you will get all related behaviours like setting default values.
907
+
908
+ ### Reference handler option
909
+
910
+ This option can be used both on type and property to specify how references to the same objects should be handled during serialization and deserialization.
911
+
912
+ ```typescript
913
+ import { Type, Property, DirectReferenceHandler, LeadReferenceHandler } from '@dipscope/type-manager';
914
+
915
+ @Type({
916
+ referenceHandler: new DirectReferenceHandler()
917
+ })
918
+ export class User
919
+ {
920
+ @Property(String, { referenceHandler: new LeadReferenceHandler() }) public name: string;
921
+ }
922
+ ```
923
+
924
+ In most cases this is not required and the common use case is to specify reference handler globally instead. You can read more about [configuring reference handler](#configuring-reference-handler) in a separate section.
925
+
926
+ ### Serializable option
927
+
928
+ This option is used to enable or disable serialization for a certain property.
929
+
930
+ ```typescript
931
+ import { Type, Property } from '@dipscope/type-manager';
932
+
933
+ @Type()
934
+ export class User
935
+ {
936
+ @Property(String, { serializable: true }) public name: string;
937
+ }
938
+ ```
939
+
940
+ By default all properties are serializable.
941
+
942
+ ### Serializer option
943
+
944
+ This option is used to define custom serializer for a type or property.
945
+
946
+ ```typescript
947
+ import { Type, Property } from '@dipscope/type-manager';
948
+
949
+ @Type({
950
+ serializer: new UserSerializer()
951
+ })
952
+ export class User
953
+ {
954
+ @Property(String, { serializer: new UserNameSerializer() }) public name: string;
955
+ }
956
+ ```
957
+
958
+ Custom serializer should be an implementation of `Serializer` interface. You can read more about [creating a custom serializer](#defining-custom-serializer) in a separate section.
959
+
960
+ ### Use default value option
961
+
962
+ This option enables or disables using default value per type or property.
963
+
964
+ ```typescript
965
+ import { Type, Property } from '@dipscope/type-manager';
966
+
967
+ @Type({
968
+ useDefaultValue: true
969
+ })
970
+ export class User
971
+ {
972
+ @Property(String, { useDefaultValue: false }) public name: string;
973
+ }
974
+ ```
975
+
976
+ Using default values is turned off by default. You can enable them globally using `TypeManager` configure method.
977
+
978
+ ### Use implicit conversion option
979
+
980
+ By default if declared type will not match provided during serialization or deserialization an error will be logged and result value will be undefined. This means that for example assigning `Number` to `String` will not work as `StringSerializer` expects `String`. However `Number` and other types can be converted to `String` for you when implicit conversion is enabled.
981
+
982
+ ```typescript
983
+ import { Type, Property } from '@dipscope/type-manager';
984
+
985
+ @Type()
986
+ export class User
987
+ {
988
+ @Property(String, { useImplicitConversion: true }) public name: string;
989
+ }
990
+ ```
991
+
992
+ With this any value which can be converted to `String` will be converted properly. Such behaviour works for other built in serializers and supported for custom ones. By default implicit conversion is turned off. You can enable it using `useImplicitConversion` option per type and property or enable globally using `TypeManager` configure method.
993
+
994
+ ## Defining configuration manually
995
+
996
+ There are circumstances when decorators cannot be used or you don't want to. For example you are using a 3rd party package and cannot decorate classes from it. Another use case when you want to configure some options globally. In such scenarios you can define the complete configuration through special configure methods.
997
+
998
+ We have separate methods to configure each type manager option, so the provided examples can be simplified to avoid creating additional object. It is useful when you need to configure only one option. In our examples we are always use the main one to give you a general overview.
999
+
1000
+ ### Configuring global options
1001
+
1002
+ There are several options which can be configured globally. For example let's override using of default value option so we don't have to specify it explicitly per type or property.
1003
+
1004
+ ```typescript
1005
+ import { TypeManagerOptions, TypeOptionsBase } from '@dipscope/type-manager';
1006
+
1007
+ const typeOptionsBase: TypeOptionsBase<any> = {
1008
+ useDefaultValue: true
1009
+ };
1010
+
1011
+ const typeManagerOptions: TypeManagerOptions = {
1012
+ typeOptionsBase: typeOptionsBase
1013
+ };
1014
+
1015
+ TypeManager.configure(typeManagerOptions);
1016
+ ```
1017
+
1018
+ For the full list of available global options check `TypeOptionsBase` definition or follow the documentation as we are going to touch them while we proceed.
1019
+
1020
+ ### Configuring options per type
1021
+
1022
+ Here is an example of declarative configuration which can be used for 3rd party or your own classes. Basically you have exact same options as with decorators but configuration is done using method calls.
1023
+
1024
+ ```typescript
1025
+ import { DateTime } from '@external-library';
1026
+ import { TypeConfiguration, TypeMetadata, TypeManager } from '@dipscope/type-manager';
1027
+
1028
+ export class DateTimeConfiguration implements TypeConfiguration<DateTime>
1029
+ {
1030
+ public configure(typeMetadata: TypeMetadata<DateTime>): void
1031
+ {
1032
+ typeMetadata.hasAlias('DateTime')
1033
+ .hasSerializer(new DateTimeSerializer());
1034
+
1035
+ return;
1036
+ }
1037
+ }
1038
+
1039
+ export class UserConfiguration implements TypeConfiguration<User>
1040
+ {
1041
+ public configure(typeMetadata: TypeMetadata<User>): void
1042
+ {
1043
+ typeMetadata.hasAlias('User')
1044
+ .hasSerializer(new DateTimeSerializer());
1045
+
1046
+ typeMetadata.configurePropertyMetadata('name')
1047
+ .isSerializable()
1048
+ .hasAlias('username')
1049
+ .hasTypeArgument(String);
1050
+
1051
+ typeMetadata.configurePropertyMetadata('createdAt')
1052
+ .hasTypeArgument(DateTime);
1053
+
1054
+ return;
1055
+ }
1056
+ }
1057
+
1058
+ TypeManager.applyTypeConfiguration(DateTime, new DateTimeConfiguration())
1059
+ .applyTypeConfiguration(User, new UserConfiguration());
1060
+ ```
1061
+
1062
+ There is a well defined order to how type options are applied when using configure methods on one type. One should remember this when combining and overriding options in different places.
1063
+
1064
+ 1. Base type options are applied;
1065
+ 2. Decorator type options are applied;
1066
+ 3. Declarative type options are applied;
1067
+ 4. Property type options are applied;
1068
+
1069
+ With declarative configuration you can declare types like the following and keep metadata in another place.
1070
+
1071
+ ```typescript
1072
+ export class User
1073
+ {
1074
+ public name: string;
1075
+ public createdAt: DateTime;
1076
+ }
1077
+ ```
1078
+
1079
+ Also if you are declaring only 3rd party classes the use case can be the following with [reflect-metadata](https://github.com/rbuckton/reflect-metadata).
1080
+
1081
+ ```typescript
1082
+ import 'reflect-metadata';
1083
+ import { Type, Property } from '@dipscope/type-manager';
1084
+
1085
+ @Type()
1086
+ export class User
1087
+ {
1088
+ @Property() public name: string;
1089
+ @Property() public createdAt: DateTime;
1090
+ }
1091
+ ```
1092
+
1093
+ And without [reflect-metadata](https://github.com/rbuckton/reflect-metadata).
1094
+
1095
+ ```typescript
1096
+ import { Type, Property } from '@dipscope/type-manager';
1097
+
1098
+ @Type()
1099
+ export class User
1100
+ {
1101
+ @Property(String) public name: string;
1102
+ @Property(DateTime) public createdAt: DateTime;
1103
+ }
1104
+ ```
1105
+
1106
+ ### Configuring usage of polymorphic types
1107
+
1108
+ Let's assume we are working with a shapes. To describe different types of shape we have to create an abstract class with several descendants.
1109
+
1110
+ ```typescript
1111
+ import { Type, Property } from '@dipscope/type-manager';
1112
+
1113
+ @Type()
1114
+ export abstract class Shape
1115
+ {
1116
+ @Property(String) public title: string;
1117
+ }
1118
+
1119
+ @Type()
1120
+ export class Rectangle extends Shape
1121
+ {
1122
+ @Property(Number) public width: number;
1123
+ @Property(Number) public height: number;
1124
+ }
1125
+
1126
+ @Type()
1127
+ export class Square extends Shape
1128
+ {
1129
+ @Property(Number) public width: number;
1130
+ }
1131
+
1132
+ @Type()
1133
+ export class Circle extends Shape
1134
+ {
1135
+ @Property(Number) public radius: number;
1136
+ }
1137
+ ```
1138
+
1139
+ Some other class declares a `shapes` property in it.
1140
+
1141
+ ```typescript
1142
+ import { Type, Property } from '@dipscope/type-manager';
1143
+
1144
+ @Type()
1145
+ export class Plot
1146
+ {
1147
+ @Property(Array, [Shape]) public shapes: Array<Shape>;
1148
+ }
1149
+ ```
1150
+
1151
+ From the perspective of declaration everything looks ok but from the point of serialization some things may become complicated. Shapes property can store `Rectangle`, `Square` or `Circle`. Each of this classes have different properties. Here is an example of JSON.
1152
+
1153
+ ```json
1154
+ {
1155
+ "shapes": [
1156
+ {
1157
+ "title": "Cool rectangle",
1158
+ "width": 10,
1159
+ "height": 10
1160
+ },
1161
+ {
1162
+ "title": "Perfect square",
1163
+ "width": 10
1164
+ },
1165
+ {
1166
+ "title": "Simple circle",
1167
+ "radius": 6
1168
+ }
1169
+ ]
1170
+ }
1171
+ ```
1172
+
1173
+ During deserialization of this JSON to a `Plot` class we only aware that all plain objects inside an `Array` are somehow related to a `Shape` type. So any options to handle this?
1174
+
1175
+ Luckily we have a `TypeManager`. When you declaring you types using decorators or declarative style it builds inheritance graph between them which can be used during serialization and deserialization.
1176
+
1177
+ It uses 2 special configurable type options:
1178
+
1179
+ * `Discriminator` which defines a property inside an object which should be used to define a type.
1180
+ * `Discriminant` which represents a certain `Discriminator` value.
1181
+
1182
+ This options have default values if you have not configured them explicitly.
1183
+
1184
+ * Default value of discriminator is a `$type`. During deserialization `TypeManager` expects such property to be present inside a polymorphic object.
1185
+ * Default value of discriminant is a `ClassName` which determined based on the type function.
1186
+
1187
+ For proper deserialization of polymorphic types you have to provide such information inside your JSON.
1188
+
1189
+ ```json
1190
+ {
1191
+ "shapes": [
1192
+ {
1193
+ "$type": "Rectangle",
1194
+ "title": "Cool rectangle",
1195
+ "width": 10,
1196
+ "height": 10
1197
+ },
1198
+ {
1199
+ "$type": "Square",
1200
+ "title": "Perfect square",
1201
+ "width": 10
1202
+ },
1203
+ {
1204
+ "$type": "Circle",
1205
+ "title": "Simple circle",
1206
+ "radius": 6
1207
+ }
1208
+ ]
1209
+ }
1210
+ ```
1211
+
1212
+ Now your JSON will be handled properly and you will get `Rectangle`, `Square` and `Circle` class instances in return.
1213
+
1214
+ In some cases your `Discriminator` or `Discriminant` values will not match to our default ones. For example library like [Json.NET](https://www.newtonsoft.com/json) can be used on the backend side to send a response from your API. It uses `$type` property as `Discriminator` and full name of class as `Discriminant`. In such scenario our JSON may look like this.
1215
+
1216
+ ```json
1217
+ {
1218
+ "shapes": [
1219
+ {
1220
+ "$type": "Company.Api.Entities.Rectangle",
1221
+ "title": "Cool rectangle",
1222
+ "width": 10,
1223
+ "height": 10
1224
+ },
1225
+ {
1226
+ "$type": "Company.Api.Entities.Square",
1227
+ "title": "Perfect square",
1228
+ "width": 10
1229
+ },
1230
+ {
1231
+ "$type": "Company.Api.Entities.Circle",
1232
+ "title": "Simple circle",
1233
+ "radius": 6
1234
+ }
1235
+ ]
1236
+ }
1237
+ ```
1238
+
1239
+ To change `Discriminator` globally you have to use `TypeManager` configure method.
1240
+
1241
+ ```typescript
1242
+ import { TypeManagerOptions, TypeOptionsBase } from '@dipscope/type-manager';
1243
+
1244
+ const typeOptionsBase: TypeOptionsBase<any> = {
1245
+ discriminator: '$customType'
1246
+ };
1247
+
1248
+ const typeManagerOptions: TypeManagerOptions = {
1249
+ typeOptionsBase: typeOptionsBase
1250
+ };
1251
+
1252
+ TypeManager.configure(typeManagerOptions);
1253
+ ```
1254
+
1255
+ To change `Discriminant` you have to use per type configuration.
1256
+
1257
+ ```typescript
1258
+ import { TypeManagerOptions, TypeFn, TypeOptions, PropertyName, PropertyOptions } from '@dipscope/type-manager';
1259
+
1260
+ const rectangleOptions: TypeOptions<Rectangle> = {
1261
+ discriminant: 'Company.Api.Entities.Rectangle'
1262
+ };
1263
+
1264
+ const squareOptions: TypeOptions<Square> = {
1265
+ discriminant: 'Company.Api.Entities.Square'
1266
+ };
1267
+
1268
+ const circleOptions: TypeOptions<Circle> = {
1269
+ discriminant: 'Company.Api.Entities.Circle'
1270
+ };
1271
+
1272
+ const typeManagerOptions: TypeManagerOptions = {
1273
+ typeOptionsMap: new Map<TypeFn<any>, TypeOptions<any>>(
1274
+ [Rectangle, rectangleOptions],
1275
+ [Square, squareOptions],
1276
+ [Circle, circleOptions]
1277
+ )
1278
+ };
1279
+
1280
+ TypeManager.configure(typeManagerOptions);
1281
+ ```
1282
+
1283
+ As an alternative you can change `Discriminant` as the following using decorators.
1284
+
1285
+ ```typescript
1286
+ import { Type, Property } from '@dipscope/type-manager';
1287
+
1288
+ @Type({
1289
+ discriminant: 'Company.Api.Entities.Shape'
1290
+ })
1291
+ export abstract class Shape
1292
+ {
1293
+ @Property(String) public title: string;
1294
+ }
1295
+
1296
+ @Type({
1297
+ discriminant: 'Company.Api.Entities.Rectangle'
1298
+ })
1299
+ export class Rectangle extends Shape
1300
+ {
1301
+ @Property(Number) public width: number;
1302
+ @Property(Number) public height: number;
1303
+ }
1304
+
1305
+ @Type({
1306
+ discriminant: 'Company.Api.Entities.Square'
1307
+ })
1308
+ export class Square extends Shape
1309
+ {
1310
+ @Property(Number) public width: number;
1311
+ }
1312
+
1313
+ @Type({
1314
+ discriminant: 'Company.Api.Entities.Circle'
1315
+ })
1316
+ export class Circle extends Shape
1317
+ {
1318
+ @Property(Number) public radius: number;
1319
+ }
1320
+ ```
1321
+
1322
+ By default `Discriminator` is not preserved inside objects and only used during deserialization. You can change this behavior by enabling preserving of discriminator globally or per type.
1323
+
1324
+ ```typescript
1325
+ import { TypeManagerOptions, TypeOptionsBase } from '@dipscope/type-manager';
1326
+
1327
+ const typeOptionsBase: TypeOptionsBase<any> = {
1328
+ preserveDiscriminator: true
1329
+ };
1330
+
1331
+ const typeManagerOptions: TypeManagerOptions = {
1332
+ typeOptionsBase: typeOptionsBase
1333
+ };
1334
+
1335
+ TypeManager.configure(typeManagerOptions);
1336
+ ```
1337
+
1338
+ With this option enabled discriminator will be present in output data.
1339
+
1340
+ ### Configuring naming convention
1341
+
1342
+ Naming convention specifies how each declared property of a type should be treated when reading it from JSON. By default names are read as is. Let's assume we have a `User` class in camel case naming convention for properties.
1343
+
1344
+ ```typescript
1345
+ import { Type, Property } from '@dipscope/type-manager';
1346
+
1347
+ @Type()
1348
+ export class User
1349
+ {
1350
+ @Property(String) public name: string;
1351
+ @Property(Number) public loginCount: number;
1352
+ @Property(DateTime) public createdAt: DateTime;
1353
+ }
1354
+ ```
1355
+
1356
+ Our JSON should match the same naming convention to work properly.
1357
+
1358
+ ```json
1359
+ [
1360
+ {
1361
+ "name": "Dmitry",
1362
+ "loginCount": 10,
1363
+ "createdAt": "2021-02-22T20:15:00.000Z"
1364
+ },
1365
+ {
1366
+ "name": "Alex",
1367
+ "loginCount": 25,
1368
+ "createdAt": "2021-02-22T21:15:00.000Z"
1369
+ },
1370
+ {
1371
+ "name": "Anna",
1372
+ "loginCount": 3,
1373
+ "createdAt": "2021-02-22T21:15:23.000Z"
1374
+ }
1375
+ ]
1376
+ ```
1377
+
1378
+ But what to do if we don't control the JSON naming convention so it comes to us in a snake case?
1379
+
1380
+ ```json
1381
+ [
1382
+ {
1383
+ "name": "Dmitry",
1384
+ "login_count": 10,
1385
+ "created_at": "2021-02-22T20:15:00.000Z"
1386
+ },
1387
+ {
1388
+ "name": "Alex",
1389
+ "login_count": 25,
1390
+ "created_at": "2021-02-22T21:15:00.000Z"
1391
+ },
1392
+ {
1393
+ "name": "Anna",
1394
+ "login_count": 3,
1395
+ "created_at": "2021-02-22T21:15:23.000Z"
1396
+ }
1397
+ ]
1398
+ ```
1399
+
1400
+ We can still parse such a JSON by specifying an alias for each property but this will become a pain in a while.
1401
+
1402
+ ```typescript
1403
+ import { Type, Property } from '@dipscope/type-manager';
1404
+
1405
+ @Type()
1406
+ export class User
1407
+ {
1408
+ @Property(String) public name: string;
1409
+ @Property(Number, { alias: 'login_count' }) public loginCount: number;
1410
+ @Property(DateTime, { alias: 'created_at' }) public createdAt: DateTime;
1411
+ }
1412
+ ```
1413
+
1414
+ `TypeManager` supports several naming conventions we can use to avoid dealing with aliases.
1415
+
1416
+ * Camel case [camelCase];
1417
+ * Flat case [flatcase];
1418
+ * Flat upper case [FLATCASE];
1419
+ * Kebab case [kebab-kase];
1420
+ * Kebab upper case [KEBAB-CASE];
1421
+ * Pascal case [PascalCase];
1422
+ * Snake case [snake_case];
1423
+ * Snake upper case [SNAKE_CASE];
1424
+
1425
+ To set one we have to configure global options.
1426
+
1427
+ ```typescript
1428
+ import { TypeManagerOptions, TypeOptionsBase, SnakeCaseNamingConvention } from '@dipscope/type-manager';
1429
+
1430
+ const typeOptionsBase: TypeOptionsBase<any> = {
1431
+ namingConvention: new SnakeCaseNamingConvention()
1432
+ };
1433
+
1434
+ const typeManagerOptions: TypeManagerOptions = {
1435
+ typeOptionsBase: typeOptionsBase
1436
+ };
1437
+
1438
+ TypeManager.configure(typeManagerOptions);
1439
+ ```
1440
+
1441
+ Now all property names will be converted to snake case while reading them from JSON. If you have not found suitable naming convention you can easily implement your own. Read more about [creating a custom naming convention](#defining-custom-naming-convention) in a separate section.
1442
+
1443
+ ### Configuring reference handler
1444
+
1445
+ Reference handler defines how references to the same objects including a circular one should be treated. We have several reference handlers: circular, json path and plain. Each of them can be used globally or per type.
1446
+
1447
+ * Circular reference handler preserves object references without making any special changes;
1448
+ * Json path reference handler preserves object references using JSONPath notation;
1449
+ * Plain reference handler preserves object references excluding a circular one. When circular reference is detected it will be set to undefined;
1450
+
1451
+ There is nothing better to show the difference than code. For example we have two classes which reference each other:
1452
+
1453
+ ```typescript
1454
+ import { Type, Property } from '@dipscope/type-manager';
1455
+
1456
+ @Type()
1457
+ class User
1458
+ {
1459
+ @Property(() => Company) public company: Company;
1460
+ }
1461
+
1462
+ @Type()
1463
+ class Company
1464
+ {
1465
+ @Property(() => User) public user: User;
1466
+ }
1467
+ ```
1468
+
1469
+ Somewhere in code you have such a logic:
1470
+
1471
+ ```typescript
1472
+ import { TypeManager } from '@dipscope/type-manager';
1473
+
1474
+ const user = new User();
1475
+ const company = new Company();
1476
+
1477
+ user.company = company;
1478
+ company.user = user;
1479
+
1480
+ const result = TypeManager.serialize(User, user);
1481
+ ```
1482
+
1483
+ Here are results returned by different reference handlers:
1484
+
1485
+ ```typescript
1486
+ // Circular reference handler...
1487
+ { company: { user: result } };
1488
+
1489
+ // Json path reference handler...
1490
+ { company: { user: { $ref: '$' } } };
1491
+
1492
+ // Plain reference handler...
1493
+ { company: { user: undefined };
1494
+ ```
1495
+
1496
+ As you can see `CircularReferenceHandler` does not make changes to your data and completely fine until you have to convert circular reference structure to a string. `JSON.stringify` method which we are using under the hood does not support such conversions so you will encounter an error. In this case you can select another reference handler. For example `JsonPathReferenceHandler` which produces JSON string using JSONPath format for circular references supported by many libraries. Or you can simply ignore circular reference when it should be converted to a string and use `PlainReferenceHandler`. To change default reference handler you have to use `TypeManager` configure methods.
1497
+
1498
+ ```typescript
1499
+ import { TypeManagerOptions, TypeOptionsBase, JsonPathReferenceHandler } from '@dipscope/type-manager';
1500
+
1501
+ const typeOptionsBase: TypeOptionsBase<any> = {
1502
+ referenceHandler: new JsonPathReferenceHandler()
1503
+ };
1504
+
1505
+ const typeManagerOptions: TypeManagerOptions = {
1506
+ typeOptionsBase: typeOptionsBase
1507
+ };
1508
+
1509
+ TypeManager.configure(typeManagerOptions);
1510
+ ```
1511
+
1512
+ With such configuration any reference will be handled using JSONPath so you are completely free from errors during conversion to a string.
1513
+
1514
+ ## Advanced usage
1515
+
1516
+ Our goal is to cover as much use cases as possible without making you to write additional code but there always be an application specific case. With that in mind we allow you to customize and extend each part of our pipeline.
1517
+
1518
+ ### Defining custom data
1519
+
1520
+ You can attach you custom metadata to our decorators using `customData` option available on `Type` and `Property`.
1521
+
1522
+ ```typescript
1523
+ import { Type, Property } from '@dipscope/type-manager';
1524
+
1525
+ @Type({
1526
+ customData: { rank: 1 }
1527
+ })
1528
+ class User
1529
+ {
1530
+ @Property(String, { customData: { priority: 10 } }) public name: string;
1531
+ }
1532
+ ```
1533
+
1534
+ This allows you to get it later in serializers, factories, injectors or your code and perform specific actions. Besides pipeline you can get this data in any place you want using `TypeManager`.
1535
+
1536
+ ```typescript
1537
+ import { TypeManager } from '@dipscope/type-manager';
1538
+
1539
+ const userMetadata = TypeManager.extractTypeMetadata(User);
1540
+ const customData = userMetadata.customData;
1541
+
1542
+ // Do something with type custom data...
1543
+
1544
+ for (const propertyMetadata of userMetadata.propertyMetadataMap.values())
1545
+ {
1546
+ const propertyCustomData = propertyMetadata.customData;
1547
+
1548
+ // Do something with property custom data...
1549
+ }
1550
+ ```
1551
+
1552
+ ### Defining custom serializer
1553
+
1554
+ You can create your own serializer or replace built in one. First you have to implement `Serializer` interface. It declares `serialize` and `deserialize` methods. Serialize method is called during conversion of `JavaScript` object instance into a plain object. Deserialize method is called during backward conversion. Here is an example of possible definition for custom `DateTime` class.
1555
+
1556
+ ```typescript
1557
+ import { Serializer, TypeLike, SerializerContext } from '@dipscope/type-manager';
1558
+ import { Fn } from '@app/module';
1559
+
1560
+ export class DateTimeSerializer implements Serializer<DateTime>
1561
+ {
1562
+ public serialize(x: TypeLike<DateTime>, serializerContext: SerializerContext<DateTime>): TypeLike<string>
1563
+ {
1564
+ if (Fn.isUndefined(x))
1565
+ {
1566
+ return serializerContext.serializedDefaultValue;
1567
+ }
1568
+
1569
+ if (Fn.isNull(x))
1570
+ {
1571
+ return x;
1572
+ }
1573
+
1574
+ if (Fn.isDateTime(x))
1575
+ {
1576
+ return x.toIsoString();
1577
+ }
1578
+
1579
+ if (serializerContext.log.errorEnabled)
1580
+ {
1581
+ serializerContext.log.error(`${serializerContext.path}: cannot serialize value as date time.`, x);
1582
+ }
1583
+
1584
+ return undefined;
1585
+ }
1586
+
1587
+ public deserialize(x: TypeLike<string>, serializerContext: SerializerContext<DateTime>): TypeLike<DateTime>
1588
+ {
1589
+ if (Fn.isUndefined(x))
1590
+ {
1591
+ return serializerContext.deserializedDefaultValue;
1592
+ }
1593
+
1594
+ if (Fn.isNull(x))
1595
+ {
1596
+ return x;
1597
+ }
1598
+
1599
+ if (Fn.isString(x))
1600
+ {
1601
+ return DateTime.fromIsoString(x);
1602
+ }
1603
+
1604
+ if (serializerContext.log.errorEnabled)
1605
+ {
1606
+ serializerContext.log.error(`${serializerContext.path}: cannot deserialize value as date time.`, x);
1607
+ }
1608
+
1609
+ return undefined;
1610
+ }
1611
+ }
1612
+ ```
1613
+
1614
+ This example follows internal conventions and gives you a picture of how real serializer may look like. `TypeManager` does not perform any checks and just passes values directly to serializer. Thats why input values can be undefined, null or others depending from what is stored inside certain property. `TypeLike` is an internal type to declare this behaviour.
1615
+
1616
+ Serializer implementation is fully responsible for return result. You can get default values, custom data and other options specified in configuration from current serializer context and react accordingly. For example you can check if implicit conversion is enabled and convert any value to the target one.
1617
+
1618
+ When you are finished with definitions there are two possible ways to register a serializer. You can use decorators.
1619
+
1620
+ ```typescript
1621
+ import { Type, Serializer } from '@dipscope/type-manager';
1622
+
1623
+ @Type({
1624
+ serializer: new DateTimeSerializer()
1625
+ })
1626
+ export class DateTime
1627
+ {
1628
+ ...
1629
+ }
1630
+ ```
1631
+
1632
+ Or declarative configuration.
1633
+
1634
+ ```typescript
1635
+ import { TypeManager, TypeConfiguration, TypeMetadata } from '@dipscope/type-manager';
1636
+
1637
+ export class DateTimeConfiguration implements TypeConfiguration<DateTime>
1638
+ {
1639
+ public configure(typeMetadata: TypeMetadata<DateTime>): void
1640
+ {
1641
+ typeMetadata.hasAlias('DateTime')
1642
+ .hasSerializer(new DateTimeSerializer());
1643
+
1644
+ return;
1645
+ }
1646
+ }
1647
+
1648
+ TypeManager.applyTypeConfiguration(DateTime, new DateTimeConfiguration());
1649
+ ```
1650
+
1651
+ With declarative configuration it is possible to override built in serializers if it's behaviour not suitable for your use cases. Also you can register serializers for types we not yet cover.
1652
+
1653
+ ### Defining custom injector
1654
+
1655
+ In modern world we are always use some kind of framework to build our application. It is definitely already have a configured dependency injection container so let's configure `TypeManager` for using it instead of build in one. You have to implement `Injector` interface with only one method. Here how it may look like in `Angular`.
1656
+
1657
+ ```typescript
1658
+ import { Injector, TypeMetadata } from '@dipscope/type-manager';
1659
+ import { Injector as AngularInjector } from '@angular/core';
1660
+
1661
+ export class CustomInjector implements Injector
1662
+ {
1663
+ private readonly angularInjector: AngularInjector;
1664
+
1665
+ public constructor(angularInjector: AngularInjector)
1666
+ {
1667
+ this.angularInjector = angularInjector;
1668
+
1669
+ return;
1670
+ }
1671
+
1672
+ public get<TType>(typeMetadata: TypeMetadata<TType>): TType | undefined
1673
+ {
1674
+ return this.angularInjector.get(typeMetadata.typeFn);
1675
+ }
1676
+ }
1677
+ ```
1678
+
1679
+ In general you have to get framework injector or create it manually. Then call method to get and instance by type when one is requested. Implementations can differ but we hope idea is clear. When you are finished with definitions you have to register custom injector for a `TypeManager`.
1680
+
1681
+ ```typescript
1682
+ import { TypeManager } from '@dipscope/type-manager';
1683
+ import { Injector as AngularInjector } from '@angular/core';
1684
+
1685
+ const angularInjector: AngularInjector = ...; // Get framework injector in core module for example.
1686
+
1687
+ TypeManager.applyTypeOptionsBase({
1688
+ injector: new CustomInjector(angularInjector)
1689
+ });
1690
+ ```
1691
+
1692
+ Now types will be resolved using framework injector.
1693
+
1694
+ ### Defining custom factory
1695
+
1696
+ When you want to apply additional logic to how types are constructed you can specify custom factory globally or per type. Let's say you want to init some properties based on your custom data specified for a type. You can do this by extending default `TypeFactory`.
1697
+
1698
+ ```typescript
1699
+ import { TypeContext, Injector, TypeFactory } from '@dipscope/type-manager';
1700
+
1701
+ export class CustomTypeFactory extends TypeFactory
1702
+ {
1703
+ public build<TType>(typeContext: TypeContext<TType>, injector: Injector): TType
1704
+ {
1705
+ // Build any type.
1706
+ const type = super.build(typeContext, injector) as any;
1707
+
1708
+ // Resolve custom data.
1709
+ const typeMetadata = typeContext.typeMetadata;
1710
+ const customData = typeMetadata.customData;
1711
+
1712
+ // Process custom data.
1713
+ for (const propertyName in customData)
1714
+ {
1715
+ type[propertyName] = customData[propertyName];
1716
+ }
1717
+
1718
+ return type;
1719
+ }
1720
+ }
1721
+ ```
1722
+
1723
+ When you are finished with definitions there are two possible ways to register a factory. You can use decorators.
1724
+
1725
+ ```typescript
1726
+ import { Type, Factory } from '@dipscope/type-manager';
1727
+
1728
+ @Type({
1729
+ customData: { rank: 1 },
1730
+ factory: new CustomTypeFactory()
1731
+ })
1732
+ export class User
1733
+ {
1734
+ ...
1735
+ }
1736
+ ```
1737
+
1738
+ Or declarative configuration.
1739
+
1740
+ ```typescript
1741
+ import { TypeManager } from '@dipscope/type-manager';
1742
+
1743
+ // Overriding only for user type.
1744
+ TypeManager.applyTypeOptions(User, {
1745
+ customData: { rank: 1 },
1746
+ factory: new CustomTypeFactory()
1747
+ });
1748
+
1749
+ // Overriding for any type.
1750
+ TypeManager.applyTypeOptionsBase({
1751
+ factory: new CustomTypeFactory()
1752
+ });
1753
+ ```
1754
+
1755
+ ### Defining custom naming convention
1756
+
1757
+ To define custom naming convention you have to implement `NamingConvention` interface with only one `convert` method. Here is an example implementation of camel case naming convention.
1758
+
1759
+ ```typescript
1760
+ import { NamingConvention, getWords } from '@dipscope/type-manager';
1761
+
1762
+ export class CamelCaseNamingConvention implements NamingConvention
1763
+ {
1764
+ public convert(name: string): string
1765
+ {
1766
+ return getWords(name).reduce(this.reduce, '');
1767
+ }
1768
+
1769
+ private reduce(result: string, word: string, index: number): string
1770
+ {
1771
+ if (word.length === 0)
1772
+ {
1773
+ return result;
1774
+ }
1775
+
1776
+ const wordLowerCased = word.toLowerCase();
1777
+
1778
+ if (index === 0)
1779
+ {
1780
+ return wordLowerCased;
1781
+ }
1782
+
1783
+ return `${result}${wordLowerCased[0].toUpperCase()}${wordLowerCased.slice(1)}`;
1784
+ }
1785
+ }
1786
+ ```
1787
+
1788
+ Public `convert` method receives a property name as it declared in a class. You have to call internal `getWords` function on it which will split property name into array of the words. In the `reduce` function you can combine this words to whatever string you want. When you are finished with definitions you have to register custom naming convention for a `TypeManager`.
1789
+
1790
+ ```typescript
1791
+ import { TypeManager } from '@dipscope/type-manager';
1792
+
1793
+ TypeManager.applyTypeOptionsBase({
1794
+ namingConvention: new CamelCaseNamingConvention()
1795
+ });
1796
+ ```
1797
+
1798
+ Now property names will be resolved using your custom naming convention.
1799
+
1800
+ ## Use cases
1801
+
1802
+ This section describes certain use cases to separate them from the main documentation part. We will point to a concrete place you should read to setup what is necessary.
1803
+
1804
+ ### Built in serializers
1805
+
1806
+ Here is a list of types with built in serializers.
1807
+
1808
+ * Any;
1809
+ * Array;
1810
+ * ArrayBuffer;
1811
+ * Boolean;
1812
+ * DataView;
1813
+ * Date;
1814
+ * Float32Array;
1815
+ * Float64Array;
1816
+ * Int8Array;
1817
+ * Int16Array;
1818
+ * Int32Array;
1819
+ * Map;
1820
+ * Number;
1821
+ * Set;
1822
+ * String;
1823
+ * Uint8Array;
1824
+ * Uint8ClampedArray;
1825
+ * Uint16Array;
1826
+ * Uint32Array;
1827
+ * Unknown;
1828
+
1829
+ For these you don't have to do anything to make them work. You are free to [create a custom serializer](#defining-custom-serializer) or override built in one if it does not cover your use case.
1830
+
1831
+ ### Circular object references
1832
+
1833
+ We have a great support for circular references with different behaviour when they are detected. Read [configuring reference handler](#configuring-reference-handler) section for more info.
1834
+
1835
+ ### Dependency injection and immutable types
1836
+
1837
+ Follow the steps described in [inject decorator](#inject-decorator) section. To integrate our injection system with you yours check [defining custom injector](#defining-custom-injector) section.
1838
+
1839
+ ### Different case usage in class and JSON
1840
+
1841
+ If your cases differs between class and JSON. For example `camelCase` vs `snake_case`. You can setup naming convention to use during serialization and deserialization. Follow the steps described in [configuring naming convention](#configuring-naming-convention) section.
1842
+
1843
+ ### Enum types
1844
+
1845
+ Enum types are supported but require special handling. This is because of how enums are represented after compiling them to `JavaScript`. You can know more about how to define them in a [property decorator](#property-decorator) section. If you are interested about the `Enum` compilation details you can check the official [documentation](https://www.typescriptlang.org/docs/handbook/enums.html).
1846
+
1847
+ ### Generic types
1848
+
1849
+ Generic types are supported and you can define them. Read [property decorator](#property-decorator) section for more info.
1850
+
1851
+ ### Integration with Angular
1852
+
1853
+ With `Angular` you do not need to install [reflect-metadata](https://github.com/rbuckton/reflect-metadata) as it is already included in `core-js`. However, you still need to instruct CLI to include it in the build. This can be achieved simply by adding `import 'reflect-metadata';` to you `main.ts` file.
1854
+
1855
+ To make `Angular` injector work for you a custom `Injector` needs to be implemented. Check [defining custom injector](#defining-custom-injector) section for more info.
1856
+
1857
+ ### Polymorphic types
1858
+
1859
+ Polymorphic types are supported. In most cases additional configuration is required. Check [configuring usage of polymorphic types](#configuring-usage-of-polymorphic-types) section for more info.
1860
+
1861
+ ## Versioning
1862
+
1863
+ We use [SemVer](http://semver.org/) for versioning. For the versions available, see the versions section on [NPM project page](https://www.npmjs.com/package/@dipscope/type-manager).
1864
+
1865
+ See information about breaking changes, release notes and migration steps between versions in [CHANGELOG.md](https://github.com/dipscope/TypeManager.TS/blob/master/CHANGELOG.md) file.
1866
+
1867
+ ## Contributing
1868
+
1869
+ Please read [CONTRIBUTING.md](https://github.com/dipscope/TypeManager.TS/blob/master/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
1870
+
1871
+ ## Authors
1872
+
1873
+ * **Dmitry Pimonov** - *Initial work* - [dpimonov](https://github.com/dpimonov)
1874
+
1875
+ See also the list of [contributors](https://github.com/dipscope/TypeManager.TS/contributors) who participated in this project.
1876
+
1877
+ ## Notes
1878
+
1879
+ Thanks for checking this package.
1880
+
1881
+ Feel free to create an issue if you find any mistakes in documentation or have any improvements in mind.
1882
+
1883
+ We wish you good luck and happy coding!
1884
+
1885
+ ## License
1886
+
1887
+ This project is licensed under the Apache 2.0 License - see the [LICENSE.md](https://github.com/dipscope/TypeManager.TS/blob/master/LICENSE.md) file for details.