Skip to content

Commit 21d2fff

Browse files
authored
Update rewriter (typicode#580)
1 parent d5ac11f commit 21d2fff

File tree

7 files changed

+43
-44
lines changed

7 files changed

+43
-44
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
# Change Log
22

3+
## 0.11.0 - 2017-07-05
4+
5+
Switch to [express-urlrewrite](https://github.com/kapouer/express-urlrewrite) to support rewriting query parameters (e.g. `/articles?id=1 # → /posts/1`)
6+
7+
If you're rewriting default routes, you'll need to update your `routes.json` file
8+
(see [add custom routes](https://github.com/typicode/json-server#add-custom-routes) for updated doc).
9+
310
## 0.10.3 - 2017-06-28
411

5-
* Fix line-break error in CLI
12+
* Fix `line-break` error in CLI
613

714
## 0.10.2 - 2017-06-28
815

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,10 @@ Create a `routes.json` file. Pay attention to start every route with `/`.
314314

315315
```json
316316
{
317-
"/api/": "/",
318-
"/blog/:resource/:id/show": "/:resource/:id",
319-
"/blog/:category": "/posts?category=:category"
317+
"/api/*": "/$1",
318+
"/:resource/:id/show": "/:resource/:id",
319+
"/posts/:category": "/posts?category=:category",
320+
"/articles\\?id=:id": "/posts/:id"
320321
}
321322
```
322323

@@ -331,8 +332,9 @@ Now you can access resources using additional routes.
331332
```sh
332333
/api/posts # → /posts
333334
/api/posts/1 # → /posts/1
334-
/blog/posts/1/show # → /posts/1
335-
/blog/javascript # → /posts?category=javascript
335+
/posts/1/show # → /posts/1
336+
/posts/javascript # → /posts?category=javascript
337+
/articles?id=1 # → /posts/1
336338
```
337339

338340
### Add middlewares

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"cors": "^2.3.0",
1616
"errorhandler": "^1.2.0",
1717
"express": "^4.9.5",
18+
"express-urlrewrite": "^1.2.0",
1819
"json-parse-helpfulerror": "^1.0.3",
1920
"lodash": "^4.11.2",
2021
"lodash-id": "^0.13.0",

src/server/rewriter.js

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,15 @@
11
const express = require('express')
2-
const url = require('url')
3-
const _ = require('lodash')
4-
function updateQueryString(target, sourceUrl) {
5-
return ~sourceUrl.indexOf('?')
6-
? _.assign(target, url.parse(sourceUrl, true).query)
7-
: {}
8-
}
2+
const rewrite = require('express-urlrewrite')
3+
94
module.exports = routes => {
105
const router = express.Router()
116

127
router.get('/__rules', (req, res) => {
138
res.json(routes)
149
})
1510

16-
Object.keys(routes).forEach(route => {
17-
if (route.indexOf(':') !== -1) {
18-
router.all(route, (req, res, next) => {
19-
// Rewrite target url using params
20-
let target = routes[route]
21-
for (let param in req.params) {
22-
target = target.replace(`:${param}`, req.params[param])
23-
}
24-
req.url = target
25-
req.query = updateQueryString(req.query, req.url)
26-
next()
27-
})
28-
} else {
29-
router.all(`${route}*`, (req, res, next) => {
30-
// Rewrite url by replacing prefix
31-
req.url = req.url.replace(route, routes[route])
32-
req.query = updateQueryString(req.query, req.url)
33-
next()
34-
})
35-
}
11+
Object.keys(routes).forEach(key => {
12+
router.use(rewrite(key, routes[key]))
3613
})
3714

3815
return router

test/cli/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('cli', () => {
4242
)
4343

4444
routesFile = tempWrite.sync(
45-
JSON.stringify({ '/blog/': '/' }),
45+
JSON.stringify({ '/blog/*': '/$1' }),
4646
'routes.json'
4747
)
4848

@@ -260,7 +260,7 @@ describe('cli', () => {
260260
})
261261

262262
it('should watch routes file', done => {
263-
fs.writeFileSync(routesFile, JSON.stringify({ '/api/': '/' }))
263+
fs.writeFileSync(routesFile, JSON.stringify({ '/api/*': '/$1' }))
264264
setTimeout(() => {
265265
request.get('/api/posts').expect(200, done)
266266
}, 1000)

test/server/plural.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ describe('Server', () => {
88
let router
99
let db
1010
const rewriterRules = {
11-
'/api/': '/',
11+
'/api/*': '/$1',
1212
'/blog/posts/:id/show': '/posts/:id',
1313
'/comments/special/:userId-:body': '/comments/?userId=:userId&body=:body',
1414
'/firstpostwithcomments': '/posts/1?_embed=comments',
15-
'/articles?_id=:id': '/posts/:id'
15+
'/articles\\?_id=:id': '/posts/:id'
1616
}
1717

1818
beforeEach(() => {
@@ -660,13 +660,8 @@ describe('Server', () => {
660660
it('should rewrite using params and query', () =>
661661
request(server).get('/comments/special/1-quux').expect([db.comments[4]]))
662662

663-
// TODO
664-
// it('should rewrite query params', () => (
665-
// request(server)
666-
// .get('/articles?_id=1')
667-
// .expect(db.posts[0])
668-
// .end(done)
669-
// })
663+
it('should rewrite query params', () =>
664+
request(server).get('/articles?_id=1').expect(db.posts[0]))
670665

671666
it('should expose routes', () =>
672667
request(server).get('/__rules').expect(rewriterRules))

0 commit comments

Comments
 (0)