Skip to content

Instantly share code, notes, and snippets.

@char0n
Created April 30, 2020 12:22
Show Gist options
  • Save char0n/3b6005c187a4bb771a27705d123af6cd to your computer and use it in GitHub Desktop.
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
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