json-server
Advanced tools
Comparing version 0.9.3 to 0.9.4
# Change Log | ||
## [0.9.4][2016-12-08] | ||
* Improve rewriter [#431](https://github.com/typicode/json-server/issues/431) | ||
* Improve watch mode [#427](https://github.com/typicode/json-server/pull/427) | ||
## [0.9.3][2016-12-07] | ||
@@ -4,0 +9,0 @@ |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var path = require('path'); | ||
var jph = require('json-parse-helpfulerror'); | ||
var _ = require('lodash'); | ||
@@ -190,2 +191,3 @@ var chalk = require('chalk'); | ||
var watchedDir = path.dirname(source); | ||
var readError = false; | ||
fs.watch(watchedDir, function (event, file) { | ||
@@ -198,3 +200,16 @@ // https://github.com/typicode/json-server/issues/420 | ||
if (is.JSON(watchedFile)) { | ||
var obj = JSON.parse(fs.readFileSync(watchedFile)); | ||
var obj = void 0; | ||
try { | ||
obj = jph.parse(fs.readFileSync(watchedFile)); | ||
if (readError) { | ||
console.log(chalk.green(' Read error has been fixed :)')); | ||
readError = false; | ||
} | ||
} catch (e) { | ||
readError = true; | ||
console.log(chalk.red(' Error reading ' + watchedFile)); | ||
console.error(e.message); | ||
return; | ||
} | ||
// Compare .json file content with in memory database | ||
@@ -207,6 +222,2 @@ var isDatabaseDifferent = !_.isEqual(obj, app.db.getState()); | ||
} | ||
} else { | ||
console.log(chalk.gray(' ' + source + ' has changed, reloading...')); | ||
server && server.destroy(); | ||
start(); | ||
} | ||
@@ -213,0 +224,0 @@ } |
@@ -6,3 +6,5 @@ 'use strict'; | ||
var _ = require('lodash'); | ||
function updateQueryString(target, sourceUrl) { | ||
return ~sourceUrl.indexOf('?') ? _.assign(target, url.parse(sourceUrl, true).query) : {}; | ||
} | ||
module.exports = function (routes) { | ||
@@ -20,6 +22,3 @@ var router = express.Router(); | ||
req.url = target; | ||
if (target.indexOf('?')) { | ||
// create query from target | ||
_.assign(req.query, url.parse(target, true).query); | ||
} | ||
req.query = updateQueryString(req.query, req.url); | ||
next(); | ||
@@ -31,2 +30,3 @@ }); | ||
req.url = req.url.replace(route, routes[route]); | ||
req.query = updateQueryString(req.query, req.url); | ||
next(); | ||
@@ -33,0 +33,0 @@ }); |
{ | ||
"name": "json-server", | ||
"version": "0.9.3", | ||
"version": "0.9.4", | ||
"description": "Serves JSON files through REST routes.", | ||
@@ -18,2 +18,3 @@ "main": "./lib/server/index.js", | ||
"express": "^4.9.5", | ||
"json-parse-helpfulerror": "^1.0.3", | ||
"lodash": "^4.11.2", | ||
@@ -20,0 +21,0 @@ "lowdb": "^0.14.0", |
@@ -81,3 +81,5 @@ const assert = require('assert') | ||
'/blog/posts/:id/show': '/posts/:id', | ||
'/comments/special/:userId-:body': '/comments/?userId=:userId&body=:body' | ||
'/comments/special/:userId-:body': '/comments/?userId=:userId&body=:body', | ||
'/firstpostwithcomments': '/posts/1?_embed=comments' | ||
})) | ||
@@ -588,7 +590,9 @@ server.use(router) | ||
it('should respond with json and update resource', (done) => { | ||
var partial = {body: 'bar'} | ||
var post = {id: 1, body: 'bar'} | ||
request(server) | ||
.patch('/posts/1') | ||
.send({body: 'bar'}) | ||
.send(partial) | ||
.expect('Content-Type', /json/) | ||
.expect({id: 1, body: 'bar'}) | ||
.expect(post) | ||
.expect(200) | ||
@@ -599,3 +603,3 @@ .end((err, res) => { | ||
// assert it was created in database too | ||
assert.deepStrictEqual(db.posts[0], {id: 1, body: 'bar'}) | ||
assert.deepStrictEqual(db.posts[0], post) | ||
done() | ||
@@ -690,2 +694,11 @@ }) | ||
it('should rewrite using query without params', function (done) { | ||
const expectedPost = _.cloneDeep(db.posts[0]) | ||
expectedPost.comments = [ db.comments[0], db.comments[1] ] | ||
request(server) | ||
.get('/firstpostwithcomments') | ||
.expect(expectedPost) | ||
.end(done) | ||
}) | ||
it('should rewrite using params and query', function (done) { | ||
@@ -692,0 +705,0 @@ request(server) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
92122
43
2077
20