diff --git a/src/printer/Arg.ts b/src/printer/Arg.ts index 62ed0d0..9f707ba 100644 --- a/src/printer/Arg.ts +++ b/src/printer/Arg.ts @@ -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); } @@ -72,7 +90,7 @@ export class Arg { uniqueName: this.named.nextVarName(this.kind), // 路径参数必填 required: true, - type: '', + type: this.defaultType, comments: {}, props, }; @@ -85,7 +103,7 @@ export class Arg { originName: this.kind, uniqueName: name, required: false, - type: '', + type: this.defaultType, comments: { [`param [${name}]`]: 'request config', }, diff --git a/src/printer/Args.ts b/src/printer/Args.ts index b1bd141..074bc7d 100644 --- a/src/printer/Args.ts +++ b/src/printer/Args.ts @@ -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(','); } @@ -37,7 +37,7 @@ export class Args { return this.fixedArgs[index]?.type || 'unknown'; } - toValues(url: string) { + toValues() { return this.fixedArgs .map((fixedArg) => { const { originName, uniqueName, arg, props } = fixedArg; @@ -45,7 +45,7 @@ export class Args { 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} 不存在`); } diff --git a/src/printer/index.ts b/src/printer/index.ts index 8587b54..63c0462 100644 --- a/src/printer/index.ts +++ b/src/printer/index.ts @@ -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) => { @@ -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()} }); }`; }