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.25 to 0.7.26

CHANGELOG.md

2

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

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

@@ -23,3 +23,4 @@ # 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)

{ "id": 1, "body": "some comment", "postId": 1 }
]
],
"profile": { "name": "typicode" }
}

@@ -52,2 +53,4 @@ ```

Plural resources
```

@@ -63,2 +66,11 @@ GET /posts

Singular resources
```
GET /profile
POST /profile
PUT /profile
PATCH /profile
```
To filter resources (use `.` to access deep properties)

@@ -95,2 +107,3 @@

```
GET /posts?_embed=comments
GET /posts/1?_embed=comments

@@ -102,2 +115,3 @@ ```

```
GET /comments?_expand=post
GET /comments/1?_expand=post

@@ -258,3 +272,3 @@ ```

### Projects
### Third-party tools

@@ -264,2 +278,3 @@ * [Grunt JSON Server](https://github.com/tfiwm/grunt-json-server)

* [JSON Server GUI](https://github.com/naholyr/json-server-gui)
* [JSON file generator](https://github.com/dfsq/json-server-init)

@@ -266,0 +281,0 @@ ## License

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

// Embed function used in GET /name and GET /name/id
function embed (resource, e) {
e && [].concat(e)
.forEach(function (externalResource) {
if (db.object[externalResource]) {
var query = {}
var singularResource = pluralize.singular(name)
query[singularResource + 'Id'] = resource.id
resource[externalResource] = db(externalResource).where(query)
}
})
}
// Expand function used in GET /name and GET /name/id
function expand (resource, e) {
e && [].concat(e)
.forEach(function (innerResource) {
var plural = pluralize(innerResource)
if (db.object[plural]) {
var prop = innerResource + 'Id'
resource[innerResource] = db(plural).getById(resource[prop])
}
})
}
// GET /name
// GET /name?q=
// GET /name?attr=&attr=
// GET /name?_end=&*
// GET /name?_start=&_end=&*
// GET /name?_end=&
// GET /name?_start=&_end=&
// GET /name?_embed=&_expand=
function list (req, res, next) {

@@ -33,2 +59,4 @@

var _limit = req.query._limit
var _embed = req.query._embed
var _expand = req.query._expand
delete req.query.q

@@ -40,2 +68,4 @@ delete req.query._start

delete req.query._limit
delete req.query._embed
delete req.query._expand

@@ -105,2 +135,10 @@ if (q) {

// embed and expand
chain = chain
.cloneDeep()
.forEach(function (element) {
embed(element, _embed)
expand(element, _expand)
})
res.locals.data = chain.value()

@@ -118,7 +156,2 @@ next()

// Filter empty params
function filter (p) {
return p && p.trim().length > 0
}
if (resource) {

@@ -128,30 +161,9 @@ // Clone resource to avoid making changes to the underlying object

// Always use an array
_embed = [].concat(_embed)
_expand = [].concat(_expand)
// Embed other resources based on resource id
// /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)
}
})
embed(resource, _embed)
// 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])
}
})
expand(resource, _expand)

@@ -158,0 +170,0 @@ res.locals.data = resource

@@ -0,3 +1,4 @@

var assert = require('assert')
var _ = require('lodash')
var request = require('supertest')
var assert = require('assert')
var jsonServer = require('../../src/server')

@@ -41,3 +42,3 @@

db.refs = [
{id: 'abcd-1234', url: 'http://example.com', postId: 1}
{id: 'abcd-1234', url: 'http://example.com', postId: 1, userId: 1}
]

@@ -242,4 +243,32 @@

describe('GET /:resource?_embed=', function () {
it('should respond with corresponding resources and embedded resources', function (done) {
var posts = _.cloneDeep(db.posts)
posts[0].comments = [db.comments[0], db.comments[1]]
posts[1].comments = [db.comments[2], db.comments[3], db.comments[4]]
request(server)
.get('/posts?_embed=comments')
.expect('Content-Type', /json/)
.expect(posts)
.expect(200, done)
})
})
describe('GET /:resource?_embed&_embed=', function () {
it('should respond with corresponding resources and embedded resources', function (done) {
var posts = _.cloneDeep(db.posts)
posts[0].comments = [db.comments[0], db.comments[1]]
posts[0].refs = [db.refs[0]]
posts[1].comments = [db.comments[2], db.comments[3], db.comments[4]]
posts[1].refs = []
request(server)
.get('/posts?_embed=comments&_embed=refs')
.expect('Content-Type', /json/)
.expect(posts)
.expect(200, done)
})
})
describe('GET /:resource/:id?_embed=', function () {
it('should respond with corresponding resource and embedded other resource', function (done) {
it('should respond with corresponding resources and embedded resources', function (done) {
var posts = db.posts[0]

@@ -256,3 +285,3 @@ posts.comments = [db.comments[0], db.comments[1]]

describe('GET /:resource/:id?_embed=&_embed=', function () {
it('should respond with corresponding resource and embedded other resources', function (done) {
it('should respond with corresponding resource and embedded resources', function (done) {
var posts = db.posts[0]

@@ -269,2 +298,14 @@ posts.comments = [db.comments[0], db.comments[1]]

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

@@ -282,2 +323,15 @@ it('should respond with corresponding resource and expanded inner resources', function (done) {

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

@@ -284,0 +338,0 @@ it('should respond with corresponding resource and expanded inner resources', function (done) {

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