Skip to content

Commit

Permalink
return error for unknown route and media type
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Mar 22, 2019
1 parent fefc300 commit 305b1e0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 13 deletions.
77 changes: 66 additions & 11 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ export interface OpenApiMiddlewareOpts extends OpenAPIFrameworkArgs {
errorTransform?: (validationResult: any) => ErrorResponse;
}

const unsupportedMediaTypeError = {
statusCode: 415,
error: {
errors: [
{
status: 415,
message: 'unsupported media type',
},
],
},
};

const notFoundError = {
statusCode: 404,
error: {
errors: [
{
status: 404,
message: 'not found',
},
],
},
};

export function OpenApiMiddleware(opts: OpenApiMiddlewareOpts) {
if (!opts.apiSpecPath) throw new Error('apiSpecPath required');
const apiContents = loadSpecFile(opts.apiSpecPath);
Expand Down Expand Up @@ -94,11 +118,22 @@ OpenApiMiddleware.prototype.middleware = function() {
if (path && method) {
// TODO add option to enable undocumented routes to pass through without 404
const documentedRoute = this.routeMap[path];
if (!documentedRoute) return res.status(404).end();
if (!documentedRoute) {
const { statusCode, error } = this._transformValidationResult(
notFoundError
);
return res.status(statusCode).json(error);
// return res.status(404).end();
}

// TODO add option to enable undocumented methods to pass through
const schema = documentedRoute[method.toUpperCase()];
if (!schema) return res.status(415).end();
if (!schema) {
const { statusCode, error } = this._transformValidationResult(
unsupportedMediaTypeError
);
return res.status(statusCode).json(error);
}

// TODO coercer and request validator fail on null parameters
if (!schema.parameters) {
Expand Down Expand Up @@ -126,22 +161,42 @@ OpenApiMiddleware.prototype.middleware = function() {
}).validate(req);

if (validationResult && validationResult.errors.length > 0) {
const { errors, status } = validationResult;
const transform =
this.opts.errorTransform ||
(v => ({
statusCode: v.status,
error: { errors: v.errors },
}));

const { statusCode, error } = transform(validationResult);
const { statusCode, error } = this._transformValidationResult(
validationResult
);
return res.status(statusCode).json(error);
// const { errors, status } = validationResult;
// const transform =
// this.opts.errorTransform ||
// (v => ({
// statusCode: v.status,
// error: { errors: v.errors },
// }));

// const { statusCode, error } = transform(validationResult);
// return res.status(statusCode).json(error);
}
}
next();
};
};

OpenApiMiddleware.prototype._transformValidationResult = function(
validationResult
) {
if (validationResult && validationResult.errors.length > 0) {
const { errors, status } = validationResult;
const transform =
this.opts.errorTransform ||
(v => ({
statusCode: v.status,
error: { errors: v.errors },
}));

return transform(validationResult);
}
};

function toExpressParams(part) {
return part.replace(/\{([^}]+)}/g, ':$1');
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-middleware-openapi",
"version": "0.1.15-alpha",
"version": "0.1.16-alpha",
"description": "",
"main": "dist/index.js",
"scripts": {
Expand Down

0 comments on commit 305b1e0

Please sign in to comment.