@umijs/zod2ts 4.0.0-canary-20240513.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017-present ChenCheng (sorrycc@gmail.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @umijs/zod2ts
2
+
3
+ See our website [umijs](https://umijs.org) for more information.
@@ -0,0 +1 @@
1
+ export { zodToTs } from './zod';
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/index.ts
20
+ var src_exports = {};
21
+ __export(src_exports, {
22
+ zodToTs: () => import_zod.zodToTs
23
+ });
24
+ module.exports = __toCommonJS(src_exports);
25
+ var import_zod = require("./zod");
26
+ // Annotate the CommonJS export names for ESM import in node:
27
+ 0 && (module.exports = {
28
+ zodToTs
29
+ });
@@ -0,0 +1,8 @@
1
+ import type { ZodTypeAny } from '@umijs/utils/compiled/zod';
2
+ export declare type LiteralType = string | number | boolean;
3
+ export declare type ZodToTsOptions = {};
4
+ export interface IZodToTsOpts {
5
+ zod: ZodTypeAny;
6
+ identifier?: string;
7
+ options?: ZodToTsOptions;
8
+ }
package/dist/types.js ADDED
@@ -0,0 +1,17 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
14
+
15
+ // src/types.ts
16
+ var types_exports = {};
17
+ module.exports = __toCommonJS(types_exports);
package/dist/zod.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { IZodToTsOpts } from './types';
2
+ export declare const zodToTs: (opts: IZodToTsOpts) => string;
package/dist/zod.js ADDED
@@ -0,0 +1,234 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/zod.ts
20
+ var zod_exports = {};
21
+ __export(zod_exports, {
22
+ zodToTs: () => zodToTs
23
+ });
24
+ module.exports = __toCommonJS(zod_exports);
25
+ var zodToTs = (opts) => {
26
+ const { zod, identifier = "Identifier", options = {} } = opts;
27
+ const { typeName } = zod == null ? void 0 : zod._def;
28
+ const props = { identifier, options };
29
+ switch (typeName) {
30
+ case "ZodString":
31
+ return "string";
32
+ case "ZodNumber":
33
+ return "number";
34
+ case "ZodBigInt":
35
+ return "bigint";
36
+ case "ZodBoolean":
37
+ return "boolean";
38
+ case "ZodDate":
39
+ return "Date";
40
+ case "ZodUndefined":
41
+ return "undefined";
42
+ case "ZodNull":
43
+ return "null";
44
+ case "ZodVoid":
45
+ return "(void | undefined)";
46
+ case "ZodAny":
47
+ return "any";
48
+ case "ZodUnknown":
49
+ return "unknown";
50
+ case "ZodNever":
51
+ return "never";
52
+ case "ZodLazy": {
53
+ return "any";
54
+ }
55
+ case "ZodLiteral": {
56
+ let literal;
57
+ const literalValue = zod._def.value;
58
+ switch (typeof literalValue) {
59
+ case "number":
60
+ literal = "number";
61
+ break;
62
+ case "boolean":
63
+ if (literalValue === true) {
64
+ literal = "true";
65
+ } else {
66
+ literal = "false";
67
+ }
68
+ break;
69
+ default:
70
+ literal = `"${literalValue}"`;
71
+ break;
72
+ }
73
+ return literal;
74
+ }
75
+ case "ZodObject": {
76
+ const properties = Object.entries(zod._def.shape());
77
+ const members = properties.map(([key, value]) => {
78
+ const nextZodNode = value;
79
+ const type = zodToTs({
80
+ zod: nextZodNode,
81
+ ...props
82
+ });
83
+ const { typeName: nextZodNodeTypeName } = nextZodNode._def;
84
+ const isOptional = nextZodNodeTypeName === "ZodOptional" || nextZodNode.isOptional();
85
+ let desc;
86
+ if (nextZodNode.description) {
87
+ desc = nextZodNode.description;
88
+ }
89
+ if (key.includes("-")) {
90
+ key = `"${key}"`;
91
+ }
92
+ if (isOptional) {
93
+ key = `${key}?`;
94
+ }
95
+ return [key, type, desc];
96
+ });
97
+ return `{
98
+ ${members.map(([key, type, desc]) => {
99
+ return [
100
+ (desc == null ? void 0 : desc.length) && ` /** ${desc} */`,
101
+ /* */
102
+ ` ${key}: ${type};`
103
+ ].filter(Boolean).join("\n");
104
+ }).join("\n")}
105
+ }`;
106
+ }
107
+ case "ZodArray": {
108
+ const type = zodToTs({ zod: zod._def.type, ...props });
109
+ const node = `Array<${type}>`;
110
+ return node;
111
+ }
112
+ case "ZodEnum": {
113
+ const types = zod._def.values.map((value) => {
114
+ return `"${value}"`;
115
+ });
116
+ const union = types.join(" | ");
117
+ return union;
118
+ }
119
+ case "ZodUnion": {
120
+ const options2 = zod._def.options;
121
+ const types = options2.map((option) => zodToTs({ zod: option }));
122
+ return types.join(" | ");
123
+ }
124
+ case "ZodDiscriminatedUnion": {
125
+ const options2 = [...zod._def.options.values()];
126
+ const types = options2.map(
127
+ (option) => zodToTs({ zod: option, ...props })
128
+ );
129
+ return types.join(" | ");
130
+ }
131
+ case "ZodEffects": {
132
+ const node = zodToTs({
133
+ zod: zod._def.schema,
134
+ ...props
135
+ });
136
+ return node;
137
+ }
138
+ case "ZodNativeEnum": {
139
+ return "any";
140
+ }
141
+ case "ZodOptional": {
142
+ const innerType = zodToTs({
143
+ zod: zod._def.innerType,
144
+ ...props
145
+ });
146
+ return `(${innerType} | undefined)`;
147
+ }
148
+ case "ZodNullable": {
149
+ const innerType = zodToTs({
150
+ zod: zod._def.innerType,
151
+ ...props
152
+ });
153
+ return `(${innerType} | null)`;
154
+ }
155
+ case "ZodTuple": {
156
+ const types = zod._def.items.map(
157
+ (option) => zodToTs({ zod: option, ...props })
158
+ );
159
+ return `[${types.join(", ")}]`;
160
+ }
161
+ case "ZodRecord": {
162
+ const valueType = zodToTs({
163
+ zod: zod._def.valueType,
164
+ ...props
165
+ });
166
+ return `{ [x: string]: ${valueType} }`;
167
+ }
168
+ case "ZodMap": {
169
+ const valueType = zodToTs({
170
+ zod: zod._def.valueType,
171
+ ...props
172
+ });
173
+ const keyType = zodToTs({
174
+ zod: zod._def.keyType,
175
+ ...props
176
+ });
177
+ return `Map<${keyType}, ${valueType}>`;
178
+ }
179
+ case "ZodSet": {
180
+ const type = zodToTs({
181
+ zod: zod._def.valueType,
182
+ ...props
183
+ });
184
+ return `Set<${type}>`;
185
+ }
186
+ case "ZodIntersection": {
187
+ const left = zodToTs({
188
+ zod: zod._def.left,
189
+ ...props
190
+ });
191
+ const right = zodToTs({
192
+ zod: zod._def.right,
193
+ ...props
194
+ });
195
+ return `${left} & ${right}`;
196
+ }
197
+ case "ZodPromise": {
198
+ const type = zodToTs({
199
+ zod: zod._def.type,
200
+ ...props
201
+ });
202
+ return `Promise<${type}>`;
203
+ }
204
+ case "ZodFunction": {
205
+ const argTypes = zod._def.args._def.items.map(
206
+ (arg, index) => {
207
+ const argType = zodToTs({
208
+ zod: arg,
209
+ ...props
210
+ });
211
+ return `args_${index}: ${argType}`;
212
+ }
213
+ );
214
+ argTypes.push("...args: any[]");
215
+ const returnType = zodToTs({
216
+ zod: zod._def.returns,
217
+ ...props
218
+ });
219
+ return `((${argTypes.join(", ")}) => ${returnType})`;
220
+ }
221
+ case "ZodDefault": {
222
+ const type = zodToTs({
223
+ zod: zod._def.innerType,
224
+ ...props
225
+ });
226
+ return type;
227
+ }
228
+ }
229
+ return "any";
230
+ };
231
+ // Annotate the CommonJS export names for ESM import in node:
232
+ 0 && (module.exports = {
233
+ zodToTs
234
+ });
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@umijs/zod2ts",
3
+ "version": "4.0.0-canary-20240513.3",
4
+ "description": "@umijs/zod2ts",
5
+ "homepage": "https://github.com/umijs/umi/tree/master/packages/zod2ts#readme",
6
+ "bugs": "https://github.com/umijs/umi/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/umijs/umi"
10
+ },
11
+ "license": "MIT",
12
+ "main": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "authors": [
21
+ "chencheng <sorrycc@gmail.com> (https://github.com/sorrycc)"
22
+ ],
23
+ "scripts": {
24
+ "build": "umi-scripts father build",
25
+ "build:deps": "umi-scripts bundleDeps",
26
+ "dev": "umi-scripts father dev",
27
+ "test": "umi-scripts jest-turbo"
28
+ }
29
+ }