Reputation: 24563
I would like to use the swagger-ui dist 'as-is'...well almost as-is.
Pulled down the latest release from github (2.0.24) and stuck it in a folder in my app. I then server it out statically with express:
app.use('/swagger', express.static('./node_modules/swagger-ui/dist'));
That works as expected when I go to:
https://mydomain.com/swagger
However I want to populate the url field to my swagger json dynamically. IE I may deploy to different domains:
https://mydomain.com/api-docs
https://otherdomain.com/api-docs
And when I visit:
https://mydomain.com/swagger
https://otherdomain.com/swagger
I would like to dynamically set the url.
Is that possible?
Upvotes: 4
Views: 12828
Reputation: 66
You could try with this on index.html file of the swagger-ui... It works for me.
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = window.location.origin + "/path/to/swagger.json";
}
Upvotes: 0
Reputation: 1053
Another method would be to use the swagger-ui middleware located in the swagger-tool.
let swaggerUi = require('../node_modules/swagger-tools/middleware/swagger-ui');
app.use(swaggerUi(config.swagger));
The variable config.swagger
contains the swagger.yaml
or swagger.json
. I have in my setting
let config = {
appRoot: __dirname,
swagger: require('./api/swagger/swagger.js')
};
Note: I am using the require('swagger-express-mw')
module
Upvotes: 0
Reputation: 14810
Assuming the /api-docs (or swagger.json) are always on the same path, and only the domain changes, you can set the url
parameter of the SwaggerUi object to "/path/to/api-docs"
or "/path/to/swagger.json"
instead of a full URL. That would make the UI load that path as relative to the domain the UI is hosted on.
For reference, I'm leaving the original answer as well, as it may prove useful in some cases.
You can use the url
parameter to set the URL the UI should load.
That is, if you're hosting it under https://mydomain.com/swagger
you can use https://mydomain.com/swagger?url=https://mydomain.com/api-docs
and https://mydomain.com/swagger?https://otherdomain.com/api-docs
to point at the different locations.
However, as far as I know, this functionality is only available at the current alpha version (which should be stable enough) and not with 2.0.24 that you use (though it's worth testing).
Upvotes: 3