@opra/common 0.5.0 → 0.7.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 (419) hide show
  1. package/cjs/constants.js +1 -0
  2. package/cjs/exception/enums/issue-severity.enum.js +23 -0
  3. package/cjs/{types.js → exception/error-issue.js} +0 -0
  4. package/cjs/exception/http-errors/bad-request.error.js +22 -0
  5. package/cjs/exception/http-errors/failed-dependency.error.js +21 -0
  6. package/cjs/exception/http-errors/forbidden.error.js +23 -0
  7. package/cjs/exception/http-errors/internal-server.error.js +21 -0
  8. package/cjs/exception/http-errors/method-not-allowed.error.js +22 -0
  9. package/cjs/exception/http-errors/not-acceptable.error.js +22 -0
  10. package/cjs/exception/http-errors/not-found.error.js +25 -0
  11. package/cjs/exception/http-errors/unauthorized.error.js +22 -0
  12. package/cjs/exception/http-errors/unprocessable-entity.error.js +21 -0
  13. package/cjs/exception/index.js +18 -0
  14. package/cjs/exception/opra-exception.js +56 -0
  15. package/cjs/exception/resource-errors/resource-conflict.error.js +20 -0
  16. package/cjs/exception/resource-errors/resource-not-found.error.js +20 -0
  17. package/cjs/exception/wrap-exception.js +42 -0
  18. package/cjs/filter/antlr/OpraFilterLexer.js +386 -0
  19. package/cjs/filter/antlr/OpraFilterParser.js +2070 -0
  20. package/cjs/filter/antlr/OpraFilterVisitor.js +3 -0
  21. package/cjs/filter/ast/abstract/ast.js +10 -0
  22. package/cjs/filter/ast/abstract/expression.js +7 -0
  23. package/cjs/filter/ast/abstract/literal.js +14 -0
  24. package/cjs/filter/ast/abstract/term.js +7 -0
  25. package/cjs/filter/ast/expressions/arithmetic-expression.js +29 -0
  26. package/cjs/filter/ast/expressions/array-expression.js +15 -0
  27. package/cjs/filter/ast/expressions/comparison-expression.js +18 -0
  28. package/cjs/filter/ast/expressions/logical-expression.js +18 -0
  29. package/cjs/filter/ast/expressions/parentheses-expression.js +15 -0
  30. package/cjs/filter/ast/index.js +19 -0
  31. package/cjs/filter/ast/terms/boolean-literal.js +10 -0
  32. package/cjs/filter/ast/terms/date-literal.js +28 -0
  33. package/cjs/filter/ast/terms/external-constant.js +13 -0
  34. package/cjs/filter/ast/terms/null-literal.js +11 -0
  35. package/cjs/filter/ast/terms/number-literal.js +40 -0
  36. package/cjs/filter/ast/terms/qualified-identifier.js +10 -0
  37. package/cjs/filter/ast/terms/string-literal.js +14 -0
  38. package/cjs/filter/ast/terms/time-literal.js +33 -0
  39. package/cjs/filter/build.js +129 -0
  40. package/cjs/filter/error-listener.js +14 -0
  41. package/cjs/filter/errors.js +21 -0
  42. package/cjs/filter/filter-tree-visitor.js +113 -0
  43. package/cjs/filter/index.js +8 -0
  44. package/cjs/filter/parse.js +37 -0
  45. package/cjs/filter/utils.js +26 -0
  46. package/cjs/helpers/index.js +4 -0
  47. package/cjs/{classes → helpers}/responsive-map.js +1 -1
  48. package/cjs/{enums → http/enums}/http-headers.enum.js +9 -0
  49. package/cjs/{enums → http/enums}/http-status.enum.js +0 -0
  50. package/cjs/http/http-request.js +118 -0
  51. package/cjs/http/index.js +9 -0
  52. package/cjs/http/interfaces/client-http-headers.interface.js +2 -0
  53. package/cjs/http/interfaces/server-http-headers.interface.js +2 -0
  54. package/cjs/http/multipart/batch-multipart.js +155 -0
  55. package/cjs/http/multipart/http-request-content.js +16 -0
  56. package/cjs/http/multipart/http-response-content.js +14 -0
  57. package/cjs/http/multipart/index.js +4 -0
  58. package/cjs/http/utils/normalize-headers.js +28 -0
  59. package/cjs/i18n/i18n.js +175 -0
  60. package/cjs/i18n/index.js +10 -0
  61. package/cjs/i18n/string-utils.js +13 -0
  62. package/cjs/i18n/translate.js +15 -0
  63. package/cjs/index.js +12 -5
  64. package/cjs/schema/constants.js +11 -0
  65. package/cjs/schema/decorators/opr-collection-resource.decorator.js +23 -0
  66. package/cjs/schema/decorators/opr-complex-type.decorator.js +27 -0
  67. package/cjs/schema/decorators/opr-field.decorator.js +28 -0
  68. package/cjs/schema/decorators/opr-resolver.decorator.js +81 -0
  69. package/cjs/schema/decorators/opr-simple-type.decorator.js +18 -0
  70. package/cjs/schema/decorators/opr-singleton-resource.decorator.js +23 -0
  71. package/cjs/schema/implementation/data-type/builtin/any.type.js +9 -0
  72. package/cjs/schema/implementation/data-type/builtin/base64-binary.type.js +15 -0
  73. package/cjs/schema/implementation/data-type/builtin/bigint.type.js +15 -0
  74. package/cjs/schema/implementation/data-type/builtin/boolean.type.js +15 -0
  75. package/cjs/schema/implementation/data-type/builtin/date-string.type.js +15 -0
  76. package/cjs/schema/implementation/data-type/builtin/date.type.js +15 -0
  77. package/cjs/schema/implementation/data-type/builtin/guid.type.js +15 -0
  78. package/cjs/schema/implementation/data-type/builtin/integer.type.js +15 -0
  79. package/cjs/schema/implementation/data-type/builtin/number.type.js +15 -0
  80. package/cjs/schema/implementation/data-type/builtin/object.type.js +15 -0
  81. package/cjs/schema/implementation/data-type/builtin/string.type.js +15 -0
  82. package/cjs/schema/implementation/data-type/builtin-data-types.js +37 -0
  83. package/cjs/schema/implementation/data-type/complex-type.js +111 -0
  84. package/cjs/schema/implementation/data-type/data-type.js +36 -0
  85. package/cjs/schema/implementation/data-type/simple-type.js +21 -0
  86. package/cjs/schema/implementation/data-type/union-type.js +25 -0
  87. package/cjs/schema/implementation/document-builder.js +140 -0
  88. package/cjs/schema/implementation/opra-document.js +181 -0
  89. package/cjs/schema/implementation/query/collection-count-query.js +19 -0
  90. package/cjs/schema/implementation/query/collection-create-query.js +28 -0
  91. package/cjs/schema/implementation/query/collection-delete-many-query.js +19 -0
  92. package/cjs/schema/implementation/query/collection-delete-query.js +27 -0
  93. package/cjs/schema/implementation/query/collection-get-query.js +38 -0
  94. package/cjs/schema/implementation/query/collection-search-query.js +55 -0
  95. package/cjs/schema/implementation/query/collection-update-many-query.js +21 -0
  96. package/cjs/schema/implementation/query/collection-update-query.js +39 -0
  97. package/cjs/schema/implementation/query/field-get-query.js +45 -0
  98. package/cjs/schema/implementation/query/index.js +22 -0
  99. package/cjs/schema/implementation/query/singleton-get-query.js +27 -0
  100. package/cjs/schema/implementation/resource/collection-resource-info.js +58 -0
  101. package/cjs/schema/implementation/resource/container-resource-info.js +30 -0
  102. package/cjs/schema/implementation/resource/resource-info.js +39 -0
  103. package/cjs/schema/implementation/resource/singleton-resource-info.js +37 -0
  104. package/cjs/schema/implementation/schema-builder/extract-resource-metadata.util.js +84 -0
  105. package/cjs/schema/implementation/schema-builder/extract-type-metadata.util.js +94 -0
  106. package/cjs/schema/index.js +28 -0
  107. package/cjs/schema/interfaces/child-field-query.interface.js +2 -0
  108. package/cjs/schema/interfaces/data-type.metadata.js +2 -0
  109. package/cjs/schema/interfaces/resource-container.interface.js +2 -0
  110. package/cjs/schema/interfaces/resource.metadata.js +2 -0
  111. package/cjs/schema/opra-schema.definition.js +49 -0
  112. package/cjs/schema/type-helpers/extend-type.helper.js +65 -0
  113. package/cjs/schema/type-helpers/mixin-type.helper.js +46 -0
  114. package/cjs/schema/type-helpers/mixin.utils.js +32 -0
  115. package/cjs/schema/types.js +2 -0
  116. package/cjs/schema/utils/class.utils.js +8 -0
  117. package/cjs/schema/utils/clone-object.util.js +19 -0
  118. package/cjs/schema/utils/inspect.util.js +7 -0
  119. package/cjs/schema/utils/normalize-field-array.util.js +44 -0
  120. package/cjs/schema/utils/path-to-tree.util.js +26 -0
  121. package/cjs/schema/utils/string-compare.util.js +11 -0
  122. package/cjs/url/formats/boolean-format.js +25 -0
  123. package/cjs/url/formats/date-format.js +48 -0
  124. package/cjs/url/formats/filter-format.js +18 -0
  125. package/cjs/url/formats/format.js +6 -0
  126. package/cjs/url/formats/integer-format.js +20 -0
  127. package/cjs/url/formats/number-format.js +28 -0
  128. package/cjs/url/formats/string-format.js +28 -0
  129. package/cjs/url/index.js +8 -0
  130. package/cjs/url/opra-url-path-component.js +33 -0
  131. package/cjs/url/opra-url-path.js +128 -0
  132. package/cjs/url/opra-url-search-params.js +247 -0
  133. package/cjs/url/opra-url.js +299 -0
  134. package/cjs/url/utils/path-utils.js +86 -0
  135. package/cjs/utils/index.js +6 -0
  136. package/cjs/utils/is-url.js +8 -0
  137. package/cjs/utils/path-to-tree.js +28 -0
  138. package/cjs/utils/type-guards.js +46 -0
  139. package/esm/constants.d.ts +0 -0
  140. package/esm/constants.js +1 -0
  141. package/esm/exception/enums/issue-severity.enum.d.ts +13 -0
  142. package/esm/exception/enums/issue-severity.enum.js +20 -0
  143. package/esm/exception/error-issue.d.ts +9 -0
  144. package/esm/{types.js → exception/error-issue.js} +0 -0
  145. package/esm/exception/http-errors/bad-request.error.d.ts +10 -0
  146. package/esm/exception/http-errors/bad-request.error.js +18 -0
  147. package/esm/exception/http-errors/failed-dependency.error.d.ts +9 -0
  148. package/esm/exception/http-errors/failed-dependency.error.js +17 -0
  149. package/esm/exception/http-errors/forbidden.error.d.ts +11 -0
  150. package/esm/exception/http-errors/forbidden.error.js +19 -0
  151. package/esm/exception/http-errors/internal-server.error.d.ts +9 -0
  152. package/esm/exception/http-errors/internal-server.error.js +17 -0
  153. package/esm/exception/http-errors/method-not-allowed.error.d.ts +10 -0
  154. package/esm/exception/http-errors/method-not-allowed.error.js +18 -0
  155. package/esm/exception/http-errors/not-acceptable.error.d.ts +10 -0
  156. package/esm/exception/http-errors/not-acceptable.error.js +18 -0
  157. package/esm/exception/http-errors/not-found.error.d.ts +13 -0
  158. package/esm/exception/http-errors/not-found.error.js +21 -0
  159. package/esm/exception/http-errors/unauthorized.error.d.ts +10 -0
  160. package/esm/exception/http-errors/unauthorized.error.js +18 -0
  161. package/esm/exception/http-errors/unprocessable-entity.error.d.ts +9 -0
  162. package/esm/exception/http-errors/unprocessable-entity.error.js +17 -0
  163. package/esm/exception/index.d.ts +15 -0
  164. package/esm/exception/index.js +15 -0
  165. package/esm/exception/opra-exception.d.ts +15 -0
  166. package/esm/exception/opra-exception.js +52 -0
  167. package/esm/exception/resource-errors/resource-conflict.error.d.ts +5 -0
  168. package/esm/exception/resource-errors/resource-conflict.error.js +16 -0
  169. package/esm/exception/resource-errors/resource-not-found.error.d.ts +4 -0
  170. package/esm/exception/resource-errors/resource-not-found.error.js +16 -0
  171. package/esm/exception/wrap-exception.d.ts +2 -0
  172. package/esm/exception/wrap-exception.js +38 -0
  173. package/esm/filter/antlr/OpraFilterLexer.d.ts +78 -0
  174. package/esm/filter/antlr/OpraFilterLexer.js +381 -0
  175. package/esm/filter/antlr/OpraFilterParser.d.ts +365 -0
  176. package/esm/filter/antlr/OpraFilterParser.js +2022 -0
  177. package/esm/filter/antlr/OpraFilterVisitor.d.ts +290 -0
  178. package/esm/filter/antlr/OpraFilterVisitor.js +2 -0
  179. package/esm/filter/ast/abstract/ast.d.ts +5 -0
  180. package/esm/filter/ast/abstract/ast.js +6 -0
  181. package/esm/filter/ast/abstract/expression.d.ts +3 -0
  182. package/esm/filter/ast/abstract/expression.js +3 -0
  183. package/esm/filter/ast/abstract/literal.d.ts +6 -0
  184. package/esm/filter/ast/abstract/literal.js +10 -0
  185. package/esm/filter/ast/abstract/term.d.ts +3 -0
  186. package/esm/filter/ast/abstract/term.js +3 -0
  187. package/esm/filter/ast/expressions/arithmetic-expression.d.ts +13 -0
  188. package/esm/filter/ast/expressions/arithmetic-expression.js +24 -0
  189. package/esm/filter/ast/expressions/array-expression.d.ts +7 -0
  190. package/esm/filter/ast/expressions/array-expression.js +11 -0
  191. package/esm/filter/ast/expressions/comparison-expression.d.ts +10 -0
  192. package/esm/filter/ast/expressions/comparison-expression.js +14 -0
  193. package/esm/filter/ast/expressions/logical-expression.d.ts +8 -0
  194. package/esm/filter/ast/expressions/logical-expression.js +14 -0
  195. package/esm/filter/ast/expressions/parentheses-expression.d.ts +6 -0
  196. package/esm/filter/ast/expressions/parentheses-expression.js +11 -0
  197. package/esm/filter/ast/index.d.ts +16 -0
  198. package/esm/filter/ast/index.js +16 -0
  199. package/esm/filter/ast/terms/boolean-literal.d.ts +5 -0
  200. package/esm/filter/ast/terms/boolean-literal.js +6 -0
  201. package/esm/filter/ast/terms/date-literal.d.ts +6 -0
  202. package/esm/filter/ast/terms/date-literal.js +24 -0
  203. package/esm/filter/ast/terms/external-constant.d.ts +5 -0
  204. package/esm/filter/ast/terms/external-constant.js +9 -0
  205. package/esm/filter/ast/terms/null-literal.d.ts +5 -0
  206. package/esm/filter/ast/terms/null-literal.js +7 -0
  207. package/esm/filter/ast/terms/number-literal.d.ts +6 -0
  208. package/esm/filter/ast/terms/number-literal.js +36 -0
  209. package/esm/filter/ast/terms/qualified-identifier.d.ts +4 -0
  210. package/esm/filter/ast/terms/qualified-identifier.js +6 -0
  211. package/esm/filter/ast/terms/string-literal.d.ts +5 -0
  212. package/esm/filter/ast/terms/string-literal.js +10 -0
  213. package/esm/filter/ast/terms/time-literal.d.ts +6 -0
  214. package/esm/filter/ast/terms/time-literal.js +29 -0
  215. package/esm/filter/build.d.ts +31 -0
  216. package/esm/filter/build.js +105 -0
  217. package/esm/filter/error-listener.d.ts +8 -0
  218. package/esm/filter/error-listener.js +10 -0
  219. package/esm/filter/errors.d.ts +20 -0
  220. package/esm/filter/errors.js +15 -0
  221. package/esm/filter/filter-tree-visitor.d.ts +30 -0
  222. package/esm/filter/filter-tree-visitor.js +109 -0
  223. package/esm/filter/index.d.ts +5 -0
  224. package/esm/filter/index.js +5 -0
  225. package/esm/filter/parse.d.ts +2 -0
  226. package/esm/filter/parse.js +33 -0
  227. package/esm/filter/utils.d.ts +2 -0
  228. package/esm/filter/utils.js +21 -0
  229. package/esm/helpers/index.d.ts +1 -0
  230. package/esm/helpers/index.js +1 -0
  231. package/esm/{classes → helpers}/responsive-map.d.ts +0 -0
  232. package/esm/{classes → helpers}/responsive-map.js +1 -1
  233. package/esm/{enums → http/enums}/http-headers.enum.d.ts +9 -0
  234. package/esm/{enums → http/enums}/http-headers.enum.js +9 -0
  235. package/esm/{enums → http/enums}/http-status.enum.d.ts +0 -0
  236. package/esm/{enums → http/enums}/http-status.enum.js +0 -0
  237. package/esm/http/http-request.d.ts +34 -0
  238. package/esm/http/http-request.js +114 -0
  239. package/esm/http/index.d.ts +6 -0
  240. package/esm/http/index.js +6 -0
  241. package/esm/http/interfaces/client-http-headers.interface.d.ts +65 -0
  242. package/esm/http/interfaces/client-http-headers.interface.js +1 -0
  243. package/esm/http/interfaces/server-http-headers.interface.d.ts +1 -0
  244. package/esm/http/interfaces/server-http-headers.interface.js +1 -0
  245. package/esm/http/multipart/batch-multipart.d.ts +30 -0
  246. package/esm/http/multipart/batch-multipart.js +150 -0
  247. package/esm/http/multipart/http-request-content.d.ts +8 -0
  248. package/esm/http/multipart/http-request-content.js +12 -0
  249. package/esm/http/multipart/http-response-content.d.ts +7 -0
  250. package/esm/http/multipart/http-response-content.js +10 -0
  251. package/esm/http/multipart/index.d.ts +1 -0
  252. package/esm/http/multipart/index.js +1 -0
  253. package/esm/http/utils/normalize-headers.d.ts +1 -0
  254. package/esm/http/utils/normalize-headers.js +24 -0
  255. package/esm/i18n/i18n.d.ts +28 -0
  256. package/esm/i18n/i18n.js +170 -0
  257. package/esm/i18n/index.d.ts +5 -0
  258. package/esm/i18n/index.js +6 -0
  259. package/esm/i18n/string-utils.d.ts +2 -0
  260. package/esm/i18n/string-utils.js +8 -0
  261. package/esm/i18n/translate.d.ts +4 -0
  262. package/esm/i18n/translate.js +11 -0
  263. package/esm/index.d.ts +10 -5
  264. package/esm/index.js +10 -5
  265. package/esm/schema/constants.d.ts +8 -0
  266. package/esm/schema/constants.js +8 -0
  267. package/esm/schema/decorators/opr-collection-resource.decorator.d.ts +8 -0
  268. package/esm/schema/decorators/opr-collection-resource.decorator.js +19 -0
  269. package/esm/schema/decorators/opr-complex-type.decorator.d.ts +6 -0
  270. package/esm/schema/decorators/opr-complex-type.decorator.js +23 -0
  271. package/esm/schema/decorators/opr-field.decorator.d.ts +3 -0
  272. package/esm/schema/decorators/opr-field.decorator.js +24 -0
  273. package/esm/schema/decorators/opr-resolver.decorator.d.ts +8 -0
  274. package/esm/schema/decorators/opr-resolver.decorator.js +71 -0
  275. package/esm/schema/decorators/opr-simple-type.decorator.d.ts +6 -0
  276. package/esm/schema/decorators/opr-simple-type.decorator.js +14 -0
  277. package/esm/schema/decorators/opr-singleton-resource.decorator.d.ts +8 -0
  278. package/esm/schema/decorators/opr-singleton-resource.decorator.js +19 -0
  279. package/esm/schema/implementation/data-type/builtin/any.type.d.ts +2 -0
  280. package/esm/schema/implementation/data-type/builtin/any.type.js +6 -0
  281. package/esm/schema/implementation/data-type/builtin/base64-binary.type.d.ts +2 -0
  282. package/esm/schema/implementation/data-type/builtin/base64-binary.type.js +12 -0
  283. package/esm/schema/implementation/data-type/builtin/bigint.type.d.ts +2 -0
  284. package/esm/schema/implementation/data-type/builtin/bigint.type.js +12 -0
  285. package/esm/schema/implementation/data-type/builtin/boolean.type.d.ts +2 -0
  286. package/esm/schema/implementation/data-type/builtin/boolean.type.js +12 -0
  287. package/esm/schema/implementation/data-type/builtin/date-string.type.d.ts +2 -0
  288. package/esm/schema/implementation/data-type/builtin/date-string.type.js +12 -0
  289. package/esm/schema/implementation/data-type/builtin/date.type.d.ts +2 -0
  290. package/esm/schema/implementation/data-type/builtin/date.type.js +12 -0
  291. package/esm/schema/implementation/data-type/builtin/guid.type.d.ts +2 -0
  292. package/esm/schema/implementation/data-type/builtin/guid.type.js +12 -0
  293. package/esm/schema/implementation/data-type/builtin/integer.type.d.ts +2 -0
  294. package/esm/schema/implementation/data-type/builtin/integer.type.js +12 -0
  295. package/esm/schema/implementation/data-type/builtin/number.type.d.ts +2 -0
  296. package/esm/schema/implementation/data-type/builtin/number.type.js +12 -0
  297. package/esm/schema/implementation/data-type/builtin/object.type.d.ts +2 -0
  298. package/esm/schema/implementation/data-type/builtin/object.type.js +12 -0
  299. package/esm/schema/implementation/data-type/builtin/string.type.d.ts +2 -0
  300. package/esm/schema/implementation/data-type/builtin/string.type.js +12 -0
  301. package/esm/schema/implementation/data-type/builtin-data-types.d.ts +4 -0
  302. package/esm/schema/implementation/data-type/builtin-data-types.js +34 -0
  303. package/esm/schema/implementation/data-type/complex-type.d.ts +29 -0
  304. package/esm/schema/implementation/data-type/complex-type.js +107 -0
  305. package/esm/schema/implementation/data-type/data-type.d.ts +16 -0
  306. package/esm/schema/implementation/data-type/data-type.js +32 -0
  307. package/esm/schema/implementation/data-type/simple-type.d.ts +12 -0
  308. package/esm/schema/implementation/data-type/simple-type.js +17 -0
  309. package/esm/schema/implementation/data-type/union-type.d.ts +16 -0
  310. package/esm/schema/implementation/data-type/union-type.js +21 -0
  311. package/esm/schema/implementation/document-builder.d.ts +16 -0
  312. package/esm/schema/implementation/document-builder.js +135 -0
  313. package/esm/schema/implementation/opra-document.d.ts +44 -0
  314. package/esm/schema/implementation/opra-document.js +177 -0
  315. package/esm/schema/implementation/query/collection-count-query.d.ts +14 -0
  316. package/esm/schema/implementation/query/collection-count-query.js +15 -0
  317. package/esm/schema/implementation/query/collection-create-query.d.ts +18 -0
  318. package/esm/schema/implementation/query/collection-create-query.js +24 -0
  319. package/esm/schema/implementation/query/collection-delete-many-query.d.ts +14 -0
  320. package/esm/schema/implementation/query/collection-delete-many-query.js +15 -0
  321. package/esm/schema/implementation/query/collection-delete-query.d.ts +10 -0
  322. package/esm/schema/implementation/query/collection-delete-query.js +23 -0
  323. package/esm/schema/implementation/query/collection-get-query.d.ts +21 -0
  324. package/esm/schema/implementation/query/collection-get-query.js +34 -0
  325. package/esm/schema/implementation/query/collection-search-query.d.ts +30 -0
  326. package/esm/schema/implementation/query/collection-search-query.js +51 -0
  327. package/esm/schema/implementation/query/collection-update-many-query.d.ts +15 -0
  328. package/esm/schema/implementation/query/collection-update-many-query.js +17 -0
  329. package/esm/schema/implementation/query/collection-update-query.d.ts +19 -0
  330. package/esm/schema/implementation/query/collection-update-query.js +35 -0
  331. package/esm/schema/implementation/query/field-get-query.d.ts +30 -0
  332. package/esm/schema/implementation/query/field-get-query.js +41 -0
  333. package/esm/schema/implementation/query/index.d.ts +27 -0
  334. package/esm/schema/implementation/query/index.js +17 -0
  335. package/esm/schema/implementation/query/singleton-get-query.d.ts +20 -0
  336. package/esm/schema/implementation/query/singleton-get-query.js +23 -0
  337. package/esm/schema/implementation/resource/collection-resource-info.d.ts +19 -0
  338. package/esm/schema/implementation/resource/collection-resource-info.js +54 -0
  339. package/esm/schema/implementation/resource/container-resource-info.d.ts +13 -0
  340. package/esm/schema/implementation/resource/container-resource-info.js +26 -0
  341. package/esm/schema/implementation/resource/resource-info.d.ts +17 -0
  342. package/esm/schema/implementation/resource/resource-info.js +35 -0
  343. package/esm/schema/implementation/resource/singleton-resource-info.d.ts +14 -0
  344. package/esm/schema/implementation/resource/singleton-resource-info.js +33 -0
  345. package/esm/schema/implementation/schema-builder/extract-resource-metadata.util.d.ts +3 -0
  346. package/esm/schema/implementation/schema-builder/extract-resource-metadata.util.js +80 -0
  347. package/esm/schema/implementation/schema-builder/extract-type-metadata.util.d.ts +4 -0
  348. package/esm/schema/implementation/schema-builder/extract-type-metadata.util.js +89 -0
  349. package/esm/schema/index.d.ts +25 -0
  350. package/esm/schema/index.js +25 -0
  351. package/esm/schema/interfaces/child-field-query.interface.d.ts +4 -0
  352. package/esm/schema/interfaces/child-field-query.interface.js +1 -0
  353. package/esm/schema/interfaces/data-type.metadata.d.ts +18 -0
  354. package/esm/schema/interfaces/data-type.metadata.js +1 -0
  355. package/esm/schema/interfaces/resource-container.interface.d.ts +8 -0
  356. package/esm/schema/interfaces/resource-container.interface.js +1 -0
  357. package/esm/schema/interfaces/resource.metadata.d.ts +18 -0
  358. package/esm/schema/interfaces/resource.metadata.js +1 -0
  359. package/esm/schema/opra-schema.definition.d.ts +178 -0
  360. package/esm/schema/opra-schema.definition.js +46 -0
  361. package/esm/schema/type-helpers/extend-type.helper.d.ts +3 -0
  362. package/esm/schema/type-helpers/extend-type.helper.js +59 -0
  363. package/esm/schema/type-helpers/mixin-type.helper.d.ts +2 -0
  364. package/esm/schema/type-helpers/mixin-type.helper.js +41 -0
  365. package/esm/schema/type-helpers/mixin.utils.d.ts +3 -0
  366. package/esm/schema/type-helpers/mixin.utils.js +27 -0
  367. package/esm/{types.d.ts → schema/types.d.ts} +8 -1
  368. package/esm/schema/types.js +1 -0
  369. package/esm/schema/utils/class.utils.d.ts +2 -0
  370. package/esm/schema/utils/class.utils.js +4 -0
  371. package/esm/schema/utils/clone-object.util.d.ts +1 -0
  372. package/esm/schema/utils/clone-object.util.js +14 -0
  373. package/esm/schema/utils/inspect.util.d.ts +4 -0
  374. package/esm/schema/utils/inspect.util.js +4 -0
  375. package/esm/schema/utils/normalize-field-array.util.d.ts +3 -0
  376. package/esm/schema/utils/normalize-field-array.util.js +40 -0
  377. package/esm/schema/utils/path-to-tree.util.d.ts +4 -0
  378. package/esm/schema/utils/path-to-tree.util.js +22 -0
  379. package/esm/schema/utils/string-compare.util.d.ts +1 -0
  380. package/esm/schema/utils/string-compare.util.js +7 -0
  381. package/esm/url/formats/boolean-format.d.ts +5 -0
  382. package/esm/url/formats/boolean-format.js +21 -0
  383. package/esm/url/formats/date-format.d.ts +16 -0
  384. package/esm/url/formats/date-format.js +44 -0
  385. package/esm/url/formats/filter-format.d.ts +6 -0
  386. package/esm/url/formats/filter-format.js +14 -0
  387. package/esm/url/formats/format.d.ts +4 -0
  388. package/esm/url/formats/format.js +2 -0
  389. package/esm/url/formats/integer-format.d.ts +9 -0
  390. package/esm/url/formats/integer-format.js +16 -0
  391. package/esm/url/formats/number-format.d.ts +12 -0
  392. package/esm/url/formats/number-format.js +24 -0
  393. package/esm/url/formats/string-format.d.ts +14 -0
  394. package/esm/url/formats/string-format.js +24 -0
  395. package/esm/url/index.d.ts +5 -0
  396. package/esm/url/index.js +5 -0
  397. package/esm/url/opra-url-path-component.d.ts +15 -0
  398. package/esm/url/opra-url-path-component.js +29 -0
  399. package/esm/url/opra-url-path.d.ts +30 -0
  400. package/esm/url/opra-url-path.js +124 -0
  401. package/esm/url/opra-url-search-params.d.ts +46 -0
  402. package/esm/url/opra-url-search-params.js +243 -0
  403. package/esm/url/opra-url.d.ts +79 -0
  404. package/esm/url/opra-url.js +295 -0
  405. package/esm/url/utils/path-utils.d.ts +8 -0
  406. package/esm/url/utils/path-utils.js +78 -0
  407. package/esm/utils/index.d.ts +3 -0
  408. package/esm/utils/index.js +3 -0
  409. package/esm/utils/is-url.d.ts +1 -0
  410. package/esm/utils/is-url.js +4 -0
  411. package/esm/utils/path-to-tree.d.ts +4 -0
  412. package/esm/utils/path-to-tree.js +24 -0
  413. package/esm/utils/type-guards.d.ts +9 -0
  414. package/esm/utils/type-guards.js +37 -0
  415. package/package.json +30 -10
  416. package/umd/opra-common.umd.min.js +1 -0
  417. package/cjs/classes/headers-map.js +0 -18
  418. package/esm/classes/headers-map.d.ts +0 -5
  419. package/esm/classes/headers-map.js +0 -14
