Skip to content

Commit f1621c3

Browse files
committedNov 15, 2020
lint
1 parent 8928b6a commit f1621c3

31 files changed

+481
-557
lines changed
 

‎.eslintrc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
singleQuote: true,
99
semi: false,
1010
},
11-
]
11+
],
1212
},
13-
env: { jest: true }
13+
env: { jest: true },
1414
}

‎__fixtures__/middlewares/en.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function(req, res, next) {
1+
module.exports = function (req, res, next) {
22
res.header('X-Hello', 'World')
33
next()
44
}

‎__fixtures__/middlewares/jp.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function(req, res, next) {
1+
module.exports = function (req, res, next) {
22
res.header('X-Konnichiwa', 'Sekai')
33
next()
44
}

‎__fixtures__/middlewares/postbody.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function(req, res, next) {
2-
res.header('name', req.body.name)
3-
next()
4-
}
1+
module.exports = function (req, res, next) {
2+
res.header('name', req.body.name)
3+
next()
4+
}

‎__fixtures__/seed.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
// Need some fake data for the gzip test to work
2-
module.exports = function() {
2+
module.exports = function () {
33
return {
44
posts: [
55
{
66
id: 1,
77
content:
8-
"Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!"
8+
"Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!",
99
},
1010
{
1111
id: 2,
1212
content:
13-
"Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!"
13+
"Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!",
1414
},
1515
{
1616
id: 3,
1717
content:
18-
"Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!"
18+
"Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!",
1919
},
2020
{
2121
id: 4,
2222
content:
23-
"Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!"
23+
"Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!",
2424
},
2525
{
2626
id: 5,
2727
content:
28-
"Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!"
28+
"Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!",
2929
},
3030
{
3131
id: 6,
3232
content:
33-
"Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!"
34-
}
35-
]
33+
"Oh! The garbage chute was a really wonderful idea. What an incredible smell you've discovered! Let's get out of here! Get away from there... No! wait! Will you forget it? I already tried it. It's magnetically sealed! Put that thing away! You're going to get us all killed. Absolutely, Your Worship. Look, I had everything under control until you led us down here. You know, it's not going to take them long to figure out what happened to us. It could be worst... It's worst. There's something alive in here! That's your imagination. Something just moves past my leg! Look! Did you see that? What? Help!",
34+
},
35+
],
3636
}
3737
}

‎__tests__/cli/index.js

+308-311
Large diffs are not rendered by default.

‎__tests__/server/mixins.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ describe('mixins', () => {
1818
{ id: 1, postId: 1 },
1919
// Comments below references a post that doesn't exist
2020
{ id: 2, postId: 2 },
21-
{ id: 3, postId: 2 }
21+
{ id: 3, postId: 2 },
2222
],
23-
photos: [{ id: '1' }, { id: '2' }]
23+
photos: [{ id: '1' }, { id: '2' }],
2424
}
2525
})
2626

