Update rewriter

This commit is contained in:
typicode
2016-12-25 07:44:36 +01:00
parent 442746efcc
commit bb64e719b7
2 changed files with 18 additions and 7 deletions

View File

@ -7,6 +7,10 @@ function updateQueryString (target, sourceUrl) {
module.exports = (routes) => {
const router = express.Router()
router.get('/__rules', (req, res) => {
res.json(routes)
})
Object.keys(routes).forEach((route) => {
if (route.indexOf(':') !== -1) {
router.all(route, (req, res, next) => {

View File

@ -7,6 +7,12 @@ describe('Server', () => {
let server
let router
let db
const rewriterRules = {
'/api/': '/',
'/blog/posts/:id/show': '/posts/:id',
'/comments/special/:userId-:body': '/comments/?userId=:userId&body=:body',
'/firstpostwithcomments': '/posts/1?_embed=comments'
}
beforeEach(() => {
db = {}
@ -75,13 +81,7 @@ describe('Server', () => {
server = jsonServer.create()
router = jsonServer.router(db)
server.use(jsonServer.defaults())
server.use(jsonServer.rewriter({
'/api/': '/',
'/blog/posts/:id/show': '/posts/:id',
'/comments/special/:userId-:body': '/comments/?userId=:userId&body=:body',
'/firstpostwithcomments': '/posts/1?_embed=comments'
}))
server.use(jsonServer.rewriter(rewriterRules))
server.use(router)
})
@ -703,6 +703,13 @@ describe('Server', () => {
.expect([db.comments[4]])
.end(done)
})
it('should expose routes', (done) => {
request(server)
.get('/__rules')
.expect(rewriterRules)
.end(done)
})
})
describe('router.render', (done) => {