@@ -5,32 +5,63 @@ import { useEffect } from "react";
55import { getBasePath } from "../../utils/base" ;
66
77export default function SwaggerUI ( ) {
8- useEffect ( ( ) => {
9- // document.querySelector("#swagger-ui").prepend(Index)
10- // TODO: Add page index to top of page
11- // Alter the page title to match the swagger page
12- document . title = "CWMS Data API for Data Retrieval - Swagger UI" ;
13- // Begin Swagger UI call region
14- // TODO: add endpoint that dynamic returns swagger generated doc
15- SwaggerUIBundle ( {
16- url : getBasePath ( ) + "/swagger-docs" ,
17- dom_id : "#swagger-ui" ,
18- deepLinking : false ,
19- presets : [ SwaggerUIBundle . presets . apis ] ,
20- plugins : [ SwaggerUIBundle . plugins . DownloadUrl ] ,
21- requestInterceptor : ( req ) => {
22- // Add a cache-busting query param
23- const sep = req . url . includes ( "?" ) ? "&" : "?" ;
24- req . url = `${ req . url } ${ sep } _cb=${ Date . now ( ) } ` ;
8+ useEffect ( ( ) => {
9+ // document.querySelector("#swagger-ui").prepend(Index)
10+ // TODO: Add page index to top of page
11+ // Alter the page title to match the swagger page
12+ document . title = "CWMS Data API for Data Retrieval - Swagger UI" ;
13+ // Begin Swagger UI call region
14+ // TODO: add endpoint that dynamic returns swagger generated doc
2515
26- // Also ask intermediaries not to serve from cache
27- req . headers [ "Cache-Control" ] = "no-cache, no-store, max-age=0" ;
28- req . headers [ "Pragma" ] = "no-cache" ;
16+ const ui = SwaggerUIBundle ( {
17+ url : getBasePath ( ) + "/swagger-docs" ,
18+
19+ dom_id : "#swagger-ui" ,
20+ deepLinking : false ,
21+ presets : [ SwaggerUIBundle . presets . apis ] ,
22+ plugins : [ SwaggerUIBundle . plugins . DownloadUrl ] ,
23+ requestInterceptor : ( req ) => {
24+ // Add a cache-busting query param... but only if it's to our api. Some
25+ // external systems, like keycloak, don't allow random unknown parameters.
26+ const origin = window . location . origin ;
27+ const re = new RegExp ( `^${ origin } .*` )
28+ if ( re . test ( req . url ) )
29+ {
30+ const sep = req . url . includes ( "?" ) ? "&" : "?" ;
31+ req . url = `${ req . url } ${ sep } _cb=${ Date . now ( ) } ` ;
2932
30- return req ;
31- } ,
32- } ) ;
33- } , [ ] ) ;
33+ // Also ask intermediaries not to serve from cache
34+ req . headers [ "Cache-Control" ] = "no-cache, no-store, max-age=0" ;
35+ req . headers [ "Pragma" ] = "no-cache" ;
36+ }
37+ return req ;
38+ } ,
39+ onComplete : ( ) => {
40+ const spec = JSON . parse ( ui . spec ( ) . get ( "spec" ) ) ;
41+ for ( const schemeName in spec . components . securitySchemes ) {
42+ const scheme = spec . components . securitySchemes [ schemeName ] ;
43+ if ( scheme . type === "openIdConnect" ) {
44+ let additionalParams = null ;
45+ let hints = scheme [ "x-kc_idp_hint" ] ;
46+ if ( hints ) {
47+ additionalParams = {
48+ // Since getting the interface to allow users to choose
49+ // is likely impossible, we will assume the first in the list
50+ // is the "primary" auth system
51+ "kc_idp_hint" : hints . values [ 0 ]
52+ } ;
53+ }
54+ ui . initOAuth ( {
55+ clientId : scheme [ "x-oidc-client-id" ] ,
56+ usePkceWithAuthorizationCodeGrant : true ,
57+ additionalQueryStringParams : additionalParams ,
58+ } ) ;
59+ break ;
60+ }
61+ }
62+ } ,
63+ } ) ;
64+ } , [ ] ) ;
3465
3566 return < div id = "swagger-ui" > </ div > ;
3667}
0 commit comments