2727
describe('getRemovable', () => {
2828
test('should return removable documents', () => {
2929
const expected = [
3030
{ name: 'comments', id: 2 },
31-
{ name: 'comments', id: 3 }
31+
{ name: 'comments', id: 3 },
3232
]
3333

3434
assert.deepStrictEqual(
@@ -40,7 +40,7 @@ describe('mixins', () => {
4040
test('should support custom foreignKeySuffix', () => {
4141
const expected = [
4242
{ name: 'comments', id: 2 },
43-
{ name: 'comments', id: 3 }
43+
{ name: 'comments', id: 3 },
4444
]
4545

4646
assert.deepStrictEqual(

‎__tests__/server/plural-fake.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ describe('Fake server', () => {
1212

1313
db.posts = [
1414
{ id: 1, body: 'foo' },
15-
{ id: 2, body: 'bar' }
15+
{ id: 2, body: 'bar' },
1616
]
1717

1818
db.comments = [
1919
{ id: 1, body: 'foo', published: true, postId: 1, userId: 1 },
2020
{ id: 2, body: 'bar', published: false, postId: 1, userId: 2 },
2121
{ id: 3, body: 'baz', published: false, postId: 2, userId: 1 },
2222
{ id: 4, body: 'qux', published: true, postId: 2, userId: 2 },
23-
{ id: 5, body: 'quux', published: false, postId: 2, userId: 1 }
23+
{ id: 5, body: 'quux', published: false, postId: 2, userId: 1 },
2424
]
2525

2626
server = jsonServer.create()
@@ -87,10 +87,7 @@ describe('Fake server', () => {
8787

8888
describe('DELETE /:resource/:id', () => {
8989
test('should not destroy resource', async () => {
90-
await request(server)
91-
.del('/posts/1')
92-
.expect({})
93-
.expect(200)
90+
await request(server).del('/posts/1').expect({}).expect(200)
9491
assert.strictEqual(db.posts.length, 2)
9592
})
9693
})

‎__tests__/server/plural-with-custom-foreign-key.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ describe('Server with custom foreign key', () => {
1313

1414
db.posts = [
1515
{ id: 1, body: 'foo' },
16-
{ id: 2, body: 'bar' }
16+
{ id: 2, body: 'bar' },
1717
]
1818

1919
db.comments = [
2020
{ id: 1, post_id: 1 },
2121
{ id: 2, post_id: 1 },
22-
{ id: 3, post_id: 2 }
22+
{ id: 3, post_id: 2 },
2323
]
2424

2525
server = jsonServer.create()
@@ -109,10 +109,7 @@ describe('Server with custom foreign key', () => {
109109

110110
describe('DELETE /:resource/:id', () => {
111111
test('should respond with empty data, destroy resource and dependent resources', async () => {
112-
await request(server)
113-
.del('/posts/1')
114-
.expect({})
115-
.expect(200)
112+
await request(server).del('/posts/1').expect({}).expect(200)
116113
assert.strictEqual(db.posts.length, 1)
117114
assert.strictEqual(db.comments.length, 1)
118115
})

‎__tests__/server/plural.js

+30-47
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,34 @@ describe('Server', () => {
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(() => {
1919
db = {}
2020

2121
db.posts = [
2222
{ id: 1, body: 'foo' },
23-
{ id: 2, body: 'bar' }
23+
{ id: 2, body: 'bar' },
2424
]
2525

2626
db.tags = [
2727
{ id: 1, body: 'Technology' },
2828
{ id: 2, body: 'Photography' },
29-
{ id: 3, body: 'photo' }
29+
{ id: 3, body: 'photo' },
3030
]
3131

3232
db.users = [
3333
{ id: 1, username: 'Jim', tel: '0123' },
34-
{ id: 2, username: 'George', tel: '123' }
34+
{ id: 2, username: 'George', tel: '123' },
3535
]
3636

3737
db.comments = [
3838
{ id: 1, body: 'foo', published: true, postId: 1, userId: 1 },
3939
{ id: 2, body: 'bar', published: false, postId: 1, userId: 2 },
4040
{ id: 3, body: 'baz', published: false, postId: 2, userId: 1 },
4141
{ id: 4, body: 'qux', published: true, postId: 2, userId: 2 },
42-
{ id: 5, body: 'quux', published: false, postId: 2, userId: 1 }
42+
{ id: 5, body: 'quux', published: false, postId: 2, userId: 1 },
4343
]
4444

4545
db.buyers = [
@@ -51,11 +51,11 @@ describe('Server', () => {
5151
{ id: 6, name: 'Frank', country: 'Belize', total: 10 },
5252
{ id: 7, name: 'Grace', country: 'Argentina', total: 1 },
5353
{ id: 8, name: 'Henry', country: 'Argentina', total: 2 },
54-
{ id: 9, name: 'Isabelle', country: 'Argentina', total: 3 }
54+
{ id: 9, name: 'Isabelle', country: 'Argentina', total: 3 },
5555
]
5656

5757
db.refs = [
58-
{ id: 'abcd-1234', url: 'http://example.com', postId: 1, userId: 1 }
58+
{ id: 'abcd-1234', url: 'http://example.com', postId: 1, userId: 1 },
5959
]
6060

6161
db.stringIds = [{ id: '1234' }]
@@ -65,7 +65,7 @@ describe('Server', () => {
6565
db.nested = [
6666
{ resource: { name: 'dewey' } },
6767
{ resource: { name: 'cheatem' } },
68-
{ resource: { name: 'howe' } }
68+
{ resource: { name: 'howe' } },
6969
]
7070

7171
db.list = [
@@ -83,7 +83,7 @@ describe('Server', () => {
8383
{ id: 12 },
8484
{ id: 13 },
8585
{ id: 14 },
86-
{ id: 15 }
86+
{ id: 15 },
8787
]
8888

8989
server = jsonServer.create()
@@ -114,9 +114,7 @@ describe('Server', () => {
114114
.expect(200))
115115

116116
test('should respond with 404 if resource is not found', () =>
117-
request(server)
118-
.get('/undefined')
119-
.expect(404))
117+
request(server).get('/undefined').expect(404))
120118
})
121119

122120
describe('GET /:resource?attr=&attr=', () => {
@@ -277,7 +275,7 @@ describe('Server', () => {
277275
db.buyers[5],
278276
db.buyers[2],
279277
db.buyers[1],
280-
db.buyers[0]
278+
db.buyers[0],
281279
])
282280
.expect(200))
283281
})
@@ -321,7 +319,7 @@ describe('Server', () => {
321319
'<http://localhost/list?_page=1&_limit=1>; rel="first"',
322320
'<http://localhost/list?_page=1&_limit=1>; rel="prev"',
323321
'<http://localhost/list?_page=3&_limit=1>; rel="next"',
324-
'<http://localhost/list?_page=15&_limit=1>; rel="last"'
322+
'<http://localhost/list?_page=15&_limit=1>; rel="last"',
325323
].join(', ')
326324
return request(server)
327325
.get('/list?_page=2&_limit=1')
@@ -518,11 +516,11 @@ describe('Server', () => {
518516
})
519517

520518
describe('GET /:resource>_delay=', () => {
521-
test('should delay response', done => {
519+
test('should delay response', (done) => {
522520
const start = new Date()
523521
request(server)
524522
.get('/posts?_delay=1100')
525-
.expect(200, function(err) {
523+
.expect(200, function (err) {
526524
const end = new Date()
527525
done(end - start > 1000 ? err : new Error("Request wasn't delayed"))
528526
})
@@ -575,12 +573,12 @@ describe('Server', () => {
575573
})
576574

577575
describe('POST /:resource?_delay=', () => {
578-
test('should delay response', done => {
576+
test('should delay response', (done) => {
579577
const start = new Date()
580578
request(server)
581579
.post('/posts?_delay=1100')
582580
.send({ body: 'foo', booleanValue: true, integerValue: 1 })
583-
.expect(201, function(err) {
581+
.expect(201, function (err) {
584582
const end = new Date()
585583
done(end - start > 1000 ? err : new Error("Request wasn't delayed"))
586584
})
@@ -615,13 +613,13 @@ describe('Server', () => {
615613
})
616614

617615
describe('PUT /:resource:id?_delay=', () => {
618-
test('should delay response', done => {
616+
test('should delay response', (done) => {
619617
const start = new Date()
620618
request(server)
621619
.put('/posts/1?_delay=1100')
622620
.set('Accept', 'application/json')
623621
.send({ id: 1, booleanValue: true, integerValue: 1 })
624-
.expect(200, function(err) {
622+
.expect(200, function (err) {
625623
const end = new Date()
626624
done(end - start > 1000 ? err : new Error("Request wasn't delayed"))
627625
})
@@ -653,13 +651,13 @@ describe('Server', () => {
653651
})
654652

655653
describe('PATCH /:resource:id?_delay=', () => {
656-
test('should delay response', done => {
654+
test('should delay response', (done) => {
657655
const start = new Date()
658656
request(server)
659657
.patch('/posts/1?_delay=1100')
660658
.send({ body: 'bar' })
661659
.send({ id: 1, booleanValue: true, integerValue: 1 })
662-
.expect(200, function(err) {
660+
.expect(200, function (err) {
663661
const end = new Date()
664662
done(end - start > 1000 ? err : new Error("Request wasn't delayed"))
665663
})
@@ -668,10 +666,7 @@ describe('Server', () => {
668666

669667
describe('DELETE /:resource/:id', () => {
670668
test('should respond with empty data, destroy resource and dependent resources', async () => {
671-
await request(server)
672-
.del('/posts/1')
673-
.expect({})
674-
.expect(200)
669+
await request(server).del('/posts/1').expect({}).expect(200)
675670
assert.strictEqual(db.posts.length, 1)
676671
assert.strictEqual(db.comments.length, 3)
677672
})
@@ -685,12 +680,12 @@ describe('Server', () => {
685680
})
686681

687682
describe('DELETE /:resource:id?_delay=', () => {
688-
test('should delay response', done => {
683+
test('should delay response', (done) => {
689684
const start = new Date()
690685
request(server)
691686
.del('/posts/1?_delay=1100')
692687
.send({ id: 1, booleanValue: true, integerValue: 1 })
693-
.expect(200, function(err) {
688+
.expect(200, function (err) {
694689
const end = new Date()
695690
done(end - start > 1000 ? err : new Error("Request wasn't delayed"))
696691
})
@@ -740,37 +735,25 @@ describe('Server', () => {
740735

741736
describe('Rewriter', () => {
742737
test('should rewrite using prefix', () =>
743-
request(server)
744-
.get('/api/posts/1')
745-
.expect(db.posts[0]))
738+
request(server).get('/api/posts/1').expect(db.posts[0]))
746739

747740
test('should rewrite using params', () =>
748-
request(server)
749-
.get('/blog/posts/1/show')
750-
.expect(db.posts[0]))
741+
request(server).get('/blog/posts/1/show').expect(db.posts[0]))
751742

752743
test('should rewrite using query without params', () => {
753744
const expectedPost = _.cloneDeep(db.posts[0])
754745
expectedPost.comments = [db.comments[0], db.comments[1]]
755-
return request(server)
756-
.get('/firstpostwithcomments')
757-
.expect(expectedPost)
746+
return request(server).get('/firstpostwithcomments').expect(expectedPost)
758747
})
759748

760749
test('should rewrite using params and query', () =>
761-
request(server)
762-
.get('/comments/special/1-quux')
763-
.expect([db.comments[4]]))
750+
request(server).get('/comments/special/1-quux').expect([db.comments[4]]))
764751

765752
test('should rewrite query params', () =>
766-
request(server)
767-
.get('/articles?_id=1')
768-
.expect(db.posts[0]))
753+
request(server).get('/articles?_id=1').expect(db.posts[0]))
769754

770755
test('should expose routes', () =>
771-
request(server)
772-
.get('/__rules')
773-
.expect(rewriterRules))
756+
request(server).get('/__rules').expect(rewriterRules))
774757
})
775758

776759
describe('router.render', () => {
@@ -791,7 +774,7 @@ describe('Server', () => {
791774
describe('router.db._.id', () => {
792775
beforeEach(() => {
793776
router.db.setState({
794-
posts: [{ _id: 1 }]
777+
posts: [{ _id: 1 }],
795778
})
796779

797780
router.db._.id = '_id'

‎__tests__/server/singular-fake.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Fake server', () => {
1212

1313
db.user = {
1414
name: 'foo',
15-
email: 'foo@example.com'
15+
email: 'foo@example.com',
1616
}
1717

1818
server = jsonServer.create()
@@ -24,23 +24,15 @@ describe('Fake server', () => {
2424
describe('POST /:resource', () => {
2525
test('should not create resource', async () => {
2626
const user = { name: 'bar' }
27-
await request(server)
28-
.post('/user')
29-
.send(user)
30-
.expect(user)
31-
.expect(201)
27+
await request(server).post('/user').send(user).expect(user).expect(201)
3228
assert.notDeepStrictEqual(db.user, user)
3329
})
3430
})
3531

3632
describe('PUT /:resource', () => {
3733
test('should not update resource', async () => {
3834
const user = { name: 'bar' }
39-
await request(server)
40-
.put('/user')
41-
.send(user)
42-
.expect(user)
43-
.expect(200)
35+
await request(server).put('/user').send(user).expect(user).expect(200)
4436
assert.notDeepStrictEqual(db.user, user)
4537
})
4638
})

‎__tests__/server/singular.js

+4-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('Server', () => {
1111

1212
db.user = {
1313
name: 'foo',
14-
email: 'foo@example.com'
14+
email: 'foo@example.com',
1515
}
1616

1717
server = jsonServer.create()
@@ -22,31 +22,20 @@ describe('Server', () => {
2222

2323
describe('GET /:resource', () => {
2424
test('should respond with corresponding resource', () =>
25-
request(server)
26-
.get('/user')
27-
.expect(db.user)
28-
.expect(200))
25+
request(server).get('/user').expect(db.user).expect(200))
2926
})
3027

3128
describe('POST /:resource', () => {
3229
test('should create resource', () => {
3330
const user = { name: 'bar' }
34-
return request(server)
35-
.post('/user')
36-
.send(user)
37-
.expect(user)
38-
.expect(201)
31+
return request(server).post('/user').send(user).expect(user).expect(201)
3932
})
4033
})
4134

4235
describe('PUT /:resource', () => {
4336
test('should update resource', () => {
4437
const user = { name: 'bar' }
45-
return request(server)
46-
.put('/user')
47-
.send(user)
48-
.expect(user)
49-
.expect(200)
38+
return request(server).put('/user').send(user).expect(user).expect(200)
5039
})
5140
})
5241

‎__tests__/server/utils.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('utils', () => {
1212
current: 1,
1313
first: 1,
1414
next: 2,
15-
last: 3
15+
last: 3,
1616
})
1717
})
1818

@@ -23,7 +23,7 @@ describe('utils', () => {
2323
first: 1,
2424
prev: 1,
2525
next: 3,
26-
last: 3
26+
last: 3,
2727
})
2828
})
2929

@@ -33,25 +33,25 @@ describe('utils', () => {
3333
current: 3,
3434
first: 1,
3535
prev: 2,
36-
last: 3
36+
last: 3,
3737
})
3838
})
3939

4040
test('should return an empty array if page is greater than the last page', () => {
4141
assert.deepStrictEqual(utils.getPage(array, 99, perPage), {
42-
items: []
42+
items: [],
4343
})
4444
})
4545

4646
test('should return the array if perPage is greater than the array size', () => {
4747
assert.deepStrictEqual(utils.getPage(array, 1, 99), {
48-
items: array
48+
items: array,
4949
})
5050
})
5151

5252
test('should return an empty array if the array is empty', () => {
5353
assert.deepStrictEqual(utils.getPage([], 1, 1), {
54-
items: []
54+
items: [],
5555
})
5656
})
5757
})

‎package.json

+23-23
Original file line numberDiff line numberDiff line change
@@ -22,52 +22,52 @@
2222
},
2323
"dependencies": {
2424
"body-parser": "^1.19.0",
25-
"chalk": "^3.0.0",
25+
"chalk": "^4.1.0",
2626
"compression": "^1.7.4",
2727
"connect-pause": "^0.1.1",
2828
"cors": "^2.8.5",
2929
"errorhandler": "^1.5.1",
3030
"express": "^4.17.1",
31-
"express-urlrewrite": "^1.2.0",
31+
"express-urlrewrite": "^1.3.0",
3232
"json-parse-helpfulerror": "^1.0.3",
33-
"lodash": "^4.17.15",
33+
"lodash": "^4.17.20",
3434
"lodash-id": "^0.14.0",
3535
"lowdb": "^1.0.0",
3636
"method-override": "^3.0.0",
37-
"morgan": "^1.9.1",
38-
"nanoid": "^2.1.11",
37+
"morgan": "^1.10.0",
38+
"nanoid": "^3.1.16",
3939
"please-upgrade-node": "^3.2.0",
4040
"pluralize": "^8.0.0",
4141
"server-destroy": "^1.0.1",
42-
"update-notifier": "^4.0.0",
43-
"yargs": "^15.1.0"
42+
"update-notifier": "^5.0.1",
43+
"yargs": "^16.1.1"
4444
},
4545
"devDependencies": {
46-
"@babel/cli": "^7.8.4",
47-
"@babel/core": "^7.8.4",
48-
"@babel/node": "^7.8.4",
49-
"@babel/preset-env": "^7.8.4",
50-
"cross-env": "^7.0.0",
51-
"eslint": "^6.8.0",
52-
"eslint-config-prettier": "^6.10.0",
53-
"eslint-config-standard": "^14.1.0",
54-
"eslint-plugin-import": "^2.20.1",
55-
"eslint-plugin-node": "^11.0.0",
56-
"eslint-plugin-prettier": "^3.1.2",
46+
"@babel/cli": "^7.12.1",
47+
"@babel/core": "^7.12.3",
48+
"@babel/node": "^7.12.6",
49+
"@babel/preset-env": "^7.12.1",
50+
"cross-env": "^7.0.2",
51+
"eslint": "^7.13.0",
52+
"eslint-config-prettier": "^6.15.0",
53+
"eslint-config-standard": "^16.0.1",
54+
"eslint-plugin-import": "^2.22.1",
55+
"eslint-plugin-node": "^11.1.0",
56+
"eslint-plugin-prettier": "^3.1.4",
5757
"eslint-plugin-promise": "^4.2.1",
58-
"eslint-plugin-standard": "^4.0.1",
58+
"eslint-plugin-standard": "^4.1.0",
5959
"husky": "^5.0.0-beta.0",
60-
"jest": "^25.1.0",
60+
"jest": "^26.6.3",
6161
"markdown-toc": "^1.2.0",
62-
"mkdirp": "^1.0.3",
62+
"mkdirp": "^1.0.4",
6363
"npm-run-all": "^4.1.5",
6464
"os-tmpdir": "^2.0.0",
6565
"pinst": "^2.1.1",
6666
"pkg-ok": "^2.3.1",
67-
"prettier": "^1.19.1",
67+
"prettier": "^2.1.2",
6868
"rimraf": "^3.0.2",
6969
"server-ready": "^0.3.1",
70-
"supertest": "^4.0.2",
70+
"supertest": "^6.0.1",
7171
"temp-write": "^4.0.0"
7272
},
7373
"repository": {

‎public/script.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ function ResourceList({ db }) {
1111
return `
1212
<ul>
1313
${Object.keys(db)
14-
.map(name =>
14+
.map((name) =>
1515
ResourceItem({
1616
name,
17-
length: Array.isArray(db[name]) && db[name].length
17+
length: Array.isArray(db[name]) && db[name].length,
1818
})
1919
)
2020
.join('')}
@@ -37,9 +37,9 @@ function ResourcesBlock({ db }) {
3737

3838
window
3939
.fetch('db')
40-
.then(response => response.json())
40+
.then((response) => response.json())
4141
.then(
42-
db =>
42+
(db) =>
4343
(document.getElementById('resources').innerHTML = ResourcesBlock({ db }))
4444
)
4545

@@ -52,7 +52,7 @@ function CustomRoutesBlock({ customRoutes }) {
5252
<table>
5353
${rules
5454
.map(
55-
rule =>
55+
(rule) =>
5656
`<tr>
5757
<td>${rule}</td>
5858
<td><code>⇢</code> ${customRoutes[rule]}</td>
@@ -67,10 +67,10 @@ function CustomRoutesBlock({ customRoutes }) {
6767

6868
window
6969
.fetch('__rules')
70-
.then(response => response.json())
70+
.then((response) => response.json())
7171
.then(
72-
customRoutes =>
72+
(customRoutes) =>
7373
(document.getElementById('custom-routes').innerHTML = CustomRoutesBlock({
74-
customRoutes
74+
customRoutes,
7575
}))
7676
)

‎src/cli/index.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const yargs = require('yargs')
33
const run = require('./run')
44
const pkg = require('../../package.json')
55

6-
module.exports = function() {
6+
module.exports = function () {
77
updateNotifier({ pkg }).notify()
88

99
const argv = yargs
@@ -13,70 +13,70 @@ module.exports = function() {
1313
port: {
1414
alias: 'p',
1515
description: 'Set port',
16-
default: 3000
16+
default: 3000,
1717
},
1818
host: {
1919
alias: 'H',
2020
description: 'Set host',
21-
default: 'localhost'
21+
default: 'localhost',
2222
},
2323
watch: {
2424
alias: 'w',
25-
description: 'Watch file(s)'
25+
description: 'Watch file(s)',
2626
},
2727
routes: {
2828
alias: 'r',
29-
description: 'Path to routes file'
29+
description: 'Path to routes file',
3030
},
3131
middlewares: {
3232
alias: 'm',
3333
array: true,
34-
description: 'Paths to middleware files'
34+
description: 'Paths to middleware files',
3535
},
3636
static: {
3737
alias: 's',
38-
description: 'Set static files directory'
38+
description: 'Set static files directory',
3939
},
4040
'read-only': {
4141
alias: 'ro',
42-
description: 'Allow only GET requests'
42+
description: 'Allow only GET requests',
4343
},
4444
'no-cors': {
4545
alias: 'nc',
46-
description: 'Disable Cross-Origin Resource Sharing'
46+
description: 'Disable Cross-Origin Resource Sharing',
4747
},
4848
'no-gzip': {
4949
alias: 'ng',
50-
description: 'Disable GZIP Content-Encoding'
50+
description: 'Disable GZIP Content-Encoding',
5151
},
5252
snapshots: {
5353
alias: 'S',
5454
description: 'Set snapshots directory',
55-
default: '.'
55+
default: '.',
5656
},
5757
delay: {
5858
alias: 'd',
59-
description: 'Add delay to responses (ms)'
59+
description: 'Add delay to responses (ms)',
6060
},
6161
id: {
6262
alias: 'i',
6363
description: 'Set database id property (e.g. _id)',
64-
default: 'id'
64+
default: 'id',
6565
},
6666
foreignKeySuffix: {
6767
alias: 'fks',
6868
description: 'Set foreign key suffix (e.g. _id as in post_id)',
69-
default: 'Id'
69+
default: 'Id',
7070
},
7171
quiet: {
7272
alias: 'q',
73-
description: 'Suppress log messages from output'
73+
description: 'Suppress log messages from output',
7474
},
7575
config: {
7676
alias: 'c',
7777
description: 'Path to config file',
78-
default: 'json-server.json'
79-
}
78+
default: 'json-server.json',
79+
},
8080
})
8181
.boolean('watch')
8282
.boolean('read-only')

‎src/cli/run.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function prettyPrint(argv, object, rules) {
2121
if (rules) {
2222
console.log()
2323
console.log(chalk.bold(' Other routes'))
24-
for (var rule in rules) {
24+
for (const rule in rules) {
2525
console.log(` ${rule} -> ${rules[rule]}`)
2626
}
2727
}
@@ -47,7 +47,7 @@ function createApp(db, routes, middlewares, argv) {
4747
readOnly: argv.readOnly,
4848
noCors: argv.noCors,
4949
noGzip: argv.noGzip,
50-
bodyParser: true
50+
bodyParser: true,
5151
}
5252

5353
if (argv.static) {
@@ -77,7 +77,7 @@ function createApp(db, routes, middlewares, argv) {
7777
return app
7878
}
7979

80-
module.exports = function(argv) {
80+
module.exports = function (argv) {
8181
const source = argv._[0]
8282
let app
8383
let server
@@ -103,7 +103,7 @@ module.exports = function(argv) {
103103
server = undefined
104104

105105
// create db and load object, JSON file, JS or HTTP database
106-
return load(source).then(db => {
106+
return load(source).then((db) => {
107107
// Load additional routes
108108
let routes
109109
if (argv.routes) {
@@ -114,7 +114,7 @@ module.exports = function(argv) {
114114
// Load middlewares
115115
let middlewares
116116
if (argv.middlewares) {
117-
middlewares = argv.middlewares.map(function(m) {
117+
middlewares = argv.middlewares.map(function (m) {
118118
console.log(chalk.gray(' Loading', m))
119119
return require(path.resolve(m))
120120
})
@@ -134,7 +134,7 @@ module.exports = function(argv) {
134134
prettyPrint(argv, db.getState(), routes)
135135

136136
// Catch and handle any error occurring in the server process
137-
process.on('uncaughtException', error => {
137+
process.on('uncaughtException', (error) => {
138138
if (error.errno === 'EADDRINUSE')
139139
console.log(
140140
chalk.red(
@@ -164,7 +164,7 @@ module.exports = function(argv) {
164164
console.log(` Creating a snapshot from the CLI won't be possible`)
165165
})
166166
process.stdin.setEncoding('utf8')
167-
process.stdin.on('data', chunk => {
167+
process.stdin.on('data', (chunk) => {
168168
if (chunk.trim().toLowerCase() === 's') {
169169
const filename = `db-${Date.now()}.json`
170170
const file = path.join(argv.snapshots, filename)
@@ -240,7 +240,7 @@ module.exports = function(argv) {
240240
}
241241
}
242242
})
243-
.catch(err => {
243+
.catch((err) => {
244244
console.log(err)
245245
process.exit(1)
246246
})

‎src/cli/utils/is.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
FILE,
33
JS,
4-
URL
4+
URL,
55
}
66

77
function FILE(s) {

‎src/cli/utils/load.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ const chalk = require('chalk')
1111
const example = {
1212
posts: [{ id: 1, title: 'json-server', author: 'typicode' }],
1313
comments: [{ id: 1, body: 'some comment', postId: 1 }],
14-
profile: { name: 'typicode' }
14+
profile: { name: 'typicode' },
1515
}
1616

17-
module.exports = function(source) {
17+
module.exports = function (source) {
1818
return new Promise((resolve, reject) => {
1919
if (is.FILE(source)) {
2020
if (!fs.existsSync(source)) {
@@ -32,17 +32,17 @@ module.exports = function(source) {
3232
const client = sourceUrl.protocol === 'https:' ? https : http
3333

3434
client
35-
.get(sourceUrl, res => {
35+
.get(sourceUrl, (res) => {
3636
let dbData = ''
37-
res.on('data', data => {
37+
res.on('data', (data) => {
3838
dbData += data
3939
})
4040

4141
res.on('end', () => {
4242
resolve(low(new Memory()).setState(JSON.parse(dbData)))
4343
})
4444
})
45-
.on('error', error => {
45+
.on('error', (error) => {
4646
return reject(error)
4747
})
4848
} else if (is.JS(source)) {

‎src/server/body-parser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ const bodyParser = require('body-parser')
22

33
module.exports = [
44
bodyParser.json({ limit: '10mb', extended: false }),
5-
bodyParser.urlencoded({ extended: false })
5+
bodyParser.urlencoded({ extended: false }),
66
]

‎src/server/defaults.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const compression = require('compression')
77
const errorhandler = require('errorhandler')
88
const bodyParser = require('./body-parser')
99

10-
module.exports = function(opts) {
10+
module.exports = function (opts) {
1111
const userDir = path.join(process.cwd(), 'public')
1212
const defaultDir = path.join(__dirname, '../../public')
1313
const staticDir = fs.existsSync(userDir) ? userDir : defaultDir
@@ -38,8 +38,8 @@ module.exports = function(opts) {
3838
if (opts.logger) {
3939
arr.push(
4040
logger('dev', {
41-
skip: req =>
42-
process.env.NODE_ENV === 'test' || req.path === '/favicon.ico'
41+
skip: (req) =>
42+
process.env.NODE_ENV === 'test' || req.path === '/favicon.ico',
4343
})
4444
)
4545
}

‎src/server/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ module.exports = {
55
defaults: require('./defaults'),
66
router: require('./router'),
77
rewriter: require('./rewriter'),
8-
bodyParser: require('./body-parser')
8+
bodyParser: require('./body-parser'),
99
}

‎src/server/mixins.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const nanoid = require('nanoid')
1+
const { nanoid } = require('nanoid')
22
const pluralize = require('pluralize')
33

44
module.exports = {
55
getRemovable,
66
createId,
7-
deepQuery
7+
deepQuery,
88
}
99

1010
// Returns document ids that have unsatisfied relations
@@ -13,7 +13,7 @@ function getRemovable(db, opts) {
1313
const _ = this
1414
const removable = []
1515
_.each(db, (coll, collName) => {
16-
_.each(coll, doc => {
16+
_.each(coll, (doc) => {
1717
_.each(doc, (value, key) => {
1818
if (new RegExp(`${opts.foreignKeySuffix}$`).test(key)) {
1919
// Remove foreign key suffix and pluralize it
@@ -67,12 +67,7 @@ function deepQuery(value, q) {
6767
return true
6868
}
6969
}
70-
} else if (
71-
value
72-
.toString()
73-
.toLowerCase()
74-
.indexOf(q) !== -1
75-
) {
70+
} else if (value.toString().toLowerCase().indexOf(q) !== -1) {
7671
return true
7772
}
7873
}

‎src/server/rewriter.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const express = require('express')
22
const rewrite = require('express-urlrewrite')
33

4-
module.exports = routes => {
4+
module.exports = (routes) => {
55
const router = express.Router()
66

77
router.get('/__rules', (req, res) => {
88
res.json(routes)
99
})
1010

11-
Object.keys(routes).forEach(key => {
11+
Object.keys(routes).forEach((key) => {
1212
router.use(rewrite(key, routes[key]))
1313
})
1414

‎src/server/router/get-full-url.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const url = require('url')
33
module.exports = function getFullURL(req) {
44
const root = url.format({
55
protocol: req.protocol,
6-
host: req.get('host')
6+
host: req.get('host'),
77
})
88

99
return `${root}${req.originalUrl}`

‎src/server/router/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ module.exports = (db, opts) => {
6464
return
6565
}
6666

67-
var sourceMessage = ''
67+
const sourceMessage = ''
6868
// if (!_.isObject(source)) {
6969
// sourceMessage = `in ${source}`
7070
// }

‎src/server/router/nested.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const express = require('express')
22
const pluralize = require('pluralize')
33
const delay = require('./delay')
44

5-
module.exports = opts => {
5+
module.exports = (opts) => {
66
const router = express.Router()
77
router.use(delay)
88

‎src/server/router/plural.js

+19-38
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = (db, name, opts) => {
1414
// Embed function used in GET /name and GET /name/id
1515
function embed(resource, e) {
1616
e &&
17-
[].concat(e).forEach(externalResource => {
17+
[].concat(e).forEach((externalResource) => {
1818
if (db.get(externalResource).value) {
1919
const query = {}
2020
const singularResource = pluralize.singular(name)
@@ -30,7 +30,7 @@ module.exports = (db, name, opts) => {
3030
// Expand function used in GET /name and GET /name/id
3131
function expand(resource, e) {
3232
e &&
33-
[].concat(e).forEach(innerResource => {
33+
[].concat(e).forEach((innerResource) => {
3434
const plural = pluralize(innerResource)
3535
if (db.get(plural).value()) {
3636
const prop = `${innerResource}${opts.foreignKeySuffix}`
@@ -74,7 +74,7 @@ module.exports = (db, name, opts) => {
7474

7575
// Automatically delete query parameters that can't be found
7676
// in the database
77-
Object.keys(req.query).forEach(query => {
77+
Object.keys(req.query).forEach((query) => {
7878
const arr = db.get(name).value()
7979
for (const i in arr) {
8080
if (
@@ -99,17 +99,18 @@ module.exports = (db, name, opts) => {
9999

100100
q = q.toLowerCase()
101101

102-
chain = chain.filter(obj => {
102+
chain = chain.filter((obj) => {
103103
for (const key in obj) {
104104
const value = obj[key]
105105
if (db._.deepQuery(value, q)) {
106106
return true
107107
}
108108
}
109+
return false
109110
})
110111
}
111112

112-
Object.keys(req.query).forEach(key => {
113+
Object.keys(req.query).forEach((key) => {
113114
// Don't take into account JSONP query parameters
114115
// jQuery adds a '_' query parameter too
115116
if (key !== 'callback' && key !== '_') {
@@ -121,16 +122,16 @@ module.exports = (db, name, opts) => {
121122
const isLike = /_like$/.test(key)
122123
const path = key.replace(/(_lte|_gte|_ne|_like)$/, '')
123124

124-
chain = chain.filter(element => {
125+
chain = chain.filter((element) => {
125126
return arr
126-
.map(function(value) {
127+
.map(function (value) {
127128
// get item value based on path
128129
// i.e post.title -> 'foo'
129130
const elementValue = _.get(element, path)
130131

131132
// Prevent toString() failing on undefined or null values
132133
if (elementValue === undefined || elementValue === null) {
133-
return
134+
return undefined
134135
}
135136

136137
if (isRange) {
@@ -155,7 +156,7 @@ module.exports = (db, name, opts) => {
155156
// Sort
156157
if (_sort) {
157158
const _sortSet = _sort.split(',')
158-
const _orderSet = (_order || '').split(',').map(s => s.toLowerCase())
159+
const _orderSet = (_order || '').split(',').map((s) => s.toLowerCase())
159160
chain = chain.orderBy(_sortSet, _orderSet)
160161
}
161162

@@ -217,7 +218,7 @@ module.exports = (db, name, opts) => {
217218
}
218219

219220
// embed and expand
220-
chain = chain.cloneDeep().forEach(function(element) {
221+
chain = chain.cloneDeep().forEach(function (element) {
221222
embed(element, _embed)
222223
expand(element, _expand)
223224
})
@@ -231,10 +232,7 @@ module.exports = (db, name, opts) => {
231232
function show(req, res, next) {
232233
const _embed = req.query._embed
233234
const _expand = req.query._expand
234-
const resource = db
235-
.get(name)
236-
.getById(req.params.id)
237-
.value()
235+
const resource = db.get(name).getById(req.params.id).value()
238236

239237
if (resource) {
240238
// Clone resource to avoid making changes to the underlying object
@@ -258,16 +256,10 @@ module.exports = (db, name, opts) => {
258256
function create(req, res, next) {
259257
let resource
260258
if (opts._isFake) {
261-
const id = db
262-
.get(name)
263-
.createId()
264-
.value()
259+
const id = db.get(name).createId().value()
265260
resource = { ...req.body, id }
266261
} else {
267-
resource = db
268-
.get(name)
269-
.insert(req.body)
270-
.value()
262+
resource = db.get(name).insert(req.body).value()
271263
}
272264

273265
res.setHeader('Access-Control-Expose-Headers', 'Location')
@@ -286,10 +278,7 @@ module.exports = (db, name, opts) => {
286278
let resource
287279

288280
if (opts._isFake) {
289-
resource = db
290-
.get(name)
291-
.getById(id)
292-
.value()
281+
resource = db.get(name).getById(id).value()
293282

294283
if (req.method === 'PATCH') {
295284
resource = { ...resource, ...req.body }
@@ -321,17 +310,12 @@ module.exports = (db, name, opts) => {
321310
if (opts._isFake) {
322311
resource = db.get(name).value()
323312
} else {
324-
resource = db
325-
.get(name)
326-
.removeById(req.params.id)
327-
.value()
313+
resource = db.get(name).removeById(req.params.id).value()
328314

329315
// Remove dependents documents
330316
const removable = db._.getRemovable(db.getState(), opts)
331-
removable.forEach(item => {
332-
db.get(item.name)
333-
.removeById(item.id)
334-
.value()
317+
removable.forEach((item) => {
318+
db.get(item.name).removeById(item.id).value()
335319
})
336320
}
337321

@@ -344,10 +328,7 @@ module.exports = (db, name, opts) => {
344328

345329
const w = write(db)
346330

347-
router
348-
.route('/')
349-
.get(list)
350-
.post(create, w)
331+
router.route('/').get(list).post(create, w)
351332

352333
router
353334
.route('/:id')

‎src/server/router/singular.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ module.exports = (db, name, opts) => {
3939
if (req.method === 'PUT') {
4040
db.set(name, req.body).value()
4141
} else {
42-
db.get(name)
43-
.assign(req.body)
44-
.value()
42+
db.get(name).assign(req.body).value()
4543
}
4644

4745
res.locals.data = db.get(name).value()
@@ -52,12 +50,7 @@ module.exports = (db, name, opts) => {
5250

5351
const w = write(db)
5452

55-
router
56-
.route('/')
57-
.get(show)
58-
.post(create, w)
59-
.put(update, w)
60-
.patch(update, w)
53+
router.route('/').get(show).post(create, w).put(update, w).patch(update, w)
6154

6255
return router
6356
}

‎src/server/router/validate-data.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ function validateKey(key) {
66
`Oops, found / character in database property '${key}'.`,
77
'',
88
"/ aren't supported, if you want to tweak default routes, see",
9-
'https://github.com/typicode/json-server/#add-custom-routes'
9+
'https://github.com/typicode/json-server/#add-custom-routes',
1010
].join('\n')
1111
throw new Error(msg)
1212
}
1313
}
1414

15-
module.exports = obj => {
15+
module.exports = (obj) => {
1616
if (_.isPlainObject(obj)) {
1717
Object.keys(obj).forEach(validateKey)
1818
} else {

‎src/server/utils.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module.exports = {
2-
getPage
2+
getPage,
33
}
44

55
function getPage(array, page, perPage) {
6-
var obj = {}
7-
var start = (page - 1) * perPage
8-
var end = page * perPage
6+
const obj = {}
7+
const start = (page - 1) * perPage
8+
const end = page * perPage
99

1010
obj.items = array.slice(start, end)
1111
if (obj.items.length === 0) {

0 commit comments

Comments
 (0)
Please sign in to comment.