Skip to content

Commit 29312cc

Browse files
committed
feat: 忽略未明确的 path 参数
1 parent 62f63ad commit 29312cc

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

src/printer/Arg.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,27 @@ export class Arg {
3131
readonly isSingle: boolean = false,
3232
) {}
3333

34+
url: string = '';
35+
urlParams: string[] = [];
36+
setUrl(url: string) {
37+
this.url = url;
38+
url.replace(/\{(.*?)}/g, (_, name) => {
39+
this.urlParams.push(name);
40+
return _;
41+
});
42+
}
43+
44+
defaultType = '';
45+
setDefaultType(type: string) {
46+
this.defaultType = type;
47+
}
48+
3449
add(parameter?: OpenApiLatest_Parameter) {
3550
if (!parameter) return;
3651

52+
// 忽略 url 无参数的情况
53+
if (this.kind === 'path' && this.urlParams.length === 0) return;
54+
3755
this.parameters.push(parameter);
3856
}
3957

@@ -72,7 +90,7 @@ export class Arg {
7290
uniqueName: this.named.nextVarName(this.kind),
7391
// 路径参数必填
7492
required: true,
75-
type: '',
93+
type: this.defaultType,
7694
comments: {},
7795
props,
7896
};
@@ -85,7 +103,7 @@ export class Arg {
85103
originName: this.kind,
86104
uniqueName: name,
87105
required: false,
88-
type: '',
106+
type: this.defaultType,
89107
comments: {
90108
[`param [${name}]`]: 'request config',
91109
},

src/printer/Args.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ export class Args {
2424
);
2525
}
2626

27-
toArgs(configTypeName: string) {
27+
toArgs() {
2828
return this.fixedArgs
29-
.filter((fixArg) => fixArg.arg.kind !== 'path' || fixArg.type !== '')
29+
.filter((fixArg) => fixArg.type !== '')
3030
.map((fixArg) => {
31-
return `${fixArg.uniqueName}${requiredTypeStringify(fixArg.required)}${fixArg.type || configTypeName}`;
31+
return `${fixArg.uniqueName}${requiredTypeStringify(fixArg.required)}${fixArg.type}`;
3232
})
3333
.join(',');
3434
}
@@ -37,15 +37,15 @@ export class Args {
3737
return this.fixedArgs[index]?.type || 'unknown';
3838
}
3939

40-
toValues(url: string) {
40+
toValues() {
4141
return this.fixedArgs
4242
.map((fixedArg) => {
4343
const { originName, uniqueName, arg, props } = fixedArg;
4444

4545
if (arg.kind === 'config') return `...${uniqueName}`;
4646

4747
if (arg.kind === 'path') {
48-
const resolvedURL = url.replace(/\{(.*?)}/g, (_, name) => {
48+
const resolvedURL = arg.url.replace(/\{(.*?)}/g, (_, name) => {
4949
if (!props.includes(name)) {
5050
throw new Error(`路径参数 ${name} 不存在`);
5151
}

src/printer/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,16 +263,18 @@ export class Printer {
263263
private _printOperation(method: string, url: string, operation: OpenApiLatest_Operation) {
264264
if (isRefOperation(operation)) return;
265265

266+
const { responseStatusCode, responseContentType, requestContentType, axiosRequestConfigTypeName = AXIOS_QUEST_CONFIG_TYPE_NAME } = this.options || {};
266267
const argNamed = new Named();
267268
const header = new Arg(argNamed, 'headers', this.schemata);
268269
const cookie = new Arg(argNamed, 'cookies', this.schemata);
269270
const query = new Arg(argNamed, 'params', this.schemata);
270271
const path = new Arg(argNamed, 'path', this.schemata);
272+
path.setUrl(url); // 设置 url,用于解析 path 参数
271273
const data = new Arg(argNamed, 'data', this.schemata, true);
272274
const config = new Arg(argNamed, 'config', this.schemata, true);
275+
config.setDefaultType(axiosRequestConfigTypeName);
273276
const resp = new Arg(argNamed, 'response', this.schemata, true);
274277
const { parameters, requestBody, responses, operationId } = operation;
275-
const { responseStatusCode, responseContentType, requestContentType } = this.options || {};
276278

277279
if (parameters) {
278280
parameters.forEach((parameter) => {
@@ -342,13 +344,12 @@ export class Printer {
342344
jsDoc.addComments(comments);
343345
jsDoc.addComments(requestArgs.toComments());
344346
jsDoc.addComments(responseArgs.toComments());
345-
const { axiosRequestConfigTypeName = AXIOS_QUEST_CONFIG_TYPE_NAME } = this.options || {};
346347

347348
return `${jsDoc.print()}
348-
export async function ${funcName}(${requestArgs.toArgs(axiosRequestConfigTypeName)}): AxiosPromise<${respType}> {
349+
export async function ${funcName}(${requestArgs.toArgs()}): AxiosPromise<${respType}> {
349350
return axios({
350351
method: ${JSON.stringify(method)},
351-
${requestArgs.toValues(url)}
352+
${requestArgs.toValues()}
352353
});
353354
}`;
354355
}

0 commit comments

Comments
 (0)