json-server 1.0.0-beta.6 → 1.0.0-beta.7
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/LICENSE +21 -44
- package/README.md +71 -35
- package/lib/matches-where.js +5 -0
- package/lib/parse-where.js +4 -0
- package/lib/where-operators.js +1 -1
- package/package.json +1 -1
package/LICENSE
CHANGED
|
@@ -1,44 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Copyright (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Software
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
control with you. If you exceed the Use Limitation, your use is
|
|
23
|
-
subject to payment of Licensor's then-current list price for licenses.
|
|
24
|
-
|
|
25
|
-
Conditions. Redistribution in source code or other forms must include
|
|
26
|
-
a copy of this license document to be provided in a reasonable
|
|
27
|
-
manner. Any redistribution of the Software is only allowed subject to
|
|
28
|
-
this license.
|
|
29
|
-
|
|
30
|
-
Trademarks. This license does not grant you any right in the
|
|
31
|
-
trademarks, service marks, brand names or logos of Licensor.
|
|
32
|
-
|
|
33
|
-
DISCLAIMER. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OR
|
|
34
|
-
CONDITION, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES
|
|
35
|
-
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
36
|
-
NONINFRINGEMENT. LICENSORS HEREBY DISCLAIM ALL LIABILITY, WHETHER IN
|
|
37
|
-
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
38
|
-
CONNECTION WITH THE SOFTWARE.
|
|
39
|
-
|
|
40
|
-
Termination. If you violate the terms of this license, your rights
|
|
41
|
-
will terminate automatically and will not be reinstated without the
|
|
42
|
-
prior written consent of Licensor. Any such termination will not
|
|
43
|
-
affect the right of others who may have received copies of the
|
|
44
|
-
Software from you.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 typicode
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -59,16 +59,26 @@ You can read more about JSON5 format [here](https://github.com/json5/json5).
|
|
|
59
59
|
|
|
60
60
|
</details>
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
Start JSON Server
|
|
63
63
|
|
|
64
|
-
```
|
|
65
|
-
|
|
64
|
+
```bash
|
|
65
|
+
npx json-server db.json
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
This starts the server at `http://localhost:3000`. You should see:
|
|
69
|
+
```
|
|
70
|
+
JSON Server started on PORT :3000
|
|
71
|
+
http://localhost:3000
|
|
72
|
+
```
|
|
69
73
|
|
|
70
|
-
|
|
71
|
-
|
|
74
|
+
Access your REST API:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
curl http://localhost:3000/posts/1
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Response:**
|
|
81
|
+
```json
|
|
72
82
|
{
|
|
73
83
|
"id": "1",
|
|
74
84
|
"title": "a title",
|
|
@@ -103,28 +113,27 @@ Run `json-server --help` for a list of options
|
|
|
103
113
|
|
|
104
114
|
[Become a sponsor and have your company logo here](https://github.com/users/typicode/sponsorship)
|
|
105
115
|
|
|
106
|
-
##
|
|
107
|
-
|
|
108
|
-
> [!NOTE]
|
|
109
|
-
> This project uses the [Fair Source License](https://fair.io/). Only organizations with 3+ users are kindly asked to contribute a small amount through sponsorship [sponsor](https://github.com/sponsors/typicode) for usage. **This license helps keep the project sustainable and healthy, benefiting everyone.**
|
|
110
|
-
>
|
|
111
|
-
> For more information, FAQs, and the rationale behind this, visit [https://fair.io/](https://fair.io/).
|
|
116
|
+
## Query Capabilities
|
|
112
117
|
|
|
113
|
-
|
|
118
|
+
JSON Server supports advanced querying out of the box:
|
|
114
119
|
|
|
115
120
|
```http
|
|
116
|
-
GET /posts?views:gt=100
|
|
117
|
-
GET /posts?_sort=-views
|
|
118
|
-
GET /posts?_page=1&_per_page=10
|
|
119
|
-
GET /posts?_embed=comments
|
|
120
|
-
GET /posts?_where={"or":[
|
|
121
|
+
GET /posts?views:gt=100 # Filter by condition
|
|
122
|
+
GET /posts?_sort=-views # Sort by field (descending)
|
|
123
|
+
GET /posts?_page=1&_per_page=10 # Pagination
|
|
124
|
+
GET /posts?_embed=comments # Include relations
|
|
125
|
+
GET /posts?_where={"or":[...]} # Complex queries
|
|
121
126
|
```
|
|
122
127
|
|
|
128
|
+
See detailed documentation below for each feature.
|
|
129
|
+
|
|
123
130
|
## Routes
|
|
124
131
|
|
|
125
|
-
|
|
132
|
+
### Array Resources
|
|
133
|
+
|
|
134
|
+
For array resources like `posts` and `comments`:
|
|
126
135
|
|
|
127
|
-
```
|
|
136
|
+
```http
|
|
128
137
|
GET /posts
|
|
129
138
|
GET /posts/:id
|
|
130
139
|
POST /posts
|
|
@@ -133,9 +142,11 @@ PATCH /posts/:id
|
|
|
133
142
|
DELETE /posts/:id
|
|
134
143
|
```
|
|
135
144
|
|
|
136
|
-
|
|
145
|
+
### Object Resources
|
|
146
|
+
|
|
147
|
+
For singular object resources like `profile`:
|
|
137
148
|
|
|
138
|
-
```
|
|
149
|
+
```http
|
|
139
150
|
GET /profile
|
|
140
151
|
PUT /profile
|
|
141
152
|
PATCH /profile
|
|
@@ -153,12 +164,14 @@ Operators:
|
|
|
153
164
|
- `lt` less than, `lte` less than or equal
|
|
154
165
|
- `gt` greater than, `gte` greater than or equal
|
|
155
166
|
- `eq` equal, `ne` not equal
|
|
167
|
+
- `in` included in comma-separated list
|
|
156
168
|
|
|
157
169
|
Examples:
|
|
158
170
|
|
|
159
171
|
```http
|
|
160
172
|
GET /posts?views:gt=100
|
|
161
173
|
GET /posts?title:eq=Hello
|
|
174
|
+
GET /posts?id:in=1,2,3
|
|
162
175
|
GET /posts?author.name:eq=typicode
|
|
163
176
|
```
|
|
164
177
|
|
|
@@ -176,8 +189,25 @@ GET /posts?_sort=author.name,-views
|
|
|
176
189
|
GET /posts?_page=1&_per_page=25
|
|
177
190
|
```
|
|
178
191
|
|
|
179
|
-
|
|
180
|
-
|
|
192
|
+
**Response:**
|
|
193
|
+
```json
|
|
194
|
+
{
|
|
195
|
+
"first": 1,
|
|
196
|
+
"prev": null,
|
|
197
|
+
"next": 2,
|
|
198
|
+
"last": 4,
|
|
199
|
+
"pages": 4,
|
|
200
|
+
"items": 100,
|
|
201
|
+
"data": [
|
|
202
|
+
{ "id": "1", "title": "...", "views": 100 },
|
|
203
|
+
{ "id": "2", "title": "...", "views": 200 }
|
|
204
|
+
]
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
**Notes:**
|
|
209
|
+
- `_per_page` defaults to `10` if not specified
|
|
210
|
+
- Invalid `_page` or `_per_page` values are automatically normalized to valid ranges
|
|
181
211
|
|
|
182
212
|
### Embed
|
|
183
213
|
|
|
@@ -200,20 +230,26 @@ GET /posts?_where={"or":[{"views":{"gt":100}},{"author":{"name":{"lt":"m"}}}]}
|
|
|
200
230
|
DELETE /posts/1?_dependent=comments
|
|
201
231
|
```
|
|
202
232
|
|
|
203
|
-
## Static
|
|
233
|
+
## Static Files
|
|
204
234
|
|
|
205
|
-
JSON Server serves `./public`
|
|
235
|
+
JSON Server automatically serves files from the `./public` directory.
|
|
206
236
|
|
|
207
|
-
|
|
237
|
+
To serve additional static directories:
|
|
208
238
|
|
|
209
|
-
```
|
|
210
|
-
json-server -s ./static
|
|
211
|
-
json-server -s ./static -s ./node_modules
|
|
239
|
+
```bash
|
|
240
|
+
json-server db.json -s ./static
|
|
241
|
+
json-server db.json -s ./static -s ./node_modules
|
|
212
242
|
```
|
|
213
243
|
|
|
214
|
-
|
|
244
|
+
Static files are served with standard MIME types and can include HTML, CSS, JavaScript, images, and other assets.
|
|
245
|
+
|
|
246
|
+
## Migration Notes (v0 → v1)
|
|
247
|
+
|
|
248
|
+
If you are upgrading from json-server v0.x, note these behavioral changes:
|
|
249
|
+
|
|
250
|
+
- **ID handling:** `id` is always a string and will be auto-generated if not provided
|
|
251
|
+
- **Pagination:** Use `_per_page` with `_page` instead of the deprecated `_limit` parameter
|
|
252
|
+
- **Relationships:** Use `_embed` instead of `_expand` for including related resources
|
|
253
|
+
- **Request delays:** Use browser DevTools (Network tab > throttling) instead of the removed `--delay` CLI option
|
|
215
254
|
|
|
216
|
-
-
|
|
217
|
-
- use `_per_page` with `_page` instead of `_limit`for pagination
|
|
218
|
-
- use `_embed` instead of `_expand`
|
|
219
|
-
- use Chrome's `Network tab > throtling` to delay requests instead of `--delay` CLI option
|
|
255
|
+
> **New to json-server?** These notes are for users migrating from v0. If this is your first time using json-server, you can ignore this section.
|
package/lib/matches-where.js
CHANGED
|
@@ -48,6 +48,11 @@ export function matchesWhere(obj, where) {
|
|
|
48
48
|
return false;
|
|
49
49
|
if (knownOps.includes('ne') && !(field !== op.ne))
|
|
50
50
|
return false;
|
|
51
|
+
if (knownOps.includes('in')) {
|
|
52
|
+
const inValues = Array.isArray(op.in) ? op.in : [op.in];
|
|
53
|
+
if (!inValues.some((v) => field === v))
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
51
56
|
continue;
|
|
52
57
|
}
|
|
53
58
|
if (isJSONObject(field)) {
|
package/lib/parse-where.js
CHANGED
|
@@ -23,6 +23,10 @@ function splitKey(key) {
|
|
|
23
23
|
}
|
|
24
24
|
function setPathOp(root, path, op, value) {
|
|
25
25
|
const fullPath = `${path}.${op}`;
|
|
26
|
+
if (op === 'in') {
|
|
27
|
+
setProperty(root, fullPath, value.split(',').map((part) => coerceValue(part.trim())));
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
26
30
|
setProperty(root, fullPath, coerceValue(value));
|
|
27
31
|
}
|
|
28
32
|
function coerceValue(value) {
|
package/lib/where-operators.js
CHANGED