Skip to content

Commit 7788aa6

Browse files
committed
refactor(printer): 移除 zod 导入相关配置项
- 删除了 const.ts 中的 ZOD_IMPORT_FILE 常量 - 更新了 index.ts 中的 zod 导入逻辑,固定使用 'zod' 包 - 移除了 Parser.ts 中硬编码的 'z' 导入,改为使用 ZOD_IMPORT_NAME 常量 - 删除了 types.ts 中与 zod 导入相关的配置项 这些修改简化了 zod 的导入过程,并且不再支持自定义 zod 包路径的配置。
1 parent 511fdbb commit 7788aa6

4 files changed

Lines changed: 29 additions & 51 deletions

File tree

src/printer/Parser.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { OpenAPILatest } from '../types/openapi';
22
import type { OpenApiLatest_Schema } from './helpers';
33
import type { Named } from './Named';
44
import { isArray, isBoolean, isNever, isNumber, isString, isUndefined } from '../utils/type-is';
5+
import { ZOD_IMPORT_NAME } from './const';
56
import { isRefSchema, requiredTypeStringify, toZodName, withGroup } from './helpers';
67
import { JsDoc } from './JsDoc';
78

@@ -53,7 +54,7 @@ export class Parser {
5354

5455
if (isRefSchema(schema)) {
5556
const typeName = this.named.getRefType(schema.$ref);
56-
const zodName = typeName ? this.#prepareVarName(schema.$ref) : 'z.unknown()';
57+
const zodName = typeName ? this.#prepareVarName(schema.$ref) : `${ZOD_IMPORT_NAME}.unknown()`;
5758

5859
if (!typeName) {
5960
throw new Error(`未找到 refId: ${schema.$ref} 的类型`);
@@ -86,7 +87,7 @@ export class Parser {
8687
group.map(g => g.zod),
8788
{
8889
sep: ',',
89-
wrap: ['z.intersection(', ')'],
90+
wrap: [`${ZOD_IMPORT_NAME}.intersection(`, ')'],
9091
},
9192
),
9293
};
@@ -108,7 +109,7 @@ export class Parser {
108109
zod: withGroup(
109110
group.map(g => g.zod),
110111
{
111-
wrap: ['z.union([', '])'],
112+
wrap: [`${ZOD_IMPORT_NAME}.union([`, '])'],
112113
},
113114
),
114115
};
@@ -128,7 +129,7 @@ export class Parser {
128129
group.map(g => g.zod),
129130
{
130131
sep: ',',
131-
wrap: ['z.union([', '])'],
132+
wrap: [`${ZOD_IMPORT_NAME}.union([`, '])'],
132133
},
133134
),
134135
};
@@ -165,7 +166,7 @@ export class Parser {
165166
zod: withGroup(
166167
group.map(g => g.zod),
167168
{
168-
wrap: ['z.union([', '])'],
169+
wrap: [`${ZOD_IMPORT_NAME}.union([`, '])'],
169170
},
170171
),
171172
};
@@ -201,15 +202,15 @@ export class Parser {
201202
zod: enumValues.length > 0
202203
? withGroup(
203204
enumValues.map(e => (isString(e))
204-
? `z.literal(${JSON.stringify(e)})`
205+
? `${ZOD_IMPORT_NAME}.literal(${JSON.stringify(e)})`
205206
: this.#prepareVarName(e.$ref)),
206207
{
207-
wrap: ['z.union([', '])'],
208+
wrap: [`${ZOD_IMPORT_NAME}.union([`, '])'],
208209
},
209210
)
210211
: isBlob
211-
? 'z.instanceof(Blob)'
212-
: 'z.string()',
212+
? `${ZOD_IMPORT_NAME}.instanceof(Blob)`
213+
: `${ZOD_IMPORT_NAME}.string()`,
213214
};
214215
}
215216

@@ -242,13 +243,13 @@ export class Parser {
242243
zod: enumValues.length > 0
243244
? withGroup(
244245
enumValues.map(e => (isNumber(e)
245-
? `z.literal(${e})`
246+
? `${ZOD_IMPORT_NAME}.literal(${e})`
246247
: this.#prepareVarName(e.$ref))),
247248
{
248-
wrap: ['z.union([', '])'],
249+
wrap: [`${ZOD_IMPORT_NAME}.union([`, '])'],
249250
},
250251
)
251-
: 'z.number()',
252+
: `${ZOD_IMPORT_NAME}.number()`,
252253
};
253254
}
254255

@@ -273,14 +274,14 @@ export class Parser {
273274
zod: enumValues.length > 0
274275
? withGroup(
275276
enumValues.map(e => (isBoolean(e)
276-
? `z.literal(${e})`
277+
? `${ZOD_IMPORT_NAME}.literal(${e})`
277278
: this.#prepareVarName(e.$ref))),
278279
{
279280
sep: ',',
280-
wrap: ['z.union([', '])'],
281+
wrap: [`${ZOD_IMPORT_NAME}.union([`, '])'],
281282
},
282283
)
283-
: 'z.boolean()',
284+
: `${ZOD_IMPORT_NAME}.boolean()`,
284285
};
285286
}
286287

@@ -292,7 +293,7 @@ export class Parser {
292293
required,
293294
deps: this.#depNames,
294295
type,
295-
zod: 'z.null()',
296+
zod: `${ZOD_IMPORT_NAME}.null()`,
296297
};
297298
}
298299

@@ -342,7 +343,7 @@ export class Parser {
342343
required,
343344
deps: [],
344345
type: spec?.type || 'unknown',
345-
zod: spec?.zod || 'z.unknown()',
346+
zod: spec?.zod || `${ZOD_IMPORT_NAME}.unknown()`,
346347
};
347348
}
348349

@@ -360,7 +361,7 @@ export class Parser {
360361
required: false,
361362
deps: result.deps,
362363
type: `Array<${result.type}>`,
363-
zod: `z.array(${result.zod})`,
364+
zod: `${ZOD_IMPORT_NAME}.array(${result.zod})`,
364365
};
365366
}
366367

