From 2efb5c51a4500b9ffe1cc54f4aef278952194eb1 Mon Sep 17 00:00:00 2001 From: geekact Date: Sun, 21 Jul 2024 10:49:10 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20getContentTypes=E9=87=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E4=B8=BApickContentType=E5=B9=B6=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E6=8A=BD=E8=B1=A1=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base-openapi-client.ts | 12 ++++---- src/lib/generate-template.ts | 21 ++------------ test/lib/generate-template.test.ts | 45 ++++-------------------------- test/lib/rebuild-dist.test.ts | 11 ++++---- test/mocks/mock-openapi-client.ts | 12 +------- 5 files changed, 22 insertions(+), 79 deletions(-) diff --git a/src/base-openapi-client.ts b/src/base-openapi-client.ts index f25dabc..d70fbae 100644 --- a/src/base-openapi-client.ts +++ b/src/base-openapi-client.ts @@ -53,7 +53,7 @@ export abstract class BaseOpenapiClient { method: Methods, opts: BaseOpenapiClient.FullOpts = {}, ) { - const contentTypes = this.getContentTypes(uri, method); + const contentTypes = this.pickContentTypes(uri, method); const requestBodyType = opts.requestBodyType || contentTypes[0] || 'application/json'; const responseType = opts.responseType || contentTypes[1] || 'json'; const headers = opts.headers || {}; @@ -79,11 +79,13 @@ export abstract class BaseOpenapiClient { ); } - protected abstract getContentTypes( - uri: string, - method: string, + protected pickContentTypes( + _uri: string, + _method: string, ): [ BaseOpenapiClient.UserInputOpts['requestBodyType'], BaseOpenapiClient.UserInputOpts['responseType'], - ]; + ] { + return [void 0, void 0]; + } } diff --git a/src/lib/generate-template.ts b/src/lib/generate-template.ts index b34e396..b620329 100644 --- a/src/lib/generate-template.ts +++ b/src/lib/generate-template.ts @@ -104,11 +104,6 @@ declare class ${className} extends BaseOpenapiClient< ): Promise<${className}_${method}_paths[K]['response']>`; }) .join('\n')} - - protected getContentTypes(uri: string, method: string) : [ - BaseOpenapiClient.UserInputOpts['requestBodyType'], - BaseOpenapiClient.UserInputOpts['responseType'], - ]; }`, js: ` var ${className} = class extends BaseOpenapiClient { @@ -121,7 +116,7 @@ var ${className} = class extends BaseOpenapiClient { }) .join('\n')} - getContentTypes(uri, method) { + pickContentTypes(uri, method) { return contentTypes${className}[method + " " + uri] || [void 0, void 0]; } };`, @@ -144,11 +139,6 @@ declare class ${className} extends BaseOpenapiClient< }); }) .join('\n')} - - protected getContentTypes(uri: string, method: string) : [ - BaseOpenapiClient.UserInputOpts['requestBodyType'], - BaseOpenapiClient.UserInputOpts['responseType'], - ]; }`, js: ` var ${className} = class extends BaseOpenapiClient { @@ -163,7 +153,7 @@ var ${className} = class extends BaseOpenapiClient { }) .join('\n')} - getContentTypes(uri, method) { + pickContentTypes(uri, method) { return contentTypes${className}[method + " " + uri] || [void 0, void 0]; } };`, @@ -200,11 +190,6 @@ declare class ${className} extends BaseOpenapiClient< }`; }) .join('\n')} - - protected getContentTypes(uri: string, method: string) : [ - BaseOpenapiClient.UserInputOpts['requestBodyType'], - BaseOpenapiClient.UserInputOpts['responseType'], - ]; }`, js: ` var ${className} = class extends BaseOpenapiClient { @@ -226,7 +211,7 @@ var ${className} = class extends BaseOpenapiClient { }) .join('\n')} - getContentTypes(uri, method) { + pickContentTypes(uri, method) { return contentTypes${className}[method + ' ' + uri] || [void 0, void 0]; } };`, diff --git a/test/lib/generate-template.test.ts b/test/lib/generate-template.test.ts index 122adc9..20a12a1 100644 --- a/test/lib/generate-template.test.ts +++ b/test/lib/generate-template.test.ts @@ -84,11 +84,6 @@ test('完整的类型提示', async () => { ? [opts?: OpenapiClient_get_paths[K]["request"] & BaseOpenapiClient.UserInputOpts] : [opts: OpenapiClient_get_paths[K]["request"] & BaseOpenapiClient.UserInputOpts] ): Promise; - - protected getContentTypes( - uri: string, - method: string, - ): [BaseOpenapiClient.UserInputOpts["requestBodyType"], BaseOpenapiClient.UserInputOpts["responseType"]]; } interface OpenapiClient_get_paths { @@ -113,7 +108,7 @@ test('完整的类型提示', async () => { return this.request(uri, "get", opts); } - getContentTypes(uri, method) { + pickContentTypes(uri, method) { return contentTypesOpenapiClient[method + " " + uri] || [void 0, void 0]; } }; @@ -152,11 +147,6 @@ test('完整的类型提示', async () => { opts?: OpenapiClient_get_paths["/users/{id}"]["request"] & BaseOpenapiClient.UserInputOpts, ): Promise; }; - - protected getContentTypes( - uri: string, - method: string, - ): [BaseOpenapiClient.UserInputOpts["requestBodyType"], BaseOpenapiClient.UserInputOpts["responseType"]]; } interface OpenapiClient_get_paths { @@ -186,7 +176,7 @@ test('完整的类型提示', async () => { }, }; - getContentTypes(uri, method) { + pickContentTypes(uri, method) { return contentTypesOpenapiClient[method + " " + uri] || [void 0, void 0]; } }; @@ -317,11 +307,6 @@ describe('类', () => { uri: K, ...rest: [opts?: Client_patch_paths[K]['request'] & BaseOpenapiClient.UserInputOpts] ): Promise; - - protected getContentTypes( - uri: string, - method: string, - ): [BaseOpenapiClient.UserInputOpts['requestBodyType'], BaseOpenapiClient.UserInputOpts['responseType']]; } " `); @@ -335,7 +320,7 @@ describe('类', () => { return this.request(uri, 'get', opts); } - getContentTypes(uri, method) { + pickContentTypes(uri, method) { return contentTypesClient[method + ' ' + uri] || [void 0, void 0]; } }; @@ -354,11 +339,6 @@ describe('类', () => { patchUsers( opts?: Client_patch_paths['/']['request'] & BaseOpenapiClient.UserInputOpts, ): Promise; - - protected getContentTypes( - uri: string, - method: string, - ): [BaseOpenapiClient.UserInputOpts['requestBodyType'], BaseOpenapiClient.UserInputOpts['responseType']]; } " `); @@ -372,7 +352,7 @@ describe('类', () => { return this.request('/', 'patch', opts); } - getContentTypes(uri, method) { + pickContentTypes(uri, method) { return contentTypesClient[method + ' ' + uri] || [void 0, void 0]; } }; @@ -416,11 +396,6 @@ describe('类', () => { ? [opts?: Client_get_paths[K]['request'] & BaseOpenapiClient.UserInputOpts] : [opts: Client_get_paths[K]['request'] & BaseOpenapiClient.UserInputOpts] ): Promise; - - protected getContentTypes( - uri: string, - method: string, - ): [BaseOpenapiClient.UserInputOpts['requestBodyType'], BaseOpenapiClient.UserInputOpts['responseType']]; } " `); @@ -449,11 +424,6 @@ describe('类', () => { uri: K, ...rest: [opts: Client_get_paths[K]['request'] & BaseOpenapiClient.UserInputOpts] ): Promise; - - protected getContentTypes( - uri: string, - method: string, - ): [BaseOpenapiClient.UserInputOpts['requestBodyType'], BaseOpenapiClient.UserInputOpts['responseType']]; } " `); @@ -478,11 +448,6 @@ describe('类', () => { opts?: Client_get_paths['/']['request'] & BaseOpenapiClient.UserInputOpts, ): Promise; }; - - protected getContentTypes( - uri: string, - method: string, - ): [BaseOpenapiClient.UserInputOpts['requestBodyType'], BaseOpenapiClient.UserInputOpts['responseType']]; } " `); @@ -503,7 +468,7 @@ describe('类', () => { }, }; - getContentTypes(uri, method) { + pickContentTypes(uri, method) { return contentTypesClient[method + ' ' + uri] || [void 0, void 0]; } }; diff --git a/test/lib/rebuild-dist.test.ts b/test/lib/rebuild-dist.test.ts index 0517f1f..8e4dc90 100644 --- a/test/lib/rebuild-dist.test.ts +++ b/test/lib/rebuild-dist.test.ts @@ -72,20 +72,21 @@ test('内容写入文件', async () => { bbbbb: string; }; + export { aaaaa }; + declare const foo = { bar: "baz" }; - export {foo, aaaaa }; - " + export { foo };" `); await expect(readFile(path.join(distDir, 'esm', 'index.js'), 'utf8')).resolves .toMatchInlineSnapshot(` "// test/fixtures/index.ts var aaaaa = { bbbbb: "ccccc" }; - var foo = { bar: "baz" } export { - foo, aaaaa }; - //# sourceMappingURL=index.js.map" + //# sourceMappingURL=index.js.map + var foo = { bar: "baz" } + export { foo };" `); }); diff --git a/test/mocks/mock-openapi-client.ts b/test/mocks/mock-openapi-client.ts index c617dce..9ea663a 100644 --- a/test/mocks/mock-openapi-client.ts +++ b/test/mocks/mock-openapi-client.ts @@ -1,13 +1,3 @@ import { BaseOpenapiClient } from '../../src'; -export class MockOpenapiClient extends BaseOpenapiClient { - protected override getContentTypes( - _uri: string, - _method: string, - ): [ - BaseOpenapiClient.UserInputOpts['requestBodyType'], - BaseOpenapiClient.UserInputOpts['responseType'], - ] { - return [void 0, void 0]; - } -} +export class MockOpenapiClient extends BaseOpenapiClient {}