json-server 1.0.0-alpha.11 → 1.0.0-alpha.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -4
- package/lib/bin.js +5 -2
- package/lib/service.js +20 -1
- package/lib/service.test.js +11 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
## Install
|
|
9
9
|
|
|
10
10
|
```shell
|
|
11
|
-
npm install json-server
|
|
11
|
+
npm install json-server
|
|
12
12
|
```
|
|
13
13
|
|
|
14
14
|
## Usage
|
|
@@ -49,17 +49,20 @@ $ curl http://localhost:3000/posts/1
|
|
|
49
49
|
|
|
50
50
|
Run `json-server --help` for a list of options
|
|
51
51
|
|
|
52
|
+
## Sponsors ✨
|
|
53
|
+
|
|
52
54
|
| Sponsors |
|
|
53
55
|
| :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
|
|
54
56
|
| <a href="https://mockend.com/" target="_blank"><img src="https://jsonplaceholder.typicode.com/mockend.svg" height="70px"></a> |
|
|
55
57
|
| <a href="https://www.storyblok.com/" target="_blank"><img src="https://github.com/typicode/json-server/assets/5502029/c6b10674-4ada-4616-91b8-59d30046b45a" height="40px"></a> |
|
|
56
|
-
|
|
|
57
|
-
|
|
58
|
+
| <a href="https://betterstack.com/" target="_blank"><img src="https://github.com/typicode/json-server/assets/5502029/44679f8f-9671-470d-b77e-26d90b90cbdc" height="40px"></a> |
|
|
58
59
|
|
|
59
60
|
[Become a sponsor and have your company logo here](https://github.com/users/typicode/sponsorship)
|
|
60
61
|
|
|
61
62
|
## Routes
|
|
62
63
|
|
|
64
|
+
Based on the example `db.json`, you'll get the following routes:
|
|
65
|
+
|
|
63
66
|
```
|
|
64
67
|
GET /posts
|
|
65
68
|
GET /posts/:id
|
|
@@ -67,6 +70,8 @@ POST /posts
|
|
|
67
70
|
PUT /posts/:id
|
|
68
71
|
PATCH /posts/:id
|
|
69
72
|
DELETE /posts/:id
|
|
73
|
+
|
|
74
|
+
# Same for comments
|
|
70
75
|
```
|
|
71
76
|
|
|
72
77
|
```
|
|
@@ -126,7 +131,7 @@ GET /posts?_sort=id,-views
|
|
|
126
131
|
```
|
|
127
132
|
GET /posts?author.name=foo
|
|
128
133
|
GET /posts?author.email=foo
|
|
129
|
-
GET /posts?
|
|
134
|
+
GET /posts?tags[0]=foo
|
|
130
135
|
```
|
|
131
136
|
|
|
132
137
|
### Embed
|
package/lib/bin.js
CHANGED
|
@@ -81,14 +81,17 @@ function logRoutes(data) {
|
|
|
81
81
|
].join('\n'));
|
|
82
82
|
}
|
|
83
83
|
const kaomojis = ['♡⸜(˶˃ ᵕ ˂˶)⸝♡', '♡( ◡‿◡ )', '( ˶ˆ ᗜ ˆ˵ )', '(˶ᵔ ᵕ ᵔ˶)'];
|
|
84
|
-
|
|
84
|
+
function randomItem(items) {
|
|
85
|
+
const index = Math.floor(Math.random() * items.length);
|
|
86
|
+
return items.at(index) ?? '';
|
|
87
|
+
}
|
|
85
88
|
app.listen(port, () => {
|
|
86
89
|
console.log([
|
|
87
90
|
chalk.bold(`JSON Server started on PORT :${port}`),
|
|
88
91
|
chalk.gray('Press CTRL-C to stop'),
|
|
89
92
|
chalk.gray(`Watching ${file}...`),
|
|
90
93
|
'',
|
|
91
|
-
chalk.magenta(
|
|
94
|
+
chalk.magenta(randomItem(kaomojis)),
|
|
92
95
|
'',
|
|
93
96
|
chalk.bold('Index:'),
|
|
94
97
|
chalk.gray(`http://localhost:${port}/`),
|
package/lib/service.js
CHANGED
|
@@ -72,9 +72,28 @@ function deleteDependents(db, name, dependents) {
|
|
|
72
72
|
}
|
|
73
73
|
});
|
|
74
74
|
}
|
|
75
|
+
function randomId() {
|
|
76
|
+
return randomBytes(2).toString('hex');
|
|
77
|
+
}
|
|
78
|
+
function ensureItemsHaveIds(items) {
|
|
79
|
+
return items.map((item) => {
|
|
80
|
+
if (item['id'] === undefined) {
|
|
81
|
+
return { ...item, id: randomId() };
|
|
82
|
+
}
|
|
83
|
+
return item;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
// Ensure all items have an id
|
|
87
|
+
function ensureAllItemsHaveIds(data) {
|
|
88
|
+
return Object.entries(data).reduce((acc, [key, value]) => ({
|
|
89
|
+
...acc,
|
|
90
|
+
[key]: Array.isArray(value) ? ensureItemsHaveIds(value) : value,
|
|
91
|
+
}), {});
|
|
92
|
+
}
|
|
75
93
|
export class Service {
|
|
76
94
|
#db;
|
|
77
95
|
constructor(db) {
|
|
96
|
+
db.data = ensureAllItemsHaveIds(db.data);
|
|
78
97
|
this.#db = db;
|
|
79
98
|
}
|
|
80
99
|
#get(name) {
|
|
@@ -226,7 +245,7 @@ export class Service {
|
|
|
226
245
|
const items = this.#get(name);
|
|
227
246
|
if (items === undefined || !Array.isArray(items))
|
|
228
247
|
return;
|
|
229
|
-
const item = { id:
|
|
248
|
+
const item = { id: randomId(), ...data };
|
|
230
249
|
items.push(item);
|
|
231
250
|
await this.#db.write();
|
|
232
251
|
return item;
|
package/lib/service.test.js
CHANGED
|
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
|
|
2
2
|
import test from 'node:test';
|
|
3
3
|
import { Low, Memory } from 'lowdb';
|
|
4
4
|
import { Service } from './service.js';
|
|
5
|
-
const defaultData = { posts: [] };
|
|
5
|
+
const defaultData = { posts: [], comments: [], object: {} };
|
|
6
6
|
const adapter = new Memory();
|
|
7
7
|
const db = new Low(adapter, defaultData);
|
|
8
8
|
const service = new Service(db);
|
|
@@ -44,6 +44,16 @@ function reset() {
|
|
|
44
44
|
object: obj,
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
|
+
await test('constructor', () => {
|
|
48
|
+
const defaultData = { posts: [{ id: '1' }, {}], object: {} };
|
|
49
|
+
const db = new Low(adapter, defaultData);
|
|
50
|
+
new Service(db);
|
|
51
|
+
if (Array.isArray(db.data['posts'])) {
|
|
52
|
+
const id = db.data['posts']?.at(1)?.['id'];
|
|
53
|
+
assert.ok(id instanceof String, 'id should be a string');
|
|
54
|
+
assert.ok(id.length > 0, 'id should not be empty');
|
|
55
|
+
}
|
|
56
|
+
});
|
|
47
57
|
await test('findById', () => {
|
|
48
58
|
reset();
|
|
49
59
|
if (!Array.isArray(db.data?.[POSTS]))
|