@@ -400,7 +401,7 @@ export class Parser {
400401
}));
401402
zodList.push(withGroup(propZodList, {
402403
sep: '\n',
403-
wrap: ['z.object({\n', '\n})'],
404+
wrap: [`${ZOD_IMPORT_NAME}.object({\n`, '\n})'],
404405
always: true,
405406
}));
406407
}
@@ -410,14 +411,14 @@ export class Parser {
410411
const { type, zod } = Parser.#parseInner(parser, genericProps as OpenApiLatest_Schema);
411412

412413
typeList.push(`Record<string, ${type}>`);
413-
zodList.push(`z.record(z.string(), ${zod})`);
414+
zodList.push(`${ZOD_IMPORT_NAME}.record(${ZOD_IMPORT_NAME}.string(), ${zod})`);
414415
}
415416

416417
// 无显式属性 && 无泛型属性
417418
if (typeList.length === 0) {
418419
return Parser.#parseAsUnknown(schema, required, {
419420
type: 'Record<string, unknown>',
420-
zod: 'z.record(z.string(), z.unknown())',
421+
zod: `${ZOD_IMPORT_NAME}.record(${ZOD_IMPORT_NAME}.string(), ${ZOD_IMPORT_NAME}.unknown())`,
421422
});
422423
}
423424

@@ -431,7 +432,7 @@ export class Parser {
431432
sep: '&',
432433
}),
433434
zod: withGroup(zodList, {
434-
wrap: ['z.intersection(', ')'],
435+
wrap: [`${ZOD_IMPORT_NAME}.intersection(`, ')'],
435436
}),
436437
};
437438
}
@@ -444,7 +445,7 @@ export class Parser {
444445

445446
return {
446447
type: [jsDoc.print(), `${JSON.stringify(propName)}${requiredTypeStringify(required)}${type};`].filter(Boolean).join('\n'),
447-
zod: `${JSON.stringify(propName)}: ${required ? zod : `z.optional(${zod})`},`,
448+
zod: `${JSON.stringify(propName)}: ${required ? zod : `${ZOD_IMPORT_NAME}.optional(${zod})`},`,
448449
};
449450
}
450451

@@ -454,7 +455,7 @@ export class Parser {
454455
deps: [],
455456
required: true,
456457
type: bool ? 'any' : 'never',
457-
zod: bool ? 'z.any()' : 'z.never()',
458+
zod: bool ? `${ZOD_IMPORT_NAME}.any()` : `${ZOD_IMPORT_NAME}.never()`,
458459
};
459460
}
460461
}

