@wener/utils 1.1.48 → 1.1.50

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 (267) hide show
  1. package/lib/arrays/MaybeArray.js.map +1 -1
  2. package/lib/arrays/arrayFromAsync.js.map +1 -1
  3. package/lib/asyncs/AsyncInterval.js.map +1 -1
  4. package/lib/asyncs/MaybePromise.js.map +1 -1
  5. package/lib/asyncs/Promises.js +9 -1
  6. package/lib/asyncs/Promises.js.map +1 -1
  7. package/lib/asyncs/createAsyncIterator.js.map +1 -1
  8. package/lib/asyncs/createLazyPromise.js +52 -51
  9. package/lib/asyncs/createLazyPromise.js.map +1 -1
  10. package/lib/asyncs/firstOfAsyncIterator.js.map +1 -1
  11. package/lib/asyncs/generatorOfStream.js.map +1 -1
  12. package/lib/asyncs/isIterator.js.map +1 -1
  13. package/lib/asyncs/isPromise.js.map +1 -1
  14. package/lib/asyncs/isThenable.js.map +1 -1
  15. package/lib/asyncs/nextOfAsyncIterator.js.map +1 -1
  16. package/lib/asyncs/promiseOfCallback.js.map +1 -1
  17. package/lib/asyncs/timeout.js +4 -4
  18. package/lib/asyncs/timeout.js.map +1 -1
  19. package/lib/browsers/copy.js.map +1 -1
  20. package/lib/browsers/download.js.map +1 -1
  21. package/lib/browsers/getFileFromDataTransfer.js.map +1 -1
  22. package/lib/browsers/loaders.js.map +1 -1
  23. package/lib/cn/division/DivisionCode.js.map +1 -1
  24. package/lib/cn/division/binarySearch.js.map +1 -1
  25. package/lib/cn/formatChineseAmount.js.map +1 -1
  26. package/lib/cn/id/Mod11.js.map +1 -1
  27. package/lib/cn/id/ResidentIdNumber.js.map +1 -1
  28. package/lib/cn/id/types.js.map +1 -1
  29. package/lib/cn/index.js.map +1 -1
  30. package/lib/cn/parseChineseNumber.js.map +1 -1
  31. package/lib/cn/pinyin/cartesianProduct.js.map +1 -1
  32. package/lib/cn/pinyin/loader.js.map +1 -1
  33. package/lib/cn/pinyin/preload.js.map +1 -1
  34. package/lib/cn/pinyin/toPinyinPure.js.map +1 -1
  35. package/lib/cn/pinyin/transform.js.map +1 -1
  36. package/lib/cn/types.js +3 -0
  37. package/lib/cn/types.js.map +1 -0
  38. package/lib/cn/uscc/Mod31.js.map +1 -1
  39. package/lib/cn/uscc/USCC.js.map +1 -1
  40. package/lib/cn/uscc/isUSCC.js.map +1 -1
  41. package/lib/crypto/base.js.map +1 -1
  42. package/lib/crypto/getNodeCrypto.js +1 -0
  43. package/lib/crypto/getNodeCrypto.js.map +1 -1
  44. package/lib/crypto/getRandomValues.js.map +1 -1
  45. package/lib/crypto/hashing.js.map +1 -1
  46. package/lib/crypto/md5.bench.js.map +1 -1
  47. package/lib/crypto/md5.d.js.map +1 -1
  48. package/lib/crypto/md5.js.map +1 -1
  49. package/lib/crypto/pem/pem.js.map +1 -1
  50. package/lib/crypto/randomUUID.js.map +1 -1
  51. package/lib/crypto/randomUUIDv7.js.map +1 -1
  52. package/lib/crypto/ulid.js.map +1 -1
  53. package/lib/emitter/types.js.map +1 -1
  54. package/lib/errors/Errors.js +38 -17
  55. package/lib/errors/Errors.js.map +1 -1
  56. package/lib/fetch/HttpStatus.js.map +1 -1
  57. package/lib/fetch/createFetchWith.js.map +1 -1
  58. package/lib/fetch/createFetchWithLogging.js.map +1 -1
  59. package/lib/fetch/createFetchWithRetry.js.map +1 -1
  60. package/lib/fetch/dumpRequest.js.map +1 -1
  61. package/lib/fetch/dumpResponse.js.map +1 -1
  62. package/lib/fetch/http.types.js.map +1 -1
  63. package/lib/fetch/index.js.map +1 -1
  64. package/lib/fetch/types.js.map +1 -1
  65. package/lib/i18n/createTranslate.js.map +1 -1
  66. package/lib/index.js +2 -0
  67. package/lib/index.js.map +1 -1
  68. package/lib/io/AbstractEncoding.js.map +1 -1
  69. package/lib/io/ArrayBuffer.test-d.js.map +1 -1
  70. package/lib/io/ArrayBuffers.js +217 -173
  71. package/lib/io/ArrayBuffers.js.map +1 -1
  72. package/lib/io/Buffer.js.map +1 -1
  73. package/lib/io/ByteBuffer.js +233 -34
  74. package/lib/io/ByteBuffer.js.map +1 -1
  75. package/lib/io/base64.js +2 -2
  76. package/lib/io/base64.js.map +1 -1
  77. package/lib/io/dump.js.map +1 -1
  78. package/lib/io/isBuffer.js.map +1 -1
  79. package/lib/io/isTransferable.js.map +1 -1
  80. package/lib/io/parseDataUri.js.map +1 -1
  81. package/lib/langs/MaybeFunction.js.map +1 -1
  82. package/lib/langs/classOf.js.map +1 -1
  83. package/lib/langs/deepEqual.js.map +1 -1
  84. package/lib/langs/deepFreeze.js.map +1 -1
  85. package/lib/langs/getGlobalStates.js +17 -0
  86. package/lib/langs/getGlobalStates.js.map +1 -0
  87. package/lib/langs/getObjectId.js +18 -0
  88. package/lib/langs/getObjectId.js.map +1 -0
  89. package/lib/langs/isClass.js.map +1 -1
  90. package/lib/langs/isDefined.js.map +1 -1
  91. package/lib/langs/isEmptyObject.js.map +1 -1
  92. package/lib/langs/isFunction.js.map +1 -1
  93. package/lib/langs/isNullish.js +5 -0
  94. package/lib/langs/isNullish.js.map +1 -0
  95. package/lib/langs/isPlainObject.js.map +1 -1
  96. package/lib/langs/memoize.js.map +1 -1
  97. package/lib/langs/mixin.js.map +1 -1
  98. package/lib/langs/parseBoolean.js.map +1 -1
  99. package/lib/langs/shallowClone.js.map +1 -1
  100. package/lib/langs/shallowEqual.js.map +1 -1
  101. package/lib/libs/ms.js.map +1 -1
  102. package/lib/logging/Logger.js.map +1 -1
  103. package/lib/logging/createChildLogger.js.map +1 -1
  104. package/lib/logging/createLogger.js.map +1 -1
  105. package/lib/logging/createNoopLogger.js.map +1 -1
  106. package/lib/logging/slog.js.map +1 -1
  107. package/lib/maths/clamp.js +7 -6
  108. package/lib/maths/clamp.js.map +1 -1
  109. package/lib/maths/random.js.map +1 -1
  110. package/lib/mitt/index.js.map +1 -1
  111. package/lib/modules/isModule.js.map +1 -1
  112. package/lib/modules/parseModuleId.js.map +1 -1
  113. package/lib/objects/computeIfAbsent.js.map +1 -1
  114. package/lib/objects/get.js.map +1 -1
  115. package/lib/objects/get.test-d.js.map +1 -1
  116. package/lib/objects/merge/index.js.map +1 -1
  117. package/lib/objects/merge/isMergeableObject.js.map +1 -1
  118. package/lib/objects/merge/merge.js.map +1 -1
  119. package/lib/objects/parseObjectPath.js.map +1 -1
  120. package/lib/objects/set.js.map +1 -1
  121. package/lib/runtime/AsyncCloser.js.map +1 -1
  122. package/lib/runtime/Closer.js.map +1 -1
  123. package/lib/runtime/getGlobalThis.js.map +1 -1
  124. package/lib/runtime/structuredClone.js.map +1 -1
  125. package/lib/schema/typebox/index.js.map +1 -1
  126. package/lib/schema/typebox/typebox.js.map +1 -1
  127. package/lib/scripts/getGenerateContext.js.map +1 -1
  128. package/lib/server/crypto/md5.js.map +1 -1
  129. package/lib/server/fetch/createFetchWithProxy.js.map +1 -1
  130. package/lib/server/fetch/createFetchWithProxyByNodeFetch.js.map +1 -1
  131. package/lib/server/fetch/createFetchWithProxyByUndici.js.map +1 -1
  132. package/lib/server/getPackageDir.js.map +1 -1
  133. package/lib/server/index.js.map +1 -1
  134. package/lib/server/jsdom.js.map +1 -1
  135. package/lib/server/node-fetch.js.map +1 -1
  136. package/lib/server/polyfill/polyfillBrowser.js.map +1 -1
  137. package/lib/server/polyfill/polyfillCrypto.js.map +1 -1
  138. package/lib/server/polyfill/polyfillFetch.js.map +1 -1
  139. package/lib/server/polyfill/polyfillJsDom.js.map +1 -1
  140. package/lib/server/polyfill/polyfillWebSocket.js.map +1 -1
  141. package/lib/server/ws.js.map +1 -1
  142. package/lib/strings/camelCase.js.map +1 -1
  143. package/lib/strings/formatBytes.js.map +1 -1
  144. package/lib/strings/renderTemplate.js.map +1 -1
  145. package/lib/typedoc.js.map +1 -1
  146. package/lib/types.d.js.map +1 -1
  147. package/lib/validations/asserts.js.map +1 -1
  148. package/lib/validations/isUUID.js.map +1 -1
  149. package/lib/validations/parseTimestamp.js.map +1 -1
  150. package/package.json +4 -4
  151. package/src/asyncs/Promises.ts +12 -2
  152. package/src/asyncs/createLazyPromise.test.ts +52 -13
  153. package/src/asyncs/createLazyPromise.ts +66 -61
  154. package/src/asyncs/firstOfAsyncIterator.ts +1 -1
  155. package/src/asyncs/nextOfAsyncIterator.ts +1 -1
  156. package/src/asyncs/timeout.ts +4 -4
  157. package/src/cn/types.ts +11 -0
  158. package/src/crypto/getNodeCrypto.ts +1 -0
  159. package/src/errors/Errors.ts +37 -16
  160. package/src/fetch/createFetchWith.ts +1 -1
  161. package/src/fetch/createFetchWithRetry.ts +1 -1
  162. package/src/index.ts +2 -0
  163. package/src/io/ArrayBuffers.base64.test.ts +7 -0
  164. package/src/io/ArrayBuffers.ts +276 -271
  165. package/src/io/ByteBuffer.test.ts +26 -14
  166. package/src/io/ByteBuffer.ts +270 -38
  167. package/src/io/base64.ts +2 -2
  168. package/src/langs/getGlobalStates.ts +21 -0
  169. package/src/langs/getObjectId.ts +20 -0
  170. package/src/langs/isNullish.ts +3 -0
  171. package/src/langs/mixin.test.ts +4 -0
  172. package/src/logging/slog.ts +1 -1
  173. package/src/maths/clamp.test.ts +5 -1
  174. package/src/maths/clamp.ts +7 -7
  175. package/src/objects/merge/merge.test.ts +1 -1
  176. package/src/schema/typebox/typebox.ts +1 -1
  177. package/src/server/fetch/createFetchWithProxyByUndici.ts +2 -2
  178. package/tsconfig.json +4 -1
  179. package/lib/schema/typebox/gen/codegen/common/encoder.js +0 -94
  180. package/lib/schema/typebox/gen/codegen/common/encoder.js.map +0 -1
  181. package/lib/schema/typebox/gen/codegen/common/formatter.js +0 -33
  182. package/lib/schema/typebox/gen/codegen/common/formatter.js.map +0 -1
  183. package/lib/schema/typebox/gen/codegen/common/index.js +0 -29
  184. package/lib/schema/typebox/gen/codegen/common/index.js.map +0 -1
  185. package/lib/schema/typebox/gen/codegen/common/jsdoc.js +0 -117
  186. package/lib/schema/typebox/gen/codegen/common/jsdoc.js.map +0 -1
  187. package/lib/schema/typebox/gen/codegen/expression/compiler.js +0 -364
  188. package/lib/schema/typebox/gen/codegen/expression/compiler.js.map +0 -1
  189. package/lib/schema/typebox/gen/codegen/expression/errors.js +0 -259
  190. package/lib/schema/typebox/gen/codegen/expression/errors.js.map +0 -1
  191. package/lib/schema/typebox/gen/codegen/expression/evaluator.js +0 -254
  192. package/lib/schema/typebox/gen/codegen/expression/evaluator.js.map +0 -1
  193. package/lib/schema/typebox/gen/codegen/expression/expression.js +0 -381
  194. package/lib/schema/typebox/gen/codegen/expression/expression.js.map +0 -1
  195. package/lib/schema/typebox/gen/codegen/expression/index.js +0 -32
  196. package/lib/schema/typebox/gen/codegen/expression/index.js.map +0 -1
  197. package/lib/schema/typebox/gen/codegen/index.js +0 -29
  198. package/lib/schema/typebox/gen/codegen/index.js.map +0 -1
  199. package/lib/schema/typebox/gen/codegen/model/index.js +0 -40
  200. package/lib/schema/typebox/gen/codegen/model/index.js.map +0 -1
  201. package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js +0 -260
  202. package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js.map +0 -1
  203. package/lib/schema/typebox/gen/codegen/model/model-to-expression.js +0 -383
  204. package/lib/schema/typebox/gen/codegen/model/model-to-expression.js.map +0 -1
  205. package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js +0 -238
  206. package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js.map +0 -1
  207. package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js +0 -274
  208. package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js.map +0 -1
  209. package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js +0 -47
  210. package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js.map +0 -1
  211. package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js +0 -192
  212. package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js.map +0 -1
  213. package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js +0 -33
  214. package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js.map +0 -1
  215. package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js +0 -188
  216. package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js.map +0 -1
  217. package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js +0 -239
  218. package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js.map +0 -1
  219. package/lib/schema/typebox/gen/codegen/model/model-to-value.js +0 -43
  220. package/lib/schema/typebox/gen/codegen/model/model-to-value.js.map +0 -1
  221. package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js +0 -227
  222. package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js.map +0 -1
  223. package/lib/schema/typebox/gen/codegen/model/model-to-yup.js +0 -225
  224. package/lib/schema/typebox/gen/codegen/model/model-to-yup.js.map +0 -1
  225. package/lib/schema/typebox/gen/codegen/model/model-to-zod.js +0 -248
  226. package/lib/schema/typebox/gen/codegen/model/model-to-zod.js.map +0 -1
  227. package/lib/schema/typebox/gen/codegen/model/model.js +0 -27
  228. package/lib/schema/typebox/gen/codegen/model/model.js.map +0 -1
  229. package/lib/schema/typebox/gen/codegen/typescript/index.js +0 -28
  230. package/lib/schema/typebox/gen/codegen/typescript/index.js.map +0 -1
  231. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js +0 -72
  232. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js.map +0 -1
  233. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js +0 -620
  234. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js.map +0 -1
  235. package/lib/schema/typebox/gen/index.js +0 -3
  236. package/lib/schema/typebox/gen/index.js.map +0 -1
  237. package/src/schema/typebox/gen/codegen/common/encoder.ts +0 -99
  238. package/src/schema/typebox/gen/codegen/common/formatter.ts +0 -31
  239. package/src/schema/typebox/gen/codegen/common/index.ts +0 -29
  240. package/src/schema/typebox/gen/codegen/common/jsdoc.ts +0 -93
  241. package/src/schema/typebox/gen/codegen/expression/compiler.ts +0 -377
  242. package/src/schema/typebox/gen/codegen/expression/errors.ts +0 -302
  243. package/src/schema/typebox/gen/codegen/expression/evaluator.ts +0 -268
  244. package/src/schema/typebox/gen/codegen/expression/expression.ts +0 -538
  245. package/src/schema/typebox/gen/codegen/expression/index.ts +0 -32
  246. package/src/schema/typebox/gen/codegen/index.ts +0 -29
  247. package/src/schema/typebox/gen/codegen/model/index.ts +0 -40
  248. package/src/schema/typebox/gen/codegen/model/model-to-arktype.ts +0 -266
  249. package/src/schema/typebox/gen/codegen/model/model-to-expression.ts +0 -378
  250. package/src/schema/typebox/gen/codegen/model/model-to-grpc.ts +0 -244
  251. package/src/schema/typebox/gen/codegen/model/model-to-io-ts.ts +0 -294
  252. package/src/schema/typebox/gen/codegen/model/model-to-javascript.ts +0 -42
  253. package/src/schema/typebox/gen/codegen/model/model-to-json-schema.ts +0 -166
  254. package/src/schema/typebox/gen/codegen/model/model-to-typebox.ts +0 -32
  255. package/src/schema/typebox/gen/codegen/model/model-to-typescript.ts +0 -189
  256. package/src/schema/typebox/gen/codegen/model/model-to-valibot.ts +0 -236
  257. package/src/schema/typebox/gen/codegen/model/model-to-value.ts +0 -42
  258. package/src/schema/typebox/gen/codegen/model/model-to-yrel.ts +0 -232
  259. package/src/schema/typebox/gen/codegen/model/model-to-yup.ts +0 -226
  260. package/src/schema/typebox/gen/codegen/model/model-to-zod.ts +0 -251
  261. package/src/schema/typebox/gen/codegen/model/model.ts +0 -33
  262. package/src/schema/typebox/gen/codegen/typescript/index.ts +0 -28
  263. package/src/schema/typebox/gen/codegen/typescript/typescript-to-model.ts +0 -61
  264. package/src/schema/typebox/gen/codegen/typescript/typescript-to-typebox.ts +0 -647
  265. package/src/schema/typebox/gen/gen.test.ts +0 -12
  266. package/src/schema/typebox/gen/index.ts +0 -1
  267. /package/src/schema/{typebox/gen/README.md → README.md} +0 -0
