Skip to content

Commit

Permalink
feat: 忽略未明确的 path 参数
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Aug 5, 2024
1 parent 62f63ad commit 29312cc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
22 changes: 20 additions & 2 deletions src/printer/Arg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,27 @@ export class Arg {
readonly isSingle: boolean = false,
) {}

url: string = '';
urlParams: string[] = [];
setUrl(url: string) {
this.url = url;
url.replace(/\{(.*?)}/g, (_, name) => {
this.urlParams.push(name);
return _;
});
}

defaultType = '';
setDefaultType(type: string) {
this.defaultType = type;
}

add(parameter?: OpenApiLatest_Parameter) {
if (!parameter) return;

// 忽略 url 无参数的情况
if (this.kind === 'path' && this.urlParams.length === 0) return;

this.parameters.push(parameter);
}

Expand Down Expand Up @@ -72,7 +90,7 @@ export class Arg {
uniqueName: this.named.nextVarName(this.kind),
// 路径参数必填
required: true,
type: '',
type: this.defaultType,
comments: {},
props,
};
Expand All @@ -85,7 +103,7 @@ export class Arg {
originName: this.kind,
uniqueName: name,
required: false,
type: '',
type: this.defaultType,
comments: {
[`param [${name}]`]: 'request config',
},
Expand Down
10 changes: 5 additions & 5 deletions src/printer/Args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export class Args {
);
}

toArgs(configTypeName: string) {
toArgs() {
return this.fixedArgs
.filter((fixArg) => fixArg.arg.kind !== 'path' || fixArg.type !== '')
.filter((fixArg) => fixArg.type !== '')
.map((fixArg) => {
return `${fixArg.uniqueName}${requiredTypeStringify(fixArg.required)}${fixArg.type || configTypeName}`;
return `${fixArg.uniqueName}${requiredTypeStringify(fixArg.required)}${fixArg.type}`;
})
.join(',');
}
Expand All @@ -37,15 +37,15 @@ export class Args {
return this.fixedArgs[index]?.type || 'unknown';
}

toValues(url: string) {
toValues() {
return this.fixedArgs
.map((fixedArg) => {
const { originName, uniqueName, arg, props } = fixedArg;

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

if (arg.kind === 'path') {
const resolvedURL = url.replace(/\{(.*?)}/g, (_, name) => {
const resolvedURL = arg.url.replace(/\{(.*?)}/g, (_, name) => {
if (!props.includes(name)) {
throw new Error(`路径参数 ${name} 不存在`);
}
Expand Down
9 changes: 5 additions & 4 deletions src/printer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,18 @@ export class Printer {
private _printOperation(method: string, url: string, operation: OpenApiLatest_Operation) {
if (isRefOperation(operation)) return;

const { responseStatusCode, responseContentType, requestContentType, axiosRequestConfigTypeName = AXIOS_QUEST_CONFIG_TYPE_NAME } = this.options || {};
const argNamed = new Named();
const header = new Arg(argNamed, 'headers', this.schemata);
const cookie = new Arg(argNamed, 'cookies', this.schemata);
const query = new Arg(argNamed, 'params', this.schemata);
const path = new Arg(argNamed, 'path', this.schemata);
path.setUrl(url); // 设置 url,用于解析 path 参数
const data = new Arg(argNamed, 'data', this.schemata, true);
const config = new Arg(argNamed, 'config', this.schemata, true);
config.setDefaultType(axiosRequestConfigTypeName);
const resp = new Arg(argNamed, 'response', this.schemata, true);
const { parameters, requestBody, responses, operationId } = operation;
const { responseStatusCode, responseContentType, requestContentType } = this.options || {};

if (parameters) {
parameters.forEach((parameter) => {
Expand Down Expand Up @@ -342,13 +344,12 @@ export class Printer {
jsDoc.addComments(comments);
jsDoc.addComments(requestArgs.toComments());
jsDoc.addComments(responseArgs.toComments());
const { axiosRequestConfigTypeName = AXIOS_QUEST_CONFIG_TYPE_NAME } = this.options || {};

return `${jsDoc.print()}
export async function ${funcName}(${requestArgs.toArgs(axiosRequestConfigTypeName)}): AxiosPromise<${respType}> {
export async function ${funcName}(${requestArgs.toArgs()}): AxiosPromise<${respType}> {
return axios({
method: ${JSON.stringify(method)},
${requestArgs.toValues(url)}
${requestArgs.toValues()}
});
}`;
}
Expand Down

0 comments on commit 29312cc

Please sign in to comment.