From 7ef510ed3b75557a694058d332a3160fb807cae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=23=E4=BA=91=E6=B7=A1=E7=84=B6?= Date: Fri, 26 Jul 2024 12:24:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(printer):=20=E6=B7=BB=E5=8A=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B4=E5=B0=BE=E4=BF=A1=E6=81=AF=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO.md | 2 ++ src/printer/index.ts | 11 +++++++---- src/printer/types.ts | 13 ++++++++++++- test/printer/path.test.ts | 24 ++++++++++++------------ test/printer/ref-parameter.test.ts | 2 +- test/printer/ref-request.test.ts | 2 +- test/printer/ref-response.test.ts | 4 ++-- test/printer/schemas.test.ts | 26 +++++++++++++------------- test/printer/unique-name.test.ts | 2 +- test/printer/v31.test.ts | 2 +- 10 files changed, 52 insertions(+), 36 deletions(-) diff --git a/TODO.md b/TODO.md index 32dd5f7..2f95af8 100644 --- a/TODO.md +++ b/TODO.md @@ -1 +1,3 @@ + +- [ ] 默认选项提取 diff --git a/src/printer/index.ts b/src/printer/index.ts index 9f8a118..a528284 100644 --- a/src/printer/index.ts +++ b/src/printer/index.ts @@ -81,19 +81,22 @@ export class Printer { print(configs?: PrinterConfigs) { Object.assign(this.configs, configs); - const { hideLintComments, hideInfo, hideComponents, hideImports, hidePaths } = this.configs; - const eslintComments = [ + const { hideHeaders, hideFooters, hideInfo, hideComponents, hideImports, hidePaths } = this.configs; + const defaultHeaders = [ // '/* eslint-disable @typescript-eslint/ban-ts-comment */', '/* eslint-disable @typescript-eslint/no-explicit-any */', - ].join('\n'); + ]; + const headers = (this.options?.headers || defaultHeaders).join('\n'); + const footers = (this.options?.footers || []).join('\n'); return [ - !hideLintComments && eslintComments, + !hideHeaders && headers, !hideInfo && this._printInfo(), !hideImports && this._printImports(), !hideComponents && this._printComponents(), !hidePaths && this._printPaths(), + !hideFooters && footers, ] .filter(Boolean) .join('\n\n'); diff --git a/src/printer/types.ts b/src/printer/types.ts index 48d215b..a60a406 100644 --- a/src/printer/types.ts +++ b/src/printer/types.ts @@ -106,6 +106,16 @@ export type PrinterOptions = { * @returns {string} */ operationIdNormalize?: OperationIdNormalize; + + /** + * 生成文件的头部信息 + */ + headers?: string[]; + + /** + * 生成文件的尾部信息 + */ + footers?: string[]; }; export type PrinterConfigs = { @@ -119,7 +129,8 @@ export type PrinterConfigs = { */ file?: string; - hideLintComments?: boolean; + hideHeaders?: boolean; + hideFooters?: boolean; hideInfo?: boolean; hideImports?: boolean; hideComponents?: boolean; diff --git a/test/printer/path.test.ts b/test/printer/path.test.ts index da75d46..10612ce 100644 --- a/test/printer/path.test.ts +++ b/test/printer/path.test.ts @@ -16,7 +16,7 @@ test('1路径 + 1请求', () => { expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -94,7 +94,7 @@ test('1路径 + 2请求', () => { expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -154,7 +154,7 @@ test('1路径 + 1请求 + 1query', () => { expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -205,7 +205,7 @@ test('1路径 + 1请求 + 1query with duplicate', () => { expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -257,7 +257,7 @@ test('1路径 + 1请求 + 1path', () => { expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -315,7 +315,7 @@ test('1路径 + 1请求 + 2path', () => { expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -380,7 +380,7 @@ test('1路径 + 1请求 + 2query', () => { expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -456,7 +456,7 @@ test('1路径 + 1请求 + 2query + 1path', () => { expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -543,7 +543,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request primitive', () => { expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -638,7 +638,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request object', () => { expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -745,7 +745,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request object + 1response primitive expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -870,7 +870,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request object + 1response object', expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), diff --git a/test/printer/ref-parameter.test.ts b/test/printer/ref-parameter.test.ts index 0490da7..bd2606e 100644 --- a/test/printer/ref-parameter.test.ts +++ b/test/printer/ref-parameter.test.ts @@ -40,7 +40,7 @@ test('ref-parameter', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), diff --git a/test/printer/ref-request.test.ts b/test/printer/ref-request.test.ts index de0fbd3..c73205a 100644 --- a/test/printer/ref-request.test.ts +++ b/test/printer/ref-request.test.ts @@ -47,7 +47,7 @@ test('ref-request', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), diff --git a/test/printer/ref-response.test.ts b/test/printer/ref-response.test.ts index a602e37..52b36be 100644 --- a/test/printer/ref-response.test.ts +++ b/test/printer/ref-response.test.ts @@ -49,7 +49,7 @@ test('ref-response', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -132,7 +132,7 @@ test('ref-response in object', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), diff --git a/test/printer/schemas.test.ts b/test/printer/schemas.test.ts index 78d1541..c8565b4 100644 --- a/test/printer/schemas.test.ts +++ b/test/printer/schemas.test.ts @@ -19,7 +19,7 @@ test('number', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -52,7 +52,7 @@ test('number enum', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -85,7 +85,7 @@ test('[number, null] enum', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -119,7 +119,7 @@ test('type[]', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -173,7 +173,7 @@ test('AllOf primitive', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -229,7 +229,7 @@ test('explicit array', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -275,7 +275,7 @@ test('generic array', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -326,7 +326,7 @@ test('explicit object', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -375,7 +375,7 @@ test('generic object', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -406,7 +406,7 @@ test('additionalProperties true', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -435,7 +435,7 @@ test('additionalProperties false', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -475,7 +475,7 @@ test('additionalProperties schema type', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), @@ -524,7 +524,7 @@ test('additionalProperties schema ref', () => { }); expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), diff --git a/test/printer/unique-name.test.ts b/test/printer/unique-name.test.ts index 2fb9e15..82f446d 100644 --- a/test/printer/unique-name.test.ts +++ b/test/printer/unique-name.test.ts @@ -41,7 +41,7 @@ test('unique name', () => { expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }), diff --git a/test/printer/v31.test.ts b/test/printer/v31.test.ts index 5bb282b..e9128b7 100644 --- a/test/printer/v31.test.ts +++ b/test/printer/v31.test.ts @@ -46,7 +46,7 @@ test('v3.1 schema', () => { expect( printer.print({ - hideLintComments: true, + hideHeaders: true, hideInfo: true, hideImports: true, }),