1- /* eslint-disable ts/ban-ts-comment */
21import type { StatementField , StatementInterface } from '@genapi/shared'
32import type { Parameter } from 'openapi-specification-types'
43import type { PathMethod } from '../traverse'
@@ -18,12 +17,14 @@ export type { InSchemas }
1817 */
1918export function parseMethodParameters ( { method, parameters, path } : PathMethod , schemas ?: InSchemas ) {
2019 const { config : userConfig } = inject ( )
21- const requestConfigs = {
22- body : [ ] as StatementField [ ] ,
23- formData : [ ] as StatementField [ ] ,
24- path : [ ] as StatementField [ ] ,
25- query : [ ] as StatementField [ ] ,
26- header : [ ] as StatementField [ ] ,
20+ const requestConfigs : Record < string , StatementField [ ] > = {
21+ body : [ ] ,
22+ formData : [ ] ,
23+ path : [ ] ,
24+ query : [ ] ,
25+ header : [ ] ,
26+ cookie : [ ] ,
27+ querystring : [ ] ,
2728 }
2829
2930 const config = {
@@ -32,14 +33,17 @@ export function parseMethodParameters({ method, parameters, path }: PathMethod,
3233 interfaces : [ ] as StatementInterface [ ] ,
3334 }
3435
35- for ( const parameter of parameters )
36- requestConfigs [ parameter . in ] . push ( parseParameterFiled ( parameter ) )
36+ for ( const parameter of parameters ) {
37+ const key = parameter . in
38+ if ( key in requestConfigs )
39+ requestConfigs [ key ] . push ( parseParameterFiled ( parameter ) )
40+ }
3741
38- for ( const [ inType , properties ] of Object . entries ( requestConfigs ) as [ Parameter [ 'in' ] , StatementField [ ] ] [ ] ) {
42+ for ( const [ inType , properties ] of Object . entries ( requestConfigs ) ) {
3943 if ( properties . length === 0 )
4044 continue
4145
42- const name = toUndefField ( inType , schemas )
46+ const name = toUndefField ( inType as Parameter [ 'in' ] , schemas )
4347
4448 if ( inType !== 'path' )
4549 config . options . push ( name )
@@ -56,7 +60,7 @@ export function parseMethodParameters({ method, parameters, path }: PathMethod,
5660 continue
5761 }
5862
59- if ( [ 'header' , 'path' , 'query' ] . includes ( inType ) ) {
63+ if ( [ 'header' , 'path' , 'query' , 'cookie' , 'querystring' ] . includes ( inType ) ) {
6064 const typeName = varName ( [ method , path , inType ] )
6165 config . interfaces . push ( { name : typeName , properties, export : true } )
6266 const required = inType === 'path' || isRequiredParameter ( properties ) || userConfig ?. parametersRequired
@@ -87,25 +91,30 @@ export function parseMethodParameters({ method, parameters, path }: PathMethod,
8791
8892export function parseMethodMetadata ( { method, path, responses, options : meta } : PathMethod ) {
8993 const { configRead, interfaces } = inject ( )
94+ const metaAny = meta as { consumes ?: string [ ] }
9095 const comments = [
9196 meta . summary && `@summary ${ meta . summary } ` ,
9297 meta . description && `@description ${ meta . description } ` ,
9398 `@method ${ method } ` ,
94- meta . tags && `@tags ${ meta . tags . join ( ' | ' ) || '-' } ` ,
95- meta . consumes && `@consumes ${ meta . consumes . join ( '; ' ) || '-' } ` ,
96- ]
99+ meta . tags ?. length ? `@tags ${ meta . tags . join ( ' | ' ) || '-' } ` : undefined ,
100+ metaAny . consumes ?. length ? `@consumes ${ metaAny . consumes . join ( '; ' ) || '-' } ` : undefined ,
101+ ] . filter ( ( c ) : c is string => typeof c === 'string' )
97102
98103 const name = camelCase ( `${ method } /${ path } ` )
99104
100105 const url = `${ path . replace ( / ( \{ ) / g, '${paths.' ) } `
101- const responseSchema
102- // @ts -expect-error
103- = responses . default ?. content ?. [ 'application/json' ] ?. schema
104- // @ts -expect-error
105- || responses [ '200' ] ?. content ?. [ 'application/json' ] ?. schema
106- || responses [ '200' ] ?. schema
107- || responses [ '200' ]
108- const responseType = responseSchema ? parseSchemaType ( responseSchema ) : 'void'
106+ function hasContent ( r : unknown ) : r is { content ?: Record < string , { schema ?: unknown } > } {
107+ return r != null && typeof r === 'object' && 'content' in r
108+ }
109+ const resDefault = responses . default && hasContent ( responses . default ) ? responses . default : null
110+ const res200 = responses [ '200' ] && typeof responses [ '200' ] === 'object' ? responses [ '200' ] : null
111+ const contentDefault = resDefault ?. content ?. [ 'application/json' ]
112+ const content200 = res200 && hasContent ( res200 ) ? res200 . content ?. [ 'application/json' ] : null
113+ const schemaFromContent = ( contentDefault && typeof contentDefault === 'object' && 'schema' in contentDefault ? ( contentDefault as { schema : unknown } ) . schema : null )
114+ ?? ( content200 && typeof content200 === 'object' && 'schema' in content200 ? ( content200 as { schema : unknown } ) . schema : null )
115+ const schemaFromRes200 = res200 && typeof res200 === 'object' && 'schema' in res200 && ! ( 'content' in res200 ) ? ( res200 as { schema : unknown } ) . schema : null
116+ const responseSchema = schemaFromContent ?? schemaFromRes200
117+ const responseType = responseSchema && typeof responseSchema === 'object' ? parseSchemaType ( responseSchema as Parameters < typeof parseSchemaType > [ 0 ] ) : 'void'
109118
110119 if ( configRead . config . responseRequired )
111120 deepSignRequired ( interfaces . find ( v => v . name === responseType ) ?. properties || [ ] )
@@ -119,5 +128,5 @@ export function parseMethodMetadata({ method, path, responses, options: meta }:
119128 }
120129 }
121130
122- return { description : comments . filter ( Boolean ) , name, url, responseType, body : [ ] as string [ ] }
131+ return { description : comments , name, url, responseType, body : [ ] as string [ ] }
123132}
0 commit comments