src/printer/const.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ export const AXIOS_IMPORT_NAME = 'axios';
7272
export const ZOD_IMPORT_NAME = 'z';
7373
export const FAKER_IMPORT_NAME = 'faker';
7474
export const AXIOS_IMPORT_FILE = 'axios';
75-
export const ZOD_IMPORT_FILE = 'zod';
7675
export const FAKER_IMPORT_FILE = '@faker-js/faker';
7776
export const AXIOS_PARAM_CONFIG_NAME = 'config';
7877
export const AXIOS_RESPONSE_NAME = 'resp';

src/printer/index.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {
2929
FAKER_IMPORT_FILE,
3030
FAKER_IMPORT_NAME,
3131
TYPE_FILE_EXPORT_NAME,
32-
ZOD_IMPORT_FILE,
3332
ZOD_IMPORT_NAME,
3433
} from './const';
3534
import { Content } from './Content';
@@ -371,8 +370,6 @@ export class Printer {
371370
const {
372371
axiosImportName = '',
373372
axiosImportFile,
374-
zodImportName = ZOD_IMPORT_NAME,
375-
zodImportFile = ZOD_IMPORT_FILE,
376373
fakerImportName = FAKER_IMPORT_NAME,
377374
fakerImportFile = FAKER_IMPORT_FILE,
378375
runtimeValidate,
@@ -381,7 +378,6 @@ export class Printer {
381378
const { cwd = '/', mainFile, typeFile = '.', zodFile = '.', mockFile = '.' } = this.configs;
382379
const axiosImportFile2 = axiosImportFile || AXIOS_IMPORT_FILE;
383380
const importPath = toImportPath(axiosImportFile2, cwd, mainFile);
384-
const zodImportPath = toImportPath(zodImportFile, cwd, mainFile);
385381
const fakerImportPath = toImportPath(fakerImportFile, cwd, mockFile);
386382

387383
this.#mainContent.push('import', [
@@ -397,9 +393,9 @@ export class Printer {
397393
}
398394

399395
// 依赖 zod
400-
if ((runtimeValidate || runtimeMock) && (!zodImportFile || zodImportFile === ZOD_IMPORT_FILE)) {
401-
if (!isPackageExists(ZOD_IMPORT_FILE)) {
402-
this.#zodContent.errors.push(`需要安装 ${ZOD_IMPORT_FILE}`);
396+
if ((runtimeValidate || runtimeMock)) {
397+
if (!isPackageExists('zod')) {
398+
this.#zodContent.errors.push(`需要安装 zod`);
403399
}
404400
}
405401

@@ -438,7 +434,7 @@ export class Printer {
438434
}
439435
}
440436

441-
this.#zodContent.push('import', toImportString(ZOD_IMPORT_NAME, zodImportName, zodImportPath));
437+
this.#zodContent.push('import', `import { ${ZOD_IMPORT_NAME} } from "zod";`);
442438
}
443439

444440
#printHeader() {

src/printer/types.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,6 @@ export interface PrinterOptions {
7272
*/
7373
axiosImportName?: string;
7474

75-
/**
76-
* zod 导入名称,为空字符串时默认导入
77-
* zod 部分兼容 https://www.npmjs.com/package/zod
78-
* @default "z"
79-
* @example
80-
* // 具名导入 zodImportName=z
81-
* import { z } from 'path/to/zod';
82-
* // 默认导入(非具名导入)
83-
* import z from 'path/to/zod';
84-
*/
85-
zodImportName?: string;
86-
8775
/**
8876
* faker 导入名称,为空字符串时默认导入
8977
* faker 部分兼容 https://www.npmjs.com/package/@faker-js/faker
@@ -102,12 +90,6 @@ export interface PrinterOptions {
10290
*/
10391
axiosImportFile?: string;
10492

105-
/**
106-
* zod 模块的导入文件
107-
* @default zod
108-
*/
109-
zodImportFile?: string;
110-
11193
/**
11294
* faker 模块的导入文件
11395
* @default @faker-js/faker

0 commit comments

Comments
 (0)