Skip to content

Commit 2ba3b45

Browse files
committed
Refactor code and update tests
1 parent 8f73d98 commit 2ba3b45

File tree

3 files changed

+65
-29
lines changed

3 files changed

+65
-29
lines changed

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ npm install json-server@alpha
1010

1111
## Usage
1212

13-
Create a `db.json` or `db.json5` file
13+
Create a `db.json` (or `db.json5`) file
1414

1515
```json
1616
{
@@ -28,13 +28,13 @@ Create a `db.json` or `db.json5` file
2828
Pass it to JSON Server CLI
2929

3030
```shell
31-
json-server db.json
31+
$ json-server db.json
3232
```
3333

3434
Get a REST API
3535

3636
```shell
37-
curl -H "Accept: application/json" -X GET http://localhost:3000/posts/1
37+
$ curl -H "Accept: application/json" -X GET http://localhost:3000/posts/1
3838
{
3939
"id": "1",
4040
"title": "a title"
@@ -58,7 +58,7 @@ DELETE /posts/:id
5858

5959
### Comparison
6060

61-
- ` ``==`
61+
- ` ` `==`
6262
- `lt``<`
6363
- `lte``<=`
6464
- `gt``>`
@@ -97,9 +97,16 @@ GET /posts?_page=1&_per_page=25
9797
GET /posts?_sort=id,-views
9898
```
9999

100-
### Nested fields
100+
### Nested and array fields
101101

102-
- `x.y.z`
102+
- `x.y.z_...`
103+
- `x.y.z[i]_...`
104+
105+
```
106+
GET /posts?author.name=foo
107+
GET /posts?author.email=foo
108+
GET /posts?names[0]=foo
109+
```
103110

104111
### Include
105112

src/app.test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ await new Promise<void>((resolve, reject) => {
5050
}
5151
})
5252

53-
await test('createApp', async () => {
53+
await test('createApp', async (t) => {
5454
// URLs
5555
const POSTS = '/posts'
5656
const POST_1 = '/posts/1'
@@ -98,13 +98,15 @@ await test('createApp', async () => {
9898
]
9999

100100
for (const tc of arr) {
101-
const response = await fetch(`http://localhost:${port}${tc.url}`, {
102-
method: tc.method,
101+
await t.test(`${tc.method} ${tc.url}`, async () => {
102+
const response = await fetch(`http://localhost:${port}${tc.url}`, {
103+
method: tc.method,
104+
})
105+
assert.equal(
106+
response.status,
107+
tc.statusCode,
108+
`${response.status} !== ${tc.statusCode} ${tc.method} ${tc.url} failed`,
109+
)
103110
})
104-
assert.equal(
105-
response.status,
106-
tc.statusCode,
107-
`${response.status} !== ${tc.statusCode} ${tc.method} ${tc.url} failed`,
108-
)
109111
}
110112
})

src/service.test.ts

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,41 @@ const defaultData = { posts: [] }
1010
const adapter = new Memory<Data>()
1111
const db = new Low<Data>(adapter, defaultData)
1212
const service = new Service(db)
13+
1314
const POSTS = 'posts'
1415
const COMMENTS = 'comments'
1516
const UNKNOWN_RESOURCE = 'xxx'
1617
const UNKNOWN_ID = 'xxx'
17-
const post1 = { id: '1', title: 'a', views: 100, author: { name: 'foo' } }
18-
const post2 = { id: '2', title: 'b', views: 200, author: { name: 'bar' } }
19-
const post3 = { id: '3', title: 'c', views: 300, author: { name: 'baz' } }
18+
19+
const post1 = {
20+
id: '1',
21+
title: 'a',
22+
views: 100,
23+
author: { name: 'foo' },
24+
tags: ['foo', 'bar'],
25+
}
26+
const post2 = {
27+
id: '2',
28+
title: 'b',
29+
views: 200,
30+
author: { name: 'bar' },
31+
tags: ['bar'],
32+
}
33+
const post3 = {
34+
id: '3',
35+
title: 'c',
36+
views: 300,
37+
author: { name: 'baz' },
38+
tags: ['foo'],
39+
}
2040
const comment1 = { id: '1', title: 'a', postId: '1' }
2141
const items = 3
42+
2243
function reset() {
23-
const post1 = { id: '1', title: 'a', views: 100, author: { name: 'foo' } }
24-
const post2 = { id: '2', title: 'b', views: 200, author: { name: 'bar' } }
25-
const post3 = { id: '3', title: 'c', views: 300, author: { name: 'baz' } }
26-
const comment1 = { id: '1', title: 'a', postId: '1' }
27-
db.data = { posts: [post1, post2, post3], comments: [comment1] }
44+
db.data = structuredClone({
45+
posts: [post1, post2, post3],
46+
comments: [comment1],
47+
})
2848
}
2949

3050
type Test = {
@@ -50,7 +70,7 @@ await test('findById', () => {
5070
assert.equal(service.findById(UNKNOWN_RESOURCE, '1', {}), undefined)
5171
})
5272

53-
await test('find', () => {
73+
await test('find', async (t) => {
5474
const arr: Test[] = [
5575
{
5676
name: POSTS,
@@ -76,6 +96,11 @@ await test('find', () => {
7696
params: { 'author.name': post1.author.name },
7797
res: [post1],
7898
},
99+
{
100+
name: POSTS,
101+
params: { 'tags[0]': 'foo' },
102+
res: [post1, post3],
103+
},
79104
{
80105
name: POSTS,
81106
params: { id: UNKNOWN_ID, views: post1.views.toString() },
@@ -213,13 +238,15 @@ await test('find', () => {
213238
},
214239
]
215240
for (const tc of arr) {
216-
if (tc.data) {
217-
db.data = tc.data
218-
} else {
219-
reset()
220-
}
241+
await t.test(`${tc.name} ${JSON.stringify(tc.params)}`, () => {
242+
if (tc.data) {
243+
db.data = tc.data
244+
} else {
245+
reset()
246+
}
221247

222-
assert.deepEqual(service.find(tc.name, tc.params), tc.res)
248+
assert.deepEqual(service.find(tc.name, tc.params), tc.res)
249+
})
223250
}
224251
})
225252

0 commit comments

Comments
 (0)