json-server 1.0.0-alpha.10 → 1.0.0-alpha.11
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 +3 -1
- package/lib/bin.js +42 -15
- package/lib/observer.d.ts +1 -1
- package/lib/observer.js +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -40,7 +40,7 @@ $ json-server db.json
|
|
|
40
40
|
Get a REST API
|
|
41
41
|
|
|
42
42
|
```shell
|
|
43
|
-
$ curl
|
|
43
|
+
$ curl http://localhost:3000/posts/1
|
|
44
44
|
{
|
|
45
45
|
"id": "1",
|
|
46
46
|
"title": "a title"
|
|
@@ -53,6 +53,8 @@ Run `json-server --help` for a list of options
|
|
|
53
53
|
| :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
|
|
54
54
|
| <a href="https://mockend.com/" target="_blank"><img src="https://jsonplaceholder.typicode.com/mockend.svg" height="70px"></a> |
|
|
55
55
|
| <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
|
+
| <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> |
|
|
57
|
+
|
|
56
58
|
|
|
57
59
|
[Become a sponsor and have your company logo here](https://github.com/users/typicode/sponsorship)
|
|
58
60
|
|
package/lib/bin.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { existsSync, readFileSync } from 'node:fs';
|
|
3
3
|
import { extname, join } from 'node:path';
|
|
4
4
|
import { parseArgs } from 'node:util';
|
|
5
|
+
import chalk from 'chalk';
|
|
5
6
|
import { watch } from 'chokidar';
|
|
6
7
|
import JSON5 from 'json5';
|
|
7
8
|
import { Low } from 'lowdb';
|
|
@@ -54,7 +55,7 @@ const file = positionals[0] ?? '';
|
|
|
54
55
|
const port = parseInt(values.port ?? process.env['PORT'] ?? '3000');
|
|
55
56
|
const host = values.host ?? process.env['HOST'] ?? 'localhost';
|
|
56
57
|
if (!existsSync(file)) {
|
|
57
|
-
console.log(`File ${file} not found`);
|
|
58
|
+
console.log(chalk.red(`File ${file} not found`));
|
|
58
59
|
process.exit(1);
|
|
59
60
|
}
|
|
60
61
|
// Set up database
|
|
@@ -73,37 +74,63 @@ const db = new Low(observer, {});
|
|
|
73
74
|
await db.read();
|
|
74
75
|
// Create app
|
|
75
76
|
const app = createApp(db, { logger: false, static: values.static });
|
|
76
|
-
function
|
|
77
|
-
|
|
77
|
+
function logRoutes(data) {
|
|
78
|
+
console.log([
|
|
79
|
+
chalk.bold('Endpoints:'),
|
|
80
|
+
...Object.keys(data).map((key) => `${chalk.gray(`http://${host}:${port}/`)}${chalk.blue(key)}`),
|
|
81
|
+
].join('\n'));
|
|
78
82
|
}
|
|
83
|
+
const kaomojis = ['♡⸜(˶˃ ᵕ ˂˶)⸝♡', '♡( ◡‿◡ )', '( ˶ˆ ᗜ ˆ˵ )', '(˶ᵔ ᵕ ᵔ˶)'];
|
|
84
|
+
// Get system current language
|
|
85
|
+
app.listen(port, () => {
|
|
86
|
+
console.log([
|
|
87
|
+
chalk.bold(`JSON Server started on PORT :${port}`),
|
|
88
|
+
chalk.gray('Press CTRL-C to stop'),
|
|
89
|
+
chalk.gray(`Watching ${file}...`),
|
|
90
|
+
'',
|
|
91
|
+
chalk.magenta(kaomojis[Math.floor(Math.random() * kaomojis.length)]),
|
|
92
|
+
'',
|
|
93
|
+
chalk.bold('Index:'),
|
|
94
|
+
chalk.gray(`http://localhost:${port}/`),
|
|
95
|
+
'',
|
|
96
|
+
chalk.bold('Static files:'),
|
|
97
|
+
chalk.gray('Serving ./public directory if it exists'),
|
|
98
|
+
'',
|
|
99
|
+
].join('\n'));
|
|
100
|
+
logRoutes(db.data);
|
|
101
|
+
});
|
|
79
102
|
// Watch file for changes
|
|
80
103
|
if (process.env['NODE_ENV'] !== 'production') {
|
|
81
104
|
let writing = false; // true if the file is being written to by the app
|
|
105
|
+
let prevEndpoints = '';
|
|
82
106
|
observer.onWriteStart = () => {
|
|
83
107
|
writing = true;
|
|
84
108
|
};
|
|
85
109
|
observer.onWriteEnd = () => {
|
|
86
110
|
writing = false;
|
|
87
111
|
};
|
|
88
|
-
observer.onReadStart = () =>
|
|
89
|
-
|
|
112
|
+
observer.onReadStart = () => {
|
|
113
|
+
prevEndpoints = JSON.stringify(Object.keys(db.data).sort());
|
|
114
|
+
};
|
|
115
|
+
observer.onReadEnd = (data) => {
|
|
116
|
+
if (data === null) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const nextEndpoints = JSON.stringify(Object.keys(data).sort());
|
|
120
|
+
if (prevEndpoints !== nextEndpoints) {
|
|
121
|
+
console.log();
|
|
122
|
+
logRoutes(data);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
90
125
|
watch(file).on('change', () => {
|
|
91
126
|
// Do no reload if the file is being written to by the app
|
|
92
127
|
if (!writing) {
|
|
93
|
-
db.read()
|
|
94
|
-
.then(() => routes(db))
|
|
95
|
-
.catch((e) => {
|
|
128
|
+
db.read().catch((e) => {
|
|
96
129
|
if (e instanceof SyntaxError) {
|
|
97
|
-
return console.log(e.message);
|
|
130
|
+
return console.log(chalk.red(['', `Error parsing ${file}`, e.message].join('\n')));
|
|
98
131
|
}
|
|
99
132
|
console.log(e);
|
|
100
133
|
});
|
|
101
134
|
}
|
|
102
135
|
});
|
|
103
136
|
}
|
|
104
|
-
app.listen(port, () => {
|
|
105
|
-
console.log(`Started on :${port}`);
|
|
106
|
-
console.log(`http://localhost:${port}/`);
|
|
107
|
-
console.log(routes(db).join('\n'));
|
|
108
|
-
console.log(`Watching ${file}...`);
|
|
109
|
-
});
|
package/lib/observer.d.ts
CHANGED
package/lib/observer.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-server",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.11",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "lib",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@tinyhttp/app": "^2.2.1",
|
|
41
41
|
"@tinyhttp/cors": "^2.0.0",
|
|
42
|
+
"chalk": "^5.3.0",
|
|
42
43
|
"chokidar": "^3.5.3",
|
|
43
44
|
"dot-prop": "^8.0.2",
|
|
44
45
|
"eta": "^3.2.0",
|