restify-swagger-jsdoc
Advanced tools
Comparing version 3.2.1 to 3.2.2
@@ -18,3 +18,3 @@ "use strict"; | ||
} | ||
function createSwaggerPage(options) { | ||
function validateOptions(options) { | ||
if (!options.title) { | ||
@@ -32,3 +32,4 @@ throw new Error('options.title is required'); | ||
} | ||
const swaggerUiPath = `${trimTrailingChar(path.dirname(require.resolve('swagger-ui-dist')), path.sep)}${path.sep}`; | ||
} | ||
function createSwaggerSpec(options) { | ||
const swaggerSpec = swaggerJSDoc({ | ||
@@ -61,4 +62,24 @@ swaggerDefinition: { | ||
} | ||
const publicPath = trimTrailingChar(options.path); | ||
options.server.get(`${publicPath}/swagger.json`, (req, res, next) => { | ||
return swaggerSpec; | ||
} | ||
function loadIndexPage(options, req, publicPath, content) { | ||
const isReqSecure = options.forceSecure || req.isSecure(); | ||
const jsonFileUrl = `${isReqSecure ? 'https' : 'http'}://${req.headers.host}${publicPath}/swagger.json`; | ||
let localContent = content.toString().replace('url: "https://petstore.swagger.io/v2/swagger.json"', `url: "${jsonFileUrl}"`); | ||
if (options.validatorUrl === null || typeof options.validatorUrl === 'string') { | ||
localContent = addSwaggerUiConfig(localContent, 'validatorUrl', options.validatorUrl); | ||
} | ||
if (Array.isArray(options.supportedSubmitMethods)) { | ||
localContent = addSwaggerUiConfig(localContent, 'supportedSubmitMethods', options.supportedSubmitMethods); | ||
} | ||
return Buffer.from(localContent); | ||
} | ||
function setContentType(file, res) { | ||
const contentType = mime.lookup(file); | ||
if (contentType !== false) { | ||
res.setHeader('Content-Type', contentType); | ||
} | ||
} | ||
function createSpecRoute(server, publicPath, swaggerSpec) { | ||
server.get(`${publicPath}/swagger.json`, (req, res, next) => { | ||
res.setHeader('Content-type', 'application/json'); | ||
@@ -68,3 +89,5 @@ res.send(swaggerSpec); | ||
}); | ||
options.server.get(publicPath, (req, res, next) => { | ||
} | ||
function createHomeRoute(server, publicPath) { | ||
server.get(publicPath, (req, res, next) => { | ||
res.setHeader('Location', `${publicPath}/index.html`); | ||
@@ -74,2 +97,4 @@ res.send(302); | ||
}); | ||
} | ||
function createDynamicRoute(options, publicPath, swaggerUiPath) { | ||
options.server.get(`${publicPath}/*`, (req, res, next) => { | ||
@@ -86,17 +111,5 @@ const file = req.params['*']; | ||
if (file === 'index.html') { | ||
const isReqSecure = options.forceSecure || req.isSecure(); | ||
const jsonFileUrl = `${isReqSecure ? 'https' : 'http'}://${req.headers.host}${publicPath}/swagger.json`; | ||
let localContent = content.toString().replace('url: "https://petstore.swagger.io/v2/swagger.json"', `url: "${jsonFileUrl}"`); | ||
if (options.validatorUrl === null || typeof options.validatorUrl === 'string') { | ||
localContent = addSwaggerUiConfig(localContent, 'validatorUrl', options.validatorUrl); | ||
} | ||
if (Array.isArray(options.supportedSubmitMethods)) { | ||
localContent = addSwaggerUiConfig(localContent, 'supportedSubmitMethods', options.supportedSubmitMethods); | ||
} | ||
content = Buffer.from(localContent); | ||
content = loadIndexPage(options, req, publicPath, content); | ||
} | ||
const contentType = mime.lookup(file); | ||
if (contentType !== false) { | ||
res.setHeader('Content-Type', contentType); | ||
} | ||
setContentType(file, res); | ||
res.write(content); | ||
@@ -108,4 +121,15 @@ res.end(); | ||
} | ||
function createRoutes(options, swaggerSpec) { | ||
const swaggerUiPath = `${trimTrailingChar(path.dirname(require.resolve('swagger-ui-dist')), path.sep)}${path.sep}`; | ||
const publicPath = trimTrailingChar(options.path); | ||
createSpecRoute(options.server, publicPath, swaggerSpec); | ||
createHomeRoute(options.server, publicPath); | ||
createDynamicRoute(options, publicPath, swaggerUiPath); | ||
} | ||
function createSwaggerPage(options) { | ||
validateOptions(options); | ||
createRoutes(options, createSwaggerSpec(options)); | ||
} | ||
exports.createSwaggerPage = createSwaggerPage; | ||
exports.default = { createSwaggerPage }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "restify-swagger-jsdoc", | ||
"version": "3.2.1", | ||
"version": "3.2.2", | ||
"description": "Create Swagger documentation page based on jsdoc", | ||
@@ -5,0 +5,0 @@ "main": "js/index.js", |
Sorry, the diff of this file is not supported yet
21277
208