Created
April 30, 2020 12:22
-
-
Save char0n/3b6005c187a4bb771a27705d123af6cd to your computer and use it in GitHub Desktop.
Plugin that provides ability to mutate `host`, `basePath` and `scheme` in swagger-ui
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const UrlMutatorPlugin = (system) => ({ | |
rootInjects: { | |
setScheme: (scheme) => { | |
const jsonSpec = system.getState().toJSON().spec.json; | |
const schemes = Array.isArray(scheme) ? scheme : [scheme]; | |
const newJsonSpec = Object.assign({}, jsonSpec, { schemes }); | |
return system.specActions.updateJsonSpec(newJsonSpec); | |
}, | |
setHost: (host) => { | |
const jsonSpec = system.getState().toJSON().spec.json; | |
const newJsonSpec = Object.assign({}, jsonSpec, { host }); | |
return system.specActions.updateJsonSpec(newJsonSpec); | |
}, | |
setBasePath: (basePath) => { | |
const jsonSpec = system.getState().toJSON().spec.json; | |
const newJsonSpec = Object.assign({}, jsonSpec, { basePath }); | |
return system.specActions.updateJsonSpec(newJsonSpec); | |
} | |
} | |
}); | |
window.ui = SwaggerUI({ | |
dom_id: '#myDomId', | |
plugins: [ | |
UrlMutatorPlugin, | |
], | |
onComplete: () => { | |
// this will set appropriate data when swagger-ui ready | |
window.ui.setScheme('http'); | |
window.ui.setHost('www.google.com'); | |
window.ui.setBasePath('v3'); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment