@wener/utils 1.1.47 → 1.1.49

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 (194) hide show
  1. package/README.md +9 -1
  2. package/lib/asyncs/Promises.js +54 -0
  3. package/lib/asyncs/Promises.js.map +1 -0
  4. package/lib/asyncs/createLazyPromise.js +52 -51
  5. package/lib/asyncs/createLazyPromise.js.map +1 -1
  6. package/lib/asyncs/firstOfAsyncIterator.js.map +1 -1
  7. package/lib/asyncs/isPromise.js +2 -3
  8. package/lib/asyncs/isPromise.js.map +1 -1
  9. package/lib/asyncs/nextOfAsyncIterator.js.map +1 -1
  10. package/lib/asyncs/timeout.js +4 -4
  11. package/lib/asyncs/timeout.js.map +1 -1
  12. package/lib/cn/division/DivisionCode.js +30 -17
  13. package/lib/cn/division/DivisionCode.js.map +1 -1
  14. package/lib/cn/id/{Mod11Checksum.js → Mod11.js} +3 -6
  15. package/lib/cn/id/Mod11.js.map +1 -0
  16. package/lib/cn/id/ResidentIdNumber.js +10 -12
  17. package/lib/cn/id/ResidentIdNumber.js.map +1 -1
  18. package/lib/cn/types.js +3 -0
  19. package/lib/cn/types.js.map +1 -0
  20. package/lib/cn/uscc/{Mod31Checksum.js → Mod31.js} +3 -6
  21. package/lib/cn/uscc/Mod31.js.map +1 -0
  22. package/lib/cn/uscc/USCC.js +6 -9
  23. package/lib/cn/uscc/USCC.js.map +1 -1
  24. package/lib/cn/uscc/isUSCC.js +1 -1
  25. package/lib/cn/uscc/isUSCC.js.map +1 -1
  26. package/lib/crypto/getNodeCrypto.js +14 -10
  27. package/lib/crypto/getNodeCrypto.js.map +1 -1
  28. package/lib/crypto/getRandomValues.js +5 -1
  29. package/lib/crypto/getRandomValues.js.map +1 -1
  30. package/lib/emitter/types.js +3 -0
  31. package/lib/emitter/types.js.map +1 -0
  32. package/lib/fetch/createFetchWith.js.map +1 -1
  33. package/lib/fetch/createFetchWithRetry.js.map +1 -1
  34. package/lib/fetch/http.types.js +4 -0
  35. package/lib/fetch/http.types.js.map +1 -0
  36. package/lib/index.js +6 -3
  37. package/lib/index.js.map +1 -1
  38. package/lib/io/ArrayBuffers.js.map +1 -1
  39. package/lib/io/ByteBuffer.js +235 -36
  40. package/lib/io/ByteBuffer.js.map +1 -1
  41. package/lib/langs/getGlobalStates.js +17 -0
  42. package/lib/langs/getGlobalStates.js.map +1 -0
  43. package/lib/langs/getObjectId.js +18 -0
  44. package/lib/langs/getObjectId.js.map +1 -0
  45. package/lib/langs/isNullish.js +5 -0
  46. package/lib/langs/isNullish.js.map +1 -0
  47. package/lib/logging/slog.js.map +1 -1
  48. package/lib/maths/clamp.js +7 -6
  49. package/lib/maths/clamp.js.map +1 -1
  50. package/lib/maths/random.js.map +1 -1
  51. package/lib/mitt/index.js +77 -0
  52. package/lib/mitt/index.js.map +1 -0
  53. package/lib/objects/computeIfAbsent.js +1 -1
  54. package/lib/objects/computeIfAbsent.js.map +1 -1
  55. package/lib/schema/typebox/typebox.js.map +1 -1
  56. package/lib/server/fetch/createFetchWithProxyByUndici.js.map +1 -1
  57. package/package.json +6 -14
  58. package/src/asyncs/Promises.ts +62 -0
  59. package/src/asyncs/createLazyPromise.test.ts +52 -13
  60. package/src/asyncs/createLazyPromise.ts +66 -61
  61. package/src/asyncs/firstOfAsyncIterator.ts +1 -1
  62. package/src/asyncs/isPromise.ts +3 -3
  63. package/src/asyncs/nextOfAsyncIterator.ts +1 -1
  64. package/src/asyncs/timeout.ts +4 -4
  65. package/src/cn/division/DivisionCode.ts +61 -8
  66. package/src/cn/division/division.test.ts +6 -0
  67. package/src/cn/id/{Mod11Checksum.ts → Mod11.ts} +3 -6
  68. package/src/cn/id/ResidentIdNumber.ts +11 -13
  69. package/src/cn/id/id.test.ts +5 -5
  70. package/src/cn/scripts/gen.test.ts +2 -1
  71. package/src/cn/types.ts +11 -0
  72. package/src/cn/uscc/{Mod31Checksum.ts → Mod31.ts} +3 -7
  73. package/src/cn/uscc/USCC.ts +7 -11
  74. package/src/cn/uscc/isUSCC.ts +1 -1
  75. package/src/cn/uscc/uscc.test.ts +4 -4
  76. package/src/crypto/getNodeCrypto.ts +16 -11
  77. package/src/crypto/getRandomValues.ts +6 -1
  78. package/src/emitter/types.ts +18 -0
  79. package/src/fetch/createFetchWith.ts +1 -1
  80. package/src/fetch/createFetchWithRetry.ts +1 -1
  81. package/src/fetch/http.types.ts +46 -0
  82. package/src/index.ts +7 -3
  83. package/src/io/ArrayBuffers.ts +7 -3
  84. package/src/io/ByteBuffer.test.ts +33 -6
  85. package/src/io/ByteBuffer.ts +272 -41
  86. package/src/langs/getGlobalStates.ts +21 -0
  87. package/src/langs/getObjectId.ts +20 -0
  88. package/src/langs/isNullish.ts +3 -0
  89. package/src/langs/mixin.test.ts +43 -0
  90. package/src/logging/slog.ts +1 -1
  91. package/src/maths/clamp.test.ts +5 -1
  92. package/src/maths/clamp.ts +7 -7
  93. package/src/maths/random.ts +1 -1
  94. package/src/mitt/README.md +1 -0
  95. package/src/mitt/index.ts +114 -0
  96. package/src/objects/computeIfAbsent.ts +3 -2
  97. package/src/objects/merge/merge.test.ts +1 -1
  98. package/src/schema/typebox/typebox.ts +1 -1
  99. package/src/server/fetch/createFetchWithProxyByUndici.ts +2 -2
  100. package/tsconfig.json +4 -1
  101. package/lib/asyncs/sleep.js +0 -3
  102. package/lib/asyncs/sleep.js.map +0 -1
  103. package/lib/cn/id/Mod11Checksum.js.map +0 -1
  104. package/lib/cn/uscc/Mod31Checksum.js.map +0 -1
  105. package/lib/schema/typebox/gen/codegen/common/encoder.js +0 -94
  106. package/lib/schema/typebox/gen/codegen/common/encoder.js.map +0 -1
  107. package/lib/schema/typebox/gen/codegen/common/formatter.js +0 -33
  108. package/lib/schema/typebox/gen/codegen/common/formatter.js.map +0 -1
  109. package/lib/schema/typebox/gen/codegen/common/index.js +0 -29
  110. package/lib/schema/typebox/gen/codegen/common/index.js.map +0 -1
  111. package/lib/schema/typebox/gen/codegen/common/jsdoc.js +0 -117
  112. package/lib/schema/typebox/gen/codegen/common/jsdoc.js.map +0 -1
  113. package/lib/schema/typebox/gen/codegen/expression/compiler.js +0 -364
  114. package/lib/schema/typebox/gen/codegen/expression/compiler.js.map +0 -1
  115. package/lib/schema/typebox/gen/codegen/expression/errors.js +0 -259
  116. package/lib/schema/typebox/gen/codegen/expression/errors.js.map +0 -1
  117. package/lib/schema/typebox/gen/codegen/expression/evaluator.js +0 -254
  118. package/lib/schema/typebox/gen/codegen/expression/evaluator.js.map +0 -1
  119. package/lib/schema/typebox/gen/codegen/expression/expression.js +0 -381
  120. package/lib/schema/typebox/gen/codegen/expression/expression.js.map +0 -1
  121. package/lib/schema/typebox/gen/codegen/expression/index.js +0 -32
  122. package/lib/schema/typebox/gen/codegen/expression/index.js.map +0 -1
  123. package/lib/schema/typebox/gen/codegen/index.js +0 -29
  124. package/lib/schema/typebox/gen/codegen/index.js.map +0 -1
  125. package/lib/schema/typebox/gen/codegen/model/index.js +0 -40
  126. package/lib/schema/typebox/gen/codegen/model/index.js.map +0 -1
  127. package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js +0 -260
  128. package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js.map +0 -1
  129. package/lib/schema/typebox/gen/codegen/model/model-to-expression.js +0 -383
  130. package/lib/schema/typebox/gen/codegen/model/model-to-expression.js.map +0 -1
  131. package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js +0 -238
  132. package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js.map +0 -1
  133. package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js +0 -274
  134. package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js.map +0 -1
  135. package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js +0 -47
  136. package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js.map +0 -1
  137. package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js +0 -192
  138. package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js.map +0 -1
  139. package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js +0 -33
  140. package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js.map +0 -1
  141. package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js +0 -188
  142. package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js.map +0 -1
  143. package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js +0 -239
  144. package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js.map +0 -1
  145. package/lib/schema/typebox/gen/codegen/model/model-to-value.js +0 -43
  146. package/lib/schema/typebox/gen/codegen/model/model-to-value.js.map +0 -1
  147. package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js +0 -227
  148. package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js.map +0 -1
  149. package/lib/schema/typebox/gen/codegen/model/model-to-yup.js +0 -225
  150. package/lib/schema/typebox/gen/codegen/model/model-to-yup.js.map +0 -1
  151. package/lib/schema/typebox/gen/codegen/model/model-to-zod.js +0 -248
  152. package/lib/schema/typebox/gen/codegen/model/model-to-zod.js.map +0 -1
  153. package/lib/schema/typebox/gen/codegen/model/model.js +0 -27
  154. package/lib/schema/typebox/gen/codegen/model/model.js.map +0 -1
  155. package/lib/schema/typebox/gen/codegen/typescript/index.js +0 -28
  156. package/lib/schema/typebox/gen/codegen/typescript/index.js.map +0 -1
  157. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js +0 -72
  158. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js.map +0 -1
  159. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js +0 -620
  160. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js.map +0 -1
  161. package/lib/schema/typebox/gen/index.js +0 -3
  162. package/lib/schema/typebox/gen/index.js.map +0 -1
  163. package/src/asyncs/sleep.ts +0 -1
  164. package/src/schema/typebox/gen/codegen/common/encoder.ts +0 -99
  165. package/src/schema/typebox/gen/codegen/common/formatter.ts +0 -31
  166. package/src/schema/typebox/gen/codegen/common/index.ts +0 -29
  167. package/src/schema/typebox/gen/codegen/common/jsdoc.ts +0 -93
  168. package/src/schema/typebox/gen/codegen/expression/compiler.ts +0 -377
  169. package/src/schema/typebox/gen/codegen/expression/errors.ts +0 -302
  170. package/src/schema/typebox/gen/codegen/expression/evaluator.ts +0 -268
  171. package/src/schema/typebox/gen/codegen/expression/expression.ts +0 -538
  172. package/src/schema/typebox/gen/codegen/expression/index.ts +0 -32
  173. package/src/schema/typebox/gen/codegen/index.ts +0 -29
  174. package/src/schema/typebox/gen/codegen/model/index.ts +0 -40
  175. package/src/schema/typebox/gen/codegen/model/model-to-arktype.ts +0 -266
  176. package/src/schema/typebox/gen/codegen/model/model-to-expression.ts +0 -378
  177. package/src/schema/typebox/gen/codegen/model/model-to-grpc.ts +0 -244
  178. package/src/schema/typebox/gen/codegen/model/model-to-io-ts.ts +0 -294
  179. package/src/schema/typebox/gen/codegen/model/model-to-javascript.ts +0 -42
  180. package/src/schema/typebox/gen/codegen/model/model-to-json-schema.ts +0 -166
  181. package/src/schema/typebox/gen/codegen/model/model-to-typebox.ts +0 -32
  182. package/src/schema/typebox/gen/codegen/model/model-to-typescript.ts +0 -189
  183. package/src/schema/typebox/gen/codegen/model/model-to-valibot.ts +0 -236
  184. package/src/schema/typebox/gen/codegen/model/model-to-value.ts +0 -42
  185. package/src/schema/typebox/gen/codegen/model/model-to-yrel.ts +0 -232
  186. package/src/schema/typebox/gen/codegen/model/model-to-yup.ts +0 -226
  187. package/src/schema/typebox/gen/codegen/model/model-to-zod.ts +0 -251
  188. package/src/schema/typebox/gen/codegen/model/model.ts +0 -33
  189. package/src/schema/typebox/gen/codegen/typescript/index.ts +0 -28
  190. package/src/schema/typebox/gen/codegen/typescript/typescript-to-model.ts +0 -61
  191. package/src/schema/typebox/gen/codegen/typescript/typescript-to-typebox.ts +0 -647
  192. package/src/schema/typebox/gen/gen.test.ts +0 -12
  193. package/src/schema/typebox/gen/index.ts +0 -1
  194. /package/src/schema/{typebox/gen/README.md → README.md} +0 -0
