Skip to content

Commit 235e4d6

Browse files
committed
feat: 打印时增加 module 信息输出
1 parent 47c638c commit 235e4d6

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/printer/index.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { record } from 'zod';
21
import { pkgName } from '../const';
32
import type { OpenApi3 } from '../types/openapi';
43
import { never } from '../utils/func';
@@ -24,7 +23,7 @@ import {
2423
import { JsDoc } from './JsDoc';
2524
import { Named } from './Named';
2625
import { Schemata } from './Schemata';
27-
import type { PrinterOptions, RequestStatusCodeMatch } from './types';
26+
import type { PrinterOptions, PrinterConfigs, RequestStatusCodeMatch } from './types';
2827

2928
const allowMethods = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'];
3029

@@ -36,6 +35,7 @@ type ResponseMatch = (statusCode: string, response: OpenApi3_Response) => boolea
3635
export class Printer {
3736
named = new Named();
3837
schemata = new Schemata(this.named);
38+
private configs: PrinterConfigs = {};
3939

4040
constructor(
4141
private readonly document: OpenApi3.Document,
@@ -76,13 +76,16 @@ export class Printer {
7676
});
7777
}
7878

79-
print(options?: { hideInfo?: boolean; hideImports?: boolean; hideComponents?: boolean; hidePaths?: boolean }) {
79+
print(configs?: PrinterConfigs) {
80+
Object.assign(this.configs, configs);
81+
const { hideInfo, hideComponents, hideImports, hidePaths } = this.configs;
82+
8083
return [
8184
//
82-
!options?.hideInfo && this._printInfo(),
83-
!options?.hideImports && this._printImports(),
84-
!options?.hideComponents && this._printComponents(),
85-
!options?.hidePaths && this._printPaths(),
85+
!hideInfo && this._printInfo(),
86+
!hideImports && this._printImports(),
87+
!hideComponents && this._printComponents(),
88+
!hidePaths && this._printPaths(),
8689
]
8790
.filter(Boolean)
8891
.join('\n\n');
@@ -121,6 +124,8 @@ export class Printer {
121124
const { externalDocs } = this.document;
122125
const { name, email, url } = contact || {};
123126
const jsDoc = new JsDoc();
127+
const { module } = this.configs;
128+
if (module) jsDoc.addComments({ module });
124129
const extDoc = JsDoc.printExternalDoc(externalDocs);
125130
jsDoc.addComments({
126131
title,
@@ -249,6 +254,8 @@ export class Printer {
249254
const respType = responseArgs.toType(0);
250255
const jsDoc = new JsDoc(this.document.tags);
251256
const comments = JsDoc.fromOperation(operation);
257+
const { module } = this.configs;
258+
if (module) jsDoc.addComments({ module });
252259
jsDoc.addComments(comments);
253260
jsDoc.addComments(requestArgs.toComments());
254261
jsDoc.addComments(responseArgs.toComments());

src/printer/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,15 @@ export type PrinterOptions = {
107107
*/
108108
operationIdNormalize?: OperationIdNormalize;
109109
};
110+
111+
export type PrinterConfigs = {
112+
/**
113+
* 表明当前 API 所在的模块
114+
*/
115+
module?: string;
116+
117+
hideInfo?: boolean;
118+
hideImports?: boolean;
119+
hideComponents?: boolean;
120+
hidePaths?: boolean;
121+
};

0 commit comments

Comments
 (0)