@@ -38,9 +38,10 @@ interface RowProps {
3838 name : string ;
3939 schema : SchemaObject ;
4040 required : boolean ;
41+ type : "request" | "response" ;
4142}
4243
43- function createRow ( { name, schema, required } : RowProps ) {
44+ function createRow ( { name, schema, required, type } : RowProps ) {
4445 return create ( "tr" , {
4546 children : create ( "td" , {
4647 children : [
@@ -49,19 +50,7 @@ function createRow({ name, schema, required }: RowProps) {
4950 style : { opacity : "0.6" } ,
5051 children : ` ${ getSchemaName ( schema , true ) } ` ,
5152 } ) ,
52- guard ( required , ( ) => [
53- create ( "span" , {
54- style : { opacity : "0.6" } ,
55- children : " — " ,
56- } ) ,
57- create ( "strong" , {
58- style : {
59- fontSize : "var(--ifm-code-font-size)" ,
60- color : "var(--openapi-required)" ,
61- } ,
62- children : " REQUIRED" ,
63- } ) ,
64- ] ) ,
53+ ...parseTitleLabel ( { required, type } ) ,
6554 guard ( getQualifierMessage ( schema ) , ( message ) =>
6655 create ( "div" , {
6756 style : { marginTop : "var(--ifm-table-cell-padding)" } ,
@@ -74,17 +63,17 @@ function createRow({ name, schema, required }: RowProps) {
7463 children : createDescription ( description ) ,
7564 } )
7665 ) ,
77- createRows ( { schema : schema } ) ,
66+ createRows ( { schema : schema , type } ) ,
7867 ] ,
7968 } ) ,
8069 } ) ;
8170}
8271
83- interface RowsProps {
72+ interface RowsProps extends Pick < RowProps , "type" > {
8473 schema : SchemaObject ;
8574}
8675
87- function createRows ( { schema } : RowsProps ) : string | undefined {
76+ function createRows ( { schema, type } : RowsProps ) : string | undefined {
8877 // object
8978 if ( schema . properties !== undefined ) {
9079 return createFullWidthTable ( {
@@ -100,6 +89,7 @@ function createRows({ schema }: RowsProps): string | undefined {
10089 required : Array . isArray ( schema . required )
10190 ? schema . required . includes ( key )
10291 : false ,
92+ type,
10393 } )
10494 ) ,
10595 } ) ,
@@ -120,6 +110,7 @@ function createRows({ schema }: RowsProps): string | undefined {
120110 name : key ,
121111 schema : val ,
122112 required : Array . isArray ( required ) ? required . includes ( key ) : false ,
113+ type,
123114 } )
124115 ) ,
125116 } ) ,
@@ -128,18 +119,18 @@ function createRows({ schema }: RowsProps): string | undefined {
128119
129120 // array
130121 if ( schema . items !== undefined ) {
131- return createRows ( { schema : schema . items } ) ;
122+ return createRows ( { schema : schema . items , type } ) ;
132123 }
133124
134125 // primitive
135126 return undefined ;
136127}
137128
138- interface RowsRootProps {
129+ interface RowsRootProps extends Pick < RowProps , "type" > {
139130 schema : SchemaObject ;
140131}
141132
142- function createRowsRoot ( { schema } : RowsRootProps ) {
133+ function createRowsRoot ( { schema, type } : RowsRootProps ) {
143134 // object
144135 if ( schema . properties !== undefined ) {
145136 return Object . entries ( schema . properties ) . map ( ( [ key , val ] ) =>
@@ -149,6 +140,7 @@ function createRowsRoot({ schema }: RowsRootProps) {
149140 required : Array . isArray ( schema . required )
150141 ? schema . required . includes ( key )
151142 : false ,
143+ type,
152144 } )
153145 ) ;
154146 }
@@ -161,6 +153,7 @@ function createRowsRoot({ schema }: RowsRootProps) {
161153 name : key ,
162154 schema : val ,
163155 required : Array . isArray ( required ) ? required . includes ( key ) : false ,
156+ type,
164157 } )
165158 ) ;
166159 }
@@ -174,7 +167,7 @@ function createRowsRoot({ schema }: RowsRootProps) {
174167 style : { opacity : "0.6" } ,
175168 children : ` ${ getSchemaName ( schema , true ) } ` ,
176169 } ) ,
177- createRows ( { schema : schema . items } ) ,
170+ createRows ( { schema : schema . items , type } ) ,
178171 ] ,
179172 } ) ,
180173 } ) ;
@@ -215,9 +208,10 @@ interface Props {
215208 description ?: string ;
216209 required ?: boolean ;
217210 } ;
211+ type : "request" | "response" ;
218212}
219213
220- export function createSchemaTable ( { title, body, ...rest } : Props ) {
214+ export function createSchemaTable ( { title, body, type , ...rest } : Props ) {
221215 if ( body === undefined || body . content === undefined ) {
222216 return undefined ;
223217 }
@@ -249,19 +243,7 @@ export function createSchemaTable({ title, body, ...rest }: Props) {
249243 style : { textAlign : "left" } ,
250244 children : [
251245 `${ title } ` ,
252- guard ( body . required , ( ) => [
253- create ( "span" , {
254- style : { opacity : "0.6" } ,
255- children : " — " ,
256- } ) ,
257- create ( "strong" , {
258- style : {
259- fontSize : "var(--ifm-code-font-size)" ,
260- color : "var(--openapi-required)" ,
261- } ,
262- children : " REQUIRED" ,
263- } ) ,
264- ] ) ,
246+ ...parseTitleLabel ( { required : body . required , type } ) ,
265247 create ( "div" , {
266248 children : createDescription ( body . description ) ,
267249 } ) ,
@@ -270,8 +252,43 @@ export function createSchemaTable({ title, body, ...rest }: Props) {
270252 } ) ,
271253 } ) ,
272254 create ( "tbody" , {
273- children : createRowsRoot ( { schema : firstBody } ) ,
255+ children : createRowsRoot ( { schema : firstBody , type } ) ,
274256 } ) ,
275257 ] ,
276258 } ) ;
277259}
260+
261+ const parseTitleLabel = ( {
262+ required,
263+ type,
264+ } : {
265+ required ?: boolean ;
266+ type : Props [ "type" ] ;
267+ } ) => [
268+ guard ( required && type === "request" , ( ) => [
269+ create ( "span" , {
270+ style : { opacity : "0.6" } ,
271+ children : " — " ,
272+ } ) ,
273+ create ( "strong" , {
274+ style : {
275+ fontSize : "var(--ifm-code-font-size)" ,
276+ color : "var(--openapi-required)" ,
277+ } ,
278+ children : " REQUIRED" ,
279+ } ) ,
280+ ] ) ,
281+ guard ( ! required && type === "response" , ( ) => [
282+ create ( "span" , {
283+ style : { opacity : "0.6" } ,
284+ children : " — " ,
285+ } ) ,
286+ create ( "strong" , {
287+ style : {
288+ fontSize : "var(--ifm-code-font-size)" ,
289+ color : "var(--openapi-optional)" ,
290+ } ,
291+ children : " OPTIONAL" ,
292+ } ) ,
293+ ] ) ,
294+ ] ;
0 commit comments