@@ -1,244 +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
- // ModelToProtoBuf
32
- // --------------------------------------------------------------------------
33
- export namespace ModelToGRPC {
34
- function ResolveTypeName(schema: Types.TSchema) {
35
- if (Types.TypeGuard.IsObject(schema) && schema.$id === undefined) return UnsupportedType(schema);
36
- if (schema.$id) return schema.$id;
37
- return Collect(schema);
38
- }
39
- function Any(schema: Types.TAny) {
40
- return UnsupportedType(schema);
41
- }
42
- function Array(schema: Types.TArray) {
43
- return `repeated ${ResolveTypeName(schema.items)}`;
44
- }
45
- function BigInt(schema: Types.TBigInt) {
46
- return UnsupportedType(schema);
47
- }
48
- function Boolean(schema: Types.TBoolean) {
49
- return 'bool';
50
- }
51
- function Date(schema: Types.TDate) {
52
- return UnsupportedType(schema);
53
- }
54
- function Constructor(schema: Types.TConstructor): string {
55
- return UnsupportedType(schema);
56
- }
57
- function Function(schema: Types.TFunction) {
58
- const parameters = schema.parameters.map((schema) => schema.$id);
59
- const returns = schema.returns.$id;
60
- return `(${parameters[0]}) returns (${returns}) {}`;
61
- }
62
- function Integer(schema: Types.TInteger) {
63
- return UnsupportedType(schema);
64
- }
65
- function Intersect(schema: Types.TIntersect) {
66
- const all = schema.allOf.every((schema) => Types.TypeGuard.IsObject(schema));
67
- return all ? Object({ ...Types.Type.Composite(schema.allOf as any), $id: schema.$id }) : '';
68
- }
69
- function Literal(schema: Types.TLiteral) {
70
- return UnsupportedType(schema);
71
- }
72
- function Never(schema: Types.TNever) {
73
- return UnsupportedType(schema);
74
- }
75
- function Null(schema: Types.TNull) {
76
- return UnsupportedType(schema);
77
- }
78
- function String(schema: Types.TString) {
79
- return 'string';
80
- }
81
- function Number(schema: Types.TNumber) {
82
- const id = schema.$id;
83
- // prettier-ignore
84
- return (
85
- id === 'double' ? 'double' :
86
- id === 'float' ? 'float' :
87
- id === 'int32' ? 'int32' :
88
- id === 'int64' ? 'int64' :
89
- id === 'uint32' ? 'uint32' :
90
- id === 'uint64' ? 'uint64' :
91
- id === 'sint32' ? 'sint32' :
92
- id === 'sint64' ? 'sint64' :
93
- id === 'fixed32' ? 'fixed32' :
94
- id === 'sfixed32' ? 'sfixed32' :
95
- id === 'sfixed64' ? 'sfixed64' :
96
- 'double'
97
- )
98
- }
99
- function Object(schema: Types.TObject) {
100
- const isService = globalThis.Object.values(schema.properties).some((schema) => Types.TypeGuard.IsFunction(schema));
101
- // prettier-ignore
102
- const properties = globalThis.Object.entries(schema.properties).map(([key, property], index) => {
103
- const propertyKey = PropertyEncoder.Encode(key)
104
- const typeName = ResolveTypeName(property)
105
- if(property.$id) return ` ${property.$id} ${propertyKey} = ${index}`
106
- return (Types.TypeGuard.IsFunction(property))
107
- ? ` rpc ${propertyKey} ${typeName}`
108
- : ` ${typeName} ${propertyKey} = ${index}`
109
- }).join(`\n`)
110
- if (isService) {
111
- return `service ${schema.$id!} {\n${properties}\n}`;
112
- } else {
113
- return `message ${schema.$id!} {\n${properties}\n}`;
114
- }
115
- }
116
- function Promise(schema: Types.TPromise) {
117
- return UnsupportedType(schema);
118
- }
119
- function Record(schema: Types.TRecord) {
120
- return UnsupportedType(schema);
121
- }
122
- function Ref(schema: Types.TRef) {
123
- if (!reference_map.has(schema.$ref!)) return UnsupportedType(schema);
124
- return schema.$ref;
125
- }
126
- function This(schema: Types.TThis) {
127
- return UnsupportedType(schema);
128
- }
129
- function Tuple(schema: Types.TTuple) {
130
- return UnsupportedType(schema);
131
- }
132
- function TemplateLiteral(schema: Types.TTemplateLiteral) {
133
- return UnsupportedType(schema);
134
- }
135
- function UInt8Array(schema: Types.TUint8Array): string {
136
- return 'bytes';
137
- }
138
- function Undefined(schema: Types.TUndefined) {
139
- return UnsupportedType(schema);
140
- }
141
- function Union(schema: Types.TUnion) {
142
- return UnsupportedType(schema);
143
- }
144
- function Unknown(schema: Types.TUnknown) {
145
- return schema.$id!;
146
- }
147
- function Void(schema: Types.TVoid) {
148
- return `void`;
149
- }
150
- function UnsupportedType(schema: Types.TSchema) {
151
- return `unsupported`;
152
- }
153
- function Visit(schema: Types.TSchema): string {
154
- if (schema.$id !== undefined) reference_map.set(schema.$id, schema);
155
- if (schema.$id !== undefined && emitted_set.has(schema.$id!)) return schema.$id!;
156
- if (Types.TypeGuard.IsAny(schema)) return Any(schema);
157
- if (Types.TypeGuard.IsArray(schema)) return Array(schema);
158
- if (Types.TypeGuard.IsBigInt(schema)) return BigInt(schema);
159
- if (Types.TypeGuard.IsBoolean(schema)) return Boolean(schema);
160
- if (Types.TypeGuard.IsDate(schema)) return Date(schema);
161
- if (Types.TypeGuard.IsConstructor(schema)) return Constructor(schema);
162
- if (Types.TypeGuard.IsFunction(schema)) return Function(schema);
163
- if (Types.TypeGuard.IsInteger(schema)) return Integer(schema);
164
- if (Types.TypeGuard.IsIntersect(schema)) return Intersect(schema);
165
- if (Types.TypeGuard.IsLiteral(schema)) return Literal(schema);
166
- if (Types.TypeGuard.IsNever(schema)) return Never(schema);
167
- if (Types.TypeGuard.IsNull(schema)) return Null(schema);
168
- if (Types.TypeGuard.IsNumber(schema)) return Number(schema);
169
- if (Types.TypeGuard.IsObject(schema)) return Object(schema);
170
- if (Types.TypeGuard.IsPromise(schema)) return Promise(schema);
171
- if (Types.TypeGuard.IsRecord(schema)) return Record(schema);
172
- if (Types.TypeGuard.IsRef(schema)) return Ref(schema);
173
- if (Types.TypeGuard.IsString(schema)) return String(schema);
174
- if (Types.TypeGuard.IsTemplateLiteral(schema)) return TemplateLiteral(schema);
175
- if (Types.TypeGuard.IsThis(schema)) return This(schema);
176
- if (Types.TypeGuard.IsTuple(schema)) return Tuple(schema);
177
- if (Types.TypeGuard.IsUint8Array(schema)) return UInt8Array(schema);
178
- if (Types.TypeGuard.IsUndefined(schema)) return Undefined(schema);
179
- if (Types.TypeGuard.IsUnion(schema)) return Union(schema);
180
- if (Types.TypeGuard.IsUnknown(schema)) return Unknown(schema);
181
- if (Types.TypeGuard.IsVoid(schema)) return Void(schema);
182
- return UnsupportedType(schema);
183
- }
184
- function Collect(schema: Types.TSchema) {
185
- return [...Visit(schema)].join(``);
186
- }
187
- function GenerateType(model: TypeBoxModel, schema: Types.TSchema, references: Types.TSchema[]) {
188
- for (const reference of references) {
189
- if (reference.$id === undefined) return UnsupportedType(schema);
190
- reference_map.set(reference.$id, reference);
191
- }
192
- return Collect(schema);
193
- }
194
- function NativeTypes(model: TypeBoxModel) {
195
- const exists = model.types.some((schema) => schema.$id === 'double');
196
- if (exists) return '';
197
- return [
198
- '/*',
199
- 'IDL Starter:',
200
- '',
201
- 'type double = number',
202
- 'type float = number',
203
- 'type int32 = number',
204
- 'type int64 = number',
205
- 'type uint32 = number',
206
- 'type uint64 = number',
207
- 'type sint32 = number',
208
- 'type sint64 = number',
209
- 'type fixed32 = number',
210
- 'type fixed64 = number',
211
- 'type sfixed32 = number',
212
- 'type sfixed64 = number',
213
- 'type bool = boolean',
214
- 'type bytes = Uint8Array',
215
- '',
216
- 'type Vector = { x: float, y: float, z: float }',
217
- '',
218
- 'type Oprand = { a: Vector, b: Vector }',
219
- '',
220
- 'interface Service {',
221
- ' add(op: Oprand): Vector',
222
- ' sub(op: Oprand): Vector',
223
- ' mul(op: Oprand): Vector',
224
- ' div(op: Oprand): Vector',
225
- '}',
226
- '*/',
227
- ].join('\n');
228
- }
229
- const reference_map = new Map<string, Types.TSchema>();
230
- const recursive_set = new Set<string>();
231
- const emitted_set = new Set<string>();
232
- export function Generate(model: TypeBoxModel): string {
233
- reference_map.clear();
234
- recursive_set.clear();
235
- emitted_set.clear();
236
- const buffer: string[] = [NativeTypes(model), '', 'const IDL = (`', 'syntax = "proto3";', ''];
237
- for (const type of model.types) {
238
- if (!(Types.TypeGuard.IsObject(type) || Types.TypeGuard.IsIntersect(type))) continue;
239
- buffer.push(GenerateType(model, type, model.types));
240
- }
241
- buffer.push('`)');
242
- return Formatter.Format(buffer.join('\n'));
243
- }
244
- }
@@ -1,294 +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
- // ModelToIoTs
33
- // --------------------------------------------------------------------------
34
- export namespace ModelToIoTs {
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 type;
40
- }
41
- function Any(schema: Types.TAny) {
42
- return Type(schema, `t.any`);
43
- }
44
- function Array(schema: Types.TArray) {
45
- const items = Visit(schema.items);
46
- const type = `t.array(${items})`;
47
- const refinements: string[] = [];
48
- if (IsDefined<number>(schema.minItems)) refinements.push(`value.length >= ${schema.minItems}`);
49
- if (IsDefined<number>(schema.maxItems)) refinements.push(`value.length <= ${schema.maxItems}`);
50
- return refinements.length === 0 ? type : `t.refinement(${type}, value => ${refinements.join('&&')})`;
51
- }
52
- function BigInt(schema: Types.TBigInt) {
53
- return `t.bigint`;
54
- }
55
- function Boolean(schema: Types.TBoolean) {
56
- return `t.boolean`;
57
- }
58
- const support_types = new Map<string, string>();
59
- function Date(schema: Types.TDate) {
60
- support_types.set(
61
- 'Date',
62
- ` const t_Date = new t.Type<Date, Date, unknown>(
63
- 'Date',
64
- (value: unknown): value is Date => value instanceof Date,
65
- (value, context) => (value instanceof Date ? t.success(value) : t.failure(value, context)),
66
- t.identity
67
- )`,
68
- );
69
- return `t_Date`;
70
- }
71
- function Constructor(schema: Types.TConstructor): string {
72
- support_types.set(
73
- 'Constructor',
74
- `const t_Constructor = new t.Type<Function, Function, unknown>(
75
- 'Constructor',
76
- (value: unknown): value is Function => typeof value === 'function',
77
- (value, context) => (typeof value === 'function' ? t.success(value) : t.failure(value, context)),
78
- t.identity
79
- )`,
80
- );
81
- return `t_Constructor`;
82
- }
83
- function Function(schema: Types.TFunction) {
84
- support_types.set(
85
- 'Function',
86
- `const t_Function = new t.Type<Function, Function, unknown>(
87
- 'Function',
88
- (value: unknown): value is Function => typeof value === 'function',
89
- (value, context) => (typeof value === 'function' ? t.success(value) : t.failure(value, context)),
90
- t.identity
91
- )`,
92
- );
93
- return `t_Function`;
94
- }
95
- function Integer(schema: Types.TInteger) {
96
- const type = `t.number`;
97
- const refinements: string[] = [];
98
- refinements.push(`value => Number.isInteger(value)`);
99
- if (IsDefined<number>(schema.minimum)) refinements.push(`value >= ${schema.minimum}`);
100
- if (IsDefined<number>(schema.maximum)) refinements.push(`value <= ${schema.maximum}`);
101
- if (IsDefined<number>(schema.exclusiveMaximum)) refinements.push(`value > ${schema.exclusiveMinimum}`);
102
- if (IsDefined<number>(schema.exclusiveMinimum)) refinements.push(`value < ${schema.exclusiveMaximum}`);
103
- if (IsDefined<number>(schema.multipleOf)) refinements.push(`value % ${schema.multipleOf} === 0`);
104
- return `t.refinement(${type}, value => ${refinements.join('&&')})`;
105
- }
106
- function Intersect(schema: Types.TIntersect) {
107
- const types = schema.allOf.map((type) => Visit(type));
108
- return `t.intersection([${types.join(', ')}])`;
109
- }
110
- function Literal(schema: Types.TLiteral) {
111
- return typeof schema.const === `string`
112
- ? Type(schema, `t.literal('${schema.const}')`)
113
- : Type(schema, `t.literal(${schema.const})`);
114
- }
115
- function Never(schema: Types.TNever) {
116
- return Type(schema, `t.never`);
117
- }
118
- function Null(schema: Types.TNull) {
119
- return Type(schema, `t.null`);
120
- }
121
- function String(schema: Types.TString) {
122
- const type = `t.string`;
123
- const refinements: string[] = [];
124
- if (IsDefined<number>(schema.minLength)) refinements.push(`value.length >= ${schema.minLength}`);
125
- if (IsDefined<number>(schema.maxLength)) refinements.push(`value.length <= ${schema.maxLength}`);
126
- return refinements.length === 0 ? type : `t.refinement(${type}, value => ${refinements.join('&&')})`;
127
- }
128
- function Number(schema: Types.TNumber) {
129
- const type = `t.number`;
130
- const refinements: string[] = [];
131
- if (IsDefined<number>(schema.minimum)) refinements.push(`value >= ${schema.minimum}`);
132
- if (IsDefined<number>(schema.maximum)) refinements.push(`value <= ${schema.maximum}`);
133
- if (IsDefined<number>(schema.exclusiveMaximum)) refinements.push(`value > ${schema.exclusiveMinimum}`);
134
- if (IsDefined<number>(schema.exclusiveMinimum)) refinements.push(`value < ${schema.exclusiveMaximum}`);
135
- if (IsDefined<number>(schema.multipleOf)) refinements.push(`value % ${schema.multipleOf} === 0`);
136
- return refinements.length === 0 ? type : `t.refinement(${type}, value => ${refinements.join('&&')})`;
137
- }
138
- function Object(schema: Types.TObject) {
139
- // prettier-ignore
140
- const properties = globalThis.Object.entries(schema.properties).map(([key, value]) => {
141
- const optional = Types.TypeGuard.IsOptional(value)
142
- const readonly = Types.TypeGuard.IsReadonly(value)
143
- const property = PropertyEncoder.Encode(key)
144
- const resolved = optional ? `t.union([t.undefined, ${Visit(value)}])` : Visit(value)
145
- return readonly ? `${property} : t.readonly(${resolved})` : `${property} : ${resolved}`
146
- }).join(`,`)
147
- const buffer: string[] = [];
148
- if (schema.additionalProperties === false) {
149
- buffer.push(`t.strict({\n${properties}\n})`);
150
- } else {
151
- buffer.push(`t.type({\n${properties}\n})`);
152
- }
153
- return Type(schema, buffer.join(``));
154
- }
155
- function Promise(schema: Types.TPromise) {
156
- support_types.set(
157
- 'Promise',
158
- `const t_Promise = new t.Type<Promise<any>, Promise<any>, unknown>(
159
- 'Promise',
160
- (value: unknown): value is Promise<unknown> => value instanceof Promise,
161
- (value, context) => (value instanceof Promise<unknown> ? t.success(value) : t.failure(value, context)),
162
- t.identity
163
- )`,
164
- );
165
- return `t_Promise`;
166
- }
167
- function Record(schema: Types.TRecord) {
168
- for (const [key, value] of globalThis.Object.entries(schema.patternProperties)) {
169
- const type = Visit(value);
170
- if (key === `^(0|[1-9][0-9]*)$`) {
171
- return `t.record(t.number, ${type})`;
172
- } else {
173
- return `t.record(t.string, ${type})`;
174
- }
175
- }
176
- throw Error(`Unreachable`);
177
- }
178
- function Ref(schema: Types.TRef) {
179
- if (!reference_map.has(schema.$ref!)) return UnsupportedType(schema); // throw new ModelToZodNonReferentialType(schema.$ref!)
180
- return schema.$ref;
181
- }
182
- function This(schema: Types.TThis) {
183
- if (!reference_map.has(schema.$ref!)) return UnsupportedType(schema); //throw new ModelToZodNonReferentialType(schema.$ref!)
184
- recursive_set.add(schema.$ref);
185
- return schema.$ref;
186
- }
187
- function Tuple(schema: Types.TTuple) {
188
- if (schema.items === undefined) return `[]`;
189
- const items = schema.items.map((schema) => Visit(schema)).join(`, `);
190
- return Type(schema, `t.tuple([${items}])`);
191
- }
192
- function TemplateLiteral(schema: Types.TTemplateLiteral) {
193
- return Type(schema, `t.refinement(t.string, value => /${schema.pattern}/.test(value))`);
194
- }
195
- function UInt8Array(schema: Types.TUint8Array): string {
196
- support_types.set(
197
- 'Uint8Array',
198
- `const t_Uint8Array = new t.Type<Uint8Array, Uint8Array, unknown>(
199
- 'Uint8Array',
200
- (value: unknown): value is Uint8Array => value instanceof Uint8Array,
201
- (value, context) => (value instanceof Uint8Array ? t.success(value) : t.failure(value, context)),
202
- t.identity
203
- )`,
204
- );
205
- return `t_Uint8Array`;
206
- }
207
- function Undefined(schema: Types.TUndefined) {
208
- return Type(schema, `t.undefined`);
209
- }
210
- function Union(schema: Types.TUnion) {
211
- return Type(schema, `t.union([${schema.anyOf.map((schema) => Visit(schema)).join(`, `)}])`);
212
- }
213
- function Unknown(schema: Types.TUnknown) {
214
- return Type(schema, `t.unknown`);
215
- }
216
- function Void(schema: Types.TVoid) {
217
- return Type(schema, `t.void`);
218
- }
219
- function UnsupportedType(schema: Types.TSchema) {
220
- return Type(schema, `t.any /* unresolved */`);
221
- }
222
- function Visit(schema: Types.TSchema): string {
223
- if (schema.$id !== undefined) reference_map.set(schema.$id, schema);
224
- if (schema.$id !== undefined && emitted_set.has(schema.$id!)) return schema.$id!;
225
- if (Types.TypeGuard.IsAny(schema)) return Any(schema);
226
- if (Types.TypeGuard.IsArray(schema)) return Array(schema);
227
- if (Types.TypeGuard.IsBigInt(schema)) return BigInt(schema);
228
- if (Types.TypeGuard.IsBoolean(schema)) return Boolean(schema);
229
- if (Types.TypeGuard.IsDate(schema)) return Date(schema);
230
- if (Types.TypeGuard.IsConstructor(schema)) return Constructor(schema);
231
- if (Types.TypeGuard.IsFunction(schema)) return Function(schema);
232
- if (Types.TypeGuard.IsInteger(schema)) return Integer(schema);
233
- if (Types.TypeGuard.IsIntersect(schema)) return Intersect(schema);
234
- if (Types.TypeGuard.IsLiteral(schema)) return Literal(schema);
235
- if (Types.TypeGuard.IsNever(schema)) return Never(schema);
236
- if (Types.TypeGuard.IsNull(schema)) return Null(schema);
237
- if (Types.TypeGuard.IsNumber(schema)) return Number(schema);
238
- if (Types.TypeGuard.IsObject(schema)) return Object(schema);
239
- if (Types.TypeGuard.IsPromise(schema)) return Promise(schema);
240
- if (Types.TypeGuard.IsRecord(schema)) return Record(schema);
241
- if (Types.TypeGuard.IsRef(schema)) return Ref(schema);
242
- if (Types.TypeGuard.IsString(schema)) return String(schema);
243
- if (Types.TypeGuard.IsTemplateLiteral(schema)) return TemplateLiteral(schema);
244
- if (Types.TypeGuard.IsThis(schema)) return This(schema);
245
- if (Types.TypeGuard.IsTuple(schema)) return Tuple(schema);
246
- if (Types.TypeGuard.IsUint8Array(schema)) return UInt8Array(schema);
247
- if (Types.TypeGuard.IsUndefined(schema)) return Undefined(schema);
248
- if (Types.TypeGuard.IsUnion(schema)) return Union(schema);
249
- if (Types.TypeGuard.IsUnknown(schema)) return Unknown(schema);
250
- if (Types.TypeGuard.IsVoid(schema)) return Void(schema);
251
- return UnsupportedType(schema);
252
- }
253
- function Collect(schema: Types.TSchema) {
254
- return [...Visit(schema)].join(``);
255
- }
256
- function GenerateType(model: TypeBoxModel, schema: Types.TSchema, references: Types.TSchema[]) {
257
- const output: string[] = [];
258
- for (const reference of references) {
259
- if (reference.$id === undefined) return UnsupportedType(schema);
260
- reference_map.set(reference.$id, reference);
261
- }
262
- const type = Collect(schema);
263
- if (recursive_set.has(schema.$id!)) {
264
- output.push(`export ${ModelToTypeScript.GenerateType(model, schema.$id!)}`);
265
- output.push(
266
- `export const ${schema.$id || `T`}: t.Type<${schema.$id}> = t.recursion('${
267
- schema.$id
268
- }', () => ${Formatter.Format(type)})`,
269
- );
270
- } else {
271
- output.push(`export type ${schema.$id} = t.TypeOf<typeof ${schema.$id}>`);
272
- output.push(`export const ${schema.$id || `T`} = ${Formatter.Format(type)}`);
273
- }
274
- if (schema.$id) emitted_set.add(schema.$id);
275
- return output.join('\n');
276
- }
277
- const reference_map = new Map<string, Types.TSchema>();
278
- const recursive_set = new Set<string>();
279
- const emitted_set = new Set<string>();
280
- export function Generate(model: TypeBoxModel): string {
281
- support_types.clear();
282
- reference_map.clear();
283
- recursive_set.clear();
284
- emitted_set.clear();
285
- const buffer: string[] = [`import t from 'io-ts'`, ''];
286
- const types = model.types
287
- .filter((type) => Types.TypeGuard.IsSchema(type))
288
- .map((type) => GenerateType(model, type, model.types));
289
- buffer.push(...support_types.values());
290
- buffer.push('\n');
291
- buffer.push(...types);
292
- return Formatter.Format(buffer.join('\n'));
293
- }
294
- }
@@ -1,42 +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 { TypeCompiler } from '@sinclair/typebox/compiler';
27
- import { Formatter } from '../common/formatter';
28
- import { TypeBoxModel } from './model';
29
-
30
- export namespace ModelToJavaScript {
31
- export function Generate(model: TypeBoxModel): string {
32
- const definitions: string[] = [];
33
- const header = `// @ts-nocheck`;
34
- for (const type of model.types) {
35
- definitions.push(`export const ${type.$id!} = (() => {
36
- ${TypeCompiler.Code(type, model.types, { language: 'javascript' })}
37
- })()`);
38
- }
39
- const output = [header, ...definitions];
40
- return output.join('\n\n');
41
- }
42
- }