mirror of
https://github.com/typicode/json-server.git
synced 2025-07-29 13:14:12 +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
|
||||
|
||||
Create a `db.json` or `db.json5` file
|
||||
Create a `db.json` (or `db.json5`) file
|
||||
|
||||
```json
|
||||
{
|
||||
@ -28,13 +28,13 @@ Create a `db.json` or `db.json5` file
|
||||
Pass it to JSON Server CLI
|
||||
|
||||
```shell
|
||||
json-server db.json
|
||||
$ json-server db.json
|
||||
```
|
||||
|
||||
Get a REST API
|
||||
|
||||
```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",
|
||||
"title": "a title"
|
||||
@ -58,7 +58,7 @@ DELETE /posts/:id
|
||||
|
||||
### Comparison
|
||||
|
||||
- ` ` →`==`
|
||||
- ` ` → `==`
|
||||
- `lt` → `<`
|
||||
- `lte` → `<=`
|
||||
- `gt` → `>`
|
||||
@ -97,9 +97,16 @@ GET /posts?_page=1&_per_page=25
|
||||
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
|
||||
|
||||
|
@ -50,7 +50,7 @@ await new Promise<void>((resolve, reject) => {
|
||||
}
|
||||
})
|
||||
|
||||
await test('createApp', async () => {
|
||||
await test('createApp', async (t) => {
|
||||
// URLs
|
||||
const POSTS = '/posts'
|
||||
const POST_1 = '/posts/1'
|
||||
@ -98,13 +98,15 @@ await test('createApp', async () => {
|
||||
]
|
||||
|
||||
for (const tc of arr) {
|
||||
const response = await fetch(`http://localhost:${port}${tc.url}`, {
|
||||
method: tc.method,
|
||||
await t.test(`${tc.method} ${tc.url}`, async () => {
|
||||
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 db = new Low<Data>(adapter, defaultData)
|
||||
const service = new Service(db)
|
||||
|
||||
const POSTS = 'posts'
|
||||
const COMMENTS = 'comments'
|
||||
const UNKNOWN_RESOURCE = '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 post3 = { id: '3', title: 'c', views: 300, author: { name: 'baz' } }
|
||||
|
||||
const post1 = {
|
||||
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 items = 3
|
||||
|
||||
function reset() {
|
||||
const post1 = { id: '1', title: 'a', views: 100, author: { name: 'foo' } }
|
||||
const post2 = { id: '2', title: 'b', views: 200, author: { name: 'bar' } }
|
||||
const post3 = { id: '3', title: 'c', views: 300, author: { name: 'baz' } }
|
||||
const comment1 = { id: '1', title: 'a', postId: '1' }
|
||||
db.data = { posts: [post1, post2, post3], comments: [comment1] }
|
||||
db.data = structuredClone({
|
||||
posts: [post1, post2, post3],
|
||||
comments: [comment1],
|
||||
})
|
||||
}
|
||||
|
||||
type Test = {
|
||||
@ -50,7 +70,7 @@ await test('findById', () => {
|
||||
assert.equal(service.findById(UNKNOWN_RESOURCE, '1', {}), undefined)
|
||||
})
|
||||
|
||||
await test('find', () => {
|
||||
await test('find', async (t) => {
|
||||
const arr: Test[] = [
|
||||
{
|
||||
name: POSTS,
|
||||
@ -76,6 +96,11 @@ await test('find', () => {
|
||||
params: { 'author.name': post1.author.name },
|
||||
res: [post1],
|
||||
},
|
||||
{
|
||||
name: POSTS,
|
||||
params: { 'tags[0]': 'foo' },
|
||||
res: [post1, post3],
|
||||
},
|
||||
{
|
||||
name: POSTS,
|
||||
params: { id: UNKNOWN_ID, views: post1.views.toString() },
|
||||
@ -213,13 +238,15 @@ await test('find', () => {
|
||||
},
|
||||
]
|
||||
for (const tc of arr) {
|
||||
if (tc.data) {
|
||||
db.data = tc.data
|
||||
} else {
|
||||
reset()
|
||||
}
|
||||
await t.test(`${tc.name} ${JSON.stringify(tc.params)}`, () => {
|
||||
if (tc.data) {
|
||||
db.data = tc.data
|
||||
} 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