Created
June 7, 2019 06:29
-
-
Save dmitryrogozhny/df7e6adb97f4e50e171b646a90b0ab01 to your computer and use it in GitHub Desktop.
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
import swaggerUi = require('swagger-ui-express'); | |
import express = require('express'); | |
const app = express(); | |
const swaggerDocument = { | |
"swagger": "2.0", | |
"info": { | |
"title": "Blah", | |
"description": "", | |
"version": "1.0" | |
}, | |
"produces": ["application/json"], | |
"paths": { | |
"/test": { | |
"post": { | |
"x-swagger-router-controller": "home", | |
"operationId": "index", | |
"tags": ["/test"], | |
"description": "[Login 123](https://www.google.com)", | |
"parameters": [{ | |
"name": "test", | |
"in": "formData", | |
"type": "array", | |
"collectionFormat": "multi", | |
"items": { | |
"type": "integer" | |
} | |
}, | |
{ "name": "profileId", "in": "formData", "required": true, "type": "string" }, | |
{ "name": "file", "in": "formData", "type": "file", "required": "true" }], | |
"responses": {} | |
} | |
}, | |
"/bar": { | |
"get": { | |
"x-swagger-router-controller": "bar", | |
"operationId": "impossible", | |
"tags": ["/test"], | |
"description": "", | |
"parameters": [], | |
"responses": {} | |
} | |
} | |
} | |
}; | |
const swaggerDocumentSplit = swaggerDocument; | |
app.use((req, res, next) => { | |
if (req.url === '/favicon.ico') { | |
res.sendFile(__dirname + '/favicon.ico'); | |
} else if (req.url === '/swagger.json') { | |
res.sendFile(__dirname + '/swagger.json'); | |
} else if (req.url === '/my-custom.css') { | |
res.sendFile(__dirname + '/my-custom.css'); | |
} else { | |
next(); | |
} | |
}); | |
var options = { | |
validatorUrl: null, | |
oauth: { | |
clientId: "your-client-id1", | |
clientSecret: "your-client-secret-if-required1", | |
realm: "your-realms1", | |
appName: "your-app-name1", | |
scopeSeparator: ",", | |
additionalQueryStringParams: {} | |
}, | |
docExpansion: 'full', | |
operationsSorter: function (a, b) { | |
var score = { | |
'/test': 1, | |
'/bar': 2 | |
} | |
console.log('a', a.get("path"), b.get("path")) | |
return score[a.get("path")] < score[b.get("path")] | |
} | |
}; | |
app.post('/test', function (req, res) { | |
console.log('req', req) | |
res.json({ status: 'OK' }); | |
}); | |
app.get('/bar', function (req, res) { res.json({ status: 'OKISH' }); }); | |
app.use('/api-docs', swaggerUi.serve) | |
app.get('/api-docs', swaggerUi.setup(swaggerDocument, false, options, '.swagger-ui .topbar { background-color: red }')); | |
app.use('/api-docs-from-url', swaggerUi.serve) | |
app.get('/api-docs-from-url', swaggerUi.setup(null, false, options, '.swagger-ui .topbar { background-color: red }', null, '/swagger.json')); | |
var swaggerUiOpts = { | |
explorer: false, | |
swaggerOptions: options, | |
customCss: '.swagger-ui .topbar { background-color: blue }' | |
} | |
app.use('/api-docs-using-object', swaggerUi.serve) | |
app.get('/api-docs-using-object', swaggerUi.setup(swaggerDocument, swaggerUiOpts)); | |
var swaggerUiOpts2 = { | |
explorer: false, | |
swaggerOptions: options, | |
customCss: '.swagger-ui .topbar { background-color: pink }', | |
swaggerUrl: '/swagger.json', | |
customJs: '/my-custom.js', | |
operationsSorter: 'alpha', | |
customCssUrl: 'https://cdn.jsdelivr.net/npm/swagger-ui-themes@3.0.0/themes/3.x/theme-newspaper.css' | |
} | |
app.use('/api-docs-from-url-using-object', swaggerUi.serve) | |
app.get('/api-docs-from-url-using-object', swaggerUi.setup(null, swaggerUiOpts2)); | |
app.use('/api-docs-from-css-url', swaggerUi.serve) | |
app.get('/api-docs-from-css-url', swaggerUi.setup(null, swaggerUiOpts2)); | |
app.use('/api-docs-with-null', swaggerUi.serve) | |
app.get('/api-docs-with-null', swaggerUi.setup(swaggerDocument, null, options, '.swagger-ui .topbar { background-color: orange }')); | |
app.use('/api-docs-split', swaggerUi.serve) | |
app.get('/api-docs-split', swaggerUi.setup(swaggerDocumentSplit, null, options, '.swagger-ui .topbar { background-color: orange }')); | |
app.use('/api-docs-with-opts/', swaggerUi.serveWithOptions({ redirect: false })) | |
app.get('/api-docs-with-opts/', swaggerUi.setup(swaggerDocumentSplit, null, options, '.swagger-ui .topbar { background-color: orange }')); | |
var swaggerHtml = swaggerUi.generateHTML(swaggerDocument, swaggerUiOpts) | |
app.use('/api-docs-html1', swaggerUi.serveFiles(swaggerDocument, swaggerUiOpts)) | |
app.get('/api-docs-html1', (req, res) => { res.send(swaggerHtml) }); | |
app.use(function (req, res) { | |
res.status(404).send('Page not found'); | |
}); | |
app.listen(3000, () => console.log(`Example app listening on port ${3000}!`)) | |
module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment