-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathindex.ts
188 lines (164 loc) · 6.02 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { SchemaObject, ReferenceObject, MapSubSchema } from '../types';
import { ZodType } from 'zod';
import { UnknownZodTypeError } from '../errors';
import { isZodType } from '../lib/zod-is-type';
import { Metadata } from '../metadata';
import { ArrayTransformer } from './array';
import { BigIntTransformer } from './big-int';
import { DiscriminatedUnionTransformer } from './discriminated-union';
import { EnumTransformer } from './enum';
import { IntersectionTransformer } from './intersection';
import { LiteralTransformer } from './literal';
import { NativeEnumTransformer } from './native-enum';
import { NumberTransformer } from './number';
import { ObjectTransformer } from './object';
import { RecordTransformer } from './record';
import { StringTransformer } from './string';
import { TupleTransformer } from './tuple';
import { UnionTransformer } from './union';
import { OpenApiVersionSpecifics } from '../openapi-generator';
export class OpenApiTransformer {
private objectTransformer = new ObjectTransformer();
private stringTransformer = new StringTransformer();
private numberTransformer = new NumberTransformer();
private bigIntTransformer = new BigIntTransformer();
private literalTransformer = new LiteralTransformer();
private enumTransformer = new EnumTransformer();
private nativeEnumTransformer = new NativeEnumTransformer();
private arrayTransformer = new ArrayTransformer();
private tupleTransformer: TupleTransformer;
private unionTransformer = new UnionTransformer();
private discriminatedUnionTransformer = new DiscriminatedUnionTransformer();
private intersectionTransformer = new IntersectionTransformer();
private recordTransformer = new RecordTransformer();
constructor(private versionSpecifics: OpenApiVersionSpecifics) {
this.tupleTransformer = new TupleTransformer(versionSpecifics);
}
transform<T>(
zodSchema: ZodType<T>,
isNullable: boolean,
mapItem: MapSubSchema,
generateSchemaRef: (ref: string) => string,
defaultValue?: T
): SchemaObject | ReferenceObject {
if (isZodType(zodSchema, 'ZodNull')) {
return this.versionSpecifics.nullType;
}
if (isZodType(zodSchema, 'ZodUnknown') || isZodType(zodSchema, 'ZodAny')) {
return this.versionSpecifics.mapNullableType(undefined, isNullable);
}
if (isZodType(zodSchema, 'ZodObject')) {
return this.objectTransformer.transform(
zodSchema,
defaultValue as object, // verified on TS level from input
_ => this.versionSpecifics.mapNullableType(_, isNullable),
mapItem
);
}
const schema = this.transformSchemaWithoutDefault(
zodSchema,
isNullable,
mapItem,
generateSchemaRef
);
return { ...schema, default: defaultValue };
}
private transformSchemaWithoutDefault<T>(
zodSchema: ZodType<T>,
isNullable: boolean,
mapItem: MapSubSchema,
generateSchemaRef: (ref: string) => string
): SchemaObject | ReferenceObject {
if (isZodType(zodSchema, 'ZodUnknown') || isZodType(zodSchema, 'ZodAny')) {
return this.versionSpecifics.mapNullableType(undefined, isNullable);
}
if (isZodType(zodSchema, 'ZodString')) {
return this.stringTransformer.transform(zodSchema, schema =>
this.versionSpecifics.mapNullableType(schema, isNullable)
);
}
if (isZodType(zodSchema, 'ZodNumber')) {
return this.numberTransformer.transform(
zodSchema,
schema => this.versionSpecifics.mapNullableType(schema, isNullable),
_ => this.versionSpecifics.getNumberChecks(_)
);
}
if (isZodType(zodSchema, 'ZodBigInt')) {
return this.bigIntTransformer.transform(schema =>
this.versionSpecifics.mapNullableType(schema, isNullable)
);
}
if (isZodType(zodSchema, 'ZodBoolean')) {
return this.versionSpecifics.mapNullableType('boolean', isNullable);
}
if (isZodType(zodSchema, 'ZodLiteral')) {
return this.literalTransformer.transform(zodSchema, schema =>
this.versionSpecifics.mapNullableType(schema, isNullable)
);
}
if (isZodType(zodSchema, 'ZodEnum')) {
return this.enumTransformer.transform(zodSchema, schema =>
this.versionSpecifics.mapNullableType(schema, isNullable)
);
}
if (isZodType(zodSchema, 'ZodNativeEnum')) {
return this.nativeEnumTransformer.transform(zodSchema, schema =>
this.versionSpecifics.mapNullableType(schema, isNullable)
);
}
if (isZodType(zodSchema, 'ZodArray')) {
return this.arrayTransformer.transform(
zodSchema,
_ => this.versionSpecifics.mapNullableType(_, isNullable),
mapItem
);
}
if (isZodType(zodSchema, 'ZodTuple')) {
return this.tupleTransformer.transform(
zodSchema,
_ => this.versionSpecifics.mapNullableType(_, isNullable),
mapItem
);
}
if (isZodType(zodSchema, 'ZodUnion')) {
return this.unionTransformer.transform(
zodSchema,
_ => this.versionSpecifics.mapNullableOfArray(_, isNullable),
mapItem
);
}
if (isZodType(zodSchema, 'ZodDiscriminatedUnion')) {
return this.discriminatedUnionTransformer.transform(
zodSchema,
isNullable,
_ => this.versionSpecifics.mapNullableOfArray(_, isNullable),
mapItem,
generateSchemaRef
);
}
if (isZodType(zodSchema, 'ZodIntersection')) {
return this.intersectionTransformer.transform(
zodSchema,
isNullable,
_ => this.versionSpecifics.mapNullableOfArray(_, isNullable),
mapItem
);
}
if (isZodType(zodSchema, 'ZodRecord')) {
return this.recordTransformer.transform(
zodSchema,
_ => this.versionSpecifics.mapNullableType(_, isNullable),
mapItem
);
}
if (isZodType(zodSchema, 'ZodDate')) {
return this.versionSpecifics.mapNullableType('string', isNullable);
}
const refId = Metadata.getRefId(zodSchema);
throw new UnknownZodTypeError({
currentSchema: zodSchema._def,
schemaName: refId,
});
}
}