Skip to content

Commit

Permalink
Update rewriter
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Dec 25, 2016
1 parent 442746e commit bb64e71
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/server/rewriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
21 changes: 14 additions & 7 deletions test/server/plural.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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)
})

Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit bb64e71

Please sign in to comment.