11import Slugger from 'github-slugger' ;
2- import { CodeSample , Operation } from '@/ui/operation' ;
2+ import { Operation } from '@/ui/operation' ;
33import type { MethodInformation , RenderContext } from '@/types' ;
44import { createMethod , NoReference } from '@/utils/schema' ;
5- import { createRenders } from '@/ui/renderer' ;
65import type { OpenAPIV3_1 } from 'openapi-types' ;
76import type { ProcessedDocument } from '@/utils/process-document' ;
87import { defaultAdapters , MediaAdapter } from '@/requests/media/adapter' ;
9- import { FC , ReactNode } from 'react' ;
8+ import { ComponentProps , FC , ReactNode } from 'react' ;
109import type {
1110 HighlightOptionsCommon ,
1211 HighlightOptionsThemes ,
1312} from 'fumadocs-core/highlight' ;
1413import type { OpenAPIServer } from '@/server' ;
1514import type { APIPageClientOptions } from './client' ;
15+ import { cn } from 'fumadocs-ui/utils/cn' ;
16+ import type { CodeUsageGenerator , ResponseTab } from './operation/api-example' ;
17+ import { ApiProvider } from './contexts/api' ;
1618
1719type Awaitable < T > = T | Promise < T > ;
1820
@@ -33,9 +35,11 @@ export interface CreateAPIPageOptions {
3335 | false ;
3436
3537 /**
36- * Generate code samples for endpoint .
38+ * Generate example code usage for endpoints .
3739 */
38- generateCodeSamples ?: ( method : MethodInformation ) => Awaitable < CodeSample [ ] > ;
40+ generateCodeSamples ?: (
41+ method : MethodInformation ,
42+ ) => Awaitable < CodeUsageGenerator [ ] > ;
3943
4044 shikiOptions ?: Omit < HighlightOptionsCommon , 'lang' | 'components' > &
4145 HighlightOptionsThemes ;
@@ -59,6 +63,28 @@ export interface CreateAPIPageOptions {
5963 * @defaultValue false
6064 */
6165 showExampleInFields ?: boolean ;
66+
67+ renderResponseTabs ?: (
68+ tabs : ResponseTab [ ] ,
69+ ctx : RenderContext ,
70+ ) => Awaitable < ReactNode > ;
71+
72+ renderAPIExampleLayout ?: (
73+ slots : {
74+ selector : ReactNode ;
75+ usageTabs : ReactNode ;
76+ responseTabs : ReactNode ;
77+ } ,
78+ ctx : RenderContext ,
79+ ) => Awaitable < ReactNode > ;
80+
81+ /**
82+ * @param generators - codegens for API example usages
83+ */
84+ renderAPIExampleUsageTabs ?: (
85+ generators : CodeUsageGenerator < unknown > [ ] ,
86+ ctx : RenderContext ,
87+ ) => Awaitable < ReactNode > ;
6288 } ;
6389
6490 /**
@@ -76,7 +102,7 @@ export interface CreateAPIPageOptions {
76102 path : string ;
77103 method : MethodInformation ;
78104 ctx : RenderContext ;
79- } ) => ReactNode | Promise < ReactNode > ;
105+ } ) => Awaitable < ReactNode > ;
80106 } ;
81107
82108 client ?: APIPageClientOptions ;
@@ -135,9 +161,6 @@ export function createAPIPage(
135161 schema : processed ,
136162 proxyUrl : server . options . proxyUrl ,
137163 showResponseSchema : options . showResponseSchema ,
138- renderer : {
139- ...createRenders ( ) ,
140- } ,
141164 shikiOptions : options . shikiOptions ,
142165 generateTypeScriptSchema : options . generateTypeScriptSchema ,
143166 generateCodeSamples : options . generateCodeSamples ,
@@ -153,6 +176,32 @@ export function createAPIPage(
153176 } ;
154177}
155178
179+ function Root ( {
180+ children,
181+ className,
182+ ctx,
183+ ...props
184+ } : { ctx : RenderContext } & ComponentProps < 'div' > ) {
185+ const mediaAdapters : Record < string , MediaAdapter > = { } ;
186+ for ( const k in ctx . mediaAdapters ) {
187+ const adapter = ctx . mediaAdapters [ k ] ;
188+
189+ if ( adapter . client ) mediaAdapters [ k ] = adapter . client ;
190+ }
191+
192+ return (
193+ < div className = { cn ( 'flex flex-col gap-24 text-sm' , className ) } { ...props } >
194+ < ApiProvider
195+ mediaAdapters = { mediaAdapters }
196+ servers = { ctx . servers }
197+ shikiOptions = { ctx . shikiOptions }
198+ >
199+ { children }
200+ </ ApiProvider >
201+ </ div >
202+ ) ;
203+ }
204+
156205async function APIPage ( {
157206 hasHead = false ,
158207 operations,
@@ -164,7 +213,7 @@ async function APIPage({
164213 const { dereferenced } = ctx . schema ;
165214
166215 return (
167- < ctx . renderer . Root ctx = { ctx } >
216+ < Root ctx = { ctx } >
168217 { operations ?. map ( ( item ) => {
169218 const pathItem = dereferenced . paths ?. [ item . path ] ;
170219 if ( ! pathItem )
@@ -216,6 +265,6 @@ async function APIPage({
216265 />
217266 ) ;
218267 } ) }
219- </ ctx . renderer . Root >
268+ </ Root >
220269 ) ;
221270}
0 commit comments