oas3-tools
Advanced tools
Comparing version 2.0.0 to 2.0.1
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const swagger_router_1 = require("./middleware/swagger.router"); | ||
const express_app_config_1 = require("./middleware/express.app.config"); | ||
function swaggerRouter() { | ||
return new swagger_router_1.SwaggerRouter(); | ||
} | ||
exports.swaggerRouter = swaggerRouter; | ||
function expressAppConfig(definitionPath, routingOptions) { | ||
@@ -10,0 +5,0 @@ return new express_app_config_1.ExpressAppConfig(definitionPath, routingOptions); |
{ | ||
"name": "oas3-tools", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Swagger-UI and API Rest Routing for Open API v3.", | ||
@@ -45,3 +45,3 @@ "main": "dist/index.js", | ||
"express": "^4.17.1", | ||
"express-openapi-validator": "^3.2.3", | ||
"express-openapi-validator": "^3.6.7", | ||
"js-yaml": "^3.13.1", | ||
@@ -48,0 +48,0 @@ "lodash": "^4.17.15", |
@@ -9,2 +9,3 @@ 'use strict'; | ||
import { SwaggerRouter } from './swagger.router'; | ||
import { SwaggerParameters } from './swagger.parameters'; | ||
import * as logger from 'morgan'; | ||
@@ -47,2 +48,3 @@ import * as fs from 'fs'; | ||
.then(() => { | ||
this.app.use(new SwaggerParameters().checkParameters()); | ||
this.app.use(new SwaggerRouter().initialize(this.routingOptions)); | ||
@@ -49,0 +51,0 @@ |
@@ -0,0 +0,0 @@ /* |
@@ -12,10 +12,2 @@ 'use strict'; | ||
getHandlerName (req) { | ||
if (req.openapi.schema['x-swagger-router-controller']) { | ||
return req.openapi.schema['x-swagger-router-controller'] + '_' + (req.openapi.schema.operationId ? req.openapi.schema.operationId : req.method.toLowerCase()); | ||
} else { | ||
return req.openapi.schema.operationId; | ||
} | ||
} | ||
handlerCacheFromDir(dirOrDirs: any) { | ||
@@ -37,3 +29,3 @@ const handlerCache: object = {}; | ||
const controllerName: string = file.replace(jsFileRegex, ''); | ||
var controller: string; | ||
let controller: string; | ||
@@ -49,3 +41,3 @@ if (file.match(jsFileRegex) && file.indexOf(".test.js") === -1) { | ||
each(controller, function (value, name) { | ||
var handlerId = controllerName + '_' + name; | ||
let handlerId = controllerName + '_' + name; | ||
@@ -68,8 +60,2 @@ debug(' %s%s', | ||
send405(req, res, next) { | ||
var err = new Error('Route defined in OpenAPI specification (' + req.openapi.openApiRoute + ') but there is no defined on' + req.method.toUpperCase() + ' operation.'); | ||
res.statusCode = 405; | ||
return next(err); | ||
} | ||
initialize(options) { | ||
@@ -104,11 +90,22 @@ var handlerCache = {}; | ||
const getHandlerName = this.getHandlerName; | ||
const send405 = this.send405; | ||
const getHandlerName = (req) => { | ||
if (req.openapi.schema['x-swagger-router-controller']) { | ||
return req.openapi.schema['x-swagger-router-controller'] + '_' + (req.openapi.schema.operationId ? req.openapi.schema.operationId : req.method.toLowerCase()); | ||
} else { | ||
return req.openapi.schema.operationId; | ||
} | ||
}; | ||
return function swaggerRouter (req, res, next) { | ||
var operation = req.openapi ? req.openapi.schema.operationId : undefined; | ||
var handler; | ||
var handlerName; | ||
var rErr; | ||
const send405 = (req, res, next) => { | ||
let err = new Error('Route defined in OpenAPI specification (' + req.openapi.openApiRoute + ') but there is no defined on' + req.method.toUpperCase() + ' operation.'); | ||
res.statusCode = 405; | ||
return next(err); | ||
}; | ||
return (req, res, next) => { | ||
let operation = req.openapi ? req.openapi.schema.operationId : undefined; | ||
let handler; | ||
let handlerName; | ||
let rErr; | ||
debug('%s %s', req.method, req.url); | ||
@@ -131,6 +128,5 @@ debug(' Will process: %s', isUndefined(operation) ? 'no' : 'yes'); | ||
try { | ||
return handler(req, res, next); | ||
return handler.apply(this, req.openapi.swaggerParameters); | ||
} catch (err) { | ||
rErr = err; | ||
debug('Handler threw an unexpected error: %s\n%s', err.message, err.stack); | ||
@@ -140,3 +136,2 @@ } | ||
rErr = new Error('Cannot resolve the configured swagger-router handler: ' + handlerName); | ||
res.statusCode = 500; | ||
@@ -146,10 +141,7 @@ } | ||
debug(' No handler for method: %s', req.method); | ||
return send405(req, res, next); | ||
} | ||
if (rErr) { | ||
debugError(rErr, debug); | ||
} | ||
return next(rErr); | ||
@@ -156,0 +148,0 @@ }; |
@@ -0,0 +0,0 @@ export class SwaggerUiOptions { |
@@ -0,0 +0,0 @@ 'use strict'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
5303536
21
9055