Socket
Socket
Sign inDemoInstall

json-server

Package Overview
Dependencies
Maintainers
1
Versions
154
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-server - npm Package Compare versions

Comparing version 0.7.23 to 0.7.24

2

package.json
{
"name": "json-server",
"version": "0.7.23",
"version": "0.7.24",
"description": "Serves JSON files through REST routes.",

@@ -5,0 +5,0 @@ "main": "./src/server/index.js",

@@ -9,3 +9,3 @@ # JSON Server [![](https://travis-ci.org/typicode/json-server.svg)](https://travis-ci.org/typicode/json-server) [![](https://badge.fury.io/js/json-server.svg)](http://badge.fury.io/js/json-server) [![](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/typicode/json-server?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

* [JSONPlaceholder - Live running version](http://jsonplaceholder.typicode.com)
_See also [hotel](https://github.com/typicode/hotel), a simple process manager for developers._

@@ -74,2 +74,3 @@

GET /posts/1/comments?_start=20&_end=30
GET /posts/1/comments?_start=20&_limit=10
```

@@ -90,3 +91,3 @@

To embed other resources, add `_embed`.
To embed resources, add `_embed`.

@@ -97,2 +98,8 @@ ```

To expand inner resources, add `_expand`.
```
GET /comments/1?_expand=post
```
Returns database.

@@ -212,3 +219,3 @@

body: res.locals.data
})
})
}

@@ -215,0 +222,0 @@ ```

@@ -11,7 +11,7 @@ var express = require('express')

// GET /:resource
// GET /:resource?q=
// GET /:resource?attr=&attr=
// GET /:resource?_end=&*
// GET /:resource?_start=&_end=&*
// GET /name
// GET /name?q=
// GET /name?attr=&attr=
// GET /name?_end=&*
// GET /name?_start=&_end=&*
function list (req, res, next) {

@@ -107,30 +107,48 @@

// GET /:resource/:id
// GET /name/:id
// GET /name/:id?_embed=&_expand
function show (req, res, next) {
var _embed = req.query._embed
var _expand = req.query._expand
var id = utils.toNative(req.params.id)
var resource = db(name)
.getById(id)
var resource = db(name).getById(id)
// Filter empty params
function filter (p) {
return p && p.trim().length > 0
}
if (resource) {
// Clone resource to avoid making changes to the underlying object
resource = _.cloneDeep(resource)
// Always use an array
_embed = _.isArray(_embed) ? _embed : [_embed]
_embed = [].concat(_embed)
_expand = [].concat(_expand)
// Embed other resources based on resource id
_embed.forEach(function (otherResource) {
// /posts/1?_embed=comments
_embed
.filter(filter)
.forEach(function (otherResource) {
if (db.object[otherResource]) {
var query = {}
var singularResource = pluralize.singular(name)
query[singularResource + 'Id'] = id
resource[otherResource] = db(otherResource).where(query)
}
})
if (otherResource
&& otherResource.trim().length > 0
&& db.object[otherResource]) {
// Expand inner resources based on id
// /posts/1?_expand=user
_expand
.filter(filter)
.forEach(function (innerResource) {
var plural = pluralize(innerResource)
if (db.object[plural]) {
var prop = innerResource + 'Id'
resource[innerResource] = db(plural).getById(resource[prop])
}
})
var query = {}
var prop = pluralize.singular(name) + 'Id'
query[prop] = id
resource[otherResource] = db(otherResource).where(query)
}
})
res.locals.data = resource

@@ -142,3 +160,3 @@ }

// POST /:resource
// POST /name
function create (req, res, next) {

@@ -157,4 +175,4 @@ for (var key in req.body) {

// PUT /:resource/:id
// PATCH /:resource/:id
// PUT /name/:id
// PATCH /name/:id
function update (req, res, next) {

@@ -175,3 +193,3 @@ for (var key in req.body) {

// DELETE /:resource/:id
// DELETE /name/:id
function destroy (req, res, next) {

@@ -178,0 +196,0 @@ db(name).removeById(utils.toNative(req.params.id))

@@ -27,8 +27,13 @@ var request = require('supertest')

db.users = [
{id: 1, username: 'Jim'},
{id: 2, username: 'George'}
]
db.comments = [
{id: 1, body: 'foo', published: true, postId: 1},
{id: 2, body: 'bar', published: false, postId: 1},
{id: 3, body: 'baz', published: false, postId: 2},
{id: 4, body: 'qux', published: true, postId: 2},
{id: 5, body: 'quux', published: false, postId: 2}
{id: 1, body: 'foo', published: true, postId: 1, userId: 1},
{id: 2, body: 'bar', published: false, postId: 1, userId: 2},
{id: 3, body: 'baz', published: false, postId: 2, userId: 1},
{id: 4, body: 'qux', published: true, postId: 2, userId: 2},
{id: 5, body: 'quux', published: false, postId: 2, userId: 1}
]

@@ -262,2 +267,27 @@

describe('GET /:resource/:id?_expand=', function () {
it('should respond with corresponding resource and expanded inner resources', function (done) {
var comments = db.comments[0]
comments.post = db.posts[0]
request(server)
.get('/comments/1?_expand=post')
.expect('Content-Type', /json/)
.expect(comments)
.expect(200, done)
})
})
describe('GET /:resource/:id?_expand=&_expand=', function () {
it('should respond with corresponding resource and expanded inner resources', function (done) {
var comments = db.comments[0]
comments.post = db.posts[0]
comments.user = db.users[0]
request(server)
.get('/comments/1?_expand=post&_expand=user')
.expect('Content-Type', /json/)
.expect(comments)
.expect(200, done)
})
})
describe('POST /:resource', function () {

@@ -264,0 +294,0 @@ it('should respond with json, create a resource and increment id',

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc