Skip to content

Commit

Permalink
feat: support router path params generator and request-api add path p…
Browse files Browse the repository at this point in the history
…arams
  • Loading branch information
liangskyli committed May 28, 2023
1 parent 22b9f82 commit 331ba0f
Show file tree
Hide file tree
Showing 26 changed files with 6,942 additions and 1,384 deletions.
5 changes: 5 additions & 0 deletions packages/openapi-gen-ts/src/gen/file/gen-interface-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class GenInterfaceApi {
public body(bodyOpts: IGenInterfaceAPITypeBodyOpts) {
const {
haveQuery,
havePath,
haveBody,
bodyMediaTypes,
responseMediaType,
Expand All @@ -60,6 +61,10 @@ export class GenInterfaceApi {
: `Query: ${queryInterface};`,
);
}
if (havePath) {
const pathInterface = `paths['${url}']['${method}']['parameters']['path']`;
this.interfaceAPIType.push(`Path: ${pathInterface};`);
}
if (haveBody) {
const omitKeys = requestBodyOmit
.map((omitItem) => `'${omitItem}'`)
Expand Down
25 changes: 19 additions & 6 deletions packages/openapi-gen-ts/src/gen/file/gen-request-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class GenRequestApi {
public body(bodyOpts: IGenRequestAPIBodyOpts) {
const {
haveQuery,
havePath,
haveBody,
methodObjRequired,
method,
Expand Down Expand Up @@ -109,29 +110,41 @@ export class GenRequestApi {
config: IConfig<
${IConfigT.join('')}
`);
this.requestAPI.push(!haveQuery && !haveBody ? 'Record<any, any>' : '{');
this.requestAPI.push(
!haveQuery && !havePath && !haveBody ? 'Record<any, any>' : '{',
);
if (haveQuery) {
this.requestAPI.push(`params: IApi['${url}']['Query'];`);
}
if (havePath) {
this.requestAPI.push(`path: IApi['${url}']['Path'];`);
}
if (haveBody) {
const requestBodyRequired =
methodObjRequired.find((item) => item === 'requestBody') !== undefined;
this.requestAPI.push(
`data${requestBodyRequired ? '' : '?'}: IApi['${url}']['Body'];`,
);
}
if (haveQuery || haveBody) {
if (haveQuery || havePath || haveBody) {
this.requestAPI.push('}');
}
const noPathFinalURL = `const finalURL = '${url}';`;
const havePathFinalURL = `
let finalURL = '${url}';
for (const [k, v] of Object.entries(path)) {
finalURL = finalURL.replace(\`{\${k}}\`, encodeURIComponent(String(v)));
}
`;
this.requestAPI.push(`>,
): Promise<IApi['${url}']['Response']> => {
const { ${haveQuery ? 'params,' : ''} ${
haveBody ? 'data,' : ''
} ...otherConfig } = config;
const { ${haveQuery ? 'params,' : ''} ${havePath ? 'path,' : ''}
${haveBody ? 'data,' : ''} ...otherConfig } = config;
${havePath ? havePathFinalURL : noPathFinalURL}
return request({
method: '${method}',
url: '${url}',
url: finalURL,
${haveQuery ? 'params: params,' : ''}
${haveBody ? 'data: data,' : ''}
...otherConfig,
Expand Down
3 changes: 3 additions & 0 deletions packages/openapi-gen-ts/src/gen/gen-interface-request-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const processMethodMediaData = (
) => {
const haveQuery =
!!itemValue![method]?.properties?.parameters?.properties?.query?.properties;
const havePath =
!!itemValue![method]?.properties?.parameters?.properties?.path?.properties;

let responseMediaType: string | undefined;
const methodObjRequired = itemValue![method]?.required ?? [];
Expand Down Expand Up @@ -59,6 +61,7 @@ const processMethodMediaData = (
}
return {
haveQuery,
havePath,
haveBody,
bodyMediaTypes,
responseMediaType,
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-gen-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type IAPIRequest = (param: {
url?: string;
method?: OpenapiMethod | Uppercase<OpenapiMethod> | string;
params?: any;
path?: any;
data?: any;
[k: string]: any;
}) => Promise<any>;
Expand Down
10 changes: 9 additions & 1 deletion packages/openapi-gen-ts/test/all-gen-dirs/gen-ts-dir/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { requestApi } from './schema-api/request-api';

requestApi['/root/v4/getQueryParams2-v4/{id}']({
baseURL: 'd',
params: { activityBases: [] },
path: { id: 1 },
}).then((res) => {
console.log(res);
});

requestApi['/root/v1/postBody2']({
baseURL: 'd',
data: { param1: '1', param2: null },
Expand All @@ -9,7 +17,7 @@ requestApi['/root/v1/postBody2']({

requestApi['/root/v1/postBody1']({
baseURL: 'd',
url: 'd',
// url: 'd',
params: { queryParam1: { param1: 1 } },
data: { postBody1param1: '1', tow: [] },
}).then((res) => {
Expand Down
Loading

0 comments on commit 331ba0f

Please sign in to comment.