-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for custom error transforms
- Loading branch information
Carmine DiMascio
committed
Mar 21, 2019
1 parent
587061a
commit 2ca55ab
Showing
6 changed files
with
98 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters