@miroir-framework/jzod-ts 0.5.3 → 0.6.1
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/dist/bundle.d.ts +177 -334
- package/dist/bundle.js +43 -257
- package/dist/out-tsc/dts/src/JzodToTs.d.ts +17 -0
- package/dist/out-tsc/dts/src/generated_jzodBootstrapElementSchema.d.ts +246 -0
- package/dist/out-tsc/dts/src/index.d.ts +3 -0
- package/dist/out-tsc/dts/src/preBuild.d.ts +1 -0
- package/dist/out-tsc/dts/src/tools.d.ts +4 -0
- package/dist/src/JzodToTs.d.ts +2 -2
- package/dist/src/generated_jzodBootstrapElementSchema.d.ts +249 -0
- package/dist/src/index.d.ts +1 -1
- package/dist/src/preBuild.d.ts +1 -0
- package/package.json +40 -38
- package/src/JzodToTs.ts +118 -109
- package/src/generated_jzodBootstrapElementSchema.ts +252 -0
- package/src/index.ts +60 -60
- package/src/preBuild.ts +54 -0
- package/tests/jzod-ts.test.ts +107 -108
- package/tests/resources/tsTypeGeneration-testJzodSchema1 - reference.ts +5 -5
- package/tests/resources/tsTypeGeneration-testJzodSchema2 - reference.ts +11 -11
- package/tests/resources/tsTypeGeneration-testJzodSchema3 - reference.ts +19 -19
- package/tests/resources/tsTypeGeneration-testJzodSchema4 - reference.ts +253 -250
- package/tsconfig.json +25 -25
- package/dist/src/JzodTsInterface.d.ts +0 -397
- package/src/JzodTsInterface.ts +0 -450
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { ZodType, z } from "zod";
|
|
2
|
+
export type JzodBaseObject = {
|
|
3
|
+
optional?: boolean | undefined;
|
|
4
|
+
nullable?: boolean | undefined;
|
|
5
|
+
extra?: {
|
|
6
|
+
[x: string]: any;
|
|
7
|
+
} | undefined;
|
|
8
|
+
};
|
|
9
|
+
export type JzodArray = {
|
|
10
|
+
optional?: boolean | undefined;
|
|
11
|
+
nullable?: boolean | undefined;
|
|
12
|
+
extra?: {
|
|
13
|
+
[x: string]: any;
|
|
14
|
+
} | undefined;
|
|
15
|
+
type: "array";
|
|
16
|
+
definition: JzodElement;
|
|
17
|
+
};
|
|
18
|
+
export type JzodAttribute = {
|
|
19
|
+
optional?: boolean | undefined;
|
|
20
|
+
nullable?: boolean | undefined;
|
|
21
|
+
extra?: {
|
|
22
|
+
[x: string]: any;
|
|
23
|
+
} | undefined;
|
|
24
|
+
type: "simpleType";
|
|
25
|
+
coerce?: boolean | undefined;
|
|
26
|
+
definition: JzodEnumAttributeTypes;
|
|
27
|
+
};
|
|
28
|
+
export type JzodAttributeDateValidations = {
|
|
29
|
+
extra?: {
|
|
30
|
+
[x: string]: any;
|
|
31
|
+
} | undefined;
|
|
32
|
+
type: "min" | "max";
|
|
33
|
+
parameter?: any;
|
|
34
|
+
};
|
|
35
|
+
export type JzodAttributeDateWithValidations = {
|
|
36
|
+
optional?: boolean | undefined;
|
|
37
|
+
nullable?: boolean | undefined;
|
|
38
|
+
extra?: {
|
|
39
|
+
[x: string]: any;
|
|
40
|
+
} | undefined;
|
|
41
|
+
type: "simpleType";
|
|
42
|
+
definition: "date";
|
|
43
|
+
coerce?: boolean | undefined;
|
|
44
|
+
validations: JzodAttributeDateValidations[];
|
|
45
|
+
};
|
|
46
|
+
export type JzodAttributeNumberValidations = {
|
|
47
|
+
extra?: {
|
|
48
|
+
[x: string]: any;
|
|
49
|
+
} | undefined;
|
|
50
|
+
type: "gt" | "gte" | "lt" | "lte" | "int" | "positive" | "nonpositive" | "negative" | "nonnegative" | "multipleOf" | "finite" | "safe";
|
|
51
|
+
parameter?: any;
|
|
52
|
+
};
|
|
53
|
+
export type JzodAttributeNumberWithValidations = {
|
|
54
|
+
optional?: boolean | undefined;
|
|
55
|
+
nullable?: boolean | undefined;
|
|
56
|
+
extra?: {
|
|
57
|
+
[x: string]: any;
|
|
58
|
+
} | undefined;
|
|
59
|
+
type: "simpleType";
|
|
60
|
+
definition: "number";
|
|
61
|
+
coerce?: boolean | undefined;
|
|
62
|
+
validations: JzodAttributeNumberValidations[];
|
|
63
|
+
};
|
|
64
|
+
export type JzodAttributeStringValidations = {
|
|
65
|
+
extra?: {
|
|
66
|
+
[x: string]: any;
|
|
67
|
+
} | undefined;
|
|
68
|
+
type: "max" | "min" | "length" | "email" | "url" | "emoji" | "uuid" | "cuid" | "cuid2" | "ulid" | "regex" | "includes" | "startsWith" | "endsWith" | "datetime" | "ip";
|
|
69
|
+
parameter?: any;
|
|
70
|
+
};
|
|
71
|
+
export type JzodAttributeStringWithValidations = {
|
|
72
|
+
optional?: boolean | undefined;
|
|
73
|
+
nullable?: boolean | undefined;
|
|
74
|
+
extra?: {
|
|
75
|
+
[x: string]: any;
|
|
76
|
+
} | undefined;
|
|
77
|
+
type: "simpleType";
|
|
78
|
+
definition: "string";
|
|
79
|
+
coerce?: boolean | undefined;
|
|
80
|
+
validations: JzodAttributeStringValidations[];
|
|
81
|
+
};
|
|
82
|
+
export type JzodElement = JzodArray | JzodAttribute | JzodAttributeDateWithValidations | JzodAttributeNumberWithValidations | JzodAttributeStringWithValidations | JzodEnum | JzodFunction | JzodLazy | JzodLiteral | JzodIntersection | JzodMap | JzodObject | JzodPromise | JzodRecord | JzodReference | JzodSet | JzodTuple | JzodUnion;
|
|
83
|
+
export type JzodEnum = {
|
|
84
|
+
optional?: boolean | undefined;
|
|
85
|
+
nullable?: boolean | undefined;
|
|
86
|
+
extra?: {
|
|
87
|
+
[x: string]: any;
|
|
88
|
+
} | undefined;
|
|
89
|
+
type: "enum";
|
|
90
|
+
definition: string[];
|
|
91
|
+
};
|
|
92
|
+
export type JzodEnumAttributeTypes = "any" | "bigint" | "boolean" | "date" | "never" | "null" | "number" | "string" | "uuid" | "undefined" | "unknown" | "void";
|
|
93
|
+
export type JzodEnumElementTypes = "array" | "enum" | "function" | "lazy" | "literal" | "intersection" | "map" | "object" | "promise" | "record" | "schemaReference" | "set" | "simpleType" | "tuple" | "union";
|
|
94
|
+
export type JzodFunction = {
|
|
95
|
+
optional?: boolean | undefined;
|
|
96
|
+
nullable?: boolean | undefined;
|
|
97
|
+
extra?: {
|
|
98
|
+
[x: string]: any;
|
|
99
|
+
} | undefined;
|
|
100
|
+
type: "function";
|
|
101
|
+
definition: {
|
|
102
|
+
args: JzodElement[];
|
|
103
|
+
returns?: JzodElement | undefined;
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
export type JzodLazy = {
|
|
107
|
+
optional?: boolean | undefined;
|
|
108
|
+
nullable?: boolean | undefined;
|
|
109
|
+
extra?: {
|
|
110
|
+
[x: string]: any;
|
|
111
|
+
} | undefined;
|
|
112
|
+
type: "lazy";
|
|
113
|
+
definition: JzodFunction;
|
|
114
|
+
};
|
|
115
|
+
export type JzodLiteral = {
|
|
116
|
+
optional?: boolean | undefined;
|
|
117
|
+
nullable?: boolean | undefined;
|
|
118
|
+
extra?: {
|
|
119
|
+
[x: string]: any;
|
|
120
|
+
} | undefined;
|
|
121
|
+
type: "literal";
|
|
122
|
+
definition: string;
|
|
123
|
+
};
|
|
124
|
+
export type JzodIntersection = {
|
|
125
|
+
optional?: boolean | undefined;
|
|
126
|
+
nullable?: boolean | undefined;
|
|
127
|
+
extra?: {
|
|
128
|
+
[x: string]: any;
|
|
129
|
+
} | undefined;
|
|
130
|
+
type: "intersection";
|
|
131
|
+
definition: {
|
|
132
|
+
left: JzodElement;
|
|
133
|
+
right: JzodElement;
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
export type JzodMap = {
|
|
137
|
+
optional?: boolean | undefined;
|
|
138
|
+
nullable?: boolean | undefined;
|
|
139
|
+
extra?: {
|
|
140
|
+
[x: string]: any;
|
|
141
|
+
} | undefined;
|
|
142
|
+
type: "map";
|
|
143
|
+
definition: [
|
|
144
|
+
JzodElement,
|
|
145
|
+
JzodElement
|
|
146
|
+
];
|
|
147
|
+
};
|
|
148
|
+
export type JzodObject = {
|
|
149
|
+
optional?: boolean | undefined;
|
|
150
|
+
nullable?: boolean | undefined;
|
|
151
|
+
extra?: {
|
|
152
|
+
[x: string]: any;
|
|
153
|
+
} | undefined;
|
|
154
|
+
extend?: (JzodReference | JzodObject) | undefined;
|
|
155
|
+
type: "object";
|
|
156
|
+
nonStrict?: boolean | undefined;
|
|
157
|
+
definition: {
|
|
158
|
+
[x: string]: JzodElement;
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
export type JzodPromise = {
|
|
162
|
+
optional?: boolean | undefined;
|
|
163
|
+
nullable?: boolean | undefined;
|
|
164
|
+
extra?: {
|
|
165
|
+
[x: string]: any;
|
|
166
|
+
} | undefined;
|
|
167
|
+
type: "promise";
|
|
168
|
+
definition: JzodElement;
|
|
169
|
+
};
|
|
170
|
+
export type JzodRecord = {
|
|
171
|
+
optional?: boolean | undefined;
|
|
172
|
+
nullable?: boolean | undefined;
|
|
173
|
+
extra?: {
|
|
174
|
+
[x: string]: any;
|
|
175
|
+
} | undefined;
|
|
176
|
+
type: "record";
|
|
177
|
+
definition: JzodElement;
|
|
178
|
+
};
|
|
179
|
+
export type JzodReference = {
|
|
180
|
+
optional?: boolean | undefined;
|
|
181
|
+
nullable?: boolean | undefined;
|
|
182
|
+
extra?: {
|
|
183
|
+
[x: string]: any;
|
|
184
|
+
} | undefined;
|
|
185
|
+
type: "schemaReference";
|
|
186
|
+
context?: {
|
|
187
|
+
[x: string]: JzodElement;
|
|
188
|
+
} | undefined;
|
|
189
|
+
definition: {
|
|
190
|
+
eager?: boolean | undefined;
|
|
191
|
+
relativePath?: string | undefined;
|
|
192
|
+
absolutePath?: string | undefined;
|
|
193
|
+
};
|
|
194
|
+
};
|
|
195
|
+
export type JzodSet = {
|
|
196
|
+
optional?: boolean | undefined;
|
|
197
|
+
nullable?: boolean | undefined;
|
|
198
|
+
extra?: {
|
|
199
|
+
[x: string]: any;
|
|
200
|
+
} | undefined;
|
|
201
|
+
type: "set";
|
|
202
|
+
definition: JzodElement;
|
|
203
|
+
};
|
|
204
|
+
export type JzodTuple = {
|
|
205
|
+
optional?: boolean | undefined;
|
|
206
|
+
nullable?: boolean | undefined;
|
|
207
|
+
extra?: {
|
|
208
|
+
[x: string]: any;
|
|
209
|
+
} | undefined;
|
|
210
|
+
type: "tuple";
|
|
211
|
+
definition: JzodElement[];
|
|
212
|
+
};
|
|
213
|
+
export type JzodUnion = {
|
|
214
|
+
optional?: boolean | undefined;
|
|
215
|
+
nullable?: boolean | undefined;
|
|
216
|
+
extra?: {
|
|
217
|
+
[x: string]: any;
|
|
218
|
+
} | undefined;
|
|
219
|
+
type: "union";
|
|
220
|
+
discriminator?: string | undefined;
|
|
221
|
+
definition: JzodElement[];
|
|
222
|
+
};
|
|
223
|
+
export type JzodBootstrapElementSchema = JzodElement;
|
|
224
|
+
export declare const jzodBaseObject: z.ZodType<JzodBaseObject>;
|
|
225
|
+
export declare const jzodArray: z.ZodType<JzodArray>;
|
|
226
|
+
export declare const jzodAttribute: z.ZodType<JzodAttribute>;
|
|
227
|
+
export declare const jzodAttributeDateValidations: z.ZodType<JzodAttributeDateValidations>;
|
|
228
|
+
export declare const jzodAttributeDateWithValidations: z.ZodType<JzodAttributeDateWithValidations>;
|
|
229
|
+
export declare const jzodAttributeNumberValidations: z.ZodType<JzodAttributeNumberValidations>;
|
|
230
|
+
export declare const jzodAttributeNumberWithValidations: z.ZodType<JzodAttributeNumberWithValidations>;
|
|
231
|
+
export declare const jzodAttributeStringValidations: z.ZodType<JzodAttributeStringValidations>;
|
|
232
|
+
export declare const jzodAttributeStringWithValidations: z.ZodType<JzodAttributeStringWithValidations>;
|
|
233
|
+
export declare const jzodElement: z.ZodType<JzodElement>;
|
|
234
|
+
export declare const jzodEnum: z.ZodType<JzodEnum>;
|
|
235
|
+
export declare const jzodEnumAttributeTypes: z.ZodType<JzodEnumAttributeTypes>;
|
|
236
|
+
export declare const jzodEnumElementTypes: z.ZodType<JzodEnumElementTypes>;
|
|
237
|
+
export declare const jzodFunction: z.ZodType<JzodFunction>;
|
|
238
|
+
export declare const jzodLazy: z.ZodType<JzodLazy>;
|
|
239
|
+
export declare const jzodLiteral: z.ZodType<JzodLiteral>;
|
|
240
|
+
export declare const jzodIntersection: z.ZodType<JzodIntersection>;
|
|
241
|
+
export declare const jzodMap: z.ZodType<JzodMap>;
|
|
242
|
+
export declare const jzodObject: z.ZodType<JzodObject>;
|
|
243
|
+
export declare const jzodPromise: z.ZodType<JzodPromise>;
|
|
244
|
+
export declare const jzodRecord: z.ZodType<JzodRecord>;
|
|
245
|
+
export declare const jzodReference: z.ZodType<JzodReference>;
|
|
246
|
+
export declare const jzodSet: z.ZodType<JzodSet>;
|
|
247
|
+
export declare const jzodTuple: z.ZodType<JzodTuple>;
|
|
248
|
+
export declare const jzodUnion: z.ZodType<JzodUnion>;
|
|
249
|
+
export declare const jzodBootstrapElementSchema: z.ZodLazy<ZodType<JzodElement, z.ZodTypeDef, JzodElement>>;
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { jzodToTsCode, jzodToTsTypeAliasesAndZodText, TsTypeAliasesAndZodText as JzodTypeAliasesAndZodText, TsTypeAliases, } from "./JzodToTs";
|
|
2
2
|
export { printTsTypeAlias, printTsTypeAliases } from "./tools";
|
|
3
|
-
export { jzodArray, jzodAttribute, jzodAttributeDateValidations, jzodAttributeDateWithValidations, jzodAttributeNumberValidations, jzodAttributeNumberWithValidations, jzodAttributeStringValidations, jzodAttributeStringWithValidations, jzodElement, jzodEnumAttributeTypes, jzodEnumElementTypes, jzodEnum, jzodFunction, jzodIntersection, jzodLazy, jzodLiteral,
|
|
3
|
+
export { jzodArray, jzodAttribute, jzodAttributeDateValidations, jzodAttributeDateWithValidations, jzodAttributeNumberValidations, jzodAttributeNumberWithValidations, jzodAttributeStringValidations, jzodAttributeStringWithValidations, jzodElement, jzodEnumAttributeTypes, jzodEnumElementTypes, jzodEnum, jzodFunction, jzodIntersection, jzodLazy, jzodLiteral, jzodMap, jzodObject, jzodPromise, jzodRecord, jzodReference, jzodSet, jzodTuple, jzodUnion, JzodArray, JzodAttribute, JzodAttributeDateValidations, JzodAttributeDateWithValidations, JzodAttributeNumberValidations, JzodAttributeNumberWithValidations, JzodAttributeStringValidations, JzodAttributeStringWithValidations, JzodElement, JzodEnum, JzodEnumAttributeTypes, JzodFunction, JzodEnumElementTypes, JzodIntersection, JzodLazy, JzodLiteral, JzodMap, JzodObject, JzodPromise, JzodRecord, JzodReference, JzodSet, JzodUnion, JzodTuple, } from "./generated_jzodBootstrapElementSchema";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function generateZodSchemaFileFromJzodSchema(jzodElement: any, targetFileName: string, jzodSchemaVariableName: string): void;
|
package/package.json
CHANGED
|
@@ -1,38 +1,40 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@miroir-framework/jzod-ts",
|
|
3
|
-
"private": false,
|
|
4
|
-
"version": "0.
|
|
5
|
-
"description": "The Typescript-related functionalities of Jzod.",
|
|
6
|
-
"main": "dist/bundle.js",
|
|
7
|
-
"types": "dist/bundle.d.ts",
|
|
8
|
-
"type": "module",
|
|
9
|
-
"scripts": {
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"@
|
|
26
|
-
"@
|
|
27
|
-
"@
|
|
28
|
-
"@
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"rollup
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@miroir-framework/jzod-ts",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.6.1",
|
|
5
|
+
"description": "The Typescript-related functionalities of Jzod.",
|
|
6
|
+
"main": "dist/bundle.js",
|
|
7
|
+
"types": "dist/bundle.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"preBuild": "tsc --rootDir . --baseUrl . --module ESnext --target ES2017 --moduleResolution node --allowSyntheticDefaultImports --outdir ./tmp/ || node ./tmp/src/preBuild.js",
|
|
11
|
+
"compile": "npx tsc",
|
|
12
|
+
"build": "rollup --config",
|
|
13
|
+
"test": "cross-env NODE_ENV=test jest --verbose --silent=false -t"
|
|
14
|
+
},
|
|
15
|
+
"author": "Miroir Framework <miroir-framework@protonmail.com>",
|
|
16
|
+
"homepage": "https://github.com/miroir-framework/jzod/#readme",
|
|
17
|
+
"bugs": "https://github.com/miroir-framework/jzod/issues",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@miroir-framework/jzod": "0.6.1",
|
|
21
|
+
"zod": "^3.22.2",
|
|
22
|
+
"zod-to-ts": "^1.2.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@babel/plugin-transform-modules-commonjs": "^7.20.11",
|
|
26
|
+
"@babel/preset-env": "^7.22.5",
|
|
27
|
+
"@jest/globals": "^29.3.1",
|
|
28
|
+
"@rollup/plugin-commonjs": "^24.0.1",
|
|
29
|
+
"@rollup/plugin-json": "^6.0.0",
|
|
30
|
+
"@types/jest": "^29.2.4",
|
|
31
|
+
"babel-jest": "^29.4.0",
|
|
32
|
+
"rollup": "^3.10.1",
|
|
33
|
+
"rollup-plugin-dts": "^5.1.1",
|
|
34
|
+
"rollup-plugin-typescript2": "^0.34.1",
|
|
35
|
+
"ts-jest": "^29.0.3",
|
|
36
|
+
"tsconfig-paths": "^4.1.2",
|
|
37
|
+
"typescript": "5.0.3",
|
|
38
|
+
"zod-to-json-schema": "^3.21.2"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/JzodToTs.ts
CHANGED
|
@@ -1,109 +1,118 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
import {
|
|
3
|
-
import { createTypeAlias, printNode, withGetType, zodToTs } from "zod-to-ts";
|
|
4
|
-
|
|
5
|
-
import { jzodElementSchemaToZodSchemaAndDescription } from "@miroir-framework/jzod";
|
|
6
|
-
|
|
7
|
-
import { JzodElement } from "./
|
|
8
|
-
|
|
9
|
-
import { printTsTypeAliases } from "./tools";
|
|
10
|
-
// ################################################################################################
|
|
11
|
-
export type TsTypeAliases = {
|
|
12
|
-
[k: string]: ts.TypeAliasDeclaration;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// ################################################################################################
|
|
16
|
-
export interface TsTypeAliasesAndZodText {
|
|
17
|
-
contextTsTypeAliases: { [k: string]: ts.TypeAliasDeclaration },
|
|
18
|
-
contextZodText: { [k: string]: string },
|
|
19
|
-
mainTsTypeAlias: ts.TypeAliasDeclaration,
|
|
20
|
-
mainZodText: string
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// ################################################################################################
|
|
24
|
-
export function jzodToTsTypeAliasesAndZodText(
|
|
25
|
-
element: JzodElement,
|
|
26
|
-
typeName?: string,
|
|
27
|
-
): TsTypeAliasesAndZodText
|
|
28
|
-
{
|
|
29
|
-
const elementZodSchemaAndDescription = jzodElementSchemaToZodSchemaAndDescription(
|
|
30
|
-
element,
|
|
31
|
-
() => ({}),
|
|
32
|
-
() => ({}),
|
|
33
|
-
(innerReference:
|
|
34
|
-
withGetType(innerReference, (ts) => {
|
|
35
|
-
const actualTypeName = relativeReference?relativeReference.replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c):"";
|
|
36
|
-
return ts.factory.createTypeReferenceNode(
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
)
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
const contextTsTypesString = Object.fromEntries(
|
|
45
|
-
Object.entries(elementZodSchemaAndDescription.contextZodSchema ?? {}).map((curr) => {
|
|
46
|
-
const actualTypeName = curr[0]?curr[0].replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c):"";
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const
|
|
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
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { ZodTypeAny } from "zod";
|
|
3
|
+
import { GetType, createTypeAlias, printNode, withGetType, zodToTs } from "zod-to-ts";
|
|
4
|
+
|
|
5
|
+
import { jzodElementSchemaToZodSchemaAndDescription } from "@miroir-framework/jzod";
|
|
6
|
+
|
|
7
|
+
import { JzodElement } from "./generated_jzodBootstrapElementSchema";
|
|
8
|
+
|
|
9
|
+
import { printTsTypeAliases } from "./tools";
|
|
10
|
+
// ################################################################################################
|
|
11
|
+
export type TsTypeAliases = {
|
|
12
|
+
[k: string]: ts.TypeAliasDeclaration;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// ################################################################################################
|
|
16
|
+
export interface TsTypeAliasesAndZodText {
|
|
17
|
+
contextTsTypeAliases: { [k: string]: ts.TypeAliasDeclaration },
|
|
18
|
+
contextZodText: { [k: string]: string },
|
|
19
|
+
mainTsTypeAlias: ts.TypeAliasDeclaration,
|
|
20
|
+
mainZodText: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// ################################################################################################
|
|
24
|
+
export function jzodToTsTypeAliasesAndZodText(
|
|
25
|
+
element: JzodElement,
|
|
26
|
+
typeName?: string,
|
|
27
|
+
): TsTypeAliasesAndZodText
|
|
28
|
+
{
|
|
29
|
+
const elementZodSchemaAndDescription = jzodElementSchemaToZodSchemaAndDescription(
|
|
30
|
+
element,
|
|
31
|
+
() => ({}),
|
|
32
|
+
() => ({}),
|
|
33
|
+
(innerReference: ZodTypeAny & GetType, relativeReference: string | undefined) =>
|
|
34
|
+
withGetType(innerReference, (ts:any) => {
|
|
35
|
+
const actualTypeName = relativeReference?relativeReference.replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c):"";
|
|
36
|
+
return ts.factory.createTypeReferenceNode(
|
|
37
|
+
ts.factory.createIdentifier(actualTypeName ?? "RELATIVEPATH_NOT_DEFINED"),
|
|
38
|
+
undefined
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
)
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const contextTsTypesString = Object.fromEntries(
|
|
45
|
+
Object.entries(elementZodSchemaAndDescription.contextZodSchema ?? {}).map((curr) => {
|
|
46
|
+
const actualTypeName = curr[0]?curr[0].replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c):"";
|
|
47
|
+
const tsNode = zodToTs(curr[1], typeName).node;
|
|
48
|
+
const typeAlias = createTypeAlias(tsNode, actualTypeName);
|
|
49
|
+
return [curr[0], typeAlias];
|
|
50
|
+
})
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
const tsTypeStringNode = zodToTs(elementZodSchemaAndDescription.zodSchema, typeName).node;
|
|
54
|
+
const tsTypeStringTypeAlias = createTypeAlias(tsTypeStringNode, typeName??"");
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
contextTsTypeAliases: contextTsTypesString,
|
|
58
|
+
contextZodText: elementZodSchemaAndDescription.contextZodText??{},
|
|
59
|
+
mainZodText: elementZodSchemaAndDescription.zodText,
|
|
60
|
+
mainTsTypeAlias: tsTypeStringTypeAlias,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
// ################################################################################################
|
|
66
|
+
export function jzodToTsCode (
|
|
67
|
+
jzodElement: JzodElement,
|
|
68
|
+
exportPrefix: boolean = true,
|
|
69
|
+
typeName?: string,
|
|
70
|
+
// typeSchema: boolean = false,
|
|
71
|
+
typeAnotationForSchema: string[] = [],
|
|
72
|
+
): string {
|
|
73
|
+
// console.log(
|
|
74
|
+
// "################################### jzodToTsCode ",
|
|
75
|
+
// "jzodElement",
|
|
76
|
+
// JSON.stringify(jzodElement, null, 2)
|
|
77
|
+
// );
|
|
78
|
+
|
|
79
|
+
const schemaName = typeName?typeName.replace(/^(.)(.*)$/, (a, b, c) => b.toLowerCase() + c):"";
|
|
80
|
+
const actualTypeName = typeName?typeName.replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c):"";
|
|
81
|
+
|
|
82
|
+
// console.log("jzodToTsCode typeAnotationForSchema", typeAnotationForSchema);
|
|
83
|
+
|
|
84
|
+
const header = `import { ZodType, ZodTypeAny, z } from "zod";`;
|
|
85
|
+
|
|
86
|
+
const typeAliasesAndZodText = jzodToTsTypeAliasesAndZodText(jzodElement, actualTypeName);
|
|
87
|
+
|
|
88
|
+
const bodyJsCode = typeAnotationForSchema.includes(schemaName??"")
|
|
89
|
+
? `export const ${schemaName}: z.ZodType<${actualTypeName}> = ${typeAliasesAndZodText.mainZodText};`
|
|
90
|
+
: `export const ${schemaName} = ${typeAliasesAndZodText.mainZodText};`;
|
|
91
|
+
|
|
92
|
+
const contextTsTypesString = printTsTypeAliases(typeAliasesAndZodText.contextTsTypeAliases, exportPrefix);
|
|
93
|
+
// console.log("jzodToTsCode zod text for converted jzodElement",typeAliasesAndZodText.contextZodText);
|
|
94
|
+
|
|
95
|
+
const contextJsCode = typeAliasesAndZodText.contextZodText
|
|
96
|
+
? Object.entries(typeAliasesAndZodText.contextZodText).reduce((acc, curr) => {
|
|
97
|
+
const contextTypeName = curr[0]?curr[0].replace(/^(.)(.*)$/, (a, b, c) => b.toUpperCase() + c):"";
|
|
98
|
+
return typeAnotationForSchema.includes(curr[0])?
|
|
99
|
+
`${acc}
|
|
100
|
+
export const ${curr[0]}: z.ZodType<${contextTypeName}> = ${curr[1]};`
|
|
101
|
+
:
|
|
102
|
+
`${acc}
|
|
103
|
+
export const ${curr[0]} = ${curr[1]};`
|
|
104
|
+
;
|
|
105
|
+
}, "")
|
|
106
|
+
: ""
|
|
107
|
+
;
|
|
108
|
+
|
|
109
|
+
const tsTypesString = (exportPrefix?"export ":"") + printNode(typeAliasesAndZodText.mainTsTypeAlias);
|
|
110
|
+
// console.log("getTsCodeCorrespondingToZodSchemaAndDescription tsTypeString",tsTypesString);
|
|
111
|
+
|
|
112
|
+
return `${header}
|
|
113
|
+
${contextTsTypesString}
|
|
114
|
+
${tsTypesString}
|
|
115
|
+
${contextJsCode}
|
|
116
|
+
${bodyJsCode}
|
|
117
|
+
`;
|
|
118
|
+
}
|