Skip to content

Commit ab2718a

Browse files
committed
refactor(printer): 重构 faker 相关配置
- 将 fakerImportName 和 fakerImportFile从 PrinterOptions 中移除 - 在 runtimeMock 配置中添加 fakerImportName 和 fakerImportFile属性 - 优化了 faker 模块的导入逻辑
1 parent 7788aa6 commit ab2718a

2 files changed

Lines changed: 25 additions & 22 deletions

File tree

src/printer/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,12 @@ export class Printer {
370370
const {
371371
axiosImportName = '',
372372
axiosImportFile,
373-
fakerImportName = FAKER_IMPORT_NAME,
374-
fakerImportFile = FAKER_IMPORT_FILE,
375373
runtimeValidate,
376374
runtimeMock,
377375
} = this.options || {};
378376
const { cwd = '/', mainFile, typeFile = '.', zodFile = '.', mockFile = '.' } = this.configs;
379377
const axiosImportFile2 = axiosImportFile || AXIOS_IMPORT_FILE;
380378
const importPath = toImportPath(axiosImportFile2, cwd, mainFile);
381-
const fakerImportPath = toImportPath(fakerImportFile, cwd, mockFile);
382379

383380
this.#mainContent.push('import', [
384381
toImportString(AXIOS_IMPORT_NAME, axiosImportName, importPath),
@@ -407,6 +404,12 @@ export class Printer {
407404
}
408405

409406
if (runtimeMock) {
407+
const {
408+
fakerImportName = FAKER_IMPORT_NAME,
409+
fakerImportFile = FAKER_IMPORT_FILE,
410+
} = isBoolean(runtimeMock) ? {} : runtimeMock;
411+
412+
const fakerImportPath = toImportPath(fakerImportFile, cwd, mockFile);
410413
const zodNames = [...this.#respZodNames.values()].join(',');
411414
this.#mainContent.push('import', [
412415
`import ${ENABLE_MOCK_NAME} from "${toRelative(mockFile, mainFile)}";`,

src/printer/types.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,25 @@ export interface RuntimeMock {
5757
* @default process.env.NODE_ENV !== 'production'
5858
*/
5959
enableCondition?: string;
60-
};
60+
61+
/**
62+
* faker 导入名称,为空字符串时默认导入
63+
* faker 部分兼容 https://www.npmjs.com/package/@faker-js/faker
64+
* @default "faker"
65+
* @example
66+
* // 具名导入 fakerImportName=faker
67+
* import { faker } from 'path/to/faker';
68+
* // 默认导入(非具名导入)
69+
* import faker from 'path/to/faker';
70+
*/
71+
fakerImportName?: string;
72+
73+
/**
74+
* faker 模块的导入文件
75+
* @default @faker-js/faker
76+
*/
77+
fakerImportFile?: string;
78+
}
6179

6280
export interface PrinterOptions {
6381
/**
@@ -72,30 +90,12 @@ export interface PrinterOptions {
7290
*/
7391
axiosImportName?: string;
7492

75-
/**
76-
* faker 导入名称,为空字符串时默认导入
77-
* faker 部分兼容 https://www.npmjs.com/package/@faker-js/faker
78-
* @default "faker"
79-
* @example
80-
* // 具名导入 fakerImportName=faker
81-
* import { faker } from 'path/to/faker';
82-
* // 默认导入(非具名导入)
83-
* import faker from 'path/to/faker';
84-
*/
85-
fakerImportName?: string;
86-
8793
/**
8894
* axios 模块的导入文件
8995
* @default axios
9096
*/
9197
axiosImportFile?: string;
9298

93-
/**
94-
* faker 模块的导入文件
95-
* @default @faker-js/faker
96-
*/
97-
fakerImportFile?: string;
98-
9999
/**
100100
* 请求内容类型判断
101101
*/

0 commit comments

Comments
 (0)