Skip to content

Commit

Permalink
add support for custom error transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Mar 21, 2019
1 parent 587061a commit 2ca55ab
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
9 changes: 0 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,6 @@ OpenApiMiddleware.prototype.middleware = function() {
}));

const { statusCode, error } = transform(validationResult);
console.log(
'----provide to custom error handler',
errors,
status,
'-----',
error,
statusCode
);

return res.status(statusCode).json(error);
}
}
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.8-alpha",
"version": "0.1.10-alpha",
"description": "",
"main": "dist/index.js",
"scripts": {
Expand Down
68 changes: 68 additions & 0 deletions test/app.with.transform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var logger = require('morgan');
const http = require('http');
const { OpenApiMiddleware } = require('../');

var app = express();

app.use(bodyParser.json());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

app.use(express.static(path.join(__dirname, 'public')));

new OpenApiMiddleware({
apiSpecPath: './openapi.yaml',
validateApiDoc: true, // is the default
enableObjectCoercion: true, // should be default
errorTransform: v => {
return {
statusCode: v.status,
error: {
code: v.status,
message: v.errors[0].message,
},
};
},
}).install(app);

app.get('/v1/pets', function(req, res, next) {
console.log('at /v1/pets here');
res.json({
test: 'hi',
});
});

app.post('/v1/pets', function(req, res, next) {
res.json({
test: 'hi',
});
});

app.get('/v1/vets/:id', function(req, res, next) {
console.log('---- get /pets/:id', req.params);
// here
res.json({
id: req.params.id,
});
});

app.get('/v1/pets/:id', function(req, res, next) {
console.log('---- get /pets/:id', req.params);
// here
res.json({
id: req.params.id,
});
});

export const server = http.createServer(app);
const port = 3001;
server.listen(port);
console.log(`Listening on port ${port}`);

export default app;
25 changes: 25 additions & 0 deletions test/error.transform.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const expect = require('chai').expect;
import * as request from 'supertest';
import app, { server } from './app.with.transform';
import { server as server2 } from './app';

describe('custom error transform', () => {
after(done => {
console.log('done', app);
server.close();
server2.close();
done();
});

it('should transform the error output', async () => {
const id = 'my_id';
return request(app)
.get(`/v1/pets/${id}`)
.expect(400)
.then(r => {
const e = r.body;
expect(e.code).equals(400);
expect(e.message).equals('should be integer');
});
});
});
4 changes: 3 additions & 1 deletion test/routes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const expect = require('chai').expect;
import * as request from 'supertest';
import app, { server } from './app';
const { OpenApiMiddleware } = require('../');
import { server as server2 } from './app.with.transform';

const packageJson = require('../package.json');

describe(packageJson.name, () => {
after(done => {
console.log('done', app);
server.close();
server2.close();
done();
});
it(`should test something`, () => {
Expand Down

0 comments on commit 2ca55ab

Please sign in to comment.