mirror of
https://github.com/typicode/json-server.git
synced 2025-07-30 21:54:11 +08:00
Refactor code and update tests
This commit is contained in:
19
README.md
19
README.md
@ -10,7 +10,7 @@ npm install json-server@alpha
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Create a `db.json` or `db.json5` file
|
Create a `db.json` (or `db.json5`) file
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
@ -28,13 +28,13 @@ Create a `db.json` or `db.json5` file
|
|||||||
Pass it to JSON Server CLI
|
Pass it to JSON Server CLI
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
json-server db.json
|
$ json-server db.json
|
||||||
```
|
```
|
||||||
|
|
||||||
Get a REST API
|
Get a REST API
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
curl -H "Accept: application/json" -X GET http://localhost:3000/posts/1
|
$ curl -H "Accept: application/json" -X GET http://localhost:3000/posts/1
|
||||||
{
|
{
|
||||||
"id": "1",
|
"id": "1",
|
||||||
"title": "a title"
|
"title": "a title"
|
||||||
@ -58,7 +58,7 @@ DELETE /posts/:id
|
|||||||
|
|
||||||
### Comparison
|
### Comparison
|
||||||
|
|
||||||
- ` ` →`==`
|
- ` ` → `==`
|
||||||
- `lt` → `<`
|
- `lt` → `<`
|
||||||
- `lte` → `<=`
|
- `lte` → `<=`
|
||||||
- `gt` → `>`
|
- `gt` → `>`
|
||||||
@ -97,9 +97,16 @@ GET /posts?_page=1&_per_page=25
|
|||||||
GET /posts?_sort=id,-views
|
GET /posts?_sort=id,-views
|
||||||
```
|
```
|
||||||
|
|
||||||
### Nested fields
|
### Nested and array fields
|
||||||
|
|
||||||
- `x.y.z`
|
- `x.y.z_...`
|
||||||
|
- `x.y.z[i]_...`
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /posts?author.name=foo
|
||||||
|
GET /posts?author.email=foo
|
||||||
|
GET /posts?names[0]=foo
|
||||||
|
```
|
||||||
|
|
||||||
### Include
|
### Include
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ await new Promise<void>((resolve, reject) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
await test('createApp', async () => {
|
await test('createApp', async (t) => {
|
||||||
// URLs
|
// URLs
|
||||||
const POSTS = '/posts'
|
const POSTS = '/posts'
|
||||||
const POST_1 = '/posts/1'
|
const POST_1 = '/posts/1'
|
||||||
@ -98,13 +98,15 @@ await test('createApp', async () => {
|
|||||||
]
|
]
|
||||||
|
|
||||||
for (const tc of arr) {
|
for (const tc of arr) {
|
||||||
const response = await fetch(`http://localhost:${port}${tc.url}`, {
|
await t.test(`${tc.method} ${tc.url}`, async () => {
|
||||||
method: tc.method,
|
const response = await fetch(`http://localhost:${port}${tc.url}`, {
|
||||||
|
method: tc.method,
|
||||||
|
})
|
||||||
|
assert.equal(
|
||||||
|
response.status,
|
||||||
|
tc.statusCode,
|
||||||
|
`${response.status} !== ${tc.statusCode} ${tc.method} ${tc.url} failed`,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
assert.equal(
|
|
||||||
response.status,
|
|
||||||
tc.statusCode,
|
|
||||||
`${response.status} !== ${tc.statusCode} ${tc.method} ${tc.url} failed`,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -10,21 +10,41 @@ const defaultData = { posts: [] }
|
|||||||
const adapter = new Memory<Data>()
|
const adapter = new Memory<Data>()
|
||||||
const db = new Low<Data>(adapter, defaultData)
|
const db = new Low<Data>(adapter, defaultData)
|
||||||
const service = new Service(db)
|
const service = new Service(db)
|
||||||
|
|
||||||
const POSTS = 'posts'
|
const POSTS = 'posts'
|
||||||
const COMMENTS = 'comments'
|
const COMMENTS = 'comments'
|
||||||
const UNKNOWN_RESOURCE = 'xxx'
|
const UNKNOWN_RESOURCE = 'xxx'
|
||||||
const UNKNOWN_ID = 'xxx'
|
const UNKNOWN_ID = 'xxx'
|
||||||
const post1 = { id: '1', title: 'a', views: 100, author: { name: 'foo' } }
|
|
||||||
const post2 = { id: '2', title: 'b', views: 200, author: { name: 'bar' } }
|
const post1 = {
|
||||||
const post3 = { id: '3', title: 'c', views: 300, author: { name: 'baz' } }
|
id: '1',
|
||||||
|
title: 'a',
|
||||||
|
views: 100,
|
||||||
|
author: { name: 'foo' },
|
||||||
|
tags: ['foo', 'bar'],
|
||||||
|
}
|
||||||
|
const post2 = {
|
||||||
|
id: '2',
|
||||||
|
title: 'b',
|
||||||
|
views: 200,
|
||||||
|
author: { name: 'bar' },
|
||||||
|
tags: ['bar'],
|
||||||
|
}
|
||||||
|
const post3 = {
|
||||||
|
id: '3',
|
||||||
|
title: 'c',
|
||||||
|
views: 300,
|
||||||
|
author: { name: 'baz' },
|
||||||
|
tags: ['foo'],
|
||||||
|
}
|
||||||
const comment1 = { id: '1', title: 'a', postId: '1' }
|
const comment1 = { id: '1', title: 'a', postId: '1' }
|
||||||
const items = 3
|
const items = 3
|
||||||
|
|
||||||
function reset() {
|
function reset() {
|
||||||
const post1 = { id: '1', title: 'a', views: 100, author: { name: 'foo' } }
|
db.data = structuredClone({
|
||||||
const post2 = { id: '2', title: 'b', views: 200, author: { name: 'bar' } }
|
posts: [post1, post2, post3],
|
||||||
const post3 = { id: '3', title: 'c', views: 300, author: { name: 'baz' } }
|
comments: [comment1],
|
||||||
const comment1 = { id: '1', title: 'a', postId: '1' }
|
})
|
||||||
db.data = { posts: [post1, post2, post3], comments: [comment1] }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Test = {
|
type Test = {
|
||||||
@ -50,7 +70,7 @@ await test('findById', () => {
|
|||||||
assert.equal(service.findById(UNKNOWN_RESOURCE, '1', {}), undefined)
|
assert.equal(service.findById(UNKNOWN_RESOURCE, '1', {}), undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
await test('find', () => {
|
await test('find', async (t) => {
|
||||||
const arr: Test[] = [
|
const arr: Test[] = [
|
||||||
{
|
{
|
||||||
name: POSTS,
|
name: POSTS,
|
||||||
@ -76,6 +96,11 @@ await test('find', () => {
|
|||||||
params: { 'author.name': post1.author.name },
|
params: { 'author.name': post1.author.name },
|
||||||
res: [post1],
|
res: [post1],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: POSTS,
|
||||||
|
params: { 'tags[0]': 'foo' },
|
||||||
|
res: [post1, post3],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: POSTS,
|
name: POSTS,
|
||||||
params: { id: UNKNOWN_ID, views: post1.views.toString() },
|
params: { id: UNKNOWN_ID, views: post1.views.toString() },
|
||||||
@ -213,13 +238,15 @@ await test('find', () => {
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
for (const tc of arr) {
|
for (const tc of arr) {
|
||||||
if (tc.data) {
|
await t.test(`${tc.name} ${JSON.stringify(tc.params)}`, () => {
|
||||||
db.data = tc.data
|
if (tc.data) {
|
||||||
} else {
|
db.data = tc.data
|
||||||
reset()
|
} else {
|
||||||
}
|
reset()
|
||||||
|
}
|
||||||
|
|
||||||
assert.deepEqual(service.find(tc.name, tc.params), tc.res)
|
assert.deepEqual(service.find(tc.name, tc.params), tc.res)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user