@@ -1,232 +0,0 @@
1
- /*--------------------------------------------------------------------------
2
-
3
- @sinclair/typebox-codegen
4
-
5
- The MIT License (MIT)
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining a copy
8
- of this software and associated documentation files (the "Software"), to deal
9
- in the Software without restriction, including without limitation the rights
10
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- copies of the Software, and to permit persons to whom the Software is
12
- furnished to do so, subject to the following conditions:
13
-
14
- The above copyright notice and this permission notice shall be included in
15
- all copies or substantial portions of the Software.
16
-
17
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
- THE SOFTWARE.
24
-
25
- ---------------------------------------------------------------------------*/
26
- import * as Types from '@sinclair/typebox';
27
- import { Formatter, PropertyEncoder } from '../common/index';
28
- import { TypeBoxModel } from './model';
29
-
30
- // --------------------------------------------------------------------------
31
- // ModelToYrel
32
- // --------------------------------------------------------------------------
33
- export namespace ModelToYrel {
34
- function IsDefined<T = any>(value: unknown): value is T {
35
- return value !== undefined;
36
- }
37
- function Type(schema: Types.TSchema, type: string) {
38
- return type;
39
- }
40
- function Any(schema: Types.TAny) {
41
- return Type(schema, `y.any()`);
42
- }
43
- function Array(schema: Types.TArray) {
44
- const items = Visit(schema.items);
45
- const buffer: string[] = [];
46
- buffer.push(`y.array(${items})`);
47
- if (IsDefined<number>(schema.minItems)) buffer.push(`.min(${schema.minItems})`);
48
- if (IsDefined<number>(schema.maxItems)) buffer.push(`.max(${schema.maxItems})`);
49
- return Type(schema, buffer.join(``));
50
- }
51
- function BigInt(schema: Types.TBigInt) {
52
- return UnsupportedType(schema);
53
- }
54
- function Boolean(schema: Types.TBoolean) {
55
- return Type(schema, `y.boolean()`);
56
- }
57
- function Date(schema: Types.TDate) {
58
- return UnsupportedType(schema);
59
- }
60
- function Constructor(schema: Types.TConstructor): string {
61
- return UnsupportedType(schema);
62
- }
63
- function Function(schema: Types.TFunction) {
64
- return UnsupportedType(schema);
65
- }
66
- function Integer(schema: Types.TInteger) {
67
- const buffer: string[] = [];
68
- buffer.push(`y.number().integer()`);
69
- if (IsDefined<number>(schema.minimum)) buffer.push(`.gte(${schema.minimum})`);
70
- if (IsDefined<number>(schema.maximum)) buffer.push(`.lte(${schema.maximum})`);
71
- if (IsDefined<number>(schema.exclusiveMinimum)) buffer.push(`.gt(${schema.exclusiveMinimum + 1})`);
72
- if (IsDefined<number>(schema.exclusiveMaximum)) buffer.push(`.lt(${schema.exclusiveMaximum - 1})`);
73
- return Type(schema, buffer.join(``));
74
- }
75
- function Intersect(schema: Types.TIntersect) {
76
- return UnsupportedType(schema);
77
- }
78
- function Literal(schema: Types.TLiteral) {
79
- return typeof schema.const === `string`
80
- ? Type(schema, `y.literal('${schema.const}')`)
81
- : Type(schema, `y.literal(${schema.const})`);
82
- }
83
- function Never(schema: Types.TNever) {
84
- return UnsupportedType(schema);
85
- }
86
- function Null(schema: Types.TNull) {
87
- return UnsupportedType(schema);
88
- }
89
- function String(schema: Types.TString) {
90
- const buffer: string[] = [];
91
- buffer.push(`y.string()`);
92
- if (IsDefined<number>(schema.maxLength)) buffer.push(`.max(${schema.maxLength})`);
93
- if (IsDefined<number>(schema.minLength)) buffer.push(`.min(${schema.minLength})`);
94
- return Type(schema, buffer.join(``));
95
- }
96
- function Number(schema: Types.TNumber) {
97
- const buffer: string[] = [];
98
- buffer.push(`y.number()`);
99
- if (IsDefined<number>(schema.minimum)) buffer.push(`.gte(${schema.minimum})`);
100
- if (IsDefined<number>(schema.maximum)) buffer.push(`.lte(${schema.maximum})`);
101
- if (IsDefined<number>(schema.exclusiveMinimum)) buffer.push(`.gt(${schema.exclusiveMinimum + 1})`);
102
- if (IsDefined<number>(schema.exclusiveMaximum)) buffer.push(`.lt(${schema.exclusiveMaximum - 1})`);
103
- return Type(schema, buffer.join(``));
104
- }
105
- function Object(schema: Types.TObject) {
106
- // prettier-ignore
107
- const properties = globalThis.Object.entries(schema.properties).map(([key, value]) => {
108
- const optional = Types.TypeGuard.IsOptional(value)
109
- const readonly = Types.TypeGuard.IsReadonly(value)
110
- const property = PropertyEncoder.Encode(key)
111
- // prettier-ignore
112
- return (
113
- readonly && optional ? `${property}: ${Visit(value)}.optional()` :
114
- optional ? `${property}: ${Visit(value)}.optional()` :
115
- `${property}: ${Visit(value)}`
116
- )
117
- }).join(`,`)
118
- const buffer: string[] = [];
119
- buffer.push(`y.object({\n${properties}\n})`);
120
- return Type(schema, buffer.join(``));
121
- }
122
- function Promise(schema: Types.TPromise) {
123
- return UnsupportedType(schema);
124
- }
125
- function Record(schema: Types.TRecord) {
126
- for (const [key, value] of globalThis.Object.entries(schema.patternProperties)) {
127
- const type = Visit(value);
128
- if (key === `^(0|[1-9][0-9]*)$`) {
129
- return UnsupportedType(schema);
130
- } else {
131
- return Type(schema, `y.record(y.string(), ${type})`);
132
- }
133
- }
134
- throw Error(`Unreachable`);
135
- }
136
- function Ref(schema: Types.TRef) {
137
- if (!reference_map.has(schema.$ref!)) return UnsupportedType(schema);
138
- return schema.$ref;
139
- }
140
- function This(schema: Types.TThis) {
141
- if (!reference_map.has(schema.$ref!)) return UnsupportedType(schema);
142
- recursive_set.add(schema.$ref);
143
- return schema.$ref;
144
- }
145
- function Tuple(schema: Types.TTuple) {
146
- if (schema.items === undefined) return `[]`;
147
- const items = schema.items.map((schema) => Visit(schema)).join(`, `);
148
- return Type(schema, `y.tuple([${items}])`);
149
- }
150
- function TemplateLiteral(schema: Types.TTemplateLiteral) {
151
- return UnsupportedType(schema);
152
- }
153
- function UInt8Array(schema: Types.TUint8Array): string {
154
- return UnsupportedType(schema);
155
- }
156
- function Undefined(schema: Types.TUndefined) {
157
- return UnsupportedType(schema);
158
- }
159
- function Union(schema: Types.TUnion) {
160
- return Type(schema, `y.union([${schema.anyOf.map((schema) => Visit(schema)).join(`, `)}])`);
161
- }
162
- function Unknown(schema: Types.TUnknown) {
163
- return UnsupportedType(schema);
164
- }
165
- function Void(schema: Types.TVoid) {
166
- return UnsupportedType(schema);
167
- }
168
- function UnsupportedType(schema: Types.TSchema) {
169
- return `${Type(schema, `y.any( /* unsupported */)`)}`;
170
- }
171
- function Visit(schema: Types.TSchema): string {
172
- if (schema.$id !== undefined) reference_map.set(schema.$id, schema);
173
- if (schema.$id !== undefined && emitted_set.has(schema.$id!)) return schema.$id!;
174
- if (Types.TypeGuard.IsAny(schema)) return Any(schema);
175
- if (Types.TypeGuard.IsArray(schema)) return Array(schema);
176
- if (Types.TypeGuard.IsBigInt(schema)) return BigInt(schema);
177
- if (Types.TypeGuard.IsBoolean(schema)) return Boolean(schema);
178
- if (Types.TypeGuard.IsDate(schema)) return Date(schema);
179
- if (Types.TypeGuard.IsConstructor(schema)) return Constructor(schema);
180
- if (Types.TypeGuard.IsFunction(schema)) return Function(schema);
181
- if (Types.TypeGuard.IsInteger(schema)) return Integer(schema);
182
- if (Types.TypeGuard.IsIntersect(schema)) return Intersect(schema);
183
- if (Types.TypeGuard.IsLiteral(schema)) return Literal(schema);
184
- if (Types.TypeGuard.IsNever(schema)) return Never(schema);
185
- if (Types.TypeGuard.IsNull(schema)) return Null(schema);
186
- if (Types.TypeGuard.IsNumber(schema)) return Number(schema);
187
- if (Types.TypeGuard.IsObject(schema)) return Object(schema);
188
- if (Types.TypeGuard.IsPromise(schema)) return Promise(schema);
189
- if (Types.TypeGuard.IsRecord(schema)) return Record(schema);
190
- if (Types.TypeGuard.IsRef(schema)) return Ref(schema);
191
- if (Types.TypeGuard.IsString(schema)) return String(schema);
192
- if (Types.TypeGuard.IsTemplateLiteral(schema)) return TemplateLiteral(schema);
193
- if (Types.TypeGuard.IsThis(schema)) return This(schema);
194
- if (Types.TypeGuard.IsTuple(schema)) return Tuple(schema);
195
- if (Types.TypeGuard.IsUint8Array(schema)) return UInt8Array(schema);
196
- if (Types.TypeGuard.IsUndefined(schema)) return Undefined(schema);
197
- if (Types.TypeGuard.IsUnion(schema)) return Union(schema);
198
- if (Types.TypeGuard.IsUnknown(schema)) return Unknown(schema);
199
- if (Types.TypeGuard.IsVoid(schema)) return Void(schema);
200
- return UnsupportedType(schema);
201
- }
202
- function Collect(schema: Types.TSchema) {
203
- return [...Visit(schema)].join(``);
204
- }
205
- function GenerateType(model: TypeBoxModel, schema: Types.TSchema, references: Types.TSchema[]) {
206
- const output: string[] = [];
207
- for (const reference of references) {
208
- if (reference.$id === undefined) return UnsupportedType(schema);
209
- reference_map.set(reference.$id, reference);
210
- }
211
- const type = Collect(schema);
212
-
213
- output.push(`export type ${schema.$id} = InferYrel<typeof ${schema.$id}>`);
214
- output.push(`export const ${schema.$id || `T`} = ${Formatter.Format(type)}`);
215
-
216
- if (schema.$id) emitted_set.add(schema.$id);
217
- return output.join('\n');
218
- }
219
- const reference_map = new Map<string, Types.TSchema>();
220
- const recursive_set = new Set<string>();
221
- const emitted_set = new Set<string>();
222
- export function Generate(model: TypeBoxModel): string {
223
- reference_map.clear();
224
- recursive_set.clear();
225
- emitted_set.clear();
226
- const buffer: string[] = [`import { y, type InferYrel } from 'yrel'`, ''];
227
- for (const type of model.types.filter((type) => Types.TypeGuard.IsSchema(type))) {
228
- buffer.push(GenerateType(model, type, model.types));
229
- }
230
- return Formatter.Format(buffer.join('\n'));
231
- }
232
- }
@@ -1,226 +0,0 @@
1
- /*--------------------------------------------------------------------------
2
-
3
- @sinclair/typebox-codegen
4
-
5
- The MIT License (MIT)
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining a copy
8
- of this software and associated documentation files (the "Software"), to deal
9
- in the Software without restriction, including without limitation the rights
10
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- copies of the Software, and to permit persons to whom the Software is
12
- furnished to do so, subject to the following conditions:
13
-
14
- The above copyright notice and this permission notice shall be included in
15
- all copies or substantial portions of the Software.
16
-
17
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
- THE SOFTWARE.
24
-
25
- ---------------------------------------------------------------------------*/
26
- import * as Types from '@sinclair/typebox';
27
- import { Formatter, PropertyEncoder } from '../common/index';
28
- import { TypeBoxModel } from './model';
29
- import { ModelToTypeScript } from './model-to-typescript';
30
-
31
- // --------------------------------------------------------------------------
32
- // ModelToYup
33
- // --------------------------------------------------------------------------
34
- export namespace ModelToYup {
35
- function IsDefined<T = any>(value: unknown): value is T {
36
- return value !== undefined;
37
- }
38
- function Type(schema: Types.TSchema, type: string) {
39
- return schema.default === undefined ? type : `${type}.default(${JSON.stringify(schema.default)})`;
40
- }
41
- function Any(schema: Types.TAny) {
42
- return Type(schema, `y.mixed((value): value is any => true).required()`);
43
- }
44
- function Array(schema: Types.TArray) {
45
- const items = Visit(schema.items);
46
- const buffer: string[] = [];
47
- buffer.push(`y.array(${items})`);
48
- if (IsDefined<number>(schema.minItems)) buffer.push(`.min(${schema.minItems})`);
49
- if (IsDefined<number>(schema.maxItems)) buffer.push(`.max(${schema.maxItems})`);
50
- return Type(schema, buffer.join(``));
51
- }
52
- function BigInt(schema: Types.TBigInt) {
53
- return Type(schema, `y.mixed((value): value is bigint => typeof value === 'bigint')`);
54
- }
55
- function Boolean(schema: Types.TBoolean) {
56
- return Type(schema, `y.boolean().required()`);
57
- }
58
- function Date(schema: Types.TDate) {
59
- return Type(schema, `y.date().required()`);
60
- }
61
- function Constructor(schema: Types.TConstructor): string {
62
- return Type(schema, `y.mixed((value): value is Function => typeof value === 'function')`);
63
- }
64
- function Function(schema: Types.TFunction) {
65
- return Type(schema, `y.mixed((value): value is Function => typeof value === 'function')`);
66
- }
67
- function Integer(schema: Types.TInteger) {
68
- const buffer: string[] = [];
69
- buffer.push(`y.number().integer().required()`);
70
- if (IsDefined<number>(schema.minimum)) buffer.push(`.min(${schema.minimum})`);
71
- if (IsDefined<number>(schema.maximum)) buffer.push(`.max(${schema.maximum})`);
72
- if (IsDefined<number>(schema.exclusiveMaximum)) buffer.push(`.max(${schema.exclusiveMaximum - 1})`);
73
- if (IsDefined<number>(schema.exclusiveMinimum)) buffer.push(`.max(${schema.exclusiveMinimum + 1})`);
74
- return Type(schema, buffer.join(``));
75
- }
76
- function Intersect(schema: Types.TIntersect) {
77
- const mergable = schema.allOf.every((schema) => Types.TypeGuard.IsObject(schema));
78
- if (!mergable) return UnsupportedType(schema);
79
- const composite = Types.Type.Composite(schema.allOf as any);
80
- return Visit(composite);
81
- }
82
- function Literal(schema: Types.TLiteral) {
83
- return typeof schema.const === `string`
84
- ? Type(schema, `y.mixed((value): value is '${schema.const}' => value === '${schema.const}').required()`)
85
- : Type(schema, `y.mixed((value): value is ${schema.const} => value === ${schema.const}).required()`);
86
- }
87
- function Never(schema: Types.TNever) {
88
- return Type(schema, `y.mixed((value): value is never => false).required()`);
89
- }
90
- function Null(schema: Types.TNull) {
91
- return Type(schema, `(y.mixed((value): value is any => value === null).required() as y.MixedSchema<null>)`);
92
- }
93
- function String(schema: Types.TString) {
94
- const buffer: string[] = [];
95
- buffer.push(`y.string().required()`);
96
- if (IsDefined<number>(schema.maxLength)) buffer.push(`.max(${schema.maxLength})`);
97
- if (IsDefined<number>(schema.minLength)) buffer.push(`.min(${schema.minLength})`);
98
- return Type(schema, buffer.join(``));
99
- }
100
- function Number(schema: Types.TNumber) {
101
- const buffer: string[] = [];
102
- buffer.push(`y.number().required()`);
103
- if (IsDefined<number>(schema.minimum)) buffer.push(`.min(${schema.minimum})`);
104
- if (IsDefined<number>(schema.maximum)) buffer.push(`.max(${schema.maximum})`);
105
- if (IsDefined<number>(schema.exclusiveMaximum)) buffer.push(`.max(${schema.exclusiveMaximum - 1})`);
106
- if (IsDefined<number>(schema.exclusiveMinimum)) buffer.push(`.max(${schema.exclusiveMinimum + 1})`);
107
- if (IsDefined<number>(schema.multipleOf)) buffer.push(`.multipleOf(${schema.multipleOf})`);
108
- return Type(schema, buffer.join(``));
109
- }
110
- function Object(schema: Types.TObject) {
111
- // prettier-ignore
112
- const properties = globalThis.Object.entries(schema.properties).map(([key, value]) => {
113
- const optional = Types.TypeGuard.IsOptional(value)
114
- const property = PropertyEncoder.Encode(key)
115
- return optional ? `${property}: ${Visit(value)}.optional()` : `${property}: ${Visit(value)}`
116
- }).join(`,`)
117
- const buffer: string[] = [];
118
- buffer.push(`y.object({\n${properties}\n})`);
119
- if (schema.additionalProperties === false) buffer.push(`.strict()`);
120
- return Type(schema, buffer.join(``));
121
- }
122
- function Promise(schema: Types.TPromise) {
123
- return Type(schema, `y.mixed((value): value is Promise<unknown> => value instanceof Promise<unknown>)`);
124
- }
125
- function Record(schema: Types.TRecord) {
126
- return UnsupportedType(schema);
127
- }
128
- function Ref(schema: Types.TRef) {
129
- return `${schema.$ref}`;
130
- }
131
- function This(schema: Types.TThis) {
132
- return UnsupportedType(schema);
133
- }
134
- function Tuple(schema: Types.TTuple) {
135
- if (schema.items === undefined) return `[]`;
136
- const items = schema.items.map((schema) => `${Visit(schema)}.required()`).join(`, `);
137
- return Type(schema, `y.tuple([${items}]).required()`);
138
- }
139
- function TemplateLiteral(schema: Types.TTemplateLiteral) {
140
- return Type(schema, `y.string().matches(/${schema.pattern}/)`);
141
- }
142
- function UInt8Array(schema: Types.TUint8Array): string {
143
- return Type(schema, `y.mixed((value): value is Uint8Array => value instanceof Uint8Array).required()`);
144
- }
145
- function Undefined(schema: Types.TUndefined) {
146
- return Type(schema, `(y.mixed((value): value is any => value === undefined) as y.MixedSchema<undefined>)`);
147
- }
148
- function Union(schema: Types.TUnion) {
149
- return Type(schema, `y.mixed().oneOf([${schema.anyOf.map((schema) => Visit(schema)).join(`, `)}]).required()`);
150
- }
151
- function Unknown(schema: Types.TUnknown) {
152
- return Type(schema, `(y.mixed((value): value is any => true).required() as y.MixedSchema<unknown>)`);
153
- }
154
- function Void(schema: Types.TVoid) {
155
- return Type(schema, `(y.mixed((value): value is any => value === undefined) as y.MixedSchema<void>)`);
156
- }
157
- function UnsupportedType(schema: Types.TSchema) {
158
- return `${Type(schema, `y.mixed(/* unsupported */)`)}`;
159
- }
160
- function Visit(schema: Types.TSchema): string {
161
- if (schema.$id !== undefined) reference_map.set(schema.$id, schema);
162
- if (schema.$id !== undefined && emitted_set.has(schema.$id!)) return schema.$id!;
163
- if (Types.TypeGuard.IsAny(schema)) return Any(schema);
164
- if (Types.TypeGuard.IsArray(schema)) return Array(schema);
165
- if (Types.TypeGuard.IsBigInt(schema)) return BigInt(schema);
166
- if (Types.TypeGuard.IsBoolean(schema)) return Boolean(schema);
167
- if (Types.TypeGuard.IsDate(schema)) return Date(schema);
168
- if (Types.TypeGuard.IsConstructor(schema)) return Constructor(schema);
169
- if (Types.TypeGuard.IsFunction(schema)) return Function(schema);
170
- if (Types.TypeGuard.IsInteger(schema)) return Integer(schema);
171
- if (Types.TypeGuard.IsIntersect(schema)) return Intersect(schema);
172
- if (Types.TypeGuard.IsLiteral(schema)) return Literal(schema);
173
- if (Types.TypeGuard.IsNever(schema)) return Never(schema);
174
- if (Types.TypeGuard.IsNull(schema)) return Null(schema);
175
- if (Types.TypeGuard.IsNumber(schema)) return Number(schema);
176
- if (Types.TypeGuard.IsObject(schema)) return Object(schema);
177
- if (Types.TypeGuard.IsPromise(schema)) return Promise(schema);
178
- if (Types.TypeGuard.IsRecord(schema)) return Record(schema);
179
- if (Types.TypeGuard.IsRef(schema)) return Ref(schema);
180
- if (Types.TypeGuard.IsString(schema)) return String(schema);
181
- if (Types.TypeGuard.IsTemplateLiteral(schema)) return TemplateLiteral(schema);
182
- if (Types.TypeGuard.IsThis(schema)) return This(schema);
183
- if (Types.TypeGuard.IsTuple(schema)) return Tuple(schema);
184
- if (Types.TypeGuard.IsUint8Array(schema)) return UInt8Array(schema);
185
- if (Types.TypeGuard.IsUndefined(schema)) return Undefined(schema);
186
- if (Types.TypeGuard.IsUnion(schema)) return Union(schema);
187
- if (Types.TypeGuard.IsUnknown(schema)) return Unknown(schema);
188
- if (Types.TypeGuard.IsVoid(schema)) return Void(schema);
189
- return UnsupportedType(schema);
190
- }
191
- function Collect(schema: Types.TSchema) {
192
- return [...Visit(schema)].join(``);
193
- }
194
- function GenerateType(model: TypeBoxModel, schema: Types.TSchema, references: Types.TSchema[]) {
195
- const output: string[] = [];
196
- for (const reference of references) {
197
- if (reference.$id === undefined) return UnsupportedType(schema);
198
- reference_map.set(reference.$id, reference);
199
- }
200
- const type = Collect(schema);
201
- if (recursive_set.has(schema.$id!)) {
202
- output.push(`export ${ModelToTypeScript.GenerateType(model, schema.$id!)}`);
203
- output.push(
204
- `export const ${schema.$id || `T`}: y.InferType<${schema.$id}> = z.lazy(() => ${Formatter.Format(type)})`,
205
- );
206
- } else {
207
- output.push(`export type ${schema.$id} = y.InferType<typeof ${schema.$id}>`);
208
- output.push(`export const ${schema.$id || `T`} = ${Formatter.Format(type)}`);
209
- }
210
- if (schema.$id) emitted_set.add(schema.$id);
211
- return output.join('\n');
212
- }
213
- const reference_map = new Map<string, Types.TSchema>();
214
- const recursive_set = new Set<string>();
215
- const emitted_set = new Set<string>();
216
- export function Generate(model: TypeBoxModel): string {
217
- reference_map.clear();
218
- recursive_set.clear();
219
- emitted_set.clear();
220
- const buffer: string[] = [`import y from 'yup'`, ''];
221
- for (const type of model.types.filter((type) => Types.TypeGuard.IsSchema(type))) {
222
- buffer.push(GenerateType(model, type, model.types));
223
- }
224
- return Formatter.Format(buffer.join('\n'));
225
- }
226
- }