@@ -0,0 +1,114 @@
1
+ import { HTTPParser } from 'http-parser-js';
2
+ import { Readable } from 'stream';
3
+ const crlfBuffer = Buffer.from('\r\n');
4
+ const kHeaders = Symbol('kHeaders');
5
+ const kHeadersCount = Symbol('kHeadersCount');
6
+ const kTrailers = Symbol('kTrailers');
7
+ const kTrailersCount = Symbol('kTrailersCount');
8
+ export class HttpRequest extends Readable {
9
+ httpVersionMajor;
10
+ httpVersionMinor;
11
+ httpVersion;
12
+ complete;
13
+ rawHeaders;
14
+ rawTrailers;
15
+ aborted = false;
16
+ upgrade;
17
+ url;
18
+ originalUrl;
19
+ method;
20
+ shouldKeepAlive;
21
+ data;
22
+ body;
23
+ [kHeaders];
24
+ [kHeadersCount];
25
+ [kTrailers];
26
+ [kTrailersCount];
27
+ get headers() {
28
+ if (!this[kHeaders])
29
+ this[kHeaders] = arrayToHeaders(this.rawHeaders);
30
+ return this[kHeaders];
31
+ }
32
+ get headersCount() {
33
+ return this[kHeadersCount];
34
+ }
35
+ get trailers() {
36
+ if (!this[kTrailers])
37
+ this[kTrailers] = arrayToHeaders(this.rawTrailers);
38
+ return this[kTrailers];
39
+ }
40
+ get trailersCount() {
41
+ return this[kTrailersCount];
42
+ }
43
+ _read() {
44
+ if (this.data) {
45
+ this.push(this.data);
46
+ }
47
+ this.push(null);
48
+ }
49
+ static parse(input) {
50
+ const parser = new HTTPParser(HTTPParser.REQUEST);
51
+ const out = new HttpRequest();
52
+ const bodyChunks = [];
53
+ parser[HTTPParser.kOnHeadersComplete] = function (req) {
54
+ out.shouldKeepAlive = req.shouldKeepAlive;
55
+ out.upgrade = req.upgrade;
56
+ out.method = HTTPParser.methods[req.method];
57
+ out.url = req.url;
58
+ out.originalUrl = req.url;
59
+ out.httpVersionMajor = req.versionMajor;
60
+ out.httpVersionMinor = req.versionMinor;
61
+ out.httpVersion = req.versionMajor + '.' + req.versionMinor;
62
+ out.rawHeaders = req.headers;
63
+ out[kHeadersCount] = Math.ceil(req.headers.length / 2);
64
+ out[kTrailersCount] = 0;
65
+ };
66
+ parser[HTTPParser.kOnBody] = function (chunk, offset, length) {
67
+ bodyChunks.push(chunk.subarray(offset, offset + length));
68
+ };
69
+ // This is actually the event for trailers, go figure.
70
+ parser[HTTPParser.kOnHeaders] = function (t) {
71
+ out.rawTrailers = t;
72
+ out[kTrailersCount] = Math.ceil(t.length / 2);
73
+ };
74
+ parser[HTTPParser.kOnMessageComplete] = function () {
75
+ out.complete = true;
76
+ };
77
+ // Since we are sending the entire Buffer at once here all callbacks above happen synchronously.
78
+ // The parser does not do _anything_ asynchronous.
79
+ // However, you can of course call execute() multiple times with multiple chunks, e.g. from a stream.
80
+ // But then you have to refactor the entire logic to be async (e.g. resolve a Promise in kOnMessageComplete and add timeout logic).
81
+ parser.execute(input);
82
+ if (!out.complete)
83
+ parser.execute(crlfBuffer);
84
+ parser.finish();
85
+ if (!out.complete) {
86
+ throw new Error('Could not parse request');
87
+ }
88
+ out.rawTrailers = out.rawTrailers || [];
89
+ if (bodyChunks.length)
90
+ out.data = Buffer.concat(bodyChunks);
91
+ out.resume();
92
+ return out;
93
+ }
94
+ }
95
+ function arrayToHeaders(arr) {
96
+ const headers = {};
97
+ for (let i = 0; i < arr.length; i++) {
98
+ const k = arr[i].toLowerCase();
99
+ const v = arr[++i];
100
+ const trgV = headers[k];
101
+ // Array header -- only Set-Cookie at the moment
102
+ if (trgV && k === 'set-cookie') {
103
+ const a = Array.isArray(trgV) ? trgV : [trgV];
104
+ a.push(v);
105
+ headers[k] = a;
106
+ }
107
+ else if (typeof trgV === 'string') {
108
+ headers[k] += (k === 'cookie' ? '; ' : ', ') + v;
109
+ }
110
+ else
111
+ headers[k] = v;
112
+ }
113
+ return headers;
114
+ }
@@ -0,0 +1,6 @@
1
+ export * from './http-request.js';
2
+ export * from './enums/http-headers.enum.js';
3
+ export * from './enums/http-status.enum.js';
4
+ export * from './interfaces/client-http-headers.interface.js';
5
+ export * from './interfaces/server-http-headers.interface.js';
6
+ export * from './utils/normalize-headers.js';
@@ -0,0 +1,6 @@
1
+ export * from './http-request.js';
2
+ export * from './enums/http-headers.enum.js';
3
+ export * from './enums/http-status.enum.js';
4
+ export * from './interfaces/client-http-headers.interface.js';
5
+ export * from './interfaces/server-http-headers.interface.js';
6
+ export * from './utils/normalize-headers.js';
@@ -0,0 +1,65 @@
1
+ export declare type ClientHttpHeaders = Record<string, string | string[]> & {
2
+ accept?: string;
3
+ 'accept-language'?: string;
4
+ 'accept-patch'?: string;
5
+ 'accept-ranges'?: string;
6
+ 'access-control-allow-credentials'?: string;
7
+ 'access-control-allow-headers'?: string;
8
+ 'access-control-allow-methods'?: string;
9
+ 'access-control-allow-origin'?: string;
10
+ 'access-control-expose-headers'?: string;
11
+ 'access-control-max-age'?: string;
12
+ 'access-control-request-headers'?: string;
13
+ 'access-control-request-method'?: string;
14
+ age?: string;
15
+ allow?: string;
16
+ 'alt-svc'?: string;
17
+ authorization?: string;
18
+ 'cache-control'?: string;
19
+ connection?: string;
20
+ 'content-disposition'?: string;
21
+ 'content-encoding'?: string;
22
+ 'content-language'?: string;
23
+ 'content-length'?: string;
24
+ 'content-location'?: string;
25
+ 'content-range'?: string;
26
+ 'content-type'?: string;
27
+ cookie?: string;
28
+ date?: string;
29
+ etag?: string;
30
+ expect?: string;
31
+ expires?: string;
32
+ forwarded?: string;
33
+ from?: string;
34
+ host?: string;
35
+ 'if-match'?: string;
36
+ 'if-modified-since'?: string;
37
+ 'if-none-match'?: string;
38
+ 'if-unmodified-since'?: string;
39
+ 'last-modified'?: string;
40
+ location?: string;
41
+ origin?: string;
42
+ pragma?: string;
43
+ 'proxy-authenticate'?: string;
44
+ 'proxy-authorization'?: string;
45
+ 'public-key-pins'?: string;
46
+ range?: string;
47
+ referer?: string;
48
+ 'retry-after'?: string;
49
+ 'sec-websocket-accept'?: string;
50
+ 'sec-websocket-extensions'?: string;
51
+ 'sec-websocket-key'?: string;
52
+ 'sec-websocket-protocol'?: string;
53
+ 'sec-websocket-version'?: string;
54
+ 'set-cookie'?: string[];
55
+ 'strict-transport-security'?: string;
56
+ tk?: string;
57
+ trailer?: string;
58
+ 'transfer-encoding'?: string;
59
+ upgrade?: string;
60
+ 'user-agent'?: string;
61
+ vary?: string;
62
+ via?: string;
63
+ warning?: string;
64
+ 'www-authenticate'?: string;
65
+ };
@@ -0,0 +1 @@
1
+ export declare type ServerHttpHeaders = Record<string, string | string[]> & {};
@@ -0,0 +1,30 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { Readable } from 'stream';
4
+ import { HttpRequestContent, IHttpRequestContent } from './http-request-content.js';
5
+ import { HttpResponseContent, IHttpResponseContent } from './http-response-content.js';
6
+ declare type Part = {
7
+ headers: Record<string, string | string[]>;
8
+ contentId?: string;
9
+ content: HttpRequestContent | HttpResponseContent | BatchMultipart;
10
+ };
11
+ export declare class BatchMultipart {
12
+ protected _parts: Part[];
13
+ readonly boundary: string;
14
+ constructor();
15
+ addRequestPart(content: IHttpRequestContent, part?: {
16
+ contentId?: string;
17
+ headers?: Record<string, string | string[]>;
18
+ }): this;
19
+ addHttpResponse(content: IHttpResponseContent, part?: {
20
+ contentId?: string;
21
+ headers?: Record<string, string | string[]>;
22
+ }): this;
23
+ addBatch(batch: BatchMultipart, part?: {
24
+ contentId?: string;
25
+ headers?: Record<string, string | string[]>;
26
+ }): this;
27
+ stream(): Highland.Stream<Readable | Buffer>;
28
+ protected _build(target: (Buffer | Readable)[]): void;
29
+ }
30
+ export {};
@@ -0,0 +1,150 @@
1
+ import Highland from 'highland';
2
+ import { uid } from 'uid';
3
+ import { isReadable } from '../../utils/type-guards.js';
4
+ import { HttpHeaders } from '../enums/http-headers.enum.js';
5
+ import { HttpStatus } from '../enums/http-status.enum.js';
6
+ import { normalizeHeaders } from '../utils/normalize-headers.js';
7
+ import { HttpRequestContent } from './http-request-content.js';
8
+ import { HttpResponseContent } from './http-response-content.js';
9
+ const CRLF = '\r\n';
10
+ const CHARSET_PATTERN = / *charset=./i;
11
+ export class BatchMultipart {
12
+ _parts = [];
13
+ boundary;
14
+ constructor() {
15
+ this.boundary = 'batch_' + uid(12);
16
+ }
17
+ addRequestPart(content, part) {
18
+ const headers = {
19
+ ...normalizeHeaders(part?.headers || {}, true),
20
+ [HttpHeaders.Content_Type]: 'application/http',
21
+ [HttpHeaders.Content_Transfer_Encoding]: 'binary'
22
+ };
23
+ if (part?.contentId)
24
+ headers[HttpHeaders.Content_ID] = part.contentId;
25
+ this._parts.push({
26
+ headers,
27
+ contentId: part?.contentId,
28
+ content: new HttpRequestContent(content)
29
+ });
30
+ return this;
31
+ }
32
+ addHttpResponse(content, part) {
33
+ const headers = {
34
+ ...normalizeHeaders(part?.headers || {}, true),
35
+ [HttpHeaders.Content_Type]: 'application/http',
36
+ [HttpHeaders.Content_Transfer_Encoding]: 'binary'
37
+ };
38
+ if (part?.contentId)
39
+ headers[HttpHeaders.Content_ID] = part.contentId;
40
+ this._parts.push({
41
+ headers,
42
+ contentId: part?.contentId,
43
+ content: new HttpResponseContent(content)
44
+ });
45
+ return this;
46
+ }
47
+ addBatch(batch, part) {
48
+ const headers = {
49
+ ...normalizeHeaders(part?.headers || {}, true),
50
+ [HttpHeaders.Content_Type]: 'application/http',
51
+ [HttpHeaders.Content_Transfer_Encoding]: 'binary'
52
+ };
53
+ if (part?.contentId)
54
+ headers[HttpHeaders.Content_ID] = part.contentId;
55
+ this._parts.push({
56
+ headers,
57
+ contentId: part?.contentId,
58
+ content: batch
59
+ });
60
+ return this;
61
+ }
62
+ stream() {
63
+ const chunks = [];
64
+ this._build(chunks);
65
+ return Highland(chunks).flatten();
66
+ }
67
+ _build(target) {
68
+ for (const part of this._parts) {
69
+ if (part.content instanceof HttpRequestContent || part.content instanceof HttpResponseContent) {
70
+ let contentBody = part.content.data;
71
+ let contentLength = 0;
72
+ const contentHeaders = normalizeHeaders(part.content.headers);
73
+ if (contentBody) {
74
+ const contentType = String(contentHeaders['content-type'] || '').split(/\s*;\s*/);
75
+ let charset = '';
76
+ if (isReadable(contentBody)) {
77
+ contentLength = parseInt(String(contentHeaders['content-length']), 10) || 0;
78
+ // const l = parseInt(String(contentHeaders['content-length']), 10);
79
+ // if (isNaN(l))
80
+ // throw new TypeError('"content-length" header required for streamed responses');
81
+ }
82
+ else if (typeof contentBody === 'object') {
83
+ if (typeof contentBody.stream === 'function') { // File and Blob
84
+ contentType[0] = contentBody.type || 'binary';
85
+ contentLength = contentBody.size;
86
+ contentBody = contentBody.stream();
87
+ }
88
+ else if (Buffer.isBuffer(contentBody)) {
89
+ contentHeaders['content-length'] = String(contentBody.length);
90
+ }
91
+ else {
92
+ contentType[0] = contentType[0] || 'application/json';
93
+ charset = 'utf-8';
94
+ contentBody = Buffer.from(JSON.stringify(contentBody), 'utf-8');
95
+ contentLength = contentBody.length;
96
+ }
97
+ }
98
+ else {
99
+ contentType[0] = contentType[0] || 'text/plain';
100
+ charset = 'utf-8';
101
+ contentBody = Buffer.from(String(contentBody), 'utf-8');
102
+ contentLength = contentBody.length;
103
+ }
104
+ if (contentType[0]) {
105
+ if (charset) {
106
+ const i = contentType.findIndex(x => CHARSET_PATTERN.test(String(x)));
107
+ if (i > 0)
108
+ contentType[i] = 'charset=' + charset;
109
+ else
110
+ contentType.join('charset=' + charset);
111
+ }
112
+ contentHeaders['content-type'] = contentType.join(';');
113
+ }
114
+ if (contentLength)
115
+ contentHeaders['content-length'] = String(contentLength);
116
+ }
117
+ let s = '--' + this.boundary + CRLF;
118
+ for (const [k, v] of Object.entries(part.headers)) {
119
+ if (!(v === '' || v == null))
120
+ s += k + ': ' + (Array.isArray(v) ? v.join(';') : v) + CRLF;
121
+ }
122
+ s += CRLF;
123
+ if (part.content instanceof HttpRequestContent)
124
+ s += (part.content.method || 'GET').toUpperCase() + ' ' + part.content.url + ' HTTP/1.1' + CRLF;
125
+ else
126
+ s += 'HTTP/1.1 ' + part.content.status + (HttpStatus[part.content.status] || 'Unknown') + CRLF;
127
+ if (part.content.headers) {
128
+ for (const [k, v] of Object.entries(part.content.headers)) {
129
+ if (v === '' || v == null)
130
+ continue;
131
+ if (k === 'set-cookie' && Array.isArray(v)) {
132
+ v.forEach(x => s += k + ': ' + x);
133
+ }
134
+ else
135
+ s += k + ': ' + (Array.isArray(v) ? v.join(';') : v) + CRLF;
136
+ }
137
+ }
138
+ s += CRLF;
139
+ target.push(Buffer.from(s, 'utf-8'));
140
+ if (contentBody) {
141
+ target.push(contentBody);
142
+ target.push(Buffer.from(CRLF + CRLF));
143
+ }
144
+ }
145
+ else
146
+ throw new Error('Not implemented yet');
147
+ }
148
+ target.push(Buffer.from('--' + this.boundary + '--' + CRLF, 'utf-8'));
149
+ }
150
+ }
@@ -0,0 +1,8 @@
1
+ export declare type IHttpRequestContent = Pick<HttpRequestContent, 'method' | 'url' | 'headers' | 'data'>;
2
+ export declare class HttpRequestContent {
3
+ method: string;
4
+ url: string;
5
+ headers?: Record<string, string | string[]>;
6
+ data?: any;
7
+ constructor(init: IHttpRequestContent);
8
+ }
@@ -0,0 +1,12 @@
1
+ export class HttpRequestContent {
2
+ method;
3
+ url;
4
+ headers;
5
+ data;
6
+ constructor(init) {
7
+ this.method = init.method;
8
+ this.url = encodeURI(decodeURI(init.url));
9
+ this.headers = init.headers;
10
+ this.data = init.data;
11
+ }
12
+ }
@@ -0,0 +1,7 @@
1
+ export declare type IHttpResponseContent = Pick<HttpResponseContent, 'status' | 'headers' | 'data'>;
2
+ export declare class HttpResponseContent {
3
+ status: number;
4
+ headers?: Record<string, string | string[]>;
5
+ data?: any;
6
+ constructor(init: IHttpResponseContent);
7
+ }
@@ -0,0 +1,10 @@
1
+ export class HttpResponseContent {
2
+ status;
3
+ headers;
4
+ data;
5
+ constructor(init) {
6
+ this.status = init.status;
7
+ this.headers = init.headers;
8
+ this.data = init.data;
9
+ }
10
+ }
@@ -0,0 +1 @@
1
+ export * from './batch-multipart.js';
@@ -0,0 +1 @@
1
+ export * from './batch-multipart.js';
@@ -0,0 +1 @@
1
+ export declare function normalizeHeaders(headers: any, normalCase?: boolean): Record<string, string | string[]>;
@@ -0,0 +1,24 @@
1
+ import { HttpHeaders } from '../enums/http-headers.enum.js';
2
+ const knownKeys = Object.keys(HttpHeaders);
3
+ const knownKeysLower = knownKeys.map(x => x.toLowerCase());
4
+ export function normalizeHeaders(headers, normalCase) {
5
+ if (!headers)
6
+ return {};
7
+ return Object.keys(headers).reduce((o, k) => {
8
+ const v = headers[k];
9
+ const key = normalCase
10
+ ? knownKeys[knownKeysLower.indexOf(k.toLowerCase())] || camelize(k) :
11
+ k.toLowerCase();
12
+ if (k.toLowerCase() === 'set-cookie')
13
+ o[key] = Array.isArray(v) ? v : [v];
14
+ else {
15
+ o[key] = Array.isArray(v) ? v.join(';') : v;
16
+ }
17
+ return o;
18
+ }, {}) || {};
19
+ }
20
+ function camelize(str) {
21
+ return str.replace(/(^\w|[A-Z]|\b\w)/g, function (word) {
22
+ return word.toUpperCase();
23
+ });
24
+ }
@@ -0,0 +1,28 @@
1
+ import { FallbackLng, Formatter as I18nextFormatter, i18n, InitOptions as I18nextInitOptions, Resource as I18nextResource, TFunction as I18nextTFunction, TOptions } from 'i18next';
2
+ import * as I18next from 'i18next';
3
+ import { Type } from 'ts-gems';
4
+ export declare type BaseI18n = Type<I18next.i18n>;
5
+ export declare const BaseI18n: BaseI18n;
6
+ export declare type DeepTranslateOptions = TOptions & {
7
+ ignore?: (input: any, inst: i18n) => boolean;
8
+ };
9
+ export declare type InitCallback = I18next.Callback;
10
+ export declare type TranslateFunction = I18nextTFunction;
11
+ export declare type Formatter = I18nextFormatter;
12
+ export declare type LanguageResource = I18nextResource;
13
+ export { FallbackLng };
14
+ export interface InitOptions extends I18nextInitOptions {
15
+ resourceDirs?: string[];
16
+ }
17
+ export declare class I18n extends BaseI18n {
18
+ init(callback?: InitCallback): Promise<TranslateFunction>;
19
+ init(options: InitOptions, callback?: InitCallback): Promise<TranslateFunction>;
20
+ deep(input: any, options?: DeepTranslateOptions): any;
21
+ registerLocaleDir(...dirname: string[]): void;
22
+ loadResourceBundle(lang: string, ns: string, filePath: string, deep?: boolean, overwrite?: boolean): Promise<void>;
23
+ loadResourceDir(dirnames: string | string[], deep?: boolean, overwrite?: boolean): Promise<void>;
24
+ createInstance(options: {} | undefined, callback: any): I18n;
25
+ static createInstance(options?: InitOptions, callback?: InitCallback): I18n;
26
+ protected _deepTranslate(input: any, objectStack: WeakMap<object, any>, options?: DeepTranslateOptions): any;
27
+ static get defaultInstance(): I18n;
28
+ }
@@ -0,0 +1,170 @@
1
+ import { splitString, tokenize } from 'fast-tokenizer';
2
+ import i18next from 'i18next';
3
+ import { isUrl } from '../utils/index.js';
4
+ import { unescapeString } from './string-utils.js';
5
+ export const BaseI18n = Object.getPrototypeOf(i18next).constructor;
6
+ const globalLocaleDirs = [];
7
+ export class I18n extends BaseI18n {
8
+ async init(arg0, arg1) {
9
+ const options = typeof arg0 === 'object' ? arg0 : {};
10
+ const callback = typeof arg0 === 'function' ? arg0 : arg1;
11
+ try {
12
+ const t = await super.init(options);
13
+ // Add formatters
14
+ const formatter = this.services.formatter;
15
+ formatter.add('lowercase', (value, lng) => value.toLocaleLowerCase(lng));
16
+ formatter.add('uppercase', (value, lng) => value.toLocaleUpperCase(lng));
17
+ formatter.add('upperFirst', (value, lng) => value.charAt(0).toLocaleUpperCase(lng) + value.substring(1));
18
+ // Load globally registered resources
19
+ if (globalLocaleDirs.length)
20
+ await this.loadResourceDir(globalLocaleDirs, false, true);
21
+ // Load resource dirs and overwrite existing
22
+ if (options?.resourceDirs?.length) {
23
+ await this.loadResourceDir(options.resourceDirs, false, true);
24
+ }
25
+ // overwrite existing resources with options.resources
26
+ if (options?.resources) {
27
+ for (const lang of Object.keys(options.resources)) {
28
+ const langObj = options.resources[lang];
29
+ for (const ns of Object.keys(langObj)) {
30
+ this.addResourceBundle(lang, ns, langObj[ns], false, true);
31
+ }
32
+ }
33
+ }
34
+ if (callback)
35
+ callback(null, t);
36
+ return t;
37
+ }
38
+ catch (err) {
39
+ if (callback)
40
+ callback(err, this.t);
41
+ throw err;
42
+ }
43
+ }
44
+ deep(input, options) {
45
+ if (input == null)
46
+ return input;
47
+ const objectStack = new WeakMap();
48
+ return this._deepTranslate(input, objectStack, options);
49
+ }
50
+ registerLocaleDir(...dirname) {
51
+ globalLocaleDirs.push(...dirname);
52
+ }
53
+ async loadResourceBundle(lang, ns, filePath, deep, overwrite) {
54
+ let obj;
55
+ if (isUrl(filePath)) {
56
+ obj = (await fetch(filePath, { headers: { accept: 'application/json' } })).json();
57
+ }
58
+ else {
59
+ const fs = await import('fs/promises');
60
+ const content = await fs.readFile(filePath, 'utf8');
61
+ obj = JSON.parse(content);
62
+ }
63
+ this.addResourceBundle(lang, ns, obj, deep, overwrite);
64
+ }
65
+ async loadResourceDir(dirnames, deep, overwrite) {
66
+ const fs = await import('fs/promises');
67
+ const path = await import('path');
68
+ for (const dirname of Array.isArray(dirnames) ? dirnames : [dirnames]) {
69
+ /* istanbul ignore next */
70
+ if (!(await fs.stat(dirname)).isDirectory()) {
71
+ // eslint-disable-next-line no-console
72
+ console.warn(`Locale directory does not exists. (${dirname})`);
73
+ continue;
74
+ }
75
+ const languageDirs = await fs.readdir(dirname);
76
+ for (const lang of languageDirs) {
77
+ const langDir = path.join(dirname, lang);
78
+ if ((await fs.stat(langDir)).isDirectory()) {
79
+ const nsDirs = await fs.readdir(langDir);
80
+ for (const nsfile of nsDirs) {
81
+ const nsFilePath = path.join(langDir, nsfile);
82
+ const ext = path.extname(nsfile);
83
+ if (ext === '.json' && (await fs.stat(nsFilePath)).isFile()) {
84
+ const ns = path.basename(nsfile, ext);
85
+ await this.loadResourceBundle(lang, ns, nsFilePath, deep, overwrite);
86
+ }
87
+ }
88
+ }
89
+ }
90
+ }
91
+ }
92
+ createInstance(options = {}, callback) {
93
+ return new I18n(options, callback);
94
+ }
95
+ static createInstance(options, callback) {
96
+ return new I18n(options, callback);
97
+ }
98
+ _deepTranslate(input, objectStack, options) {
99
+ if (input == null)
100
+ return input;
101
+ if (options?.ignore && options.ignore(input, this))
102
+ return input;
103
+ if (typeof input === 'object' && objectStack.has(input))
104
+ return objectStack.get(input);
105
+ if (typeof input === 'string') {
106
+ let s = '';
107
+ for (let token of tokenize(input, {
108
+ brackets: { '$t(': ')' },
109
+ quotes: true,
110
+ keepQuotes: true,
111
+ keepBrackets: true,
112
+ keepDelimiters: true,
113
+ })) {
114
+ if (token.startsWith('$t(') && token.endsWith(')')) {
115
+ token = token.substring(3, token.length - 1);
116
+ const a = splitString(token, { delimiters: '?', quotes: true, brackets: { '{': '}' } });
117
+ const fallback = unescapeString(token.substring((a[0] || '').length + 1));
118
+ token = a[0] || '';
119
+ const keys = [];
120
+ let opts = null;
121
+ for (const token2 of tokenize(token, { delimiters: ',', quotes: true, brackets: { '{': '}' } })) {
122
+ if (token2.startsWith('{')) {
123
+ opts = JSON.parse(token2);
124
+ continue;
125
+ }
126
+ keys.push(token2);
127
+ }
128
+ const k = keys.length > 1 ? '$t(' + keys.join(',') + ')' : keys[0];
129
+ s += fallback
130
+ ? this.t(k, fallback, { ...options, ...opts })
131
+ : this.t(k, { ...options, ...opts });
132
+ continue;
133
+ }
134
+ s += token;
135
+ }
136
+ return s;
137
+ }
138
+ if (Array.isArray(input)) {
139
+ const out = Array(input.length);
140
+ objectStack.set(input, out);
141
+ for (let i = 0, l = input.length; i < l; i++) {
142
+ out[i] = this._deepTranslate(input[i], objectStack, options);
143
+ }
144
+ objectStack.delete(input);
145
+ return out;
146
+ }
147
+ if (typeof input === 'object') {
148
+ if (Buffer.isBuffer(input))
149
+ return input;
150
+ if (Buffer.isBuffer(input) || input instanceof Symbol ||
151
+ input instanceof RegExp || input instanceof Map || input instanceof Set ||
152
+ input instanceof WeakMap || input instanceof WeakSet)
153
+ return input;
154
+ const out = {};
155
+ objectStack.set(input, out);
156
+ const keys = Object.keys(input);
157
+ for (let i = 0, l = keys.length; i < l; i++) {
158
+ const k = keys[i];
159
+ out[k] = this._deepTranslate(input[k], objectStack, options);
160
+ }
161
+ objectStack.delete(input);
162
+ return out;
163
+ }
164
+ return input;
165
+ }
166
+ static get defaultInstance() {
167
+ return defaultInstance;
168
+ }
169
+ }
170
+ const defaultInstance = I18n.createInstance();
@@ -0,0 +1,5 @@
1
+ import { I18n } from './i18n.js';
2
+ export * from './i18n.js';
3
+ export * from './translate.js';
4
+ declare const i18n: I18n;
5
+ export { i18n };