@@ -7,8 +7,8 @@ import type { EndpointSample } from '@/utils/generate-sample';
7
7
import { type ReactNode } from 'react' ;
8
8
import { Markdown } from '@/render/markdown' ;
9
9
import { type CodeSample } from '@/render/operation' ;
10
- import type { ResponseTypeProps } from '@/render/renderer' ;
11
10
import { getTypescriptSchema } from '@/utils/get-typescript-schema' ;
11
+ import { CodeBlock } from '@/render/codeblock' ;
12
12
13
13
const defaultSamples : CodeSample [ ] = [
14
14
{
@@ -111,16 +111,14 @@ export async function APIExample({
111
111
) ;
112
112
}
113
113
114
+ const exclusiveCodeSamples = method [ 'x-exclusiveCodeSample' ] ;
114
115
if (
115
116
( samples . size === 1 && samples . has ( '_default' ) ) ||
116
- ( method [ 'x-exclusiveCodeSample' ] &&
117
- samples . has ( method [ 'x-exclusiveCodeSample' ] ) )
117
+ ( exclusiveCodeSamples && samples . has ( exclusiveCodeSamples ) )
118
118
) {
119
119
// if exclusiveSampleKey is present, we don't use tabs
120
120
// if only the fallback or non described openapi legacy example is present, we don't use tabs
121
- children = renderRequest (
122
- samples . get ( method [ 'x-exclusiveCodeSample' ] ?? '_default' ) ! ,
123
- ) ;
121
+ children = renderRequest ( samples . get ( exclusiveCodeSamples ?? '_default' ) ! ) ;
124
122
} else if ( samples . size > 0 ) {
125
123
const entries = Array . from ( samples . entries ( ) ) ;
126
124
@@ -133,7 +131,7 @@ export async function APIExample({
133
131
) : null ,
134
132
value : key ,
135
133
} ) ) }
136
- defaultValue = { method [ 'x-selectedCodeSample' ] }
134
+ defaultValue = { exclusiveCodeSamples }
137
135
>
138
136
{ entries . map ( ( [ key , sample ] ) => (
139
137
< renderer . Sample key = { key } value = { key } >
@@ -180,19 +178,13 @@ function ResponseTabs({
180
178
if ( ! operation . responses ) return null ;
181
179
182
180
async function renderResponse ( code : string ) {
183
- const types : ResponseTypeProps [ ] = [ ] ;
181
+ const response =
182
+ code in endpoint . responses ? endpoint . responses [ code ] : null ;
184
183
185
- let description = operation . responses ?. [ code ] . description ;
186
- if ( ! description && code in endpoint . responses )
187
- description = endpoint . responses [ code ] . schema . description ?? '' ;
188
-
189
- if ( code in endpoint . responses ) {
190
- types . push ( {
191
- lang : 'json' ,
192
- label : 'Response' ,
193
- code : JSON . stringify ( endpoint . responses [ code ] . sample , null , 2 ) ,
194
- } ) ;
195
- }
184
+ const description =
185
+ operation . responses ?. [ code ] . description ??
186
+ response ?. schema . description ??
187
+ '' ;
196
188
197
189
let ts : string | undefined ;
198
190
if ( generateTypeScriptSchema ) {
@@ -201,24 +193,53 @@ function ResponseTabs({
201
193
ts = await getTypescriptSchema ( endpoint , code , schema . dereferenceMap ) ;
202
194
}
203
195
204
- if ( ts ) {
205
- types . push ( {
206
- code : ts ,
207
- lang : 'ts' ,
208
- label : 'TypeScript' ,
196
+ const values : string [ ] = [ ] ;
197
+ let exampleSlot : ReactNode ;
198
+
199
+ if ( response ?. samples . _default ) {
200
+ values . push ( 'Response' ) ;
201
+
202
+ exampleSlot = (
203
+ < renderer . ResponseType label = "Response" >
204
+ < CodeBlock
205
+ lang = "json"
206
+ code = { JSON . stringify (
207
+ endpoint . responses [ code ] . samples . _default ,
208
+ null ,
209
+ 2 ,
210
+ ) }
211
+ />
212
+ </ renderer . ResponseType >
213
+ ) ;
214
+ } else if ( response ) {
215
+ exampleSlot = Object . entries ( response . samples ) . map ( ( [ key , sample ] , i ) => {
216
+ const title = sample ?. summary ?? `Example ${ i + 1 } ` ;
217
+
218
+ values . push ( title ) ;
219
+ return (
220
+ < renderer . ResponseType key = { key } label = { title } >
221
+ { sample ?. description ? (
222
+ < Markdown text = { sample . description } />
223
+ ) : null }
224
+ < CodeBlock lang = "json" code = { JSON . stringify ( sample , null , 2 ) } />
225
+ </ renderer . ResponseType >
226
+ ) ;
209
227
} ) ;
210
228
}
211
229
212
230
return (
213
231
< renderer . Response value = { code } >
214
232
{ description ? < Markdown text = { description } /> : null }
215
- { types . length > 0 ? (
216
- < renderer . ResponseTypes >
217
- { types . map ( ( type ) => (
218
- < renderer . ResponseType key = { type . lang } { ...type } />
219
- ) ) }
233
+ { response && (
234
+ < renderer . ResponseTypes defaultValue = { values [ 0 ] } >
235
+ { exampleSlot }
236
+ { ts ? (
237
+ < renderer . ResponseType label = "TypeScript" >
238
+ < CodeBlock code = { ts } lang = "ts" />
239
+ </ renderer . ResponseType >
240
+ ) : null }
220
241
</ renderer . ResponseTypes >
221
- ) : null }
242
+ ) }
222
243
</ renderer . Response >
223
244
) ;
224
245
}
0 commit comments