11import type { AxiosRequestConfig } from 'axios' ;
22import { groupBy } from 'lodash' ;
3+ import { pkgName } from '../const' ;
4+ import { AXIOS_PROMISE_TYPE_NAME , AXIOS_QUEST_CONFIG_TYPE_NAME } from '../parsers/const' ;
35import type { TypeItem , TypeList , TypeOperation , TypeOperations , TypeOrigin } from '../parsers/types' ;
46import { joinSlices , nextUniqueName , varString } from '../utils/string' ;
57import { isBoolean , isString } from '../utils/type-is' ;
@@ -10,10 +12,9 @@ const { stringify } = JSON;
1012export class PathsPrinter extends ComponentsPrinter {
1113 protected init ( ) {
1214 super . init ( ) ;
13- this . imports . push ( 'import type { AxiosPromise, AxiosRequestConfig } from "axios";' ) ;
14- this . imports . push ( 'import { DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, resolveURL } from "openapi-axios/helpers";' ) ;
15- this . imports . push ( this . options . axiosImport ) ;
16- this . helpers . push ( `const request = axios.request;` ) ;
15+ this . imports . push ( `import { DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, resolveURL } from "${ pkgName } /client";` ) ;
16+ this . imports . push ( `import type {AxiosRequestConfig, AxiosPromise} from "${ this . options . axiosImportPath } ";` ) ;
17+ this . imports . push ( `import axios from "${ this . options . axiosImportPath } ";` ) ;
1718 if ( this . document . info . baseURL ) this . helpers . push ( `const BASE_URL = ${ stringify ( this . document . info . baseURL ) } ;` ) ;
1819 }
1920
@@ -40,32 +41,30 @@ export class PathsPrinter extends ComponentsPrinter {
4041 request : { path, query, body : reqBody } ,
4142 response : { body : resBody } ,
4243 } = type ;
43- const { responseTypeName } = this . options ;
4444 const argNameCountMap = new Map < string , number > ( ) ;
4545 const requestPathArgName = nextUniqueName ( this . options . requestPathArgName , argNameCountMap ) ;
4646 const requestQueryArgName = nextUniqueName ( this . options . requestQueryArgName , argNameCountMap ) ;
4747 const requestBodyArgName = nextUniqueName ( this . options . requestBodyArgName , argNameCountMap ) ;
48- const configArgName = nextUniqueName ( 'config' , argNameCountMap ) ;
48+ const requestConfigArgName = nextUniqueName ( this . options . requestConfigArgName , argNameCountMap ) ;
4949 const comments = this . printComments ( type , true ) ;
50- const argsGroup = groupBy (
51- [
52- this . printArg ( requestPathArgName , path ) ,
53- this . printArg ( requestQueryArgName , query ) ,
54- this . printArg ( requestBodyArgName , reqBody ) ,
55- this . printArg ( configArgName , 'AxiosRequestConfig' , false ) ,
56- ] ,
57- ( item ) => item ?. required ,
58- ) ;
59- const args_ = joinSlices (
50+ const args = [
51+ //
52+ this . printArg ( requestPathArgName , path ) ,
53+ this . printArg ( requestQueryArgName , query ) ,
54+ this . printArg ( requestBodyArgName , reqBody ) ,
55+ this . printArg ( requestConfigArgName , AXIOS_QUEST_CONFIG_TYPE_NAME , false ) ,
56+ ] ;
57+ const argsGroup = groupBy ( args , ( item ) => item ?. required ) ;
58+ const argStr = joinSlices (
6059 [
6160 // 可能没有任何必填参数
6261 ...( argsGroup [ 'true' ] || [ ] ) ,
63- // 至少有一个 config 可选参数
64- ...argsGroup [ 'false' ] ,
62+ // 可能没有任何可选参数
63+ ...( argsGroup [ 'false' ] || [ ] ) ,
6564 ] . map ( ( desc ) => desc ?. text ) ,
6665 ', ' ,
6766 ) ;
68- const return_ = `${ responseTypeName } <${ resBody ?. name || 'never' } >` ;
67+ const return_ = `${ AXIOS_PROMISE_TYPE_NAME } <${ resBody ?. name || 'never' } >` ;
6968 const url_ = this . printAxiosProp ( 'url' , this . toURL ( type , requestPathArgName ) ) ;
7069 const method_ = this . printAxiosProp ( 'method' , type . method . toUpperCase ( ) ) ;
7170 const params_ = this . printAxiosProp ( 'params' , query ? requestQueryArgName : '' ) ;
@@ -76,11 +75,11 @@ export class PathsPrinter extends ComponentsPrinter {
7675 method_ ,
7776 params_ ,
7877 data_ ,
79- `...${ configArgName } ` ,
78+ `...${ requestConfigArgName } ` ,
8079 ] ) ;
8180
82- return `${ comments } export async function ${ name } (${ args_ } ): ${ return_ } {
83- return request ({
81+ return `${ comments } export async function ${ name } (${ argStr } ): ${ return_ } {
82+ return axios ({
8483 ${ props }
8584 });
8685 }` ;
0 commit comments