@opra/common 0.33.13 → 1.0.0-alpha.1

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 (582) hide show
  1. package/browser.js +4657 -3899
  2. package/cjs/document/api-document.js +112 -23
  3. package/cjs/document/common/api-base.js +27 -0
  4. package/cjs/document/common/data-type-map.js +66 -0
  5. package/cjs/document/common/document-element.js +31 -0
  6. package/cjs/document/common/document-init-context.js +69 -0
  7. package/cjs/document/common/document-node.js +124 -0
  8. package/cjs/document/common/opra-document-error.js +15 -0
  9. package/cjs/document/common/value.js +33 -0
  10. package/cjs/document/constants.js +13 -7
  11. package/cjs/document/data-type/api-field.js +64 -0
  12. package/cjs/document/data-type/complex-type-base.js +183 -0
  13. package/cjs/document/data-type/complex-type.js +89 -13
  14. package/cjs/document/data-type/data-type.js +32 -25
  15. package/cjs/document/data-type/decorators/api-field-decorator.js +26 -0
  16. package/cjs/document/data-type/decorators/complex-type.decorator.js +33 -0
  17. package/cjs/document/data-type/decorators/simple-type.decorator.js +67 -0
  18. package/cjs/document/data-type/enum-type.js +92 -36
  19. package/cjs/document/data-type/{builtin → extended-types}/base64.type.js +11 -2
  20. package/cjs/document/data-type/extended-types/date-string.type.js +44 -0
  21. package/cjs/document/data-type/extended-types/date-time-string.type.js +47 -0
  22. package/cjs/document/data-type/extended-types/date-time.type.js +58 -0
  23. package/cjs/document/data-type/extended-types/date.type.js +55 -0
  24. package/cjs/document/data-type/extended-types/email.type.js +80 -0
  25. package/cjs/document/data-type/extended-types/field-path.type.js +53 -0
  26. package/cjs/document/data-type/extended-types/filter.type.js +74 -0
  27. package/cjs/document/data-type/extended-types/index.js +16 -0
  28. package/cjs/document/data-type/{builtin → extended-types}/object-id.type.js +11 -2
  29. package/cjs/document/data-type/extended-types/operation-result.type.js +56 -0
  30. package/cjs/document/data-type/extended-types/time.type.js +47 -0
  31. package/cjs/document/data-type/{builtin → extended-types}/url.type.js +12 -3
  32. package/cjs/document/data-type/extended-types/uuid.type.js +30 -0
  33. package/cjs/document/data-type/mapped-type.js +77 -61
  34. package/cjs/document/data-type/mixin-type.js +78 -23
  35. package/cjs/document/data-type/omit-type.js +11 -0
  36. package/cjs/document/data-type/partial-type.js +13 -0
  37. package/cjs/document/data-type/pick-type.js +11 -0
  38. package/cjs/document/data-type/primitive-types/any.type.js +25 -0
  39. package/cjs/document/data-type/primitive-types/bigint.type.js +30 -0
  40. package/cjs/document/data-type/{builtin → primitive-types}/boolean.type.js +11 -2
  41. package/cjs/document/data-type/primitive-types/index.js +11 -0
  42. package/cjs/document/data-type/primitive-types/integer.type.js +30 -0
  43. package/cjs/document/data-type/{builtin → primitive-types}/null.type.js +11 -2
  44. package/cjs/document/data-type/primitive-types/number.type.js +40 -0
  45. package/cjs/document/data-type/{builtin → primitive-types}/object.type.js +7 -1
  46. package/cjs/document/data-type/primitive-types/string.type.js +58 -0
  47. package/cjs/document/data-type/required-type.js +13 -0
  48. package/cjs/document/data-type/simple-type.js +93 -14
  49. package/cjs/document/data-type/utils/create-mapped-class.js +54 -0
  50. package/cjs/document/data-type/utils/get-is-inherited-predicate-fn.js +15 -0
  51. package/cjs/document/{data-type/field-decorator.js → decorators/api-field-decorator.js} +7 -7
  52. package/cjs/document/{data-type → decorators}/complex-type.decorator.js +11 -1
  53. package/cjs/document/decorators/http-controller.decorator.js +117 -0
  54. package/cjs/document/decorators/http-operation-entity.decorator.js +462 -0
  55. package/cjs/document/decorators/http-operation.decorator.js +183 -0
  56. package/cjs/document/decorators/simple-type.decorator.js +67 -0
  57. package/cjs/document/factory/api-document.factory.js +178 -0
  58. package/cjs/document/factory/data-type.factory.js +498 -0
  59. package/cjs/document/factory/http-api.factory.js +315 -0
  60. package/cjs/document/http/decorators/http-controller.decorator.js +117 -0
  61. package/cjs/document/http/decorators/http-operation-entity.decorator.js +461 -0
  62. package/cjs/document/http/decorators/http-operation.decorator.js +183 -0
  63. package/cjs/document/http/http-api.js +40 -0
  64. package/cjs/document/http/http-controller.js +145 -0
  65. package/cjs/document/http/http-media-type.js +68 -0
  66. package/cjs/document/http/http-multipart-field.js +33 -0
  67. package/cjs/document/http/http-operation-response.js +42 -0
  68. package/cjs/document/http/http-operation.js +93 -0
  69. package/cjs/document/http/http-parameter.js +44 -0
  70. package/cjs/document/http/http-request-body.js +25 -0
  71. package/cjs/document/http/http-status-range.js +41 -0
  72. package/cjs/document/index.js +35 -22
  73. package/cjs/document/utils/inspect.util.js +3 -3
  74. package/cjs/document/utils/parse-regexp.util.js +22 -0
  75. package/cjs/exception/http-errors/bad-request.error.js +3 -3
  76. package/cjs/exception/http-errors/conflict.error.js +3 -3
  77. package/cjs/exception/http-errors/failed-dependency.error.js +3 -3
  78. package/cjs/exception/http-errors/forbidden.error.js +3 -3
  79. package/cjs/exception/http-errors/internal-server.error.js +3 -3
  80. package/cjs/exception/http-errors/method-not-allowed.error.js +3 -3
  81. package/cjs/exception/http-errors/not-acceptable.error.js +3 -3
  82. package/cjs/exception/http-errors/not-found.error.js +3 -3
  83. package/cjs/exception/{resource-errors → http-errors}/permission.error.js +2 -2
  84. package/cjs/exception/{resource-errors → http-errors}/resource-conflict.error.js +3 -3
  85. package/cjs/exception/{resource-errors → http-errors}/resource-not.available.error.js +5 -5
  86. package/cjs/exception/http-errors/unauthorized.error.js +3 -3
  87. package/cjs/exception/http-errors/unprocessable-entity.error.js +3 -3
  88. package/cjs/exception/index.js +4 -4
  89. package/cjs/exception/issue-severity.enum.js +1 -1
  90. package/cjs/exception/opra-exception.js +5 -16
  91. package/cjs/exception/opra-http-error.js +25 -0
  92. package/cjs/filter/antlr/OpraFilterLexer.js +259 -193
  93. package/cjs/filter/antlr/OpraFilterParser.js +137 -84
  94. package/cjs/filter/ast/expressions/arithmetic-expression.js +1 -1
  95. package/cjs/filter/ast/expressions/logical-expression.js +1 -3
  96. package/cjs/filter/ast/terms/number-literal.js +1 -3
  97. package/cjs/filter/ast/terms/time-literal.js +6 -4
  98. package/cjs/filter/build.js +1 -3
  99. package/cjs/filter/filter-rules.js +93 -0
  100. package/cjs/filter/filter-tree-visitor.js +4 -7
  101. package/cjs/filter/parse.js +2 -2
  102. package/cjs/filter/utils.js +1 -3
  103. package/cjs/helpers/function-utils.js +2 -11
  104. package/cjs/helpers/get-stack-filename.js +28 -4
  105. package/cjs/helpers/index.js +2 -2
  106. package/cjs/helpers/is-url-string.js +1 -2
  107. package/cjs/helpers/mixin-utils.js +5 -8
  108. package/cjs/helpers/monkey-patches.js +12 -0
  109. package/cjs/helpers/object-utils.js +4 -5
  110. package/cjs/helpers/parse-fields-projection.js +67 -0
  111. package/cjs/helpers/responsive-map.js +93 -117
  112. package/cjs/helpers/type-guards.js +22 -29
  113. package/cjs/http/enums/http-status-messages.js +63 -63
  114. package/cjs/http/enums/mime-types.enum.js +17 -0
  115. package/cjs/http/index.js +1 -2
  116. package/cjs/http/opra-url-path.js +14 -9
  117. package/cjs/http/opra-url.js +18 -15
  118. package/cjs/i18n/i18n.js +9 -5
  119. package/cjs/i18n/translate.js +2 -2
  120. package/cjs/index.js +1 -1
  121. package/cjs/schema/http/http-controller.interface.js +11 -0
  122. package/cjs/schema/http/http-operation.interface.js +7 -0
  123. package/cjs/schema/index.js +27 -2
  124. package/cjs/schema/type-guards.js +8 -30
  125. package/cjs/schema/value.interface.js +2 -0
  126. package/esm/document/api-document.js +112 -23
  127. package/esm/document/common/api-base.js +23 -0
  128. package/esm/document/common/data-type-map.js +62 -0
  129. package/esm/document/common/document-element.js +28 -0
  130. package/esm/document/common/document-init-context.js +65 -0
  131. package/esm/document/common/document-node.js +120 -0
  132. package/esm/document/common/opra-document-error.js +11 -0
  133. package/esm/document/common/value.js +30 -0
  134. package/esm/document/constants.js +12 -6
  135. package/esm/document/data-type/api-field.js +61 -0
  136. package/esm/document/data-type/complex-type-base.js +180 -0
  137. package/esm/document/data-type/complex-type.js +88 -11
  138. package/esm/document/data-type/data-type.js +32 -24
  139. package/esm/document/data-type/decorators/api-field-decorator.js +22 -0
  140. package/esm/document/data-type/decorators/complex-type.decorator.js +28 -0
  141. package/esm/document/data-type/decorators/simple-type.decorator.js +61 -0
  142. package/esm/document/data-type/enum-type.js +93 -36
  143. package/esm/document/data-type/extended-types/base64.type.js +22 -0
  144. package/esm/document/data-type/extended-types/date-string.type.js +41 -0
  145. package/esm/document/data-type/extended-types/date-time-string.type.js +44 -0
  146. package/esm/document/data-type/extended-types/date-time.type.js +55 -0
  147. package/esm/document/data-type/extended-types/date.type.js +52 -0
  148. package/esm/document/data-type/extended-types/email.type.js +77 -0
  149. package/esm/document/data-type/extended-types/field-path.type.js +50 -0
  150. package/esm/document/data-type/extended-types/filter.type.js +71 -0
  151. package/esm/document/data-type/extended-types/index.js +13 -0
  152. package/esm/document/data-type/extended-types/object-id.type.js +22 -0
  153. package/esm/document/{resource/types → data-type/extended-types}/operation-result.type.js +25 -14
  154. package/esm/document/data-type/extended-types/time.type.js +44 -0
  155. package/esm/document/data-type/extended-types/url.type.js +22 -0
  156. package/esm/document/data-type/extended-types/uuid.type.js +27 -0
  157. package/esm/document/data-type/mapped-type.js +76 -56
  158. package/esm/document/data-type/mixin-type.js +80 -24
  159. package/esm/document/data-type/omit-type.js +7 -0
  160. package/esm/document/data-type/partial-type.js +9 -0
  161. package/esm/document/data-type/pick-type.js +7 -0
  162. package/esm/document/data-type/primitive-types/any.type.js +22 -0
  163. package/esm/document/data-type/primitive-types/bigint.type.js +27 -0
  164. package/esm/document/data-type/primitive-types/boolean.type.js +22 -0
  165. package/esm/document/data-type/primitive-types/index.js +8 -0
  166. package/esm/document/data-type/primitive-types/integer.type.js +27 -0
  167. package/esm/document/data-type/{builtin → primitive-types}/null.type.js +11 -2
  168. package/esm/document/data-type/primitive-types/number.type.js +37 -0
  169. package/esm/document/data-type/{builtin → primitive-types}/object.type.js +7 -1
  170. package/esm/document/data-type/primitive-types/string.type.js +55 -0
  171. package/esm/document/data-type/required-type.js +9 -0
  172. package/esm/document/data-type/simple-type.js +92 -12
  173. package/esm/document/data-type/utils/create-mapped-class.js +50 -0
  174. package/esm/document/data-type/utils/get-is-inherited-predicate-fn.js +11 -0
  175. package/esm/document/{data-type/field-decorator.js → decorators/api-field-decorator.js} +5 -5
  176. package/esm/document/{data-type → decorators}/complex-type.decorator.js +12 -2
  177. package/esm/document/decorators/http-controller.decorator.js +112 -0
  178. package/esm/document/decorators/http-operation-entity.decorator.js +460 -0
  179. package/esm/document/decorators/http-operation.decorator.js +178 -0
  180. package/esm/document/decorators/simple-type.decorator.js +61 -0
  181. package/esm/document/factory/api-document.factory.js +174 -0
  182. package/esm/document/factory/data-type.factory.js +494 -0
  183. package/esm/document/factory/http-api.factory.js +311 -0
  184. package/esm/document/http/decorators/http-controller.decorator.js +112 -0
  185. package/esm/document/http/decorators/http-operation-entity.decorator.js +459 -0
  186. package/esm/document/http/decorators/http-operation.decorator.js +178 -0
  187. package/esm/document/http/http-api.js +36 -0
  188. package/esm/document/http/http-controller.js +142 -0
  189. package/esm/document/http/http-media-type.js +65 -0
  190. package/esm/document/http/http-multipart-field.js +29 -0
  191. package/esm/document/http/http-operation-response.js +38 -0
  192. package/esm/document/http/http-operation.js +90 -0
  193. package/esm/document/http/http-parameter.js +41 -0
  194. package/esm/document/http/http-request-body.js +21 -0
  195. package/esm/document/http/http-status-range.js +37 -0
  196. package/esm/document/index.js +34 -22
  197. package/esm/document/utils/inspect.util.js +3 -3
  198. package/esm/document/utils/parse-regexp.util.js +18 -0
  199. package/esm/exception/http-errors/bad-request.error.js +3 -3
  200. package/esm/exception/http-errors/conflict.error.js +3 -3
  201. package/esm/exception/http-errors/failed-dependency.error.js +3 -3
  202. package/esm/exception/http-errors/forbidden.error.js +3 -3
  203. package/esm/exception/http-errors/internal-server.error.js +3 -3
  204. package/esm/exception/http-errors/method-not-allowed.error.js +3 -3
  205. package/esm/exception/http-errors/not-acceptable.error.js +3 -3
  206. package/esm/exception/http-errors/not-found.error.js +3 -3
  207. package/esm/exception/{resource-errors → http-errors}/permission.error.js +2 -2
  208. package/esm/exception/{resource-errors → http-errors}/resource-conflict.error.js +3 -3
  209. package/esm/exception/{resource-errors → http-errors}/resource-not.available.error.js +5 -5
  210. package/esm/exception/http-errors/unauthorized.error.js +3 -3
  211. package/esm/exception/http-errors/unprocessable-entity.error.js +3 -3
  212. package/esm/exception/index.js +4 -4
  213. package/esm/exception/issue-severity.enum.js +1 -1
  214. package/esm/exception/opra-exception.js +5 -16
  215. package/esm/exception/opra-http-error.js +21 -0
  216. package/esm/filter/antlr/OpraFilterLexer.js +260 -194
  217. package/esm/filter/antlr/OpraFilterListener.js +1 -1
  218. package/esm/filter/antlr/OpraFilterParser.js +138 -85
  219. package/esm/filter/ast/expressions/arithmetic-expression.js +1 -1
  220. package/esm/filter/ast/expressions/logical-expression.js +1 -3
  221. package/esm/filter/ast/terms/number-literal.js +1 -3
  222. package/esm/filter/ast/terms/time-literal.js +6 -4
  223. package/esm/filter/build.js +2 -4
  224. package/esm/filter/filter-rules.js +89 -0
  225. package/esm/filter/filter-tree-visitor.js +4 -6
  226. package/esm/filter/parse.js +2 -2
  227. package/esm/filter/utils.js +1 -3
  228. package/esm/helpers/function-utils.js +1 -9
  229. package/esm/helpers/get-stack-filename.js +26 -3
  230. package/esm/helpers/index.js +2 -2
  231. package/esm/helpers/is-url-string.js +1 -2
  232. package/esm/helpers/mixin-utils.js +5 -8
  233. package/esm/helpers/monkey-patches.js +12 -0
  234. package/esm/helpers/object-utils.js +4 -5
  235. package/esm/helpers/parse-fields-projection.js +61 -0
  236. package/esm/helpers/responsive-map.js +93 -117
  237. package/esm/helpers/type-guards.js +22 -29
  238. package/esm/http/enums/http-status-messages.js +63 -63
  239. package/esm/http/enums/mime-types.enum.js +14 -0
  240. package/esm/http/index.js +1 -2
  241. package/esm/http/opra-url-path.js +14 -9
  242. package/esm/http/opra-url.js +18 -15
  243. package/esm/i18n/i18n.js +9 -5
  244. package/esm/i18n/translate.js +2 -2
  245. package/esm/index.js +1 -1
  246. package/esm/schema/http/http-controller.interface.js +8 -0
  247. package/esm/schema/http/http-operation.interface.js +4 -0
  248. package/esm/schema/index.js +27 -2
  249. package/esm/schema/type-guards.js +6 -24
  250. package/esm/schema/value.interface.js +1 -0
  251. package/package.json +14 -13
  252. package/types/document/api-document.d.ts +31 -51
  253. package/types/document/common/api-base.d.ts +17 -0
  254. package/types/document/common/data-type-map.d.ts +23 -0
  255. package/types/document/common/document-element.d.ts +23 -0
  256. package/types/document/common/document-init-context.d.ts +17 -0
  257. package/types/document/common/document-node.d.ts +65 -0
  258. package/types/document/common/opra-document-error.d.ts +12 -0
  259. package/types/document/common/value.d.ts +48 -0
  260. package/types/document/constants.d.ts +9 -3
  261. package/types/document/data-type/api-field.d.ts +65 -0
  262. package/types/document/data-type/complex-type-base.d.ts +80 -0
  263. package/types/document/data-type/complex-type.d.ts +61 -35
  264. package/types/document/data-type/data-type.d.ts +49 -46
  265. package/types/document/data-type/decorators/api-field-decorator.d.ts +5 -0
  266. package/types/document/data-type/decorators/complex-type.decorator.d.ts +2 -0
  267. package/types/document/data-type/decorators/simple-type.decorator.d.ts +20 -0
  268. package/types/document/data-type/enum-type.d.ts +59 -23
  269. package/types/document/data-type/extended-types/base64.type.d.ts +7 -0
  270. package/types/document/data-type/extended-types/date-string.type.d.ts +9 -0
  271. package/types/document/data-type/extended-types/date-time-string.type.d.ts +9 -0
  272. package/types/document/data-type/extended-types/date-time.type.d.ts +9 -0
  273. package/types/document/data-type/extended-types/date.type.d.ts +9 -0
  274. package/types/document/data-type/extended-types/email.type.d.ts +16 -0
  275. package/types/document/data-type/extended-types/field-path.type.d.ts +15 -0
  276. package/types/document/data-type/extended-types/filter.type.d.ts +16 -0
  277. package/types/document/data-type/extended-types/index.d.ts +13 -0
  278. package/types/document/data-type/extended-types/object-id.type.d.ts +7 -0
  279. package/types/document/{resource/types → data-type/extended-types}/operation-result.type.d.ts +7 -6
  280. package/types/document/data-type/extended-types/time.type.d.ts +9 -0
  281. package/types/document/data-type/extended-types/url.type.d.ts +7 -0
  282. package/types/document/data-type/extended-types/uuid.type.d.ts +8 -0
  283. package/types/document/data-type/mapped-type.d.ts +55 -44
  284. package/types/document/data-type/mixin-type.d.ts +65 -26
  285. package/types/document/data-type/omit-type.d.ts +15 -0
  286. package/types/document/data-type/partial-type.d.ts +28 -0
  287. package/types/document/data-type/pick-type.d.ts +16 -0
  288. package/types/document/data-type/primitive-types/any.type.d.ts +7 -0
  289. package/types/document/data-type/primitive-types/bigint.type.d.ts +8 -0
  290. package/types/document/data-type/primitive-types/boolean.type.d.ts +7 -0
  291. package/types/document/data-type/primitive-types/index.d.ts +8 -0
  292. package/types/document/data-type/primitive-types/integer.type.d.ts +8 -0
  293. package/types/document/data-type/primitive-types/null.type.d.ts +7 -0
  294. package/types/document/data-type/primitive-types/number.type.d.ts +9 -0
  295. package/types/document/data-type/primitive-types/object.type.d.ts +3 -0
  296. package/types/document/data-type/primitive-types/string.type.d.ts +11 -0
  297. package/types/document/data-type/required-type.d.ts +22 -0
  298. package/types/document/data-type/simple-type.d.ts +63 -24
  299. package/types/document/data-type/utils/create-mapped-class.d.ts +3 -0
  300. package/types/document/data-type/utils/get-is-inherited-predicate-fn.d.ts +1 -0
  301. package/types/document/decorators/api-field-decorator.d.ts +5 -0
  302. package/types/document/decorators/complex-type.decorator.d.ts +2 -0
  303. package/types/document/decorators/http-controller.decorator.d.ts +14 -0
  304. package/types/document/decorators/http-operation-entity.decorator.d.ts +100 -0
  305. package/types/document/decorators/http-operation.decorator.d.ts +30 -0
  306. package/types/document/decorators/simple-type.decorator.d.ts +20 -0
  307. package/types/document/factory/api-document.factory.d.ts +35 -0
  308. package/types/document/factory/data-type.factory.d.ts +103 -0
  309. package/types/document/factory/http-api.factory.d.ts +60 -0
  310. package/types/document/http/decorators/http-controller.decorator.d.ts +14 -0
  311. package/types/document/http/decorators/http-operation-entity.decorator.d.ts +100 -0
  312. package/types/document/http/decorators/http-operation.decorator.d.ts +30 -0
  313. package/types/document/http/http-api.d.ts +23 -0
  314. package/types/document/http/http-controller.d.ts +92 -0
  315. package/types/document/http/http-media-type.d.ts +59 -0
  316. package/types/document/http/http-multipart-field.d.ts +26 -0
  317. package/types/document/http/http-operation-response.d.ts +36 -0
  318. package/types/document/http/http-operation.d.ts +84 -0
  319. package/types/document/http/http-parameter.d.ts +50 -0
  320. package/types/document/http/http-request-body.d.ts +31 -0
  321. package/types/document/http/http-status-range.d.ts +14 -0
  322. package/types/document/index.d.ts +33 -22
  323. package/types/document/utils/parse-regexp.util.d.ts +4 -0
  324. package/types/exception/http-errors/bad-request.error.d.ts +2 -2
  325. package/types/exception/http-errors/conflict.error.d.ts +2 -2
  326. package/types/exception/http-errors/failed-dependency.error.d.ts +2 -2
  327. package/types/exception/http-errors/forbidden.error.d.ts +2 -2
  328. package/types/exception/http-errors/internal-server.error.d.ts +2 -2
  329. package/types/exception/http-errors/method-not-allowed.error.d.ts +2 -2
  330. package/types/exception/http-errors/not-acceptable.error.d.ts +2 -2
  331. package/types/exception/http-errors/not-found.error.d.ts +2 -2
  332. package/types/exception/{resource-errors → http-errors}/permission.error.d.ts +1 -1
  333. package/types/exception/http-errors/resource-conflict.error.d.ts +5 -0
  334. package/types/exception/{resource-errors → http-errors}/resource-not.available.error.d.ts +2 -2
  335. package/types/exception/http-errors/unauthorized.error.d.ts +2 -2
  336. package/types/exception/http-errors/unprocessable-entity.error.d.ts +2 -2
  337. package/types/exception/index.d.ts +4 -4
  338. package/types/exception/opra-exception.d.ts +1 -5
  339. package/types/exception/opra-http-error.d.ts +13 -0
  340. package/types/filter/antlr/OpraFilterLexer.d.ts +1 -1
  341. package/types/filter/antlr/OpraFilterListener.d.ts +2 -2
  342. package/types/filter/antlr/OpraFilterParser.d.ts +2 -2
  343. package/types/filter/antlr/OpraFilterVisitor.d.ts +1 -1
  344. package/types/filter/ast/terms/qualified-identifier.d.ts +1 -1
  345. package/types/filter/filter-rules.d.ts +24 -0
  346. package/types/filter/filter-tree-visitor.d.ts +1 -3
  347. package/types/helpers/function-utils.d.ts +2 -4
  348. package/types/helpers/get-stack-filename.d.ts +1 -0
  349. package/types/helpers/index.d.ts +2 -2
  350. package/types/helpers/monkey-patches.d.ts +0 -0
  351. package/types/helpers/parse-fields-projection.d.ts +11 -0
  352. package/types/helpers/responsive-map.d.ts +8 -4
  353. package/types/helpers/type-guards.d.ts +2 -2
  354. package/types/http/enums/http-status-messages.d.ts +63 -63
  355. package/types/http/enums/mime-types.enum.d.ts +12 -0
  356. package/types/http/index.d.ts +1 -2
  357. package/types/i18n/i18n.d.ts +2 -1
  358. package/types/schema/data-type/complex-type.interface.d.ts +3 -3
  359. package/types/schema/data-type/data-type.interface.d.ts +6 -0
  360. package/types/schema/data-type/enum-type.interface.d.ts +3 -4
  361. package/types/schema/data-type/field.interface.d.ts +2 -10
  362. package/types/schema/data-type/mapped-type.interface.d.ts +7 -3
  363. package/types/schema/data-type/mixin-type.interface.d.ts +2 -2
  364. package/types/schema/data-type/simple-type.interface.d.ts +35 -7
  365. package/types/schema/data-type-container.interface.d.ts +7 -0
  366. package/types/schema/document.interface.d.ts +55 -0
  367. package/types/schema/http/http-controller.interface.d.ts +24 -0
  368. package/types/schema/http/http-media-type.interface.d.ts +65 -0
  369. package/types/schema/http/http-multipart-field.interface.d.ts +7 -0
  370. package/types/schema/http/http-operation-response.interface.d.ts +24 -0
  371. package/types/schema/http/http-operation.interface.d.ts +23 -0
  372. package/types/schema/http/http-parameter.interface.d.ts +28 -0
  373. package/types/schema/http/http-request-body.interface.d.ts +29 -0
  374. package/types/schema/http/http-status-range.interface.d.ts +4 -0
  375. package/types/schema/index.d.ts +61 -2
  376. package/types/schema/type-guards.d.ts +2 -10
  377. package/types/schema/types.d.ts +3 -0
  378. package/types/schema/value.interface.d.ts +23 -0
  379. package/types/types.d.ts +1 -14
  380. package/cjs/document/data-type/builtin/any.type.js +0 -13
  381. package/cjs/document/data-type/builtin/approx-date.type.js +0 -21
  382. package/cjs/document/data-type/builtin/approx-datetime.type.js +0 -24
  383. package/cjs/document/data-type/builtin/bigint.type.js +0 -16
  384. package/cjs/document/data-type/builtin/date.type.js +0 -17
  385. package/cjs/document/data-type/builtin/datetime.type.js +0 -21
  386. package/cjs/document/data-type/builtin/email.type.js +0 -16
  387. package/cjs/document/data-type/builtin/index.js +0 -21
  388. package/cjs/document/data-type/builtin/integer.type.js +0 -16
  389. package/cjs/document/data-type/builtin/number.type.js +0 -16
  390. package/cjs/document/data-type/builtin/string.type.js +0 -16
  391. package/cjs/document/data-type/builtin/time.type.js +0 -23
  392. package/cjs/document/data-type/builtin/uuid.type.js +0 -16
  393. package/cjs/document/data-type/complex-type-class.js +0 -274
  394. package/cjs/document/data-type/enum-type-class.js +0 -39
  395. package/cjs/document/data-type/field-class.js +0 -67
  396. package/cjs/document/data-type/field.js +0 -25
  397. package/cjs/document/data-type/mapped-type-class.js +0 -53
  398. package/cjs/document/data-type/mixin-type-class.js +0 -38
  399. package/cjs/document/data-type/simple-type-class.js +0 -37
  400. package/cjs/document/data-type/simple-type.decorator.js +0 -20
  401. package/cjs/document/document-base.js +0 -24
  402. package/cjs/document/factory/api-document-factory.js +0 -262
  403. package/cjs/document/factory/type-document-factory.js +0 -326
  404. package/cjs/document/resource/action-decorator.js +0 -33
  405. package/cjs/document/resource/action.js +0 -34
  406. package/cjs/document/resource/collection-class.js +0 -257
  407. package/cjs/document/resource/collection-decorator.js +0 -211
  408. package/cjs/document/resource/collection.js +0 -25
  409. package/cjs/document/resource/container-class.js +0 -84
  410. package/cjs/document/resource/container-decorator.js +0 -24
  411. package/cjs/document/resource/container.js +0 -29
  412. package/cjs/document/resource/crud-operation-decorator.js +0 -28
  413. package/cjs/document/resource/crud-operation.js +0 -49
  414. package/cjs/document/resource/crud-resource.js +0 -34
  415. package/cjs/document/resource/endpoint.js +0 -59
  416. package/cjs/document/resource/enums/metadata-mode.enum.js +0 -19
  417. package/cjs/document/resource/parameter.js +0 -50
  418. package/cjs/document/resource/resource-decorator.js +0 -33
  419. package/cjs/document/resource/resource.js +0 -60
  420. package/cjs/document/resource/singleton-class.js +0 -83
  421. package/cjs/document/resource/singleton-decorator.js +0 -57
  422. package/cjs/document/resource/singleton.js +0 -25
  423. package/cjs/document/resource/storage-class.js +0 -18
  424. package/cjs/document/resource/storage-decorator.js +0 -70
  425. package/cjs/document/resource/storage.js +0 -25
  426. package/cjs/document/resource/types/operation-result.type.js +0 -45
  427. package/cjs/document/type-document.js +0 -204
  428. package/cjs/exception/wrap-exception.js +0 -43
  429. package/cjs/helpers/path-to-object-tree.js +0 -28
  430. package/cjs/schema/opra-schema.ns.js +0 -22
  431. package/cjs/schema/resource/collection.interface.js +0 -7
  432. package/cjs/schema/resource/container.interface.js +0 -7
  433. package/cjs/schema/resource/singleton.interface.js +0 -7
  434. package/cjs/schema/resource/storage.interface.js +0 -7
  435. package/esm/document/data-type/builtin/any.type.js +0 -10
  436. package/esm/document/data-type/builtin/approx-date.type.js +0 -18
  437. package/esm/document/data-type/builtin/approx-datetime.type.js +0 -21
  438. package/esm/document/data-type/builtin/base64.type.js +0 -13
  439. package/esm/document/data-type/builtin/bigint.type.js +0 -13
  440. package/esm/document/data-type/builtin/boolean.type.js +0 -13
  441. package/esm/document/data-type/builtin/date.type.js +0 -14
  442. package/esm/document/data-type/builtin/datetime.type.js +0 -18
  443. package/esm/document/data-type/builtin/email.type.js +0 -13
  444. package/esm/document/data-type/builtin/index.js +0 -18
  445. package/esm/document/data-type/builtin/integer.type.js +0 -13
  446. package/esm/document/data-type/builtin/number.type.js +0 -13
  447. package/esm/document/data-type/builtin/object-id.type.js +0 -13
  448. package/esm/document/data-type/builtin/string.type.js +0 -13
  449. package/esm/document/data-type/builtin/time.type.js +0 -20
  450. package/esm/document/data-type/builtin/url.type.js +0 -13
  451. package/esm/document/data-type/builtin/uuid.type.js +0 -13
  452. package/esm/document/data-type/complex-type-class.js +0 -270
  453. package/esm/document/data-type/enum-type-class.js +0 -35
  454. package/esm/document/data-type/field-class.js +0 -63
  455. package/esm/document/data-type/field.js +0 -21
  456. package/esm/document/data-type/mapped-type-class.js +0 -48
  457. package/esm/document/data-type/mixin-type-class.js +0 -34
  458. package/esm/document/data-type/simple-type-class.js +0 -33
  459. package/esm/document/data-type/simple-type.decorator.js +0 -15
  460. package/esm/document/document-base.js +0 -20
  461. package/esm/document/factory/api-document-factory.js +0 -258
  462. package/esm/document/factory/type-document-factory.js +0 -322
  463. package/esm/document/resource/action-decorator.js +0 -29
  464. package/esm/document/resource/action.js +0 -30
  465. package/esm/document/resource/collection-class.js +0 -253
  466. package/esm/document/resource/collection-decorator.js +0 -207
  467. package/esm/document/resource/collection.js +0 -21
  468. package/esm/document/resource/container-class.js +0 -80
  469. package/esm/document/resource/container-decorator.js +0 -20
  470. package/esm/document/resource/container.js +0 -25
  471. package/esm/document/resource/crud-operation-decorator.js +0 -24
  472. package/esm/document/resource/crud-operation.js +0 -45
  473. package/esm/document/resource/crud-resource.js +0 -30
  474. package/esm/document/resource/endpoint.js +0 -55
  475. package/esm/document/resource/enums/metadata-mode.enum.js +0 -16
  476. package/esm/document/resource/parameter.js +0 -46
  477. package/esm/document/resource/resource-decorator.js +0 -28
  478. package/esm/document/resource/resource.js +0 -56
  479. package/esm/document/resource/singleton-class.js +0 -79
  480. package/esm/document/resource/singleton-decorator.js +0 -53
  481. package/esm/document/resource/singleton.js +0 -21
  482. package/esm/document/resource/storage-class.js +0 -14
  483. package/esm/document/resource/storage-decorator.js +0 -66
  484. package/esm/document/resource/storage.js +0 -21
  485. package/esm/document/type-document.js +0 -200
  486. package/esm/exception/wrap-exception.js +0 -39
  487. package/esm/helpers/path-to-object-tree.js +0 -24
  488. package/esm/schema/opra-schema.ns.js +0 -19
  489. package/esm/schema/resource/collection.interface.js +0 -4
  490. package/esm/schema/resource/container.interface.js +0 -4
  491. package/esm/schema/resource/singleton.interface.js +0 -4
  492. package/esm/schema/resource/storage.interface.js +0 -4
  493. package/types/document/data-type/builtin/any.type.d.ts +0 -2
  494. package/types/document/data-type/builtin/approx-date.type.d.ts +0 -2
  495. package/types/document/data-type/builtin/approx-datetime.type.d.ts +0 -2
  496. package/types/document/data-type/builtin/base64.type.d.ts +0 -2
  497. package/types/document/data-type/builtin/bigint.type.d.ts +0 -2
  498. package/types/document/data-type/builtin/boolean.type.d.ts +0 -2
  499. package/types/document/data-type/builtin/date.type.d.ts +0 -2
  500. package/types/document/data-type/builtin/datetime.type.d.ts +0 -2
  501. package/types/document/data-type/builtin/email.type.d.ts +0 -2
  502. package/types/document/data-type/builtin/index.d.ts +0 -18
  503. package/types/document/data-type/builtin/integer.type.d.ts +0 -2
  504. package/types/document/data-type/builtin/null.type.d.ts +0 -2
  505. package/types/document/data-type/builtin/number.type.d.ts +0 -2
  506. package/types/document/data-type/builtin/object-id.type.d.ts +0 -2
  507. package/types/document/data-type/builtin/object.type.d.ts +0 -2
  508. package/types/document/data-type/builtin/string.type.d.ts +0 -2
  509. package/types/document/data-type/builtin/time.type.d.ts +0 -2
  510. package/types/document/data-type/builtin/url.type.d.ts +0 -2
  511. package/types/document/data-type/builtin/uuid.type.d.ts +0 -2
  512. package/types/document/data-type/complex-type-class.d.ts +0 -36
  513. package/types/document/data-type/complex-type.decorator.d.ts +0 -2
  514. package/types/document/data-type/enum-type-class.d.ts +0 -18
  515. package/types/document/data-type/field-class.d.ts +0 -31
  516. package/types/document/data-type/field-decorator.d.ts +0 -5
  517. package/types/document/data-type/field.d.ts +0 -46
  518. package/types/document/data-type/mapped-type-class.d.ts +0 -18
  519. package/types/document/data-type/mixin-type-class.d.ts +0 -12
  520. package/types/document/data-type/simple-type-class.d.ts +0 -19
  521. package/types/document/data-type/simple-type.decorator.d.ts +0 -2
  522. package/types/document/document-base.d.ts +0 -10
  523. package/types/document/factory/api-document-factory.d.ts +0 -49
  524. package/types/document/factory/type-document-factory.d.ts +0 -57
  525. package/types/document/interfaces/collection.interface.d.ts +0 -10
  526. package/types/document/interfaces/singleton.interface.d.ts +0 -7
  527. package/types/document/interfaces/storage.interface.d.ts +0 -8
  528. package/types/document/resource/action-decorator.d.ts +0 -8
  529. package/types/document/resource/action.d.ts +0 -30
  530. package/types/document/resource/collection-class.d.ts +0 -20
  531. package/types/document/resource/collection-decorator.d.ts +0 -204
  532. package/types/document/resource/collection.d.ts +0 -62
  533. package/types/document/resource/container-class.d.ts +0 -66
  534. package/types/document/resource/container-decorator.d.ts +0 -37
  535. package/types/document/resource/container.d.ts +0 -27
  536. package/types/document/resource/crud-operation-decorator.d.ts +0 -6
  537. package/types/document/resource/crud-operation.d.ts +0 -32
  538. package/types/document/resource/crud-resource.d.ts +0 -16
  539. package/types/document/resource/endpoint.d.ts +0 -32
  540. package/types/document/resource/enums/metadata-mode.enum.d.ts +0 -5
  541. package/types/document/resource/parameter.d.ts +0 -35
  542. package/types/document/resource/resource-decorator.d.ts +0 -40
  543. package/types/document/resource/resource.d.ts +0 -35
  544. package/types/document/resource/singleton-class.d.ts +0 -21
  545. package/types/document/resource/singleton-decorator.d.ts +0 -131
  546. package/types/document/resource/singleton.d.ts +0 -49
  547. package/types/document/resource/storage-class.d.ts +0 -17
  548. package/types/document/resource/storage-decorator.d.ts +0 -100
  549. package/types/document/resource/storage.d.ts +0 -39
  550. package/types/document/type-document.d.ts +0 -79
  551. package/types/exception/resource-errors/resource-conflict.error.d.ts +0 -5
  552. package/types/exception/wrap-exception.d.ts +0 -2
  553. package/types/helpers/path-to-object-tree.d.ts +0 -4
  554. package/types/schema/document/api-document.interface.d.ts +0 -11
  555. package/types/schema/document/document-base.interface.d.ts +0 -24
  556. package/types/schema/document/type-document.interface.d.ts +0 -6
  557. package/types/schema/opra-schema.ns.d.ts +0 -19
  558. package/types/schema/resource/action.interface.d.ts +0 -6
  559. package/types/schema/resource/collection.interface.d.ts +0 -55
  560. package/types/schema/resource/container.interface.d.ts +0 -10
  561. package/types/schema/resource/endpoint.interface.d.ts +0 -13
  562. package/types/schema/resource/resource.interface.d.ts +0 -15
  563. package/types/schema/resource/singleton.interface.d.ts +0 -25
  564. package/types/schema/resource/storage.interface.d.ts +0 -61
  565. /package/cjs/{document/interfaces/collection.interface.js → schema/data-type-container.interface.js} +0 -0
  566. /package/cjs/{document/interfaces/singleton.interface.js → schema/document.interface.js} +0 -0
  567. /package/cjs/{document/interfaces/storage.interface.js → schema/http/http-media-type.interface.js} +0 -0
  568. /package/cjs/schema/{document/api-document.interface.js → http/http-multipart-field.interface.js} +0 -0
  569. /package/cjs/schema/{document/document-base.interface.js → http/http-operation-response.interface.js} +0 -0
  570. /package/cjs/schema/{document/type-document.interface.js → http/http-parameter.interface.js} +0 -0
  571. /package/cjs/schema/{resource/action.interface.js → http/http-request-body.interface.js} +0 -0
  572. /package/cjs/schema/{resource/endpoint.interface.js → http/http-status-range.interface.js} +0 -0
  573. /package/cjs/schema/{resource/resource.interface.js → types.js} +0 -0
  574. /package/esm/{document/interfaces/collection.interface.js → schema/data-type-container.interface.js} +0 -0
  575. /package/esm/{document/interfaces/singleton.interface.js → schema/document.interface.js} +0 -0
  576. /package/esm/{document/interfaces/storage.interface.js → schema/http/http-media-type.interface.js} +0 -0
  577. /package/esm/schema/{document/api-document.interface.js → http/http-multipart-field.interface.js} +0 -0
  578. /package/esm/schema/{document/document-base.interface.js → http/http-operation-response.interface.js} +0 -0
  579. /package/esm/schema/{document/type-document.interface.js → http/http-parameter.interface.js} +0 -0
  580. /package/esm/schema/{resource/action.interface.js → http/http-request-body.interface.js} +0 -0
  581. /package/esm/schema/{resource/endpoint.interface.js → http/http-status-range.interface.js} +0 -0
  582. /package/esm/schema/{resource/resource.interface.js → types.js} +0 -0
