@@ -47,7 +47,7 @@ import {
4747function SwaggerUI ( userOptions ) {
4848 const queryOptions = optionsFromQuery ( ) ( userOptions )
4949 const runtimeOptions = optionsFromRuntime ( ) ( )
50- let mergedOptions = SwaggerUI . config . merge (
50+ const mergedOptions = SwaggerUI . config . merge (
5151 { } ,
5252 SwaggerUI . config . defaults ,
5353 runtimeOptions ,
@@ -57,53 +57,39 @@ function SwaggerUI(userOptions) {
5757 const storeOptions = systemOptionsFactorization ( mergedOptions )
5858 const InlinePlugin = inlinePluginOptionsFactorization ( mergedOptions )
5959
60- const system = new System ( storeOptions )
61- system . register ( [ mergedOptions . plugins , InlinePlugin ] )
60+ const unboundSystem = new System ( storeOptions )
61+ unboundSystem . register ( [ mergedOptions . plugins , InlinePlugin ] )
62+ const system = unboundSystem . getSystem ( )
6263
63- const boundSystem = system . getSystem ( )
64-
65- optionsFromURL ( { url : mergedOptions . configUrl , system : boundSystem } ) (
66- mergedOptions
67- ) . then ( ( urlOptions ) => {
68- const urlOptionsFailedToFetch = urlOptions === null
69-
70- mergedOptions = SwaggerUI . config . merge (
71- { } ,
72- mergedOptions ,
73- urlOptions ,
74- queryOptions
75- )
76- system . setConfigs ( mergedOptions )
77- boundSystem . configsActions . loaded ( )
78-
79- if ( ! urlOptionsFailedToFetch ) {
80- if (
81- ! queryOptions . url &&
82- typeof mergedOptions . spec === "object" &&
83- Object . keys ( mergedOptions . spec ) . length > 0
84- ) {
85- boundSystem . specActions . updateUrl ( "" )
86- boundSystem . specActions . updateLoadingStatus ( "success" )
87- boundSystem . specActions . updateSpec ( JSON . stringify ( mergedOptions . spec ) )
88- } else if (
89- typeof boundSystem . specActions . download === "function" &&
90- mergedOptions . url &&
91- ! mergedOptions . urls
92- ) {
93- boundSystem . specActions . updateUrl ( mergedOptions . url )
94- boundSystem . specActions . download ( mergedOptions . url )
95- }
96- }
97-
98- if ( mergedOptions . domNode ) {
99- boundSystem . render ( mergedOptions . domNode , "App" )
100- } else if ( mergedOptions . dom_id ) {
101- const domNode = document . querySelector ( mergedOptions . dom_id )
102- boundSystem . render ( domNode , "App" )
64+ const persistConfigs = ( options ) => {
65+ unboundSystem . setConfigs ( options )
66+ system . configsActions . loaded ( )
67+ }
68+ const updateSpec = ( options ) => {
69+ if (
70+ ! queryOptions . url &&
71+ typeof options . spec === "object" &&
72+ Object . keys ( options . spec ) . length > 0
73+ ) {
74+ system . specActions . updateUrl ( "" )
75+ system . specActions . updateLoadingStatus ( "success" )
76+ system . specActions . updateSpec ( JSON . stringify ( options . spec ) )
10377 } else if (
104- mergedOptions . dom_id === null ||
105- mergedOptions . domNode === null
78+ typeof system . specActions . download === "function" &&
79+ options . url &&
80+ ! options . urls
10681 ) {
82+ system . specActions . updateUrl ( options . url )
83+ system . specActions . download ( options . url )
84+ }
85+ }
86+ const render = ( options ) => {
87+ if ( options . domNode ) {
88+ system . render ( options . domNode , "App" )
89+ } else if ( options . dom_id ) {
90+ const domNode = document . querySelector ( options . dom_id )
91+ system . render ( domNode , "App" )
92+ } else if ( options . dom_id === null || options . domNode === null ) {
10793 /**
10894 * noop
10995 *
@@ -113,9 +99,34 @@ function SwaggerUI(userOptions) {
11399 } else {
114100 console . error ( "Skipped rendering: no `dom_id` or `domNode` was specified" )
115101 }
116- } )
102+ }
103+
104+ // if no configUrl is provided, we can safely persist the configs and render
105+ if ( ! mergedOptions . configUrl ) {
106+ persistConfigs ( mergedOptions )
107+ updateSpec ( mergedOptions )
108+ render ( mergedOptions )
109+
110+ return system
111+ }
112+
113+ // eslint-disable-next-line no-extra-semi
114+ ; ( async ( ) => {
115+ const { configUrl : url } = mergedOptions
116+ const urlOptions = await optionsFromURL ( { url, system } ) ( mergedOptions )
117+ const urlMergedOptions = SwaggerUI . config . merge (
118+ { } ,
119+ mergedOptions ,
120+ urlOptions ,
121+ queryOptions
122+ )
123+
124+ persistConfigs ( urlMergedOptions )
125+ if ( urlOptions !== null ) updateSpec ( urlMergedOptions )
126+ render ( urlMergedOptions )
127+ } ) ( )
117128
118- return boundSystem
129+ return system
119130}
120131
121132SwaggerUI . System = System
0 commit comments