11import type { OpenAPILatest } from '../types/openapi' ;
22import type { Named } from './Named' ;
3+ import type { PrinterOptions } from './types' ;
34import { isRefParameter , type OpenApiLatest_Parameter , requiredKeyStringify } from './helpers' ;
45import { Schemata } from './Schemata' ;
56
6- export type ArgKind = 'path' | 'headers' | 'cookies' | 'params' | 'data' | 'config' | 'response' ;
7- export interface ArgParsed {
8- arg : Arg ;
9- originName : string ;
10- uniqueName : string ;
11- required : boolean ;
12- type : string ;
13- comments : Record < string , unknown > ;
14- props : string [ ] ;
15- }
16- interface InternalArgItem {
7+ export type ArgKind = 'path' | 'header' | 'cookie' | 'param' | 'data' | 'config' | 'response' ;
8+
9+ const kindAxiosPropNames : Record < ArgKind , string > = {
10+ path : 'url' ,
11+ header : 'headers' ,
12+ cookie : 'cookies' ,
13+ param : 'params' ,
14+ data : 'data' ,
15+ config : 'config' ,
16+ response : 'response' ,
17+ } ;
18+ const kindAxiosDocNames : Record < ArgKind , string > = {
19+ path : 'path' ,
20+ header : 'headers' ,
21+ cookie : 'cookies' ,
22+ param : 'params' ,
23+ data : 'data' ,
24+ config : 'config' ,
25+ response : 'response' ,
26+ } ;
27+
28+ export interface ArgProp {
29+ name : string ;
1730 parameter : OpenAPILatest . ParameterObject ;
1831 schema : OpenAPILatest . SchemaObject ;
1932}
2033
2134export class Arg {
2235 parameters : OpenApiLatest_Parameter [ ] = [ ] ;
36+ originName : string = '' ;
37+
38+ /**
39+ * 作为属性的名称
40+ */
41+ propName = '' ;
42+ /**
43+ * 作为参数的注释名称
44+ */
45+ docName = '' ;
46+ /**
47+ * 作为参数的变量名称
48+ */
49+ varName = '' ;
50+ /**
51+ * 是否必填
52+ */
53+ required : boolean = false ;
54+ /**
55+ * 类型
56+ */
57+ type : string = '' ;
58+ comments : Record < string , unknown > = { } ;
59+ props : ArgProp [ ] = [ ] ;
2360
2461 constructor (
2562 readonly named : Named ,
2663 readonly kind : ArgKind ,
2764 readonly schemata : Schemata ,
65+ readonly printOptions : PrinterOptions ,
2866 /**
2967 * 是否单参数(如 data、config、response)
3068 */
3169 readonly isSingle : boolean = false ,
32- ) { }
70+ ) {
71+ this . originName = this . kind ;
72+ this . propName = kindAxiosPropNames [ this . kind ] ;
73+ this . docName = kindAxiosDocNames [ this . kind ] ;
74+ this . varName = '' ;
75+ }
3376
3477 url : string = '' ;
3578 urlParams : string [ ] = [ ] ;
@@ -57,11 +100,9 @@ export class Arg {
57100 this . parameters . push ( parameter ) ;
58101 }
59102
60- parse ( ) : ArgParsed | null {
61- const internalArgItems : InternalArgItem [ ] = [ ] ;
103+ parse ( ) : Arg | null {
62104 const fixedParameters = this . parameters . filter ( p => ! isRefParameter ( p ) && 'schema' in p && p . schema ) as OpenAPILatest . ParameterObject [ ] ;
63105 const propLength = fixedParameters . length ;
64- const props = fixedParameters . map ( p => p . name ) ;
65106 const requiredNames : string [ ] = [ ] ;
66107
67108 fixedParameters . forEach ( ( parameter ) => {
@@ -75,79 +116,64 @@ export class Arg {
75116 parameter . required = true ;
76117 }
77118
78- internalArgItems . push ( {
119+ this . props . push ( {
79120 parameter,
121+ name,
80122 schema : schema ! ,
81123 } ) ;
82124 } ) ;
83125
84126 switch ( propLength ) {
85127 case 0 : {
86- if ( this . kind === 'path' ) {
87- return {
88- arg : this ,
89- originName : this . kind ,
90- uniqueName : this . named . nextVarName ( this . kind ) ,
91- // 路径参数必填
92- required : true ,
93- type : this . defaultType ,
94- comments : { } ,
95- props,
96- } ;
97- }
98-
99- if ( this . kind === 'config' ) {
100- const name = this . named . nextVarName ( this . kind ) ;
101- return {
102- arg : this ,
103- originName : this . kind ,
104- uniqueName : name ,
105- required : false ,
106- type : this . defaultType ,
107- comments : {
108- [ `param [${ name } ]` ] : 'request config' ,
109- } ,
110- props,
111- } ;
128+ switch ( this . kind ) {
129+ case 'path' :
130+ this . required = true ;
131+ this . type = this . defaultType ;
132+ this . varName = this . named . nextVarName ( this . docName ) ;
133+ return this ;
134+
135+ case 'config' :
136+ this . type = this . defaultType ;
137+ this . varName = this . named . nextVarName ( this . docName ) ;
138+ this . comments = {
139+ [ `param [${ this . varName } ]` ] : `request ${ this . propName } ` ,
140+ } ;
141+ return this ;
112142 }
113-
114143 return null ;
115144 }
116145
117146 case 1 : {
118147 // prop0: type0
119- const [ firstArg ] = internalArgItems ;
148+ const [ firstArg ] = this . props ;
120149 const { parameter, schema } = firstArg ;
121150 const result = this . schemata . print ( schema ) ;
122- const name = this . kind === 'response' ? this . kind : this . named . nextVarName ( parameter . name || this . kind ) ;
151+ const isResponse = this . kind === 'response' ;
123152 const required = parameter . required || result . required || false ;
124153
125- return {
126- arg : this ,
127- originName : parameter . name ,
128- uniqueName : name ,
129- required,
130- type : Schemata . toString ( result , true ) ,
131- props,
132- comments :
133- this . kind === 'response'
134- ? {
135- returns : parameter . description || schema . description || false ,
136- }
137- : {
138- [ `param ${ requiredKeyStringify ( name , required ) } ` ] :
139- parameter . description || schema . description || `request ${ this . kind === 'data' ? 'data' : 'param' } ` ,
140- } ,
141- } ;
154+ this . originName = firstArg . name ;
155+ this . varName = this . named . nextVarName ( firstArg . name ) ;
156+ this . required = required ;
157+ this . type = Schemata . toString ( result ) ;
158+ this . comments = isResponse
159+ ? {
160+ returns : parameter . description || schema . description || false ,
161+ }
162+ : {
163+ [
164+ `param ${ requiredKeyStringify ( this . varName , required ) } ` ] : parameter . description || schema . description
165+ || ( this . kind === 'data' ? 'request data' : `request ${ this . docName } ${ JSON . stringify ( firstArg . name ) } ` ) ,
166+ } ;
167+ return this ;
142168 }
143169
144170 default : {
145171 // name: {prop0: type0, prop1: type1, ...}
146172 const rootSchema : OpenAPILatest . SchemaObject = {
147173 type : 'object' ,
148- properties : internalArgItems . reduce (
149- ( acc , { parameter, schema } ) => {
150- acc [ parameter . name ] = {
174+ properties : this . props . reduce (
175+ ( acc , { parameter, schema, name : originName } ) => {
176+ acc [ originName ] = {
151177 ...schema ,
152178 description : parameter . description || schema . description ,
153179 deprecated : parameter . deprecated || schema . deprecated ,
@@ -159,25 +185,19 @@ export class Arg {
159185 required : requiredNames ,
160186 } ;
161187 const result = this . schemata . print ( rootSchema ) ;
162- const name = this . named . nextVarName ( this . kind ) ;
163188 const required = requiredNames . length > 0 ;
164189
165- return {
166- arg : this ,
167- originName : this . kind ,
168- uniqueName : name ,
169- required,
170- type : Schemata . toString ( result ) ,
171- props,
172- comments :
173- this . kind === 'response'
174- ? {
175- returns : result . comments . description ,
176- }
177- : {
178- [ `param ${ requiredKeyStringify ( name , required ) } ` ] : 'request params' ,
179- } ,
180- } ;
190+ this . required = required ;
191+ this . type = Schemata . toString ( result ) ;
192+ this . varName = this . named . nextVarName ( this . docName ) ;
193+ this . comments = this . kind === 'response'
194+ ? {
195+ returns : result . comments . description ,
196+ }
197+ : {
198+ [ `param ${ requiredKeyStringify ( this . docName , required ) } ` ] : `request ${ this . docName } ` ,
199+ } ;
200+ return this ;
181201 }
182202 }
183203 }
0 commit comments