@@ -0,0 +1,174 @@
1
+ import { resolveThunk } from '../../helpers/index.js';
2
+ import { OpraSchema } from '../../schema/index.js';
3
+ import { ApiDocument } from '../api-document.js';
4
+ import { DocumentInitContext } from '../common/document-init-context.js';
5
+ import { OpraDocumentError } from '../common/opra-document-error.js';
6
+ import { BUILTIN, CLASS_NAME_PATTERN, kCtorMap } from '../constants.js';
7
+ import { Base64Type, DateStringType, DateTimeStringType, DateTimeType, DateType, EmailType, FieldPathType, FilterType, ObjectIdType, OperationResult, TimeType, UrlType, UuidType, } from '../data-type/extended-types/index.js';
8
+ import { AnyType, BigintType, BooleanType, IntegerType, NullType, NumberType, ObjectType, StringType, } from '../data-type/primitive-types/index.js';
9
+ import { DataTypeFactory } from './data-type.factory.js';
10
+ import { HttpApiFactory } from './http-api.factory.js';
11
+ const OPRA_SPEC_URL = 'https://oprajs.com/spec/v' + OpraSchema.SpecVersion;
12
+ /**
13
+ * @class ApiDocumentFactory
14
+ */
15
+ export class ApiDocumentFactory {
16
+ /**
17
+ * Creates ApiDocument instance from given schema object
18
+ */
19
+ static async createDocument(schemaOrUrl, options) {
20
+ const factory = new ApiDocumentFactory();
21
+ const context = options instanceof DocumentInitContext ? options : new DocumentInitContext(options);
22
+ try {
23
+ const document = new ApiDocument();
24
+ await factory.initDocument(document, context, schemaOrUrl);
25
+ if (context.error.details.length)
26
+ throw context.error;
27
+ return document;
28
+ }
29
+ catch (e) {
30
+ try {
31
+ if (!(e instanceof OpraDocumentError)) {
32
+ context.addError(e);
33
+ }
34
+ }
35
+ catch {
36
+ //
37
+ }
38
+ if (!context.error.message) {
39
+ const l = context.error.details.length;
40
+ context.error.message = `(${l}) error${l > 1 ? 's' : ''} found in document schema.`;
41
+ if (context.showErrorDetails) {
42
+ context.error.message += context.error.details
43
+ .map(d => {
44
+ return `\n\n - ${d.message}` + (d.path ? `\n @${d.path}` : '');
45
+ })
46
+ .join('');
47
+ }
48
+ }
49
+ throw context.error;
50
+ }
51
+ }
52
+ /**
53
+ * Downloads schema from the given URL and creates the document instance * @param url
54
+ */
55
+ async initDocument(document, context, schemaOrUrl) {
56
+ let init;
57
+ if (typeof schemaOrUrl === 'string') {
58
+ const resp = await fetch(schemaOrUrl, { method: 'GET' });
59
+ init = await resp.json();
60
+ if (!init)
61
+ return context.addError(`Invalid response returned from url: ${schemaOrUrl}`);
62
+ init.url = schemaOrUrl;
63
+ }
64
+ else
65
+ init = schemaOrUrl;
66
+ // Add builtin data types if this document is the root
67
+ if (!document[BUILTIN]) {
68
+ const builtinDocument = await this.createBuiltinDocument(context);
69
+ document.references.set('opra', builtinDocument);
70
+ }
71
+ init.spec = init.spec || OpraSchema.SpecVersion;
72
+ document.url = init.url;
73
+ if (init.info)
74
+ document.info = { ...init.info };
75
+ /** Add references */
76
+ if (init.references) {
77
+ await context.enterAsync('.references', async () => {
78
+ let ns;
79
+ let r;
80
+ for ([ns, r] of Object.entries(init.references)) {
81
+ r = await resolveThunk(r);
82
+ await context.enterAsync(`[${ns}]`, async () => {
83
+ if (!CLASS_NAME_PATTERN.test(ns))
84
+ throw new TypeError(`Invalid namespace (${ns})`);
85
+ if (r instanceof ApiDocument) {
86
+ document.references.set(ns, r);
87
+ return;
88
+ }
89
+ const refDoc = new ApiDocument();
90
+ await this.initDocument(refDoc, context, r);
91
+ document.references.set(ns, refDoc);
92
+ });
93
+ }
94
+ });
95
+ }
96
+ if (init.types) {
97
+ await context.enterAsync('.types', async () => {
98
+ await DataTypeFactory.addDataTypes(context, document, init.types);
99
+ });
100
+ }
101
+ if (init.api) {
102
+ await context.enterAsync(`.api`, async () => {
103
+ if (init.api.protocol === 'http') {
104
+ const api = await HttpApiFactory.createApi(context, document, init.api);
105
+ if (api)
106
+ document.api = api;
107
+ }
108
+ else
109
+ context.addError(`Unknown service protocol (${init.api.protocol})`);
110
+ });
111
+ }
112
+ }
113
+ /**
114
+ *
115
+ * @param context
116
+ * @protected
117
+ */
118
+ async createBuiltinDocument(context) {
119
+ const init = {
120
+ spec: OpraSchema.SpecVersion,
121
+ url: OPRA_SPEC_URL,
122
+ info: {
123
+ version: OpraSchema.SpecVersion,
124
+ title: 'Opra built-in types',
125
+ license: {
126
+ url: 'https://github.com/oprajs/opra/blob/main/LICENSE',
127
+ name: 'MIT',
128
+ },
129
+ },
130
+ types: [
131
+ // Primitive types
132
+ AnyType,
133
+ BigintType,
134
+ BooleanType,
135
+ IntegerType,
136
+ NullType,
137
+ NumberType,
138
+ ObjectType,
139
+ StringType,
140
+ // Extended types
141
+ Base64Type,
142
+ DateType,
143
+ DateStringType,
144
+ DateTimeType,
145
+ DateTimeStringType,
146
+ EmailType,
147
+ FieldPathType,
148
+ FilterType,
149
+ ObjectIdType,
150
+ OperationResult,
151
+ TimeType,
152
+ UrlType,
153
+ UuidType,
154
+ ],
155
+ };
156
+ const document = new ApiDocument();
157
+ document[BUILTIN] = true;
158
+ const BigIntConstructor = Object.getPrototypeOf(BigInt(0)).constructor;
159
+ const BufferConstructor = Object.getPrototypeOf(Buffer.from([]));
160
+ const _ctorTypeMap = document.types[kCtorMap];
161
+ _ctorTypeMap.set(Object, 'object');
162
+ _ctorTypeMap.set(String, 'string');
163
+ _ctorTypeMap.set(Number, 'number');
164
+ _ctorTypeMap.set(Boolean, 'boolean');
165
+ _ctorTypeMap.set(Object, 'any');
166
+ _ctorTypeMap.set(Date, 'datetime');
167
+ _ctorTypeMap.set(BigIntConstructor, 'bigint');
168
+ _ctorTypeMap.set(ArrayBuffer, 'base64');
169
+ _ctorTypeMap.set(SharedArrayBuffer, 'base64');
170
+ _ctorTypeMap.set(BufferConstructor, 'base64');
171
+ await this.initDocument(document, context, init);
172
+ return document;
173
+ }
174
+ }
@@ -0,0 +1,494 @@
1
+ import { cloneObject, resolveThunk, ResponsiveMap } from '../../helpers/index.js';
2
+ import { OpraSchema } from '../../schema/index.js';
3
+ import { DocumentInitContext } from '../common/document-init-context.js';
4
+ import { DATATYPE_METADATA, DECODER, ENCODER, kCtorMap, kDataTypeMap } from '../constants.js';
5
+ import { ComplexType } from '../data-type/complex-type.js';
6
+ import { ComplexTypeBase } from '../data-type/complex-type-base.js';
7
+ import { EnumType } from '../data-type/enum-type.js';
8
+ import { MappedType } from '../data-type/mapped-type.js';
9
+ import { MixinType } from '../data-type/mixin-type.js';
10
+ import { SimpleType } from '../data-type/simple-type.js';
11
+ const initializingSymbol = Symbol('initializing');
12
+ /**
13
+ *
14
+ * @class DataTypeFactory
15
+ */
16
+ export class DataTypeFactory {
17
+ static async createDataType(context, owner, thunk) {
18
+ context = context || new DocumentInitContext({ maxErrors: 0 });
19
+ const initArgs = await this._importDataTypeArgs(context, owner, thunk);
20
+ if (initArgs) {
21
+ if (typeof initArgs === 'string')
22
+ return owner.node.getDataType(initArgs);
23
+ return this._createDataType(context, owner, initArgs);
24
+ }
25
+ }
26
+ /**
27
+ *
28
+ * @param context
29
+ * @param target
30
+ * @param types
31
+ */
32
+ static async addDataTypes(context, target, types) {
33
+ const dataTypeMap = target.node[kDataTypeMap];
34
+ if (!dataTypeMap)
35
+ throw new TypeError('DocumentElement should has [kDataTypeMap] property');
36
+ const importedTypes = await this.createAllDataTypes(context, target, types);
37
+ if (!importedTypes)
38
+ return;
39
+ for (const dataType of importedTypes) {
40
+ if (dataType?.name) {
41
+ dataTypeMap.set(dataType.name, dataType);
42
+ }
43
+ }
44
+ }
45
+ /**
46
+ *
47
+ * @param context
48
+ * @param owner
49
+ * @param types
50
+ */
51
+ static async createAllDataTypes(context, owner, types) {
52
+ context = context || new DocumentInitContext({ maxErrors: 0 });
53
+ const initArgs = await this._prepareAllInitArgs(context, owner, types);
54
+ if (!initArgs)
55
+ return;
56
+ const initArgsMap = new ResponsiveMap();
57
+ for (const initArg of initArgs) {
58
+ initArgsMap.set(initArg.name, initArg);
59
+ }
60
+ const ctx = context.extend({ initArgsMap });
61
+ const out = [];
62
+ for (const [name, initArg] of initArgsMap.entries()) {
63
+ context.enter(`[${name}]`, () => {
64
+ if (!initArgsMap.has(name))
65
+ return;
66
+ const dataType = this._createDataType(ctx, owner, initArg);
67
+ if (dataType)
68
+ out.push(dataType);
69
+ });
70
+ }
71
+ return out;
72
+ }
73
+ /**
74
+ *
75
+ * @param ctx
76
+ * @param owner
77
+ * @param types
78
+ */
79
+ static async _prepareAllInitArgs(ctx, owner, types) {
80
+ const importQueue = new ResponsiveMap();
81
+ const initArgsMap = new ResponsiveMap();
82
+ const context = ctx.extend({ owner, importQueue, initArgsMap });
83
+ // istanbul ignore next
84
+ if (!owner.node[kDataTypeMap])
85
+ throw new TypeError('DocumentElement should has [kDataTypeMap] property');
86
+ // Add type sources into typeQueue
87
+ if (Array.isArray(types)) {
88
+ let i = 0;
89
+ for (let thunk of types) {
90
+ await context.enterAsync(`$[${i++}]`, async () => {
91
+ thunk = await resolveThunk(thunk);
92
+ const metadata = Reflect.getMetadata(DATATYPE_METADATA, thunk) || thunk[DATATYPE_METADATA];
93
+ if (!(metadata && metadata.name)) {
94
+ if (typeof thunk === 'function')
95
+ return context.addError(`Class "${thunk.name}" doesn't have a valid data type metadata`);
96
+ else
97
+ return context.addError(`Object doesn't have a valid data type metadata`);
98
+ }
99
+ importQueue.set(metadata.name, thunk);
100
+ });
101
+ }
102
+ }
103
+ else {
104
+ let thunk;
105
+ let name;
106
+ for ([name, thunk] of Object.entries(types)) {
107
+ thunk = await resolveThunk(thunk);
108
+ importQueue.set(name, typeof thunk === 'object' ? { ...thunk, name } : thunk);
109
+ }
110
+ }
111
+ for (const name of Array.from(importQueue.keys())) {
112
+ if (!importQueue.has(name))
113
+ continue;
114
+ const dt = await this._importDataTypeArgs(context, owner, name);
115
+ // istanbul ignore next
116
+ if (dt && typeof dt !== 'string')
117
+ context.addError(`Embedded data type can't be loaded into document node directly`);
118
+ }
119
+ return Array.from(initArgsMap.values());
120
+ }
121
+ /**
122
+ *
123
+ * @param context
124
+ * @param owner
125
+ * @param thunk
126
+ * @protected
127
+ */
128
+ static async _importDataTypeArgs(context, owner, thunk) {
129
+ thunk = await resolveThunk(thunk);
130
+ const { importQueue, initArgsMap } = context;
131
+ // Check if data type already exist (maybe a builtin type or already imported)
132
+ const dataType = owner.node.findDataType(thunk);
133
+ if (dataType)
134
+ return dataType.name;
135
+ let metadata;
136
+ let ctor;
137
+ let instance;
138
+ if (typeof thunk !== 'string') {
139
+ const _ctorTypeMap = owner.node.getDocument().types[kCtorMap];
140
+ const name = _ctorTypeMap.get(thunk);
141
+ if (name)
142
+ thunk = name;
143
+ }
144
+ if (typeof thunk === 'string') {
145
+ const name = thunk;
146
+ thunk = importQueue?.get(name) || context.initArgsMap?.get(name);
147
+ if (!thunk)
148
+ return context.addError(`Unknown data type (${name})`);
149
+ }
150
+ else {
151
+ //
152
+ }
153
+ if (typeof thunk === 'function') {
154
+ // Check if class has metadata
155
+ metadata = Reflect.getMetadata(DATATYPE_METADATA, thunk);
156
+ if (!metadata)
157
+ return context.addError(`Class "${thunk.name}" doesn't have a valid DataType metadata`);
158
+ ctor = thunk;
159
+ }
160
+ else if (typeof thunk === 'object') {
161
+ // It may be an enum object or enum array
162
+ metadata = thunk[DATATYPE_METADATA];
163
+ if (metadata) {
164
+ instance = thunk;
165
+ if (metadata.kind !== OpraSchema.EnumType.Kind)
166
+ metadata = undefined;
167
+ }
168
+ else if (OpraSchema.isDataType(thunk)) {
169
+ metadata = thunk;
170
+ ctor = metadata.ctor;
171
+ }
172
+ else {
173
+ // Or may be a DataType instance, e.g. an extended SimpleType object
174
+ ctor = Object.getPrototypeOf(thunk).constructor;
175
+ metadata = ctor && Reflect.getMetadata(DATATYPE_METADATA, ctor);
176
+ if (metadata) {
177
+ if (metadata.kind === OpraSchema.SimpleType.Kind) {
178
+ const baseArgs = await this._importDataTypeArgs(context, owner, metadata.name);
179
+ if (!baseArgs)
180
+ return;
181
+ if (typeof baseArgs === 'object' && baseArgs.kind !== OpraSchema.SimpleType.Kind)
182
+ return context.addError('Kind of base data type is not same');
183
+ return {
184
+ kind: OpraSchema.SimpleType.Kind,
185
+ name: undefined,
186
+ base: baseArgs,
187
+ properties: thunk,
188
+ };
189
+ }
190
+ }
191
+ }
192
+ }
193
+ if (!metadata)
194
+ return context.addError(`No DataType metadata found`);
195
+ return context.enterAsync(metadata.name ? `[${metadata.name}]` : '', async () => {
196
+ /** Check for circular dependencies */
197
+ if (metadata.name) {
198
+ const curr = initArgsMap?.get(metadata.name);
199
+ if (curr) {
200
+ if (curr[initializingSymbol])
201
+ return context.addError('Circular reference detected');
202
+ return metadata.name;
203
+ }
204
+ }
205
+ const out = {
206
+ kind: metadata.kind,
207
+ name: metadata.name,
208
+ };
209
+ /** Mark "out" object as initializing. This will help us to detect circular dependencies */
210
+ out[initializingSymbol] = true;
211
+ try {
212
+ if (out.name && importQueue?.has(out.name)) {
213
+ initArgsMap?.set(metadata.name, out);
214
+ }
215
+ else if (out.name)
216
+ return context.addError(`Data Type (${out.name}) must be explicitly added to type list in the document scope`);
217
+ switch (out.kind) {
218
+ case OpraSchema.ComplexType.Kind:
219
+ out.ctor = ctor;
220
+ await this._prepareComplexTypeArgs(context, owner, out, metadata);
221
+ break;
222
+ case OpraSchema.EnumType.Kind:
223
+ out.instance = instance;
224
+ await this._prepareEnumTypeArgs(context, owner, out, metadata);
225
+ break;
226
+ case OpraSchema.MappedType.Kind:
227
+ await this._prepareMappedTypeArgs(context, owner, out, metadata);
228
+ break;
229
+ case OpraSchema.MixinType.Kind:
230
+ await this._prepareMixinTypeArgs(context, owner, out, metadata);
231
+ break;
232
+ case OpraSchema.SimpleType.Kind:
233
+ out.ctor = ctor;
234
+ await this._prepareSimpleTypeArgs(context, owner, out, metadata);
235
+ break;
236
+ default:
237
+ /** istanbul ignore next */
238
+ return context.addError(`Invalid data type kind ${metadata.kind}`);
239
+ }
240
+ }
241
+ finally {
242
+ if (out.name)
243
+ importQueue?.delete(out.name);
244
+ delete out[initializingSymbol];
245
+ }
246
+ return importQueue && out.name ? out.name : out;
247
+ });
248
+ }
249
+ static async _prepareDataTypeArgs(context, initArgs, metadata) {
250
+ initArgs.description = metadata.description;
251
+ initArgs.abstract = metadata.abstract;
252
+ initArgs.examples = metadata.examples;
253
+ }
254
+ static async _prepareComplexTypeArgs(context, owner, initArgs, metadata) {
255
+ await this._prepareDataTypeArgs(context, initArgs, metadata);
256
+ await context.enterAsync('.base', async () => {
257
+ let baseArgs;
258
+ if (metadata.base) {
259
+ baseArgs = await this._importDataTypeArgs(context, owner, metadata.base);
260
+ }
261
+ else if (initArgs.ctor) {
262
+ const baseClass = Object.getPrototypeOf(initArgs.ctor.prototype).constructor;
263
+ if (Reflect.hasMetadata(DATATYPE_METADATA, baseClass))
264
+ baseArgs = await this._importDataTypeArgs(context, owner, baseClass);
265
+ }
266
+ if (!baseArgs)
267
+ return;
268
+ initArgs.base = preferName(baseArgs);
269
+ initArgs.ctor = initArgs.ctor || baseArgs.ctor;
270
+ });
271
+ // Initialize additionalFields
272
+ if (metadata.additionalFields != null) {
273
+ if (typeof metadata.additionalFields === 'boolean' || Array.isArray(metadata.additionalFields))
274
+ initArgs.additionalFields = metadata.additionalFields;
275
+ else
276
+ await context.enterAsync('.additionalFields', async () => {
277
+ const t = await this._importDataTypeArgs(context, owner, metadata.additionalFields);
278
+ if (t)
279
+ initArgs.additionalFields = preferName(t);
280
+ });
281
+ }
282
+ if (metadata.fields) {
283
+ initArgs.fields = {};
284
+ await context.enterAsync('.fields', async () => {
285
+ for (const [k, v] of Object.entries(metadata.fields)) {
286
+ await context.enterAsync(`[${k}]`, async () => {
287
+ const fieldMeta = typeof v === 'string' ? { type: v } : v;
288
+ if (fieldMeta.isArray && !fieldMeta.type)
289
+ return context.addError(`"type" must be defined explicitly for array fields`);
290
+ const t = await this._importDataTypeArgs(context, owner, fieldMeta.type);
291
+ if (!t)
292
+ return;
293
+ initArgs.fields[k] = {
294
+ ...fieldMeta,
295
+ type: preferName(t),
296
+ };
297
+ });
298
+ }
299
+ });
300
+ }
301
+ }
302
+ static async _prepareEnumTypeArgs(context, owner, initArgs, metadata) {
303
+ await this._prepareDataTypeArgs(context, initArgs, metadata);
304
+ if (metadata.base) {
305
+ await context.enterAsync('.base', async () => {
306
+ const baseArgs = await this._importDataTypeArgs(context, owner, metadata.base);
307
+ if (!baseArgs)
308
+ return;
309
+ initArgs.base = preferName(baseArgs);
310
+ });
311
+ }
312
+ initArgs.attributes = cloneObject(metadata.attributes);
313
+ }
314
+ static async _prepareSimpleTypeArgs(context, owner, initArgs, metadata) {
315
+ await this._prepareDataTypeArgs(context, initArgs, metadata);
316
+ await context.enterAsync('.base', async () => {
317
+ let baseArgs;
318
+ if (metadata.base) {
319
+ baseArgs = await this._importDataTypeArgs(context, owner, metadata.base);
320
+ }
321
+ else if (initArgs.ctor) {
322
+ const baseClass = Object.getPrototypeOf(initArgs.ctor.prototype).constructor;
323
+ if (Reflect.hasMetadata(DATATYPE_METADATA, baseClass))
324
+ baseArgs = await this._importDataTypeArgs(context, owner, baseClass);
325
+ }
326
+ if (!baseArgs)
327
+ return;
328
+ initArgs.base = preferName(baseArgs);
329
+ initArgs.ctor = initArgs.ctor || baseArgs.ctor;
330
+ });
331
+ initArgs.properties = metadata.properties;
332
+ if (!initArgs.properties && initArgs.ctor)
333
+ initArgs.properties = new initArgs.ctor();
334
+ if (metadata.attributes)
335
+ initArgs.attributes = cloneObject(metadata.attributes);
336
+ if (typeof initArgs.properties?.[DECODER] === 'function')
337
+ initArgs.generateDecoder = initArgs.properties?.[DECODER].bind(initArgs.properties);
338
+ if (typeof initArgs.properties?.[ENCODER] === 'function')
339
+ initArgs.generateEncoder = initArgs.properties?.[ENCODER].bind(initArgs.properties);
340
+ }
341
+ static async _prepareMappedTypeArgs(context, owner, initArgs, metadata) {
342
+ await this._prepareDataTypeArgs(context, initArgs, metadata);
343
+ await context.enterAsync('.base', async () => {
344
+ let baseArgs;
345
+ if (metadata.base) {
346
+ baseArgs = await this._importDataTypeArgs(context, owner, metadata.base);
347
+ }
348
+ else if (initArgs.ctor) {
349
+ const baseClass = Object.getPrototypeOf(initArgs.ctor.prototype).constructor;
350
+ if (Reflect.hasMetadata(DATATYPE_METADATA, baseClass))
351
+ baseArgs = await this._importDataTypeArgs(context, owner, baseClass);
352
+ }
353
+ if (!baseArgs)
354
+ return;
355
+ initArgs.base = preferName(baseArgs);
356
+ initArgs.ctor = initArgs.ctor || baseArgs.ctor;
357
+ });
358
+ if (metadata.pick)
359
+ initArgs.pick = [...metadata.pick];
360
+ else if (metadata.omit)
361
+ initArgs.omit = [...metadata.omit];
362
+ else if (metadata.partial)
363
+ initArgs.partial = Array.isArray(metadata.partial) ? [...metadata.partial] : metadata.partial;
364
+ else if (metadata.required)
365
+ initArgs.required = Array.isArray(metadata.required) ? [...metadata.required] : metadata.required;
366
+ }
367
+ static async _prepareMixinTypeArgs(context, owner, initArgs, metadata) {
368
+ await this._prepareDataTypeArgs(context, initArgs, metadata);
369
+ initArgs.types = [];
370
+ await context.enterAsync('.types', async () => {
371
+ const _initTypes = metadata.types;
372
+ let i = 0;
373
+ for (const t of _initTypes) {
374
+ await context.enterAsync(`[${i++}]`, async () => {
375
+ const baseArgs = await this._importDataTypeArgs(context, owner, t);
376
+ if (!baseArgs)
377
+ return;
378
+ initArgs.types.push(preferName(baseArgs));
379
+ });
380
+ }
381
+ });
382
+ }
383
+ static _createDataType(context, owner, args) {
384
+ const initArgs = typeof args === 'string' ? context.initArgsMap?.get(args) : args;
385
+ switch (initArgs?.kind) {
386
+ case OpraSchema.ComplexType.Kind:
387
+ return this._createComplexType(context, owner, initArgs);
388
+ case OpraSchema.EnumType.Kind:
389
+ return this._createEnumType(context, owner, initArgs);
390
+ case OpraSchema.MappedType.Kind:
391
+ return this._createMappedType(context, owner, initArgs);
392
+ case OpraSchema.MixinType.Kind:
393
+ return this._createMixinType(context, owner, initArgs);
394
+ case OpraSchema.SimpleType.Kind:
395
+ return this._createSimpleType(context, owner, initArgs);
396
+ }
397
+ context.addError(`Unknown data type (${String(args)})`);
398
+ }
399
+ static _createComplexType(context, owner, args) {
400
+ const initArgs = cloneObject(args);
401
+ if (args.base) {
402
+ context.enter('.base', () => {
403
+ initArgs.base = (owner.node.findDataType(args.base) ||
404
+ this._createDataType(context, owner, args.base));
405
+ });
406
+ }
407
+ /** Set additionalFields */
408
+ if (args.additionalFields) {
409
+ context.enter('.additionalFields', () => {
410
+ if (typeof args.additionalFields === 'boolean' || Array.isArray(args.additionalFields))
411
+ initArgs.additionalFields = args.additionalFields;
412
+ else {
413
+ if (typeof args.additionalFields === 'string') {
414
+ initArgs.additionalFields =
415
+ owner.node.findDataType(args.additionalFields) ||
416
+ this._createDataType(context, owner, args.additionalFields);
417
+ }
418
+ else {
419
+ initArgs.additionalFields = this._createDataType(context, owner, args.additionalFields);
420
+ }
421
+ }
422
+ });
423
+ }
424
+ /** Add own fields */
425
+ initArgs.fields = {};
426
+ if (args.fields) {
427
+ context.enter('.fields', () => {
428
+ for (const [k, v] of Object.entries(args.fields)) {
429
+ context.enter(`[${k}]`, () => {
430
+ const type = owner.node.findDataType(v.type) || this._createDataType(context, owner, v.type);
431
+ if (type)
432
+ initArgs.fields[k] = {
433
+ ...v,
434
+ name: k,
435
+ type,
436
+ };
437
+ });
438
+ }
439
+ });
440
+ }
441
+ return new ComplexType(owner, initArgs);
442
+ }
443
+ static _createEnumType(context, owner, args) {
444
+ const initArgs = cloneObject(args);
445
+ if (args.base) {
446
+ context.enter('.base', () => {
447
+ initArgs.base = (owner.node.findDataType(args.base) ||
448
+ this._createDataType(context, owner, args.base));
449
+ });
450
+ }
451
+ initArgs.attributes = args.attributes;
452
+ return new EnumType(owner, initArgs);
453
+ }
454
+ static _createMappedType(context, owner, args) {
455
+ const initArgs = cloneObject(args);
456
+ if (args.base) {
457
+ context.enter('.base', () => {
458
+ initArgs.base = (owner.node.findDataType(args.base) || this._createDataType(context, owner, args.base));
459
+ });
460
+ }
461
+ return new MappedType(owner, initArgs);
462
+ }
463
+ static _createMixinType(context, owner, args) {
464
+ const initArgs = cloneObject(args);
465
+ if (args.types) {
466
+ context.enter('.types', () => {
467
+ initArgs.types = [];
468
+ let i = 0;
469
+ for (const t of args.types) {
470
+ context.enter(`[${i++}]`, () => {
471
+ const base = owner.node.findDataType(t) || this._createDataType(context, owner, t);
472
+ if (!(base instanceof ComplexTypeBase))
473
+ throw new TypeError(`"${base?.kind}" can't be set as base for a "${initArgs.kind}"`);
474
+ initArgs.types.push(base);
475
+ });
476
+ }
477
+ });
478
+ }
479
+ return new MixinType(owner, initArgs);
480
+ }
481
+ static _createSimpleType(context, owner, args) {
482
+ const initArgs = cloneObject(args);
483
+ if (args.base) {
484
+ context.enter('.base', () => {
485
+ initArgs.base = (owner.node.findDataType(args.base) ||
486
+ this._createDataType(context, owner, args.base));
487
+ });
488
+ }
489
+ return new SimpleType(owner, initArgs);
490
+ }
491
+ }
492
+ function preferName(initArgs) {
493
+ return typeof initArgs === 'object' ? (initArgs.name ? initArgs.name : initArgs) : initArgs;
494
+ }