From 3f355b3adabc3f62d4400e0f09906ce8fb32a267 Mon Sep 17 00:00:00 2001
From: typicode
Date: Sun, 27 Nov 2016 20:12:38 +0100
Subject: [PATCH 001/449] Check if file is not null when watching dir
---
src/cli/run.js | 38 ++++++++++++++++++++++----------------
1 file changed, 22 insertions(+), 16 deletions(-)
diff --git a/src/cli/run.js b/src/cli/run.js
index 648974c2d..1dc7fd1a4 100644
--- a/src/cli/run.js
+++ b/src/cli/run.js
@@ -185,21 +185,25 @@ module.exports = function (argv) {
// Since lowdb uses atomic writing, directory is watched instead of file
const watchedDir = path.dirname(source)
fs.watch(watchedDir, (event, file) => {
- const watchedFile = path.resolve(watchedDir, file)
- if (watchedFile === path.resolve(source)) {
- if (is.JSON(watchedFile)) {
- var obj = JSON.parse(fs.readFileSync(watchedFile))
- // Compare .json file content with in memory database
- var isDatabaseDifferent = !_.isEqual(obj, app.db.getState())
- if (isDatabaseDifferent) {
+ // https://github.com/typicode/json-server/issues/420
+ // file can be null
+ if (file) {
+ const watchedFile = path.resolve(watchedDir, file)
+ if (watchedFile === path.resolve(source)) {
+ if (is.JSON(watchedFile)) {
+ var obj = JSON.parse(fs.readFileSync(watchedFile))
+ // Compare .json file content with in memory database
+ var isDatabaseDifferent = !_.isEqual(obj, app.db.getState())
+ if (isDatabaseDifferent) {
+ console.log(chalk.gray(` ${source} has changed, reloading...`))
+ server && server.destroy()
+ start()
+ }
+ } else {
console.log(chalk.gray(` ${source} has changed, reloading...`))
server && server.destroy()
start()
}
- } else {
- console.log(chalk.gray(` ${source} has changed, reloading...`))
- server && server.destroy()
- start()
}
}
})
@@ -208,11 +212,13 @@ module.exports = function (argv) {
if (argv.routes) {
const watchedDir = path.dirname(argv.routes)
fs.watch(watchedDir, (event, file) => {
- const watchedFile = path.resolve(watchedDir, file)
- if (watchedFile === path.resolve(argv.routes)) {
- console.log(chalk.gray(` ${argv.routes} has changed, reloading...`))
- server && server.destroy()
- start()
+ if (file) {
+ const watchedFile = path.resolve(watchedDir, file)
+ if (watchedFile === path.resolve(argv.routes)) {
+ console.log(chalk.gray(` ${argv.routes} has changed, reloading...`))
+ server && server.destroy()
+ start()
+ }
}
})
}
From fdab5ee4d0cd4a21693b86631aeb3f93344f2999 Mon Sep 17 00:00:00 2001
From: typicode
Date: Mon, 28 Nov 2016 08:55:14 +0100
Subject: [PATCH 002/449] Create appveyor.yml
---
appveyor.yml | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
create mode 100644 appveyor.yml
diff --git a/appveyor.yml b/appveyor.yml
new file mode 100644
index 000000000..5fef32bbf
--- /dev/null
+++ b/appveyor.yml
@@ -0,0 +1,21 @@
+# Test against this version of Node.js
+environment:
+ nodejs_version: "7"
+
+# Install scripts. (runs after repo cloning)
+install:
+ # Get the latest stable version of Node.js
+ - ps: Install-Product node $env:nodejs_version
+ # install modules
+ - npm install
+
+# Post-install test scripts.
+test_script:
+ # Output useful info for debugging.
+ - node --version
+ - npm --version
+ # run tests
+ - npm test
+
+# Don't actually build.
+build: off
From 2ce7cd37b289e6e286667262f223939ecf46c954 Mon Sep 17 00:00:00 2001
From: Sam Blowes
Date: Mon, 28 Nov 2016 11:09:58 +0000
Subject: [PATCH 003/449] Fix broken links (#426)
---
README.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index 4a9b8a73e..945a9aaf4 100644
--- a/README.md
+++ b/README.md
@@ -102,7 +102,7 @@ $ npm install -g json-server
## Routes
-Based on the previous `db.json` file, here are all the default routes. You can also add [other routes](#add-routes) using `--routes`.
+Based on the previous `db.json` file, here are all the default routes. You can also add [other routes](#add-custom-routes) using `--routes`.
### Plural routes
@@ -211,7 +211,7 @@ GET /comments?_expand=post
GET /comments/1?_expand=post
```
-To get or create nested resources (by default one level, [add routes](#add-routes) for more)
+To get or create nested resources (by default one level, [add custom routes](#add-custom-routes) for more)
```
GET /posts/1/comments
From c527e1ef87ec79818c7c1376e8451676782e4f6e Mon Sep 17 00:00:00 2001
From: typicode
Date: Mon, 28 Nov 2016 13:30:43 +0100
Subject: [PATCH 004/449] Fix nohup issue
---
CHANGELOG.md | 5 +++++
src/cli/run.js | 6 +++++-
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8b90412d7..4179153a3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,10 @@
# Change Log
+## [Unreleased][Unreleased]
+
+* Fix [#221](https://github.com/typicode/json-server/issues/221) nohup support
+* Fix [#420](https://github.com/typicode/json-server/issues/420) TypeError when watching db.json
+
## [0.9.1][2016-11-21]
* Fix
diff --git a/src/cli/run.js b/src/cli/run.js
index 1dc7fd1a4..49a5fd3e9 100644
--- a/src/cli/run.js
+++ b/src/cli/run.js
@@ -160,7 +160,11 @@ module.exports = function (argv) {
chalk.gray(' Type s + enter at any time to create a snapshot of the database')
)
- process.stdin.resume()
+ // Support nohup
+ // https://github.com/typicode/json-server/issues/221
+ process.stdin.on('error', (err) => {
+ console.log(' Error, can\'t read from stdin')
+ })
process.stdin.setEncoding('utf8')
process.stdin.on('data', (chunk) => {
if (chunk.trim().toLowerCase() === 's') {
From b6b48faceea09a145da3e3d0b64567bce6f5b33a Mon Sep 17 00:00:00 2001
From: typicode
Date: Mon, 28 Nov 2016 13:33:29 +0100
Subject: [PATCH 005/449] Fix standard error
---
src/cli/run.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/cli/run.js b/src/cli/run.js
index 49a5fd3e9..92fcbd5c9 100644
--- a/src/cli/run.js
+++ b/src/cli/run.js
@@ -162,7 +162,7 @@ module.exports = function (argv) {
// Support nohup
// https://github.com/typicode/json-server/issues/221
- process.stdin.on('error', (err) => {
+ process.stdin.on('error', () => {
console.log(' Error, can\'t read from stdin')
})
process.stdin.setEncoding('utf8')
From cb1d474ac63690bd3db9fd76b142a4fe9d9d1b62 Mon Sep 17 00:00:00 2001
From: typicode
Date: Mon, 28 Nov 2016 14:07:30 +0100
Subject: [PATCH 006/449] Update run.js
---
src/cli/run.js | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/cli/run.js b/src/cli/run.js
index 92fcbd5c9..9c739097b 100644
--- a/src/cli/run.js
+++ b/src/cli/run.js
@@ -163,7 +163,8 @@ module.exports = function (argv) {
// Support nohup
// https://github.com/typicode/json-server/issues/221
process.stdin.on('error', () => {
- console.log(' Error, can\'t read from stdin')
+ console.log(` Error, can't read from stdin`)
+ console.log(` Creating a snapshot from the CLI won't be possible`)
})
process.stdin.setEncoding('utf8')
process.stdin.on('data', (chunk) => {
From 814bae7969b226f9cd69fecc59d99b8012323893 Mon Sep 17 00:00:00 2001
From: typicode
Date: Tue, 29 Nov 2016 08:54:28 +0100
Subject: [PATCH 007/449] 0.9.2
---
package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package.json b/package.json
index 5781fd0bd..8886f1ceb 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "json-server",
- "version": "0.9.1",
+ "version": "0.9.2",
"description": "Serves JSON files through REST routes.",
"main": "./lib/server/index.js",
"bin": "./bin/index.js",
From 060d194b044835bf7eb5adfec0eca72ee9b193fe Mon Sep 17 00:00:00 2001
From: typicode
Date: Tue, 29 Nov 2016 07:56:14 +0100
Subject: [PATCH 008/449] Update CHANGELOG.md
---
CHANGELOG.md | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4179153a3..8e9fdbb9b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,9 +1,9 @@
# Change Log
-## [Unreleased][Unreleased]
+## [0.9.2][2016-11-29]
-* Fix [#221](https://github.com/typicode/json-server/issues/221) nohup support
-* Fix [#420](https://github.com/typicode/json-server/issues/420) TypeError when watching db.json
+* Fix [#221](https://github.com/typicode/json-server/issues/221) `nohup` support
+* Fix [#420](https://github.com/typicode/json-server/issues/420) TypeError when watching `db.json`
## [0.9.1][2016-11-21]
From 485378d22203778f839e4f3ff66072d4d6f157b6 Mon Sep 17 00:00:00 2001
From: zhangbiaoguang
Date: Tue, 29 Nov 2016 18:55:30 +0800
Subject: [PATCH 009/449] fixed the bug on windows: SyntaxError-Unexpected end
of input
---
src/cli/run.js | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/src/cli/run.js b/src/cli/run.js
index 648974c2d..ebdbd2c05 100644
--- a/src/cli/run.js
+++ b/src/cli/run.js
@@ -188,7 +188,14 @@ module.exports = function (argv) {
const watchedFile = path.resolve(watchedDir, file)
if (watchedFile === path.resolve(source)) {
if (is.JSON(watchedFile)) {
- var obj = JSON.parse(fs.readFileSync(watchedFile))
+ var obj = null;
+ try {
+ obj = JSON.parse(fs.readFileSync(watchedFile));
+ } catch (e) {
+ console.log('The format of the json file is not right, please check.');
+ console.dir(e);
+ return;
+ }
// Compare .json file content with in memory database
var isDatabaseDifferent = !_.isEqual(obj, app.db.getState())
if (isDatabaseDifferent) {
From f2f96514e5984962237103f0adc8f4d2e084922f Mon Sep 17 00:00:00 2001
From: typicode
Date: Tue, 6 Dec 2016 13:05:28 +0100
Subject: [PATCH 010/449] Update README.md
---
README.md | 1 +
1 file changed, 1 insertion(+)
diff --git a/README.md b/README.md
index 945a9aaf4..1916c1aa2 100644
--- a/README.md
+++ b/README.md
@@ -530,6 +530,7 @@ You can deploy JSON Server. For example, [JSONPlaceholder](http://jsonplaceholde
* [Docker JSON Server](https://github.com/clue/docker-json-server)
* [JSON Server GUI](https://github.com/naholyr/json-server-gui)
* [JSON file generator](https://github.com/dfsq/json-server-init)
+* [JSON Server extension](https://github.com/maty21/json-server-extension)
## License
From 46a5259584fde5860f8c58b9c125947617cded76 Mon Sep 17 00:00:00 2001
From: typicode
Date: Tue, 6 Dec 2016 23:43:26 +0100
Subject: [PATCH 011/449] Add stricter tests
---
src/server/router/plural.js | 1 +
test/server/plural.js | 11 ++++++++---
2 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/src/server/router/plural.js b/src/server/router/plural.js
index 0c8b1173b..42a5e03be 100644
--- a/src/server/router/plural.js
+++ b/src/server/router/plural.js
@@ -253,6 +253,7 @@ module.exports = (db, name) => {
function update (req, res, next) {
const id = req.params.id
let chain = db.get(name)
+ console.log(req.body)
chain = req.method === 'PATCH'
? chain.updateById(id, req.body)
diff --git a/test/server/plural.js b/test/server/plural.js
index fa0b751b9..d9993303d 100644
--- a/test/server/plural.js
+++ b/test/server/plural.js
@@ -551,10 +551,11 @@ describe('Server', () => {
})
describe('PUT /:resource/:id', () => {
- it('should respond with json and replace resource', (done) => {
+ it.only('should respond with json and replace resource', (done) => {
var post = {id: 1, booleanValue: true, integerValue: 1}
request(server)
.put('/posts/1')
+ .set('Accept', 'application/json')
// body property omitted to test that the resource is replaced
.send(post)
.expect('Content-Type', /json/)
@@ -562,8 +563,11 @@ describe('Server', () => {
.expect(200)
.end((err, res) => {
if (err) return done(err)
+ // TODO find a "supertest" way to test this
+ // https://github.com/typicode/json-server/issues/396
+ assert.deepStrictEqual(res.body, post)
// assert it was created in database too
- assert.deepEqual(db.posts[0], post)
+ assert.deepStrictEqual(db.posts[0], post)
done()
})
})
@@ -588,8 +592,9 @@ describe('Server', () => {
.expect(200)
.end((err, res) => {
if (err) return done(err)
+ assert.deepStrictEqual(res.body, post)
// assert it was created in database too
- assert.deepEqual(db.posts[0], {id: 1, body: 'bar'})
+ assert.deepStrictEqual(db.posts[0], {id: 1, body: 'bar'})
done()
})
})
From 3df272dbe331a0ea5703b724717b1c9df1955af1 Mon Sep 17 00:00:00 2001
From: typicode
Date: Tue, 6 Dec 2016 23:49:21 +0100
Subject: [PATCH 012/449] Update package.json
---
package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package.json b/package.json
index 8886f1ceb..be3d73a6e 100644
--- a/package.json
+++ b/package.json
@@ -24,7 +24,7 @@
"request": "^2.72.0",
"server-destroy": "^1.0.1",
"shortid": "^2.2.6",
- "underscore-db": "^0.12.1",
+ "underscore-db": "^0.12.2",
"update-notifier": "^1.0.2",
"yargs": "^6.0.0"
},
From d0596e1386214d4211a30700e951e209ead852b2 Mon Sep 17 00:00:00 2001
From: typicode
Date: Tue, 6 Dec 2016 23:51:01 +0100
Subject: [PATCH 013/449] clean
---
src/server/router/plural.js | 1 -
test/server/plural.js | 2 +-
2 files changed, 1 insertion(+), 2 deletions(-)
diff --git a/src/server/router/plural.js b/src/server/router/plural.js
index 42a5e03be..0c8b1173b 100644
--- a/src/server/router/plural.js
+++ b/src/server/router/plural.js
@@ -253,7 +253,6 @@ module.exports = (db, name) => {
function update (req, res, next) {
const id = req.params.id
let chain = db.get(name)
- console.log(req.body)
chain = req.method === 'PATCH'
? chain.updateById(id, req.body)
diff --git a/test/server/plural.js b/test/server/plural.js
index d9993303d..6408796e1 100644
--- a/test/server/plural.js
+++ b/test/server/plural.js
@@ -551,7 +551,7 @@ describe('Server', () => {
})
describe('PUT /:resource/:id', () => {
- it.only('should respond with json and replace resource', (done) => {
+ it('should respond with json and replace resource', (done) => {
var post = {id: 1, booleanValue: true, integerValue: 1}
request(server)
.put('/posts/1')
From 24b2c9f1b6c8db28950a619000d84faf9c4b2df8 Mon Sep 17 00:00:00 2001
From: typicode
Date: Tue, 6 Dec 2016 23:52:26 +0100
Subject: [PATCH 014/449] Update CHANGELOG.md
---
CHANGELOG.md | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8e9fdbb9b..2ac401867 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
# Change Log
+## [0.9.3][unreleased]
+
+* Fix [#396](https://github.com/typicode/json-server/issues/396)
+
## [0.9.2][2016-11-29]
* Fix [#221](https://github.com/typicode/json-server/issues/221) `nohup` support
From d6528665a13b9a8e925337b3ea2c8159e0cbb693 Mon Sep 17 00:00:00 2001
From: typicode
Date: Wed, 7 Dec 2016 09:28:33 +0100
Subject: [PATCH 015/449] Update CHANGELOG.md
---
CHANGELOG.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2ac401867..89ce05272 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,8 +1,8 @@
# Change Log
-## [0.9.3][unreleased]
+## [0.9.3][2016-12-07]
-* Fix [#396](https://github.com/typicode/json-server/issues/396)
+* Fix [#396](https://github.com/typicode/json-server/issues/396) PUT/PATCH saves the updated item with an id that has been converted to string
## [0.9.2][2016-11-29]
From 84493e07db7d5b9a165c3075fdd4a61127fc9f20 Mon Sep 17 00:00:00 2001
From: typicode
Date: Wed, 7 Dec 2016 09:28:52 +0100
Subject: [PATCH 016/449] 0.9.3
---
package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package.json b/package.json
index be3d73a6e..c6216dfc4 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "json-server",
- "version": "0.9.2",
+ "version": "0.9.3",
"description": "Serves JSON files through REST routes.",
"main": "./lib/server/index.js",
"bin": "./bin/index.js",
From b2bcabb71e273a2b655d71a89a9a4b5378b73450 Mon Sep 17 00:00:00 2001
From: typicode
Date: Wed, 7 Dec 2016 09:39:44 +0100
Subject: [PATCH 017/449] Fix PATCH test
---
test/server/plural.js | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/test/server/plural.js b/test/server/plural.js
index 6408796e1..9a0918b04 100644
--- a/test/server/plural.js
+++ b/test/server/plural.js
@@ -584,17 +584,19 @@ describe('Server', () => {
describe('PATCH /:resource/:id', () => {
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)
.end((err, res) => {
if (err) return done(err)
assert.deepStrictEqual(res.body, post)
// assert it was created in database too
- assert.deepStrictEqual(db.posts[0], {id: 1, body: 'bar'})
+ assert.deepStrictEqual(db.posts[0], post)
done()
})
})
From 427e6add1fc0bcc8d9f27dd00ba28e587d9cd89a Mon Sep 17 00:00:00 2001
From: typicode
Date: Wed, 7 Dec 2016 09:41:42 +0100
Subject: [PATCH 018/449] 0.9.4
---
package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package.json b/package.json
index c6216dfc4..8527da58c 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "json-server",
- "version": "0.9.3",
+ "version": "0.9.4",
"description": "Serves JSON files through REST routes.",
"main": "./lib/server/index.js",
"bin": "./bin/index.js",
From 7c402a855665713c131dd14e72dc25eb0a62f417 Mon Sep 17 00:00:00 2001
From: typicode
Date: Wed, 7 Dec 2016 09:43:34 +0100
Subject: [PATCH 019/449] Update package.json
---
package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package.json b/package.json
index 8527da58c..c6216dfc4 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "json-server",
- "version": "0.9.4",
+ "version": "0.9.3",
"description": "Serves JSON files through REST routes.",
"main": "./lib/server/index.js",
"bin": "./bin/index.js",
From 202a4160980361e025e952c4b8f27a1404f24d5a Mon Sep 17 00:00:00 2001
From: Ahmed Ayoub
Date: Thu, 8 Dec 2016 22:53:16 +0100
Subject: [PATCH 020/449] update req.query when rewrite without params (#432)
* update req.query when rewrite without params #431
* add tests for rewrite rules with query and without paramas for PR #432
* [typo] add tests for rewrite rules with query and without paramas for PR #432
---
src/server/rewriter.js | 10 +++++-----
test/server/plural.js | 13 ++++++++++++-
2 files changed, 17 insertions(+), 6 deletions(-)
diff --git a/src/server/rewriter.js b/src/server/rewriter.js
index 718220f37..a48e91069 100644
--- a/src/server/rewriter.js
+++ b/src/server/rewriter.js
@@ -1,7 +1,9 @@
const express = require('express')
const url = require('url')
const _ = require('lodash')
-
+function updateQueryString(target,sourceUrl) {
+ return !!~sourceUrl.indexOf('?') ? _.assign(target, url.parse(sourceUrl, true).query) : {};
+}
module.exports = (routes) => {
const router = express.Router()
@@ -14,16 +16,14 @@ module.exports = (routes) => {
target = target.replace(':' + param, req.params[param])
}
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()
})
} else {
router.all(route + '*', (req, res, next) => {
// Rewrite url by replacing prefix
req.url = req.url.replace(route, routes[route])
+ req.query = updateQueryString(req.query,req.url)
next()
})
}
diff --git a/test/server/plural.js b/test/server/plural.js
index 9a0918b04..25103b67d 100644
--- a/test/server/plural.js
+++ b/test/server/plural.js
@@ -78,7 +78,9 @@ describe('Server', () => {
server.use(jsonServer.rewriter({
'/api/': '/',
'/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'
+
}))
server.use(router)
})
@@ -686,6 +688,15 @@ describe('Server', () => {
.end(done)
})
+ 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) {
request(server)
.get('/comments/special/1-quux')
From 760c37d288dfbe3ba4a18b24036de1eb31324292 Mon Sep 17 00:00:00 2001
From: typicode
Date: Thu, 8 Dec 2016 22:55:35 +0100
Subject: [PATCH 021/449] Update CHANGELOG.md
---
CHANGELOG.md | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 89ce05272..a87051abc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
# Change Log
+## [0.9.4][2016-12-08]
+
+* Improve rewriter [#431](https://github.com/typicode/json-server/issues/431)
+
## [0.9.3][2016-12-07]
* Fix [#396](https://github.com/typicode/json-server/issues/396) PUT/PATCH saves the updated item with an id that has been converted to string
From 0bac0352587ff58ac494b6390840f4309a75de82 Mon Sep 17 00:00:00 2001
From: typicode
Date: Thu, 8 Dec 2016 23:29:43 +0100
Subject: [PATCH 022/449] Update
---
package.json | 1 +
src/cli/run.js | 22 ++++++++++++++--------
src/server/rewriter.js | 8 ++++----
3 files changed, 19 insertions(+), 12 deletions(-)
diff --git a/package.json b/package.json
index c6216dfc4..d574c5179 100644
--- a/package.json
+++ b/package.json
@@ -15,6 +15,7 @@
"cors": "^2.3.0",
"errorhandler": "^1.2.0",
"express": "^4.9.5",
+ "json-parse-helpfulerror": "^1.0.3",
"lodash": "^4.11.2",
"lowdb": "^0.14.0",
"method-override": "^2.1.2",
diff --git a/src/cli/run.js b/src/cli/run.js
index fc78cfd22..a67d7303b 100644
--- a/src/cli/run.js
+++ b/src/cli/run.js
@@ -1,5 +1,6 @@
const fs = require('fs')
const path = require('path')
+const jph = require('json-parse-helpfulerror')
const _ = require('lodash')
const chalk = require('chalk')
const enableDestroy = require('server-destroy')
@@ -189,25 +190,30 @@ module.exports = function (argv) {
// Watch .js or .json file
// Since lowdb uses atomic writing, directory is watched instead of file
const watchedDir = path.dirname(source)
+ let readError = false
fs.watch(watchedDir, (event, file) => {
-<<<<<<< HEAD
// https://github.com/typicode/json-server/issues/420
// file can be null
if (file) {
const watchedFile = path.resolve(watchedDir, file)
if (watchedFile === path.resolve(source)) {
if (is.JSON(watchedFile)) {
- var obj
+ let obj
try {
- obj = JSON.parse(fs.readFileSync(watchedFile))
+ obj = jph.parse(fs.readFileSync(watchedFile))
+ if (readError) {
+ console.log(chalk.green(` Read error has been fixed :)`))
+ readError = false
+ }
} catch (e) {
- console.log('Error reading JSON file');
- console.dir(e);
- return;
+ readError = true
+ console.log(chalk.red(` Error reading ${watchedFile}`))
+ console.error(e.message)
+ return
}
-
+
// Compare .json file content with in memory database
- var isDatabaseDifferent = !_.isEqual(obj, app.db.getState())
+ const isDatabaseDifferent = !_.isEqual(obj, app.db.getState())
if (isDatabaseDifferent) {
console.log(chalk.gray(` ${source} has changed, reloading...`))
server && server.destroy()
diff --git a/src/server/rewriter.js b/src/server/rewriter.js
index a48e91069..e94ceb90e 100644
--- a/src/server/rewriter.js
+++ b/src/server/rewriter.js
@@ -1,8 +1,8 @@
const express = require('express')
const url = require('url')
const _ = require('lodash')
-function updateQueryString(target,sourceUrl) {
- return !!~sourceUrl.indexOf('?') ? _.assign(target, url.parse(sourceUrl, true).query) : {};
+function updateQueryString (target, sourceUrl) {
+ return ~sourceUrl.indexOf('?') ? _.assign(target, url.parse(sourceUrl, true).query) : {}
}
module.exports = (routes) => {
const router = express.Router()
@@ -16,14 +16,14 @@ module.exports = (routes) => {
target = target.replace(':' + param, req.params[param])
}
req.url = target
- req.query = updateQueryString(req.query,req.url)
+ req.query = updateQueryString(req.query, req.url)
next()
})
} else {
router.all(route + '*', (req, res, next) => {
// Rewrite url by replacing prefix
req.url = req.url.replace(route, routes[route])
- req.query = updateQueryString(req.query,req.url)
+ req.query = updateQueryString(req.query, req.url)
next()
})
}
From 2960b273376772b2479f488cf6915cad155e975a Mon Sep 17 00:00:00 2001
From: typicode
Date: Thu, 8 Dec 2016 23:30:13 +0100
Subject: [PATCH 023/449] Update
---
.gitignore | 1 +
1 file changed, 1 insertion(+)
diff --git a/.gitignore b/.gitignore
index 0c475f8ea..306cc31e5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,4 @@ tmp
lib
.DS_Store
.idea
+db.json
\ No newline at end of file
From 47e76434103b87cf3550650b01bcca25fd11dbe5 Mon Sep 17 00:00:00 2001
From: typicode
Date: Thu, 8 Dec 2016 23:41:52 +0100
Subject: [PATCH 024/449] Update CHANGELOG.md
---
CHANGELOG.md | 1 +
1 file changed, 1 insertion(+)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a87051abc..0c99e38ca 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@
## [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]
From 442746efcc5c7fb9cb6453f649e33c7a256260ff Mon Sep 17 00:00:00 2001
From: typicode
Date: Thu, 8 Dec 2016 23:47:41 +0100
Subject: [PATCH 025/449] 0.9.4
---
package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package.json b/package.json
index d574c5179..fa5c23b5f 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "json-server",
- "version": "0.9.3",
+ "version": "0.9.4",
"description": "Serves JSON files through REST routes.",
"main": "./lib/server/index.js",
"bin": "./bin/index.js",
From bb64e719b71a048d1f8a03e9724a167cb8d6abb5 Mon Sep 17 00:00:00 2001
From: typicode
Date: Sun, 25 Dec 2016 07:44:36 +0100
Subject: [PATCH 026/449] Update rewriter
---
src/server/rewriter.js | 4 ++++
test/server/plural.js | 21 ++++++++++++++-------
2 files changed, 18 insertions(+), 7 deletions(-)
diff --git a/src/server/rewriter.js b/src/server/rewriter.js
index e94ceb90e..d7f3c7471 100644
--- a/src/server/rewriter.js
+++ b/src/server/rewriter.js
@@ -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) => {
diff --git a/test/server/plural.js b/test/server/plural.js
index 25103b67d..8a70ec8a4 100644
--- a/test/server/plural.js
+++ b/test/server/plural.js
@@ -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) => {
From ad9cbdd1ae4c68c1909c4d938285d1d3fc9d5d0d Mon Sep 17 00:00:00 2001
From: typicode
Date: Sun, 25 Dec 2016 18:49:09 +0100
Subject: [PATCH 027/449] Add failing test
---
test/server/plural.js | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/test/server/plural.js b/test/server/plural.js
index 8a70ec8a4..a144ca617 100644
--- a/test/server/plural.js
+++ b/test/server/plural.js
@@ -11,7 +11,8 @@ describe('Server', () => {
'/api/': '/',
'/blog/posts/:id/show': '/posts/:id',
'/comments/special/:userId-:body': '/comments/?userId=:userId&body=:body',
- '/firstpostwithcomments': '/posts/1?_embed=comments'
+ '/firstpostwithcomments': '/posts/1?_embed=comments',
+ '/articles?_id=:id': '/posts/:id'
}
beforeEach(() => {
@@ -704,6 +705,14 @@ describe('Server', () => {
.end(done)
})
+ // TODO
+ // it('should rewrite query params', (done) => {
+ // request(server)
+ // .get('/articles?_id=1')
+ // .expect(db.posts[0])
+ // .end(done)
+ // })
+
it('should expose routes', (done) => {
request(server)
.get('/__rules')
From 69321be00fe85d026a88e7f30d464bd51afcee2f Mon Sep 17 00:00:00 2001
From: typicode
Date: Sun, 25 Dec 2016 19:12:38 +0100
Subject: [PATCH 028/449] Display custom routes
---
package.json | 2 +-
src/server/public/index.html | 29 ++++++++++++++++++++++-------
2 files changed, 23 insertions(+), 8 deletions(-)
diff --git a/package.json b/package.json
index fa5c23b5f..6ef35be00 100644
--- a/package.json
+++ b/package.json
@@ -49,7 +49,7 @@
"test": "npm run test:cli && npm run test:server && standard --fix",
"test:cli": "npm run build && cross-env NODE_ENV=test mocha test/cli/*.js",
"test:server": "cross-env NODE_ENV=test mocha test/server/*.js",
- "start": "node bin",
+ "start": "babel-node src/cli/bin",
"prepush": "npm t",
"build": "babel src -d lib --copy-files",
"toc": "markdown-toc -i README.md",
diff --git a/src/server/public/index.html b/src/server/public/index.html
index bdac2a7ad..83a644b90 100644
--- a/src/server/public/index.html
+++ b/src/server/public/index.html
@@ -27,7 +27,9 @@ Routes
Here are the resources that JSON Server has loaded:
-
+
+
+
You can view database current state at any time:
@@ -36,10 +38,10 @@ Routes
db
-
+
You can use any HTTP verbs (GET, POST, PUT, PATCH and DELETE) and access your resources from anywhere
- using CORS and JSONP.
+ using CORS or JSONP.
Documentation
@@ -61,17 +63,30 @@ Issues
-
-
+
+