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 CHANGED
@@ -40,7 +40,7 @@ $ json-server db.json
40
40
  Get a REST API
41
41
 
42
42
  ```shell
43
- $ curl -H "Accept: application/json" -X GET http://localhost:3000/posts/1
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 routes(db) {
77
- return Object.keys(db.data).map((key) => `http://${host}:${port}/${key}`);
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 = () => console.log(`Reloading ${file}...`);
89
- observer.onReadEnd = () => console.log('Reloaded');
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
@@ -2,7 +2,7 @@ import { Adapter } from 'lowdb';
2
2
  export declare class Observer<T> {
3
3
  #private;
4
4
  onReadStart: () => void;
5
- onReadEnd: () => void;
5
+ onReadEnd: (data: T | null) => void;
6
6
  onWriteStart: () => void;
7
7
  onWriteEnd: () => void;
8
8
  constructor(adapter: Adapter<T>);
package/lib/observer.js CHANGED
@@ -19,7 +19,7 @@ export class Observer {
19
19
  async read() {
20
20
  this.onReadStart();
21
21
  const data = await this.#adapter.read();
22
- this.onReadEnd();
22
+ this.onReadEnd(data);
23
23
  return data;
24
24
  }
25
25
  async write(arg) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-server",
3
- "version": "1.0.0-alpha.10",
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",