Skip to content

Commit

Permalink
extract route id to function
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Mar 22, 2019
1 parent 12c200b commit bff3be1
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,25 @@ OpenApiMiddleware.prototype.install = function(app: ExpressApp) {
}
// install use on routes without paths
app.all(_.uniq(noPathParamRoutes), this._middleware());

// TODOD add middleware to capture routes not defined in openapi spec and throw not 404
};

OpenApiMiddleware.prototype._middleware = function() {
return (req, res, next) => {
const { path: rpath, method, route } = req;
var path = Array.isArray(route.path)
? route.path.find(r => r === rpath)
: route.path || rpath;
const path = identifyRoutePath(route, rpath);
if (path && method) {
// TODO add option to enable undocumented routes to pass through without 404
const documentedRoute = this.routeMap[path];
if (!documentedRoute) {
// TODO add option to enable undocumented routes to pass through without 404
// TODO this should not occur as we only set up middleware and params on routes defined in the openapi spec
const { statusCode, error } = this._transformValidationResult(
notFoundError(path)
);
return res.status(statusCode).json(error);
}

// TODO add option to enable undocumented methods to pass through
const schema = documentedRoute[method.toUpperCase()];
if (!schema) {
const { statusCode, error } = this._transformValidationResult(
Expand All @@ -89,6 +89,9 @@ OpenApiMiddleware.prototype._middleware = function() {
return res.status(statusCode).json(error);
}

// this req matched an openapi route, mark it
req.openapi = {};

// TODO coercer and request validator fail on null parameters
if (!schema.parameters) {
schema.parameters = [];
Expand Down Expand Up @@ -140,6 +143,12 @@ OpenApiMiddleware.prototype._transformValidationResult = function(
}
};

function identifyRoutePath(route, path) {
return Array.isArray(route.path)
? route.path.find(r => r === path)
: route.path || path;
}

function toExpressParams(part) {
return part.replace(/\{([^}]+)}/g, ':$1');
}
Expand Down

0 comments on commit bff3be1

Please sign in to comment.