json-server 1.0.0-alpha.21 → 1.0.0-alpha.22

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
@@ -169,7 +169,7 @@ GET /comments?_embed=post
169
169
 
170
170
  ```
171
171
  DELETE /posts/1
172
- DELETE /posts/1?_embed=comments
172
+ DELETE /posts/1?_dependent=comments
173
173
  ```
174
174
 
175
175
  ## Serving static files
package/lib/app.js CHANGED
@@ -29,7 +29,17 @@ export function createApp(db, options = {}) {
29
29
  app.get('/', (_req, res) => res.send(eta.render('index.html', { data: db.data })));
30
30
  app.get('/:name', (req, res, next) => {
31
31
  const { name = '' } = req.params;
32
- res.locals['data'] = service.find(name, req.query);
32
+ const query = Object.fromEntries(Object.entries(req.query)
33
+ .map(([key, value]) => {
34
+ if (['_start', '_end', '_limit', '_page', '_per_page'].includes(key) && typeof value === 'string') {
35
+ return [key, parseInt(value)];
36
+ }
37
+ else {
38
+ return [key, value];
39
+ }
40
+ })
41
+ .filter(([_, value]) => !Number.isNaN(value)));
42
+ res.locals['data'] = service.find(name, query);
33
43
  next();
34
44
  });
35
45
  app.get('/:name/:id', (req, res, next) => {
@@ -74,7 +84,7 @@ export function createApp(db, options = {}) {
74
84
  });
75
85
  app.delete('/:name/:id', async (req, res, next) => {
76
86
  const { name = '', id = '' } = req.params;
77
- res.locals['data'] = await service.destroyById(name, id);
87
+ res.locals['data'] = await service.destroyById(name, id, req.query['dependent']);
78
88
  next();
79
89
  });
80
90
  app.use('/:name', (req, res) => {
package/lib/service.d.ts CHANGED
@@ -21,8 +21,7 @@ export declare class Service {
21
21
  }): Item | undefined;
22
22
  find(name: string, query?: {
23
23
  [key: string]: unknown;
24
- } & {
25
- _embed?: string[];
24
+ _embed?: string | string[];
26
25
  _sort?: string;
27
26
  _start?: number;
28
27
  _end?: number;
@@ -35,5 +34,5 @@ export declare class Service {
35
34
  patch(name: string, body?: Item): Promise<Item | undefined>;
36
35
  updateById(name: string, id: string, body?: Item): Promise<Item | undefined>;
37
36
  patchById(name: string, id: string, body?: Item): Promise<Item | undefined>;
38
- destroyById(name: string, id: string, dependents?: string[]): Promise<Item | undefined>;
37
+ destroyById(name: string, id: string, dependent?: string | string[]): Promise<Item | undefined>;
39
38
  }
package/lib/service.js CHANGED
@@ -224,11 +224,14 @@ export class Service {
224
224
  const start = query._start;
225
225
  const end = query._end;
226
226
  const limit = query._limit;
227
- if (start === undefined && limit) {
228
- return sorted.slice(0, limit);
227
+ if (start !== undefined) {
228
+ if (end !== undefined) {
229
+ return sorted.slice(start, end);
230
+ }
231
+ return sorted.slice(start, start + (limit || 0));
229
232
  }
230
- if (start && limit) {
231
- return sorted.slice(start, start + limit);
233
+ if (limit !== undefined) {
234
+ return sorted.slice(0, limit);
232
235
  }
233
236
  // Paginate
234
237
  let page = query._page;
@@ -299,7 +302,7 @@ export class Service {
299
302
  async patchById(name, id, body = {}) {
300
303
  return this.#updateOrPatchById(name, id, body, true);
301
304
  }
302
- async destroyById(name, id, dependents = []) {
305
+ async destroyById(name, id, dependent) {
303
306
  const items = this.#get(name);
304
307
  if (items === undefined || !Array.isArray(items))
305
308
  return;
@@ -309,6 +312,7 @@ export class Service {
309
312
  const index = items.indexOf(item);
310
313
  items.splice(index, 1)[0];
311
314
  nullifyForeignKey(this.#db, name, id);
315
+ const dependents = ensureArray(dependent);
312
316
  deleteDependents(this.#db, name, dependents);
313
317
  await this.#db.write();
314
318
  return item;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-server",
3
- "version": "1.0.0-alpha.21",
3
+ "version": "1.0.0-alpha.22",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "bin": {
package/views/index.html CHANGED
@@ -31,7 +31,7 @@
31
31
  <a href="<%= name %>">/<%= name %></a>
32
32
  <span class="text-gray-500">
33
33
  <% if (Array.isArray(it.data[name])) { %>
34
- - <%= it.data[name].length %> items</span>
34
+ - <%= it.data[name].length %> <%= it.data[name].length > 1 ? 'items' : 'item' %></span>
35
35
  <% } %>
36
36
  </div>
37
37
  <% }) %>