Skip to content

Commit

Permalink
refactor: getContentTypes重命名为pickContentType并取消抽象方法
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Jul 21, 2024
1 parent c9735c6 commit 2efb5c5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 79 deletions.
12 changes: 7 additions & 5 deletions src/base-openapi-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export abstract class BaseOpenapiClient<T extends object = object> {
method: Methods,
opts: BaseOpenapiClient.FullOpts<T> = {},
) {
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 || {};
Expand All @@ -79,11 +79,13 @@ export abstract class BaseOpenapiClient<T extends object = object> {
);
}

protected abstract getContentTypes(
uri: string,
method: string,
protected pickContentTypes(
_uri: string,
_method: string,
): [
BaseOpenapiClient.UserInputOpts['requestBodyType'],
BaseOpenapiClient.UserInputOpts['responseType'],
];
] {
return [void 0, void 0];
}
}
21 changes: 3 additions & 18 deletions src/lib/generate-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,6 @@ declare class ${className}<T extends object = object> 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 {
Expand All @@ -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];
}
};`,
Expand All @@ -144,11 +139,6 @@ declare class ${className}<T extends object = object> extends BaseOpenapiClient<
});
})
.join('\n')}
protected getContentTypes(uri: string, method: string) : [
BaseOpenapiClient.UserInputOpts['requestBodyType'],
BaseOpenapiClient.UserInputOpts['responseType'],
];
}`,
js: `
var ${className} = class extends BaseOpenapiClient {
Expand All @@ -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];
}
};`,
Expand Down Expand Up @@ -200,11 +190,6 @@ declare class ${className}<T extends object = object> extends BaseOpenapiClient<
}`;
})
.join('\n')}
protected getContentTypes(uri: string, method: string) : [
BaseOpenapiClient.UserInputOpts['requestBodyType'],
BaseOpenapiClient.UserInputOpts['responseType'],
];
}`,
js: `
var ${className} = class extends BaseOpenapiClient {
Expand All @@ -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];
}
};`,
Expand Down
45 changes: 5 additions & 40 deletions test/lib/generate-template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ test('完整的类型提示', async () => {
? [opts?: OpenapiClient_get_paths[K]["request"] & BaseOpenapiClient.UserInputOpts<T>]
: [opts: OpenapiClient_get_paths[K]["request"] & BaseOpenapiClient.UserInputOpts<T>]
): Promise<OpenapiClient_get_paths[K]["response"]>;
protected getContentTypes(
uri: string,
method: string,
): [BaseOpenapiClient.UserInputOpts["requestBodyType"], BaseOpenapiClient.UserInputOpts["responseType"]];
}
interface OpenapiClient_get_paths {
Expand All @@ -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];
}
};
Expand Down Expand Up @@ -152,11 +147,6 @@ test('完整的类型提示', async () => {
opts?: OpenapiClient_get_paths["/users/{id}"]["request"] & BaseOpenapiClient.UserInputOpts<T>,
): Promise<OpenapiClient_get_paths["/users/{id}"]["response"]>;
};
protected getContentTypes(
uri: string,
method: string,
): [BaseOpenapiClient.UserInputOpts["requestBodyType"], BaseOpenapiClient.UserInputOpts["responseType"]];
}
interface OpenapiClient_get_paths {
Expand Down Expand Up @@ -186,7 +176,7 @@ test('完整的类型提示', async () => {
},
};
getContentTypes(uri, method) {
pickContentTypes(uri, method) {
return contentTypesOpenapiClient[method + " " + uri] || [void 0, void 0];
}
};
Expand Down Expand Up @@ -317,11 +307,6 @@ describe('类', () => {
uri: K,
...rest: [opts?: Client_patch_paths[K]['request'] & BaseOpenapiClient.UserInputOpts<T>]
): Promise<Client_patch_paths[K]['response']>;
protected getContentTypes(
uri: string,
method: string,
): [BaseOpenapiClient.UserInputOpts['requestBodyType'], BaseOpenapiClient.UserInputOpts['responseType']];
}
"
`);
Expand All @@ -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];
}
};
Expand All @@ -354,11 +339,6 @@ describe('类', () => {
patchUsers(
opts?: Client_patch_paths['/']['request'] & BaseOpenapiClient.UserInputOpts<T>,
): Promise<Client_patch_paths['/']['response']>;
protected getContentTypes(
uri: string,
method: string,
): [BaseOpenapiClient.UserInputOpts['requestBodyType'], BaseOpenapiClient.UserInputOpts['responseType']];
}
"
`);
Expand All @@ -372,7 +352,7 @@ describe('类', () => {
return this.request('/', 'patch', opts);
}
getContentTypes(uri, method) {
pickContentTypes(uri, method) {
return contentTypesClient[method + ' ' + uri] || [void 0, void 0];
}
};
Expand Down Expand Up @@ -416,11 +396,6 @@ describe('类', () => {
? [opts?: Client_get_paths[K]['request'] & BaseOpenapiClient.UserInputOpts<T>]
: [opts: Client_get_paths[K]['request'] & BaseOpenapiClient.UserInputOpts<T>]
): Promise<Client_get_paths[K]['response']>;
protected getContentTypes(
uri: string,
method: string,
): [BaseOpenapiClient.UserInputOpts['requestBodyType'], BaseOpenapiClient.UserInputOpts['responseType']];
}
"
`);
Expand Down Expand Up @@ -449,11 +424,6 @@ describe('类', () => {
uri: K,
...rest: [opts: Client_get_paths[K]['request'] & BaseOpenapiClient.UserInputOpts<T>]
): Promise<Client_get_paths[K]['response']>;
protected getContentTypes(
uri: string,
method: string,
): [BaseOpenapiClient.UserInputOpts['requestBodyType'], BaseOpenapiClient.UserInputOpts['responseType']];
}
"
`);
Expand All @@ -478,11 +448,6 @@ describe('类', () => {
opts?: Client_get_paths['/']['request'] & BaseOpenapiClient.UserInputOpts<T>,
): Promise<Client_get_paths['/']['response']>;
};
protected getContentTypes(
uri: string,
method: string,
): [BaseOpenapiClient.UserInputOpts['requestBodyType'], BaseOpenapiClient.UserInputOpts['responseType']];
}
"
`);
Expand All @@ -503,7 +468,7 @@ describe('类', () => {
},
};
getContentTypes(uri, method) {
pickContentTypes(uri, method) {
return contentTypesClient[method + ' ' + uri] || [void 0, void 0];
}
};
Expand Down
11 changes: 6 additions & 5 deletions test/lib/rebuild-dist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };"
`);
});
12 changes: 1 addition & 11 deletions test/mocks/mock-openapi-client.ts
Original file line number Diff line number Diff line change
@@ -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 {}

0 comments on commit 2efb5c5

Please sign in to comment.