@@ -12,6 +12,7 @@ import {
1212export interface MethodLayoutContext {
1313 name : string
1414 path : string
15+ docstring : string
1516 hasParams : boolean
1617 signatureParams : string
1718 params : Array < { name : string } >
@@ -25,19 +26,22 @@ export interface MethodLayoutContext {
2526
2627export interface AbstractClassLayoutContext {
2728 className : string
29+ docstring : string
2830 showPass : boolean
2931 childProperties : Array < { namespace : string ; abstractClassName : string } >
3032 methods : Array < {
3133 name : string
3234 hasParams : boolean
3335 signatureParams : string
3436 returnType : string
37+ docstring : string
3538 } >
3639}
3740
3841export interface RouteLayoutContext {
3942 className : string
4043 abstractClassName : string
44+ docstring : string
4145 abstractClass : AbstractClassLayoutContext
4246 resourceImportList : string
4347 childClasses : Array < {
@@ -53,6 +57,57 @@ export interface RouteLayoutContext {
5357const waitForActionAttemptParameter =
5458 'wait_for_action_attempt: Optional[Union[bool, Dict[str, float]]] = None'
5559
60+ const cleanDoc = ( value : string ) : string =>
61+ value . trim ( ) . replaceAll ( '"""' , '\\"\\"\\"' )
62+
63+ const indentDoc = ( value : string , spaces : number ) : string =>
64+ value . replaceAll ( '\n' , `\n${ ' ' . repeat ( spaces ) } ` )
65+
66+ const methodDocstring = (
67+ method : ClassMethod ,
68+ sortedParameters : ClassMethod [ 'parameters' ] ,
69+ ) : string => {
70+ const lines = [ cleanDoc ( method . description ) ]
71+
72+ for ( const parameter of sortedParameters ) {
73+ const deprecated = parameter . isDeprecated
74+ ? `Deprecated${ parameter . deprecationMessage === '' ? '.' : `: ${ cleanDoc ( parameter . deprecationMessage ) } ` } `
75+ : ''
76+ const description = cleanDoc ( parameter . description )
77+ lines . push (
78+ '' ,
79+ `:param ${ parameter . name } : ${ [ deprecated , description ] . filter ( Boolean ) . join ( ' ' ) } ` ,
80+ `:type ${ parameter . name } : ${ parameter . type } ` ,
81+ )
82+ }
83+
84+ if ( method . returnResource === 'ActionAttempt' ) {
85+ lines . push (
86+ '' ,
87+ ':param wait_for_action_attempt: Whether, and for how long, to wait for the action attempt to finish.' ,
88+ ':type wait_for_action_attempt: Optional[Union[bool, Dict[str, float]]]' ,
89+ )
90+ }
91+
92+ if ( method . returnResource !== 'None' ) {
93+ lines . push (
94+ '' ,
95+ `:returns: ${ cleanDoc ( method . responseDescription ) } ` ,
96+ `:rtype: ${ method . returnResource } ` ,
97+ )
98+ }
99+
100+ if ( method . isDeprecated ) {
101+ lines . push (
102+ '' ,
103+ '.. deprecated::' ,
104+ ` ${ cleanDoc ( method . deprecationMessage ) || 'This method is deprecated.' } ` ,
105+ )
106+ }
107+
108+ return lines . filter ( ( line , index ) => line !== '' || index !== 0 ) . join ( '\n' )
109+ }
110+
56111export const getMethodLayoutContext = (
57112 method : ClassMethod ,
58113) : MethodLayoutContext => {
@@ -83,6 +138,7 @@ export const getMethodLayoutContext = (
83138 return {
84139 name : methodName ,
85140 path,
141+ docstring : indentDoc ( methodDocstring ( method , sortedParameters ) , 8 ) ,
86142 hasParams,
87143 signatureParams,
88144 params : sortedParameters . map ( ( { name } ) => ( { name } ) ) ,
@@ -110,22 +166,27 @@ export const setRouteLayoutContext = (cls: ClassModel): RouteLayoutContext => {
110166 )
111167
112168 const abstractClassName = `Abstract${ cls . name } `
169+ const classDocstring = cls . isDeprecated
170+ ? indentDoc ( '.. deprecated::\n This route is deprecated.' , 4 )
171+ : ''
113172
114173 return {
115174 className : cls . name ,
116175 abstractClassName,
176+ docstring : classDocstring ,
117177 abstractClass : {
118178 className : abstractClassName ,
179+ docstring : classDocstring ,
119180 showPass :
120181 cls . methods . length === 0 && cls . childClassIdentifiers . length === 0 ,
121182 childProperties : cls . childClassIdentifiers . map ( ( i ) => ( {
122183 namespace : i . namespace ,
123184 abstractClassName : `Abstract${ i . className } ` ,
124185 } ) ) ,
125186 methods : cls . methods . map ( ( method ) => {
126- const { name, hasParams, signatureParams, returnType } =
187+ const { name, hasParams, signatureParams, returnType, docstring } =
127188 getMethodLayoutContext ( method )
128- return { name, hasParams, signatureParams, returnType }
189+ return { name, hasParams, signatureParams, returnType, docstring }
129190 } ) ,
130191 } ,
131192 resourceImportList : resourceClasses . join ( ',' ) ,
0 commit comments