@@ -5,6 +5,7 @@ import { generateSchemaExample } from './generateSchemaExample';
5
5
import { OpenAPIContextProps } from './types' ;
6
6
import { createStateKey , noReference } from './utils' ;
7
7
import { stringifyOpenAPI } from './stringifyOpenAPI' ;
8
+ import { OpenAPIV3 } from '@scalar/openapi-types' ;
8
9
9
10
/**
10
11
* Display an example of the response content.
@@ -39,39 +40,60 @@ export function OpenAPIResponseExample(props: {
39
40
} ) ;
40
41
41
42
const examples = responses
42
- . map ( ( response ) => {
43
- const responseObject = noReference ( response [ 1 ] ) ;
43
+ . map ( ( [ key , value ] ) => {
44
+ const responseObject = noReference ( value ) ;
45
+ const mediaTypeObject = ( ( ) => {
46
+ if ( ! responseObject . content ) {
47
+ return null ;
48
+ }
49
+ return (
50
+ responseObject . content [ 'application/json' ] ??
51
+ responseObject . content [ Object . keys ( responseObject . content ) [ 0 ] ]
52
+ ) ;
53
+ } ) ( ) ;
44
54
45
- // TODO: unnecessary with https://github.com/GitbookIO/gitbook/pull/2780
46
- if ( ! responseObject ) {
55
+ if ( ! mediaTypeObject ) {
47
56
return null ;
48
57
}
49
58
50
- const schema = noReference (
51
- (
52
- responseObject . content ?. [ 'application/json' ] ??
53
- responseObject . content ?. [ Object . keys ( responseObject . content ) [ 0 ] ]
54
- ) ?. schema ,
55
- ) ;
59
+ const example : OpenAPIV3 . ExampleObject | null = noReference (
60
+ ( ( ) => {
61
+ const { examples, example } = mediaTypeObject ;
62
+ if ( examples ) {
63
+ const firstKey = Object . keys ( examples ) [ 0 ] ;
64
+ // @TODO handle multiple examples
65
+ const firstExample = noReference ( examples [ firstKey ] ) ;
66
+ if ( firstExample ) {
67
+ return firstExample ;
68
+ }
69
+ }
56
70
57
- if ( ! schema ) {
58
- return null ;
59
- }
71
+ if ( example ) {
72
+ return { value : example } ;
73
+ }
74
+
75
+ const schema = noReference ( mediaTypeObject . schema ) ;
76
+ if ( ! schema ) {
77
+ return null ;
78
+ }
79
+
80
+ return { value : generateSchemaExample ( schema ) } ;
81
+ } ) ( ) ,
82
+ ) ;
60
83
61
- const example = generateSchemaExample ( schema ) ;
62
- if ( example === undefined ) {
84
+ if ( ! example ?. value ) {
63
85
return null ;
64
86
}
65
87
66
88
return {
67
- key : ` ${ response [ 0 ] } ` ,
68
- label : ` ${ response [ 0 ] } ` ,
89
+ key : key ,
90
+ label : key ,
69
91
body : (
70
92
< context . CodeBlock
71
93
code = {
72
- typeof example === 'string'
73
- ? example
74
- : stringifyOpenAPI ( example , null , 2 )
94
+ typeof example . value === 'string'
95
+ ? example . value
96
+ : stringifyOpenAPI ( example . value , null , 2 )
75
97
}
76
98
syntax = "json"
77
99
/>
0 commit comments