Skip to content

Commit dc3637c

Browse files
committed
style: 代码优化等
1 parent c825eeb commit dc3637c

File tree

5 files changed

+13
-15
lines changed

5 files changed

+13
-15
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,9 @@ npm i -D openapi-axios
3333
const { defineConfig } = require('openapi-axios');
3434

3535
module.exports = defineConfig({
36-
openAPIs: [
37-
{
38-
// 将会生成 src/apis/swagger/petStore3.ts 文件
39-
name: 'swagger/petStore3',
40-
// 可以是一个 URL 链接或者本地路径(必须是 JSON 格式),或者一个 Open Api v3 文档对象
41-
document: 'https://petstore31.swagger.io/api/v31/openapi.json',
36+
modules: {
37+
// 将会生成 src/apis/petStore3.ts 文件
38+
'petStore3': 'https://petstore31.swagger.io/api/v31/openapi.json'
4239
},
4340
],
4441
});
@@ -49,7 +46,7 @@ module.exports = defineConfig({
4946
# 根据配置文件生成typescript文件
5047
npx openapi-axios
5148

52-
# 将会生成 src/apis/swagger/petStore3.ts 文件
49+
# 将会生成 src/apis/petStore3.ts 文件
5350
```
5451

5552
<details>
@@ -111,6 +108,7 @@ export type Pet = {
111108
// ... 省略 ...
112109

113110
/**
111+
* @module petStore3
114112
* @description Update an existing pet by Id
115113
* @summary Update an existing pet
116114
* @see pet Everything about your Pets {@link http://swagger.io Find out more}
@@ -134,7 +132,7 @@ export async function updatePet(data: Pet, config?: AxiosRequestConfig): AxiosPr
134132
然后你可以直接导入一个函数并使用它。 调用接口就像调用本地函数一样简单。
135133

136134
```ts
137-
import { updatePet } from '@/apis/swagger/petStore3';
135+
import { updatePet } from '@/apis/petStore3';
138136

139137
// 类型安全
140138
const { data: pet } = await updatePet({

src/generators/Generator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ export class Generator extends Emitter<GeneratorEmits> {
5353
this.emit('end', payload);
5454
}
5555

56-
protected async generateOpenAPI(index: number, count: number, name: string, openAPIOptions: OpenAPIOptions, generatorOptions: StrictGeneratorOptions) {
56+
protected async generateOpenAPI(index: number, count: number, module: string, openAPIOptions: OpenAPIOptions, generatorOptions: StrictGeneratorOptions) {
5757
const { cwd, dest, ...globalPrinter } = generatorOptions;
5858
const { document, ...scopePrinter } = openAPIOptions;
59-
const fileName = `${name}.ts`;
59+
const fileName = `${module}.ts`;
6060
const filePath = path.join(cwd, dest, fileName);
6161

6262
// 1. 参数合并
@@ -70,7 +70,7 @@ export class Generator extends Emitter<GeneratorEmits> {
7070
const makePayload = (step: GeneratingStage): GeneratingPayload => ({
7171
index,
7272
count,
73-
name,
73+
module,
7474
stage: step,
7575
options,
7676
filePath,
@@ -85,7 +85,7 @@ export class Generator extends Emitter<GeneratorEmits> {
8585
// 3. 输出
8686
this.emit('process', makePayload('printing'));
8787
const printer = new Printer(openAPIV3Document, printerOptions);
88-
const code = printer.print();
88+
const code = printer.print({ module });
8989

9090
// 4. 写入
9191
this.emit('process', makePayload('writing'));

src/generators/Logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class Logger {
1414
console.log(
1515
chalk.cyanBright('▷'),
1616
chalk.yellowBright(`${step}/${payload.count}`),
17-
payload.name,
17+
payload.module,
1818
payload.stage,
1919
payload.stage === 'generated' ? path.relative(payload.options.cwd, payload.filePath) : '',
2020
);

src/generators/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface GeneratorPayload {
4343
export interface GeneratingPayload {
4444
index: number;
4545
count: number;
46-
name: string;
46+
module: string;
4747
stage: GeneratingStage;
4848
options: GeneratingOptions;
4949
filePath: string;

test/generators/Logger.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test('Logger', async () => {
1212
logger.pipeProcessEvent({
1313
index: 5,
1414
count: 99,
15-
name: 'test',
15+
module: 'test',
1616
stage: 'generated',
1717
filePath: '/a/b/c/d/e/f',
1818
options: {

0 commit comments

Comments
 (0)