1
1
import { DocumentBlockSwagger } from '@gitbook/api' ;
2
2
import { Icon } from '@gitbook/icons' ;
3
- import { OpenAPIOperation } from '@gitbook/react-openapi' ;
3
+ import { OpenAPIOperation , OpenAPIOperationData } from '@gitbook/react-openapi' ;
4
4
import React from 'react' ;
5
5
6
6
import { LoadingPane } from '@/components/primitives' ;
@@ -34,8 +34,8 @@ export async function OpenAPI(props: BlockProps<DocumentBlockSwagger>) {
34
34
35
35
async function OpenAPIBody ( props : BlockProps < DocumentBlockSwagger > ) {
36
36
const { block, context } = props ;
37
- const { data, specUrl, error } = await fetchOpenAPIBlock ( block , context . resolveContentRef ) ;
38
37
38
+ const { data, specUrl, error } = await fetchOpenAPIBlock ( block , context . resolveContentRef ) ;
39
39
if ( error ) {
40
40
return (
41
41
< div className = { tcls ( 'hidden' ) } >
@@ -50,6 +50,10 @@ async function OpenAPIBody(props: BlockProps<DocumentBlockSwagger>) {
50
50
return null ;
51
51
}
52
52
53
+ const enumSelectors =
54
+ context . searchParams && context . searchParams . block === block . key
55
+ ? parseModifiers ( data , context . searchParams )
56
+ : undefined ;
53
57
return (
54
58
< OpenAPIOperation
55
59
data = { data }
@@ -61,6 +65,8 @@ async function OpenAPIBody(props: BlockProps<DocumentBlockSwagger>) {
61
65
CodeBlock : PlainCodeBlock ,
62
66
defaultInteractiveOpened : context . mode === 'print' ,
63
67
specUrl,
68
+ enumSelectors,
69
+ blockKey : block . key ,
64
70
} }
65
71
className = "openapi-block"
66
72
/>
@@ -95,3 +101,27 @@ function OpenAPIFallback() {
95
101
</ div >
96
102
) ;
97
103
}
104
+
105
+ function parseModifiers ( data : OpenAPIOperationData , params : Record < string , string > ) {
106
+ if ( ! data ) {
107
+ return ;
108
+ }
109
+ const { servers } = params ;
110
+ const serverIndex =
111
+ servers && ! isNaN ( Number ( servers ) )
112
+ ? Math . min ( 0 , Math . max ( Number ( servers ) , servers . length - 1 ) )
113
+ : 0 ;
114
+ const server = data . servers [ serverIndex ] ;
115
+ if ( server && server . variables ) {
116
+ return Object . keys ( server . variables ) . reduce < Record < string , number > > (
117
+ ( result , key ) => {
118
+ const selection = Number ( params [ key ] ) ;
119
+ if ( ! isNaN ( selection ) ) {
120
+ result [ key ] = selection ;
121
+ }
122
+ return result ;
123
+ } ,
124
+ { servers : serverIndex } ,
125
+ ) ;
126
+ }
127
+ }
0 commit comments