Skip to content

Commit

Permalink
feat: request file generate support parame type,and post method for f…
Browse files Browse the repository at this point in the history
…ix query data
  • Loading branch information
liangskyli committed Jul 10, 2022
1 parent 28e6ae2 commit 4282057
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 19 deletions.
60 changes: 45 additions & 15 deletions packages/openapi-gen-ts/src/gen/gen-interface-file.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import { colors, prettierData } from '@liangskyli/utils';
import fs from 'fs-extra';
import path from 'path';
import type prettier from 'prettier';
import { fileTip, packageName } from '../utils';
import type { IGenTsDataOpts } from './index';

type IOpts = {
genSchemaAPIAbsolutePath: string;
prettierOptions?: prettier.Options;
requestFilePath?: string;
requestQueryOmit: string[];
requestBodyOmit: string[];
};
} & Pick<
IGenTsDataOpts,
| 'prettierOptions'
| 'requestFilePath'
| 'requestParamsType'
| 'requestQueryOmit'
| 'requestBodyOmit'
>;

const genInterfaceFile = async (opts: IOpts) => {
const {
genSchemaAPIAbsolutePath,
prettierOptions,
requestFilePath,
requestQueryOmit,
requestBodyOmit,
requestQueryOmit = [],
requestBodyOmit = [],
} = opts;
let { requestParamsType = '' } = opts;

if (!requestFilePath) {
requestParamsType = 'AxiosRequestConfig';
const requestData: string[] = [];
requestData.push(`${fileTip}
import type { AxiosRequestConfig } from 'axios';
import axios from 'axios';
import type { IAPIRequest } from '${packageName}';
Expand All @@ -32,6 +38,7 @@ const genInterfaceFile = async (opts: IOpts) => {
};
export default request;
export { AxiosRequestConfig };
`);
const requestDataAbsolutePath = path.join(genSchemaAPIAbsolutePath, 'request.ts');
fs.writeFileSync(
Expand All @@ -49,6 +56,13 @@ const genInterfaceFile = async (opts: IOpts) => {

const requestAPI: string[] = [];
requestAPI.push(`${fileTip}
${
requestParamsType !== ''
? `import type {${requestParamsType} } from '${
requestFilePath ? requestFilePath : './request'
}';`
: ''
}
import request from '${requestFilePath ? requestFilePath : './request'}';
import type { IApi } from './interface-api';
`);
Expand Down Expand Up @@ -85,9 +99,9 @@ const genInterfaceFile = async (opts: IOpts) => {
) {
responseMediaType = 'text/plain';
}
haveQuery = !!itemValue.get?.properties?.parameters?.properties?.query?.properties;
haveQuery = !!itemValue.post?.properties?.parameters?.properties?.query?.properties;
haveBody =
itemValue.post?.properties?.requestBody?.properties?.content?.properties?.['text/plain'];
!!itemValue.post?.properties?.requestBody?.properties?.content?.properties?.['text/plain'];
if (haveBody) {
bodyMediaType = 'text/plain';
} else {
Expand All @@ -100,17 +114,33 @@ const genInterfaceFile = async (opts: IOpts) => {
if (method) {
const IConfigT: string[] = [];
if (haveQuery || haveBody) {
IConfigT.push('Omit<T, ');
IConfigT.push('Omit<T');
if (requestParamsType !== '') {
IConfigT.push(` & ${requestParamsType}, 'method' | 'url'`);
} else {
IConfigT.push(', ');
}
if (haveQuery) {
IConfigT.push('"params"');
if (requestParamsType !== '') {
IConfigT.push('| "params"');
} else {
IConfigT.push('"params"');
}
}
if (haveBody) {
if (haveQuery) {
IConfigT.push(' | ');
if (requestParamsType !== '') {
IConfigT.push('| "data"');
} else {
IConfigT.push('"data"');
}
IConfigT.push('"data"');
}
IConfigT.push('>,');
} else {
if (requestParamsType !== '') {
IConfigT.push(`Omit<T & ${requestParamsType}, 'method' | 'url'>,`);
} else {
IConfigT.push('T,');
}
}

interfaceAPIType.push(`'${item}': {`);
Expand Down
10 changes: 8 additions & 2 deletions packages/openapi-gen-ts/src/gen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export type IGenTsDataOpts = {
* 需要注意的是此文件必须是使用 export default 默认导出
*/
requestFilePath?: string;
/**
* 请求库文件里导出的请求库方法入参类型定义名称(非默认导出)
*/
requestParamsType?: string;
requestQueryOmit?: string[];
requestBodyOmit?: string[];
};
Expand All @@ -32,8 +36,9 @@ const genTsData = async (opts: IGenTsDataOpts) => {
openapiPath,
prettierOptions,
requestFilePath,
requestQueryOmit = [],
requestBodyOmit = [],
requestParamsType,
requestQueryOmit,
requestBodyOmit,
} = opts;

const genTsPath = path.join(genTsDir, 'schema-api');
Expand Down Expand Up @@ -70,6 +75,7 @@ const genTsData = async (opts: IGenTsDataOpts) => {
genSchemaAPIAbsolutePath: genTsAbsolutePath,
prettierOptions: copyOptions(prettierOptions),
requestFilePath,
requestParamsType,
requestQueryOmit,
requestBodyOmit,
});
Expand Down
3 changes: 2 additions & 1 deletion packages/openapi-gen-ts/test/http/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import genTsData from '../../src/index';
genTsData({
genTsDir: './test/gen-ts-dir',
openapiPath: './test/openapi/openapiv3-example.json',
//requestFilePath: '../../http/request',
requestFilePath: '../../http/request',
//requestFilePath: path.join(__dirname, './request'),
requestParamsType: 'AxiosRequestConfig',
//requestQueryOmit: ['activityId','b'],
//requestBodyOmit: ['a','b'],
}).then();
4 changes: 3 additions & 1 deletion packages/openapi-gen-ts/test/http/request.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { AxiosRequestConfig } from 'axios';
import axios from 'axios';
import type { IAPIRequest } from '@liangskyli/openapi-gen-ts';

const request = (config: AxiosRequestConfig) => {
const request: IAPIRequest = (config) => {
return axios(config).then((res) => res.data);
};

export default request;
export { AxiosRequestConfig };
20 changes: 20 additions & 0 deletions packages/openapi-gen-ts/test/openapi/openapiv3-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,16 @@
}
}
},
"parameters": [
{
"name": "queryParam1",
"in": "query",
"required": true,
"schema": {
"$ref": "#/components/schemas/IParam1"
}
}
],
"requestBody": {
"content": {
"application/json": {
Expand Down Expand Up @@ -1403,6 +1413,16 @@
}
}
},
"parameters": [
{
"name": "queryParam1",
"in": "query",
"required": true,
"schema": {
"type": "number"
}
}
],
"requestBody": {
"content": {
"text/plain": {
Expand Down

0 comments on commit 4282057

Please sign in to comment.