@wener/utils 1.1.26 → 1.1.27
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.
- package/README.md +4 -0
- package/dist/cjs/createFetchWith-qhRObsE4.js +2 -0
- package/dist/cjs/createFetchWith-qhRObsE4.js.map +1 -0
- package/dist/cjs/{getGlobalThis-304f74e0.js → getGlobalThis-GhffAgiG.js} +1 -1
- package/dist/cjs/{getGlobalThis-304f74e0.js.map → getGlobalThis-GhffAgiG.js.map} +1 -1
- package/dist/cjs/index.cjs +11 -11
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/schema/typebox/gen/index.cjs +118 -0
- package/dist/cjs/schema/typebox/gen/index.cjs.map +1 -0
- package/dist/cjs/schema/typebox/index.cjs +2 -0
- package/dist/cjs/schema/typebox/index.cjs.map +1 -0
- package/dist/cjs/server.cjs +1 -1
- package/dist/cjs/server.cjs.map +1 -1
- package/dist/cjs/servers/jsdom.cjs +1 -1
- package/dist/cjs/servers/jsdom.cjs.map +1 -1
- package/dist/cjs/servers/node-fetch.cjs +1 -1
- package/dist/cjs/servers/ws.cjs +1 -1
- package/dist/esm/createFetchWith-1q1S-Xro.js +2 -0
- package/dist/esm/createFetchWith-1q1S-Xro.js.map +1 -0
- package/dist/esm/{getGlobalThis-b7ad0cf9.js → getGlobalThis-A8F-nU7J.js} +1 -1
- package/dist/esm/{getGlobalThis-b7ad0cf9.js.map → getGlobalThis-A8F-nU7J.js.map} +1 -1
- package/dist/esm/index.js +11 -11
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/schema/typebox/gen.js +118 -0
- package/dist/esm/schema/typebox/gen.js.map +1 -0
- package/dist/esm/schema/typebox.js +2 -0
- package/dist/esm/schema/typebox.js.map +1 -0
- package/dist/esm/server.js +1 -1
- package/dist/esm/server.js.map +1 -1
- package/dist/esm/servers/jsdom.js +1 -1
- package/dist/esm/servers/jsdom.js.map +1 -1
- package/dist/esm/servers/node-fetch.js +1 -1
- package/dist/esm/servers/ws.js +1 -1
- package/dist/system/createFetchWith-TjUle-dI.js +2 -0
- package/dist/system/createFetchWith-TjUle-dI.js.map +1 -0
- package/dist/system/{getGlobalThis-8951eb0e.js → getGlobalThis-X5ifUXDI.js} +1 -1
- package/dist/system/{getGlobalThis-8951eb0e.js.map → getGlobalThis-X5ifUXDI.js.map} +1 -1
- package/dist/system/index.js +11 -11
- package/dist/system/index.js.map +1 -1
- package/dist/system/schema/typebox/gen.js +118 -0
- package/dist/system/schema/typebox/gen.js.map +1 -0
- package/dist/system/schema/typebox.js +2 -0
- package/dist/system/schema/typebox.js.map +1 -0
- package/dist/system/server.js +1 -1
- package/dist/system/server.js.map +1 -1
- package/dist/system/servers/jsdom.js +1 -1
- package/dist/system/servers/jsdom.js.map +1 -1
- package/dist/system/servers/node-fetch.js +1 -1
- package/dist/system/servers/ws.js +1 -1
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/lib/langs/deepFreeze.js +16 -0
- package/lib/langs/deepFreeze.js.map +1 -0
- package/lib/maths/clamp.js +21 -0
- package/lib/maths/clamp.js.map +1 -0
- package/lib/objects/get.js.map +1 -1
- package/lib/objects/set.js.map +1 -1
- package/lib/schema/typebox/gen/codegen/common/encoder.js +74 -0
- package/lib/schema/typebox/gen/codegen/common/encoder.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/common/formatter.js +10 -0
- package/lib/schema/typebox/gen/codegen/common/formatter.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/common/jsdoc.js +68 -0
- package/lib/schema/typebox/gen/codegen/common/jsdoc.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/expression/compiler.js +325 -0
- package/lib/schema/typebox/gen/codegen/expression/compiler.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/expression/errors.js +237 -0
- package/lib/schema/typebox/gen/codegen/expression/errors.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/expression/evaluator.js +241 -0
- package/lib/schema/typebox/gen/codegen/expression/evaluator.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/expression/expression.js +200 -0
- package/lib/schema/typebox/gen/codegen/expression/expression.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/index.js +19 -0
- package/lib/schema/typebox/gen/codegen/index.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js +263 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-expression.js +363 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-expression.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js +245 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js +316 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js +20 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js +171 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js +10 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js +198 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js +252 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-value.js +19 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-value.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js +247 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-yup.js +249 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-yup.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-zod.js +272 -0
- package/lib/schema/typebox/gen/codegen/model/model-to-zod.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js +45 -0
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js.map +1 -0
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js +620 -0
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js.map +1 -0
- package/lib/schema/typebox/gen/index.js +3 -0
- package/lib/schema/typebox/gen/index.js.map +1 -0
- package/lib/schema/typebox/index.js +3 -0
- package/lib/schema/typebox/index.js.map +1 -0
- package/lib/schema/typebox/typebox.js +34 -0
- package/lib/schema/typebox/typebox.js.map +1 -0
- package/lib/servers/fetch/createFetchWithProxyByUndici.js +15 -0
- package/lib/servers/fetch/createFetchWithProxyByUndici.js.map +1 -1
- package/package.json +21 -6
- package/src/index.ts +3 -3
- package/src/langs/deepFreeze.ts +19 -0
- package/src/langs/hashCode.ignored.ts +125 -0
- package/src/libs/README.md +2 -0
- package/src/logging/slog.ts +121 -0
- package/src/maths/clamp.test.ts +19 -0
- package/src/maths/clamp.ts +23 -1
- package/src/objects/get.ts +14 -14
- package/src/objects/set.ts +5 -5
- package/src/schema/typebox/gen/codegen/common/encoder.ts +99 -0
- package/src/schema/typebox/gen/codegen/common/formatter.ts +31 -0
- package/src/schema/typebox/gen/codegen/common/index.ts +29 -0
- package/src/schema/typebox/gen/codegen/common/jsdoc.ts +93 -0
- package/src/schema/typebox/gen/codegen/expression/compiler.ts +377 -0
- package/src/schema/typebox/gen/codegen/expression/errors.ts +302 -0
- package/src/schema/typebox/gen/codegen/expression/evaluator.ts +268 -0
- package/src/schema/typebox/gen/codegen/expression/expression.ts +538 -0
- package/src/schema/typebox/gen/codegen/expression/index.ts +32 -0
- package/src/schema/typebox/gen/codegen/index.ts +29 -0
- package/src/schema/typebox/gen/codegen/model/index.ts +40 -0
- package/src/schema/typebox/gen/codegen/model/model-to-arktype.ts +266 -0
- package/src/schema/typebox/gen/codegen/model/model-to-expression.ts +378 -0
- package/src/schema/typebox/gen/codegen/model/model-to-grpc.ts +244 -0
- package/src/schema/typebox/gen/codegen/model/model-to-io-ts.ts +294 -0
- package/src/schema/typebox/gen/codegen/model/model-to-javascript.ts +42 -0
- package/src/schema/typebox/gen/codegen/model/model-to-json-schema.ts +166 -0
- package/src/schema/typebox/gen/codegen/model/model-to-typebox.ts +32 -0
- package/src/schema/typebox/gen/codegen/model/model-to-typescript.ts +189 -0
- package/src/schema/typebox/gen/codegen/model/model-to-valibot.ts +236 -0
- package/src/schema/typebox/gen/codegen/model/model-to-value.ts +42 -0
- package/src/schema/typebox/gen/codegen/model/model-to-yrel.ts +232 -0
- package/src/schema/typebox/gen/codegen/model/model-to-yup.ts +226 -0
- package/src/schema/typebox/gen/codegen/model/model-to-zod.ts +251 -0
- package/src/schema/typebox/gen/codegen/model/model.ts +33 -0
- package/src/schema/typebox/gen/codegen/typescript/index.ts +28 -0
- package/src/schema/typebox/gen/codegen/typescript/typescript-to-model.ts +61 -0
- package/src/schema/typebox/gen/codegen/typescript/typescript-to-typebox.ts +647 -0
- package/src/schema/typebox/gen/gen.test.ts +12 -0
- package/src/schema/typebox/gen/index.ts +1 -0
- package/src/schema/typebox/index.ts +1 -0
- package/src/schema/typebox/typebox.ts +34 -0
- package/src/servers/fetch/createFetchWithProxyByUndici.ts +16 -1
- package/src/types/index.d.ts +1 -0
- package/src/langs/hashCode.ts +0 -37
|
@@ -0,0 +1,266 @@
|
|
|
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
|
+
// ModelToArkType
|
|
32
|
+
// --------------------------------------------------------------------------
|
|
33
|
+
export namespace ModelToArkType {
|
|
34
|
+
// ------------------------------------------------------------------------
|
|
35
|
+
// Constraints
|
|
36
|
+
// ------------------------------------------------------------------------
|
|
37
|
+
function Wrap(value: string) {
|
|
38
|
+
return `'${value}'`;
|
|
39
|
+
}
|
|
40
|
+
function Unwrap(value: string) {
|
|
41
|
+
if (value.indexOf("'") !== 0) return value;
|
|
42
|
+
return value.slice(1, value.length - 1);
|
|
43
|
+
}
|
|
44
|
+
// ------------------------------------------------------------------------
|
|
45
|
+
// Composite
|
|
46
|
+
// ------------------------------------------------------------------------
|
|
47
|
+
function IsTuple(types: string[]) {
|
|
48
|
+
return types.some((type) => type.indexOf('[') === 0);
|
|
49
|
+
}
|
|
50
|
+
function ConstructUnionOrIntersect(types: string[], operator: string) {
|
|
51
|
+
function Reduce(types: string[]): string {
|
|
52
|
+
if (types.length === 0) return '';
|
|
53
|
+
const [left, ...right] = types;
|
|
54
|
+
return right.length > 0 ? `[${left}, '${operator}', ${Reduce(right)}]` : `${left}`;
|
|
55
|
+
}
|
|
56
|
+
if (IsTuple(types)) {
|
|
57
|
+
return Reduce(types);
|
|
58
|
+
} else {
|
|
59
|
+
const mapped = types.map((type) => Unwrap(type)).join(` ${operator} `);
|
|
60
|
+
return Wrap(mapped);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// ------------------------------------------------------------------------
|
|
64
|
+
// Constraints
|
|
65
|
+
// ------------------------------------------------------------------------
|
|
66
|
+
function ConstrainedSequenceType(type: string, options: { minimum?: number; maximum?: number }) {
|
|
67
|
+
if (IsDefined<number>(options.minimum) && IsDefined<number>(options.maximum)) {
|
|
68
|
+
return Wrap(`${options.minimum}<=${type}<=${options.maximum}`);
|
|
69
|
+
} else if (IsDefined<number>(options.minimum)) {
|
|
70
|
+
return Wrap(`${type}>=${options.minimum}`);
|
|
71
|
+
} else if (IsDefined<number>(options.maximum)) {
|
|
72
|
+
return Wrap(`${type}<=${options.maximum}`);
|
|
73
|
+
} else {
|
|
74
|
+
return Wrap(`${type}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function ConstrainedNumericType(type: string, options: Types.NumberOptions | Types.BigIntOptions) {
|
|
78
|
+
// prettier-ignore
|
|
79
|
+
const minimum = IsDefined<number>(options.exclusiveMinimum) ? (options.exclusiveMinimum + 1) :
|
|
80
|
+
IsDefined<number>(options.minimum) ? (options.minimum) :
|
|
81
|
+
undefined
|
|
82
|
+
// prettier-ignore
|
|
83
|
+
const maximum = IsDefined<number>(options.exclusiveMaximum) ? (options.exclusiveMaximum - 1) :
|
|
84
|
+
IsDefined<number>(options.maximum) ? (options.maximum) :
|
|
85
|
+
undefined
|
|
86
|
+
if (IsDefined<number>(minimum) && IsDefined<number>(maximum)) {
|
|
87
|
+
return Wrap(`${minimum}<=${type}<=${maximum}`);
|
|
88
|
+
} else if (IsDefined<number>(minimum)) {
|
|
89
|
+
return Wrap(`${type}>=${minimum}`);
|
|
90
|
+
} else if (IsDefined<number>(maximum)) {
|
|
91
|
+
return Wrap(`${type}<=${maximum}`);
|
|
92
|
+
} else {
|
|
93
|
+
return Wrap(`${type}`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function IsDefined<T = any>(value: unknown): value is T {
|
|
97
|
+
return value !== undefined;
|
|
98
|
+
}
|
|
99
|
+
function Any(schema: Types.TAny) {
|
|
100
|
+
return Wrap('any');
|
|
101
|
+
}
|
|
102
|
+
function Array(schema: Types.TArray) {
|
|
103
|
+
const type = Visit(schema.items);
|
|
104
|
+
const mapped = `${Unwrap(type)}[]`;
|
|
105
|
+
return ConstrainedSequenceType(mapped, {
|
|
106
|
+
minimum: schema.minItems,
|
|
107
|
+
maximum: schema.maxItems,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function BigInt(schema: Types.TBigInt) {
|
|
111
|
+
return Wrap('bigint');
|
|
112
|
+
}
|
|
113
|
+
function Boolean(schema: Types.TBoolean) {
|
|
114
|
+
return Wrap('boolean');
|
|
115
|
+
}
|
|
116
|
+
function Constructor(schema: Types.TConstructor): string {
|
|
117
|
+
return Wrap('Function');
|
|
118
|
+
}
|
|
119
|
+
function Date(schema: Types.TDate): string {
|
|
120
|
+
return Wrap('Date');
|
|
121
|
+
}
|
|
122
|
+
function Function(schema: Types.TFunction) {
|
|
123
|
+
return Wrap('Function');
|
|
124
|
+
}
|
|
125
|
+
function Integer(schema: Types.TInteger) {
|
|
126
|
+
return ConstrainedNumericType('integer', schema);
|
|
127
|
+
}
|
|
128
|
+
function Intersect(schema: Types.TIntersect) {
|
|
129
|
+
const types = schema.allOf.map((schema) => Collect(schema));
|
|
130
|
+
return ConstructUnionOrIntersect(types, '&');
|
|
131
|
+
}
|
|
132
|
+
function Literal(schema: Types.TLiteral) {
|
|
133
|
+
return typeof schema.const === 'string' ? Wrap(`"${schema.const}"`) : Wrap(schema.const.toString());
|
|
134
|
+
}
|
|
135
|
+
function Never(schema: Types.TNever) {
|
|
136
|
+
return Wrap('never');
|
|
137
|
+
}
|
|
138
|
+
function Null(schema: Types.TNull) {
|
|
139
|
+
return Wrap('null');
|
|
140
|
+
}
|
|
141
|
+
function String(schema: Types.TString) {
|
|
142
|
+
return ConstrainedSequenceType('string', {
|
|
143
|
+
minimum: schema.minLength,
|
|
144
|
+
maximum: schema.maxLength,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
function Number(schema: Types.TNumber) {
|
|
148
|
+
return ConstrainedNumericType('number', schema);
|
|
149
|
+
}
|
|
150
|
+
function Object(schema: Types.TObject) {
|
|
151
|
+
const properties = globalThis.Object.entries(schema.properties)
|
|
152
|
+
.map(([key, schema]) => {
|
|
153
|
+
const optional = Types.TypeGuard.IsOptional(schema);
|
|
154
|
+
const property1 = PropertyEncoder.Encode(key);
|
|
155
|
+
const property2 = optional ? `'${property1}?'` : `${property1}`;
|
|
156
|
+
return `${property2}: ${Visit(schema)}`;
|
|
157
|
+
})
|
|
158
|
+
.join(`,`);
|
|
159
|
+
const buffer: string[] = [];
|
|
160
|
+
buffer.push(`{\n${properties}\n}`);
|
|
161
|
+
return buffer.join(`\n`);
|
|
162
|
+
}
|
|
163
|
+
function Promise(schema: Types.TPromise) {
|
|
164
|
+
return Wrap('Promise');
|
|
165
|
+
}
|
|
166
|
+
function Record(schema: Types.TRecord) {
|
|
167
|
+
return Wrap('never'); // not sure how to express
|
|
168
|
+
}
|
|
169
|
+
function Ref(schema: Types.TRef) {
|
|
170
|
+
return Wrap(schema.$ref);
|
|
171
|
+
}
|
|
172
|
+
function This(schema: Types.TThis) {
|
|
173
|
+
return Wrap(schema.$ref);
|
|
174
|
+
}
|
|
175
|
+
function Tuple(schema: Types.TTuple) {
|
|
176
|
+
if (schema.items === undefined) return `[]`;
|
|
177
|
+
const items = schema.items.map((schema) => Visit(schema)).join(`, `);
|
|
178
|
+
return `[${items}]`;
|
|
179
|
+
}
|
|
180
|
+
function TemplateLiteral(schema: Types.TTemplateLiteral) {
|
|
181
|
+
return `/${schema.pattern}/`;
|
|
182
|
+
}
|
|
183
|
+
function UInt8Array(schema: Types.TUint8Array): string {
|
|
184
|
+
return `['instanceof', Uint8Array]`;
|
|
185
|
+
}
|
|
186
|
+
function Undefined(schema: Types.TUndefined) {
|
|
187
|
+
return Wrap('undefined');
|
|
188
|
+
}
|
|
189
|
+
function Union(schema: Types.TUnion) {
|
|
190
|
+
const types = schema.anyOf.map((schema) => Collect(schema));
|
|
191
|
+
return ConstructUnionOrIntersect(types, '|');
|
|
192
|
+
}
|
|
193
|
+
function Unknown(schema: Types.TUnknown) {
|
|
194
|
+
return Wrap('unknown');
|
|
195
|
+
}
|
|
196
|
+
function Void(schema: Types.TVoid) {
|
|
197
|
+
return Wrap('void');
|
|
198
|
+
}
|
|
199
|
+
function UnsupportedType(schema: Types.TSchema) {
|
|
200
|
+
return Wrap('never');
|
|
201
|
+
}
|
|
202
|
+
function Visit(schema: Types.TSchema): string {
|
|
203
|
+
if (schema.$id !== undefined) reference_map.set(schema.$id, schema);
|
|
204
|
+
if (schema.$id !== undefined && emitted_types.has(schema.$id!)) return `'${schema.$id!}'`;
|
|
205
|
+
if (Types.TypeGuard.IsAny(schema)) return Any(schema);
|
|
206
|
+
if (Types.TypeGuard.IsArray(schema)) return Array(schema);
|
|
207
|
+
if (Types.TypeGuard.IsBigInt(schema)) return BigInt(schema);
|
|
208
|
+
if (Types.TypeGuard.IsBoolean(schema)) return Boolean(schema);
|
|
209
|
+
if (Types.TypeGuard.IsConstructor(schema)) return Constructor(schema);
|
|
210
|
+
if (Types.TypeGuard.IsDate(schema)) return Date(schema);
|
|
211
|
+
if (Types.TypeGuard.IsFunction(schema)) return Function(schema);
|
|
212
|
+
if (Types.TypeGuard.IsInteger(schema)) return Integer(schema);
|
|
213
|
+
if (Types.TypeGuard.IsIntersect(schema)) return Intersect(schema);
|
|
214
|
+
if (Types.TypeGuard.IsLiteral(schema)) return Literal(schema);
|
|
215
|
+
if (Types.TypeGuard.IsNever(schema)) return Never(schema);
|
|
216
|
+
if (Types.TypeGuard.IsNull(schema)) return Null(schema);
|
|
217
|
+
if (Types.TypeGuard.IsNumber(schema)) return Number(schema);
|
|
218
|
+
if (Types.TypeGuard.IsObject(schema)) return Object(schema);
|
|
219
|
+
if (Types.TypeGuard.IsPromise(schema)) return Promise(schema);
|
|
220
|
+
if (Types.TypeGuard.IsRecord(schema)) return Record(schema);
|
|
221
|
+
if (Types.TypeGuard.IsRef(schema)) return Ref(schema);
|
|
222
|
+
if (Types.TypeGuard.IsString(schema)) return String(schema);
|
|
223
|
+
if (Types.TypeGuard.IsTemplateLiteral(schema)) return TemplateLiteral(schema);
|
|
224
|
+
if (Types.TypeGuard.IsThis(schema)) return This(schema);
|
|
225
|
+
if (Types.TypeGuard.IsTuple(schema)) return Tuple(schema);
|
|
226
|
+
if (Types.TypeGuard.IsUint8Array(schema)) return UInt8Array(schema);
|
|
227
|
+
if (Types.TypeGuard.IsUndefined(schema)) return Undefined(schema);
|
|
228
|
+
if (Types.TypeGuard.IsUnion(schema)) return Union(schema);
|
|
229
|
+
if (Types.TypeGuard.IsUnknown(schema)) return Unknown(schema);
|
|
230
|
+
if (Types.TypeGuard.IsVoid(schema)) return Void(schema);
|
|
231
|
+
return UnsupportedType(schema);
|
|
232
|
+
}
|
|
233
|
+
function Collect(schema: Types.TSchema) {
|
|
234
|
+
return [...Visit(schema)].join(``);
|
|
235
|
+
}
|
|
236
|
+
function GenerateType(schema: Types.TSchema, references: Types.TSchema[]) {
|
|
237
|
+
const buffer: string[] = [];
|
|
238
|
+
for (const reference of references) {
|
|
239
|
+
if (reference.$id === undefined) return UnsupportedType(schema);
|
|
240
|
+
reference_map.set(reference.$id, reference);
|
|
241
|
+
}
|
|
242
|
+
const type = Collect(schema);
|
|
243
|
+
buffer.push(`${schema.$id || `T`}: ${type}`);
|
|
244
|
+
if (schema.$id) emitted_types.add(schema.$id);
|
|
245
|
+
return buffer.join('\n');
|
|
246
|
+
}
|
|
247
|
+
const reference_map = new Map<string, Types.TSchema>();
|
|
248
|
+
const emitted_types = new Set<string>();
|
|
249
|
+
export function Generate(model: TypeBoxModel): string {
|
|
250
|
+
reference_map.clear();
|
|
251
|
+
emitted_types.clear();
|
|
252
|
+
const buffer: string[] = [];
|
|
253
|
+
buffer.push('export const types = scope({');
|
|
254
|
+
for (const type of model.types.filter((type) => Types.TypeGuard.IsSchema(type))) {
|
|
255
|
+
buffer.push(`${GenerateType(type, model.types)},`);
|
|
256
|
+
}
|
|
257
|
+
buffer.push('}).compile()');
|
|
258
|
+
buffer.push('\n');
|
|
259
|
+
for (const type of model.types.filter((type) => Types.TypeGuard.IsSchema(type))) {
|
|
260
|
+
buffer.push(`export type ${type.$id} = typeof ${type.$id}.infer`);
|
|
261
|
+
buffer.push(`export const ${type.$id} = types.${type.$id}`);
|
|
262
|
+
}
|
|
263
|
+
buffer.unshift(`import { scope } from 'arktype'`, '');
|
|
264
|
+
return Formatter.Format(buffer.join('\n'));
|
|
265
|
+
}
|
|
266
|
+
}
|
|
@@ -0,0 +1,378 @@
|
|
|
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 { TypeSystem, TypeSystemPolicy } from '@sinclair/typebox/system';
|
|
28
|
+
import { Formatter } from '../common/formatter';
|
|
29
|
+
import { Expression } from '../expression/index';
|
|
30
|
+
import { TypeBoxModel } from './model';
|
|
31
|
+
|
|
32
|
+
export class TypeTransformUnknownSchemaError extends Error {
|
|
33
|
+
constructor(public readonly schema: unknown) {
|
|
34
|
+
super('TypeTransformUnknownSchemaError: Unknown schema');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export namespace TypeBoxToExpression {
|
|
38
|
+
// --------------------------------------------------------------
|
|
39
|
+
// Guards
|
|
40
|
+
// --------------------------------------------------------------
|
|
41
|
+
function IsArray(value: unknown): value is unknown[] {
|
|
42
|
+
return globalThis.Array.isArray(value);
|
|
43
|
+
}
|
|
44
|
+
function IsObject(value: unknown): value is object {
|
|
45
|
+
return typeof value === 'object' && !IsArray(value);
|
|
46
|
+
}
|
|
47
|
+
function IsNumber(value: unknown): value is number {
|
|
48
|
+
return typeof value === 'number' && !isNaN(value);
|
|
49
|
+
}
|
|
50
|
+
function IsString(value: unknown): value is string {
|
|
51
|
+
return typeof value === 'string';
|
|
52
|
+
}
|
|
53
|
+
function IsUndefined(value: unknown): value is undefined {
|
|
54
|
+
return value === undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// --------------------------------------------------------------
|
|
58
|
+
// Conditional: If Then Else
|
|
59
|
+
// --------------------------------------------------------------
|
|
60
|
+
// function* Conditional(schema: Types.TSchema, references: Types.TSchema[]): IterableIterator<Expression> {
|
|
61
|
+
// if (IsUndefined(schema.if)) return
|
|
62
|
+
// const if_expression = Visit(schema.if, references)
|
|
63
|
+
// const then_expression = IsUndefined(schema.then) ? Expression.True() : Visit(schema.then, references)
|
|
64
|
+
// const else_expression = IsUndefined(schema.else) ? Expression.True() : Visit(schema.else, references)
|
|
65
|
+
// yield Expression.IfThenElse(if_expression, then_expression, else_expression)
|
|
66
|
+
// }
|
|
67
|
+
|
|
68
|
+
// --------------------------------------------------------------
|
|
69
|
+
// Transform
|
|
70
|
+
// --------------------------------------------------------------
|
|
71
|
+
function Any(schema: Types.TAny, references: Types.TSchema[]): Expression {
|
|
72
|
+
return Expression.True({ $id: schema.$id });
|
|
73
|
+
}
|
|
74
|
+
function Array(schema: Types.TArray, references: Types.TSchema[]) {
|
|
75
|
+
return Expression.And(
|
|
76
|
+
function* () {
|
|
77
|
+
yield Expression.IsArray();
|
|
78
|
+
if (IsNumber(schema.maxItems)) yield Expression.Property('length', Expression.LessThanEqual(schema.maxItems));
|
|
79
|
+
if (IsNumber(schema.minItems))
|
|
80
|
+
yield Expression.Property('length', Expression.GreaterThanEqual(schema.minItems));
|
|
81
|
+
yield Expression.Elements(Visit(schema.items, references));
|
|
82
|
+
if (schema.uniqueItems === true) yield Expression.ElementsDistinct();
|
|
83
|
+
},
|
|
84
|
+
{ $id: schema.$id },
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
function Boolean(schema: Types.TBoolean, references: Types.TSchema[]): Expression {
|
|
88
|
+
return Expression.IsBoolean({ $id: schema.$id });
|
|
89
|
+
}
|
|
90
|
+
function Constructor(schema: Types.TConstructor, references: Types.TSchema[]): Expression {
|
|
91
|
+
return Expression.Property('prototype', Visit(schema.returns, references), { $id: schema.$id });
|
|
92
|
+
}
|
|
93
|
+
function Date(schema: Types.TDate, references: Types.TSchema[]): Expression {
|
|
94
|
+
return Expression.And(
|
|
95
|
+
function* () {
|
|
96
|
+
yield Expression.InstanceOf('Date');
|
|
97
|
+
if (IsNumber(schema.maximumTimestamp))
|
|
98
|
+
yield Expression.Call('getTime', [], Expression.LessThanEqual(schema.maximumTimestamp));
|
|
99
|
+
if (IsNumber(schema.minimumTimestamp))
|
|
100
|
+
yield Expression.Call('getTime', [], Expression.GreaterThanEqual(schema.minimumTimestamp));
|
|
101
|
+
if (IsNumber(schema.exclusiveMaximumTimestamp))
|
|
102
|
+
yield Expression.Call('getTime', [], Expression.LessThan(schema.exclusiveMaximumTimestamp));
|
|
103
|
+
if (IsNumber(schema.exclusiveMinimumTimestamp))
|
|
104
|
+
yield Expression.Call('getTime', [], Expression.GreaterThan(schema.exclusiveMinimumTimestamp));
|
|
105
|
+
},
|
|
106
|
+
{ $id: schema.$id },
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
function Function(schema: Types.TFunction, references: Types.TSchema[]): Expression {
|
|
110
|
+
return Expression.IsFunction({ $id: schema.$id });
|
|
111
|
+
}
|
|
112
|
+
function Intersect(schema: Types.TIntersect, references: Types.TSchema[]): Expression {
|
|
113
|
+
return Expression.And(
|
|
114
|
+
function* () {
|
|
115
|
+
for (const inner of schema.allOf) {
|
|
116
|
+
yield Visit(inner, references);
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
{ $id: schema.$id },
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
function Integer(schema: Types.TInteger, references: Types.TSchema[]): Expression {
|
|
123
|
+
return Expression.And(
|
|
124
|
+
function* () {
|
|
125
|
+
yield Expression.IsInteger();
|
|
126
|
+
if (IsNumber(schema.multipleOf)) yield Expression.MultipleOf(schema.multipleOf);
|
|
127
|
+
if (IsNumber(schema.maximum)) yield Expression.LessThanEqual(schema.maximum);
|
|
128
|
+
if (IsNumber(schema.minimum)) yield Expression.GreaterThanEqual(schema.minimum);
|
|
129
|
+
if (IsNumber(schema.exclusiveMaximum)) yield Expression.LessThan(schema.exclusiveMaximum);
|
|
130
|
+
if (IsNumber(schema.exclusiveMinimum)) yield Expression.GreaterThan(schema.exclusiveMinimum);
|
|
131
|
+
},
|
|
132
|
+
{ $id: schema.$id },
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
function Literal(schema: Types.TLiteral, references: Types.TSchema[]): Expression {
|
|
136
|
+
return Expression.Equals(schema.const, { $id: schema.$id });
|
|
137
|
+
}
|
|
138
|
+
function Never(schema: Types.TNever, references: Types.TSchema[]): Expression {
|
|
139
|
+
return Expression.False({ $id: schema.$id });
|
|
140
|
+
}
|
|
141
|
+
function Null(schema: Types.TNull, references: Types.TSchema[]): Expression {
|
|
142
|
+
return Expression.IsNull({ $id: schema.$id });
|
|
143
|
+
}
|
|
144
|
+
function Number(schema: Types.TNumber, references: Types.TSchema[]): Expression {
|
|
145
|
+
return Expression.And(
|
|
146
|
+
function* () {
|
|
147
|
+
yield Expression.IsNumber();
|
|
148
|
+
if (TypeSystemPolicy.AllowNaN === false) yield Expression.IsFinite();
|
|
149
|
+
if (IsNumber(schema.multipleOf)) yield Expression.MultipleOf(schema.multipleOf);
|
|
150
|
+
if (IsNumber(schema.maximum)) yield Expression.LessThanEqual(schema.maximum);
|
|
151
|
+
if (IsNumber(schema.minimum)) yield Expression.GreaterThanEqual(schema.minimum);
|
|
152
|
+
if (IsNumber(schema.exclusiveMaximum)) yield Expression.LessThan(schema.exclusiveMaximum);
|
|
153
|
+
if (IsNumber(schema.exclusiveMinimum)) yield Expression.GreaterThan(schema.exclusiveMinimum);
|
|
154
|
+
},
|
|
155
|
+
{ $id: schema.$id },
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
function Object(schema: Types.TObject, references: Types.TSchema[]): Expression {
|
|
159
|
+
const propertyKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
|
|
160
|
+
const requiredKeys = IsArray(schema.required) ? schema.required : [];
|
|
161
|
+
const optionalKeys = new Set(propertyKeys);
|
|
162
|
+
requiredKeys.forEach((key) => optionalKeys.delete(key));
|
|
163
|
+
return Expression.And(
|
|
164
|
+
function* () {
|
|
165
|
+
yield Expression.IsObject();
|
|
166
|
+
yield Expression.Not(Expression.IsNull());
|
|
167
|
+
if (TypeSystemPolicy.AllowArrayObject === false) yield Expression.Not(Expression.IsArray());
|
|
168
|
+
if (IsNumber(schema.minProperties)) yield Expression.PropertiesMinimum(schema.minProperties);
|
|
169
|
+
if (IsNumber(schema.maxProperties)) yield Expression.PropertiesMaximum(schema.maxProperties);
|
|
170
|
+
if (schema.additionalProperties === false) {
|
|
171
|
+
if (schema.required && schema.required.length === propertyKeys.length) {
|
|
172
|
+
yield Expression.PropertiesLength(propertyKeys.length);
|
|
173
|
+
} else {
|
|
174
|
+
yield Expression.PropertiesExclude(propertyKeys, Expression.False());
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (IsObject(schema.additionalProperties)) {
|
|
178
|
+
yield Expression.PropertiesExclude(propertyKeys, Visit(schema.additionalProperties, references));
|
|
179
|
+
}
|
|
180
|
+
for (const propertyKey of requiredKeys) {
|
|
181
|
+
yield Expression.Property(propertyKey, Visit(schema.properties[propertyKey], references));
|
|
182
|
+
}
|
|
183
|
+
for (const propertyKey of optionalKeys) {
|
|
184
|
+
yield Expression.Or(function* () {
|
|
185
|
+
yield Expression.Not(Expression.KeyIn(propertyKey));
|
|
186
|
+
yield Expression.Property(
|
|
187
|
+
propertyKey,
|
|
188
|
+
Expression.Or(function* () {
|
|
189
|
+
yield Expression.Not(Expression.KeyIn(propertyKey));
|
|
190
|
+
yield Visit(schema.properties[propertyKey], references);
|
|
191
|
+
}),
|
|
192
|
+
);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
{ $id: schema.$id },
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
function Promise(schema: Types.TPromise, references: Types.TSchema[]): Expression {
|
|
200
|
+
return Expression.And(
|
|
201
|
+
function* () {
|
|
202
|
+
yield Expression.IsObject();
|
|
203
|
+
yield Expression.Property('then', Expression.IsFunction());
|
|
204
|
+
},
|
|
205
|
+
{ $id: schema.$id },
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
function Record(schema: Types.TRecord, references: Types.TSchema[]): Expression {
|
|
209
|
+
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
210
|
+
return Expression.And(
|
|
211
|
+
function* () {
|
|
212
|
+
yield Expression.IsObject();
|
|
213
|
+
yield Expression.Not(Expression.IsNull());
|
|
214
|
+
yield Expression.Not(Expression.InstanceOf('Date'));
|
|
215
|
+
if (IsNumber(schema.minProperties)) yield Expression.PropertiesMinimum(schema.minProperties);
|
|
216
|
+
if (IsNumber(schema.maxProperties)) yield Expression.PropertiesMaximum(schema.maxProperties);
|
|
217
|
+
yield Expression.PropertiesIncludePattern(keyPattern, Visit(valueSchema, references));
|
|
218
|
+
if (schema.additionalProperties === false) {
|
|
219
|
+
yield Expression.PropertiesExcludePattern(keyPattern, Expression.False());
|
|
220
|
+
}
|
|
221
|
+
if (IsObject(schema.additionalProperties)) {
|
|
222
|
+
yield Expression.PropertiesExcludePattern(keyPattern, Visit(schema.additionalProperties, references));
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
{ $id: schema.$id },
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
function Ref(schema: Types.TRef<any>, references: Types.TSchema[]): Expression {
|
|
229
|
+
return Expression.Ref(schema.$ref);
|
|
230
|
+
}
|
|
231
|
+
function Self(schema: Types.TThis, references: Types.TSchema[]): Expression {
|
|
232
|
+
return Expression.Ref(schema.$ref, { $id: schema.$id });
|
|
233
|
+
}
|
|
234
|
+
function String(schema: Types.TString, references: Types.TSchema[]): Expression {
|
|
235
|
+
return Expression.And(
|
|
236
|
+
function* () {
|
|
237
|
+
yield Expression.IsString();
|
|
238
|
+
if (IsNumber(schema.minLength))
|
|
239
|
+
yield Expression.Property('length', Expression.GreaterThanEqual(schema.minLength));
|
|
240
|
+
if (IsNumber(schema.maxLength)) yield Expression.Property('length', Expression.LessThanEqual(schema.maxLength));
|
|
241
|
+
if (!IsUndefined(schema.pattern)) yield Expression.IsPattern(schema.pattern);
|
|
242
|
+
},
|
|
243
|
+
{ $id: schema.$id },
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
function TemplateLiteral(schema: Types.TTemplateLiteral, references: Types.TSchema[]): Expression {
|
|
247
|
+
return Expression.IsPattern(schema.pattern);
|
|
248
|
+
}
|
|
249
|
+
function Tuple(schema: Types.TTuple, references: Types.TSchema[]): Expression {
|
|
250
|
+
return Expression.And(
|
|
251
|
+
function* () {
|
|
252
|
+
yield Expression.IsArray();
|
|
253
|
+
if (schema.items === undefined) return yield Expression.Property('length', Expression.Equals(0));
|
|
254
|
+
if (schema.maxItems) yield Expression.Property('length', Expression.Equals(schema.maxItems));
|
|
255
|
+
yield Expression.And(function* () {
|
|
256
|
+
for (let index = 0; index < schema.items!.length; index++) {
|
|
257
|
+
yield Expression.Index(index, Visit(schema.items![index], references));
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
},
|
|
261
|
+
{ $id: schema.$id },
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
function Undefined(schema: Types.TUndefined, references: Types.TSchema[]): Expression {
|
|
265
|
+
return Expression.IsUndefined({ $id: schema.$id });
|
|
266
|
+
}
|
|
267
|
+
function Union(schema: Types.TUnion<any[]>, references: Types.TSchema[]): Expression {
|
|
268
|
+
return Expression.Or(
|
|
269
|
+
function* () {
|
|
270
|
+
for (const inner of schema.anyOf) {
|
|
271
|
+
yield Visit(inner, references);
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
{ $id: schema.$id },
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
function Uint8Array(schema: Types.TUint8Array, references: Types.TSchema[]): Expression {
|
|
278
|
+
return Expression.And(
|
|
279
|
+
function* () {
|
|
280
|
+
yield Expression.InstanceOf('Uint8Array');
|
|
281
|
+
if (IsNumber(schema.maxByteLength))
|
|
282
|
+
yield Expression.Property('length', Expression.LessThanEqual(schema.maxByteLength));
|
|
283
|
+
if (IsNumber(schema.minByteLength))
|
|
284
|
+
yield Expression.Property('length', Expression.GreaterThanEqual(schema.minByteLength));
|
|
285
|
+
},
|
|
286
|
+
{ $id: schema.$id },
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
function Unknown(schema: Types.TUnknown, references: Types.TSchema[]): Expression {
|
|
290
|
+
return Expression.True({ $id: schema.$id });
|
|
291
|
+
}
|
|
292
|
+
function Void(schema: Types.TVoid, references: Types.TSchema[]): Expression {
|
|
293
|
+
return Expression.IsUndefined({ $id: schema.$id });
|
|
294
|
+
}
|
|
295
|
+
function Unresolved(schema: Types.TSchema, references: Types.TSchema[]): Expression {
|
|
296
|
+
return Expression.False({ unresolved: schema });
|
|
297
|
+
}
|
|
298
|
+
function Visit<T extends Types.TSchema>(schema: T, references: Types.TSchema[]): Expression {
|
|
299
|
+
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
300
|
+
const anySchema = schema as any;
|
|
301
|
+
switch (anySchema[Types.Kind]) {
|
|
302
|
+
case 'Any':
|
|
303
|
+
return Any(anySchema, anyReferences);
|
|
304
|
+
case 'Array':
|
|
305
|
+
return Array(anySchema, anyReferences);
|
|
306
|
+
case 'Boolean':
|
|
307
|
+
return Boolean(anySchema, anyReferences);
|
|
308
|
+
case 'Constructor':
|
|
309
|
+
return Constructor(anySchema, anyReferences);
|
|
310
|
+
case 'Date':
|
|
311
|
+
return Date(anySchema, anyReferences);
|
|
312
|
+
case 'Function':
|
|
313
|
+
return Function(anySchema, anyReferences);
|
|
314
|
+
case 'Integer':
|
|
315
|
+
return Integer(anySchema, anyReferences);
|
|
316
|
+
case 'Intersect':
|
|
317
|
+
return Intersect(anySchema, anyReferences);
|
|
318
|
+
case 'Literal':
|
|
319
|
+
return Literal(anySchema, anyReferences);
|
|
320
|
+
case 'Never':
|
|
321
|
+
return Never(anySchema, anyReferences);
|
|
322
|
+
case 'Null':
|
|
323
|
+
return Null(anySchema, anyReferences);
|
|
324
|
+
case 'Number':
|
|
325
|
+
return Number(anySchema, anyReferences);
|
|
326
|
+
case 'Object':
|
|
327
|
+
return Object(anySchema, anyReferences);
|
|
328
|
+
case 'Promise':
|
|
329
|
+
return Promise(anySchema, anyReferences);
|
|
330
|
+
case 'Record':
|
|
331
|
+
return Record(anySchema, anyReferences);
|
|
332
|
+
case 'Ref':
|
|
333
|
+
return Ref(anySchema, anyReferences);
|
|
334
|
+
case 'Self':
|
|
335
|
+
return Self(anySchema, anyReferences);
|
|
336
|
+
case 'String':
|
|
337
|
+
return String(anySchema, anyReferences);
|
|
338
|
+
case 'TemplateLiteral':
|
|
339
|
+
return TemplateLiteral(anySchema, anyReferences);
|
|
340
|
+
case 'Tuple':
|
|
341
|
+
return Tuple(anySchema, anyReferences);
|
|
342
|
+
case 'Undefined':
|
|
343
|
+
return Undefined(anySchema, anyReferences);
|
|
344
|
+
case 'Union':
|
|
345
|
+
return Union(anySchema, anyReferences);
|
|
346
|
+
case 'Uint8Array':
|
|
347
|
+
return Uint8Array(anySchema, anyReferences);
|
|
348
|
+
case 'Unknown':
|
|
349
|
+
return Unknown(anySchema, anyReferences);
|
|
350
|
+
case 'Void':
|
|
351
|
+
return Void(anySchema, anyReferences);
|
|
352
|
+
default:
|
|
353
|
+
return Unresolved(schema, anyReferences);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/** Transforms a Type to an Expression */
|
|
357
|
+
export function Transform(schema: Types.TSchema, references: Types.TSchema[] = []): Expression {
|
|
358
|
+
const expression = Visit(schema, references);
|
|
359
|
+
return expression;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
export namespace ModelToExpr {
|
|
363
|
+
export function Generate(model: TypeBoxModel): string {
|
|
364
|
+
const definitions: string[] = [];
|
|
365
|
+
for (const type of model.types.filter((type) => Types.TypeGuard.IsSchema(type))) {
|
|
366
|
+
const expression = TypeBoxToExpression.Transform(
|
|
367
|
+
type,
|
|
368
|
+
model.types.filter((t) => t.$id !== type.$id),
|
|
369
|
+
);
|
|
370
|
+
delete expression.$id;
|
|
371
|
+
definitions.push(`
|
|
372
|
+
export const ${type.$id!} = ${JSON.stringify(expression)}
|
|
373
|
+
`);
|
|
374
|
+
}
|
|
375
|
+
const output = [...definitions];
|
|
376
|
+
return Formatter.Format(output.join('\n\n'));
|
|
377
|
+
}
|
|
378
|
+
}
|