Fix code style

This commit is contained in:
Typicode
2015-05-13 18:34:22 +02:00
parent ce73c479b4
commit b1ca61ba9e
8 changed files with 105 additions and 100 deletions

View File

@ -33,7 +33,7 @@ var argv = yargs
.argv
// Start server function
function start(object, filename) {
function start (object, filename) {
var port = process.env.PORT || argv.port
var hostname = argv.host === '0.0.0.0' ? 'localhost' : argv.host
@ -59,10 +59,11 @@ function start(object, filename) {
}
})
var router
if (filename) {
var router = jsonServer.router(filename)
router = jsonServer.router(filename)
} else {
var router = jsonServer.router(object)
router = jsonServer.router(object)
}
var server = jsonServer.create()
@ -90,7 +91,7 @@ if (/\.js$/.test(source)) {
}
if (/^http/.test(source)) {
got(source, function(err, data) {
got(source, function (err, data) {
if (err) throw err
var object = JSON.parse(data)
start(object)

View File

@ -27,10 +27,11 @@
"devDependencies": {
"husky": "^0.6.1",
"mocha": "^2.2.4",
"standard": "^3.8.0",
"supertest": "~0.8.1"
},
"scripts": {
"test": "mocha -R spec test",
"test": "standard && mocha -R spec test",
"start": "node bin",
"prepush": "npm t"
},

View File

@ -8,14 +8,14 @@ var arr = []
// Logger
arr.push(logger('dev', {
skip: function(req, res) { return req.path === '/favicon.ico' }
skip: function (req, res) { return req.path === '/favicon.ico' }
}))
// Serve static files
if (fs.existsSync(process.cwd() + '/public')) {
arr.push(express.static(process.cwd() + '/public'));
arr.push(express.static(process.cwd() + '/public'))
} else {
arr.push(express.static(__dirname + '/public'));
arr.push(express.static(__dirname + '/public'))
}
// CORS

View File

@ -13,7 +13,7 @@ low.mixin(require('underscore.inflections'))
// utils.createId can generate incremental id or uuid
low.mixin({createId: utils.createId})
module.exports = function(source) {
module.exports = function (source) {
// Create router
var router = express.Router()
@ -23,18 +23,19 @@ module.exports = function(source) {
router.use(methodOverride())
// Create database
var db
if (_.isObject(source)) {
var db = low()
db = low()
db.object = source
} else {
var db = low(source)
db = low(source)
}
// Expose database
router.db = db
// GET /db
function showDatabase(req, res, next) {
function showDatabase (req, res, next) {
res.jsonp(db.object)
}
@ -44,7 +45,7 @@ module.exports = function(source) {
// GET /:parent/:parentId/:resource?attr=&attr=
// GET /*?*&_end=
// GET /*?*&_start=&_end=
function list(req, res, next) {
function list (req, res, next) {
// Test if resource exists
if (!db.object.hasOwnProperty(req.params.resource)) {
return res.sendStatus(404)
@ -74,7 +75,7 @@ module.exports = function(source) {
// Full-text search
var q = req.query.q.toLowerCase()
array = db(req.params.resource).filter(function(obj) {
array = db(req.params.resource).filter(function (obj) {
for (var key in obj) {
var value = obj[key]
if (_.isString(value) && value.toLowerCase().indexOf(q) !== -1) {
@ -112,12 +113,12 @@ module.exports = function(source) {
if (_sort) {
_order = _order || 'ASC'
array = _.sortBy(array, function(element) {
return element[_sort];
array = _.sortBy(array, function (element) {
return element[_sort]
})
if (_order === 'DESC') {
array.reverse();
array.reverse()
}
}
@ -127,20 +128,21 @@ module.exports = function(source) {
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
}
_start = parseInt(_start) || 0
_start = parseInt(_start, 10) || 0
if (_end) {
array = array.slice(_start, parseInt(_end))
_end = parseInt(_end, 10)
array = array.slice(_start, _end)
} else if (_limit) {
// Convert strings to int and sum to get end value
array = array.slice(_start, parseInt(_start) + parseInt(_limit))
_limit = parseInt(_limit, 10)
array = array.slice(_start, _start + _limit)
}
res.jsonp(array)
}
// GET /:resource/:id
function show(req, res, next) {
function show (req, res, next) {
var resource = db(req.params.resource)
.get(utils.toNative(req.params.id))
@ -152,7 +154,7 @@ module.exports = function(source) {
}
// POST /:resource
function create(req, res, next) {
function create (req, res, next) {
for (var key in req.body) {
req.body[key] = utils.toNative(req.body[key])
}
@ -165,7 +167,7 @@ module.exports = function(source) {
// PUT /:resource/:id
// PATCH /:resource/:id
function update(req, res, next) {
function update (req, res, next) {
for (var key in req.body) {
req.body[key] = utils.toNative(req.body[key])
}
@ -181,13 +183,13 @@ module.exports = function(source) {
}
// DELETE /:resource/:id
function destroy(req, res, next) {
function destroy (req, res, next) {
db(req.params.resource).remove(utils.toNative(req.params.id))
// Remove dependents documents
var removable = utils.getRemovable(db.object)
_(removable).each(function(item) {
_(removable).each(function (item) {
db(item.name).remove(item.id)
})

View File

@ -7,7 +7,7 @@ _.mixin(_inflections)
// Example:
// 'true' -> true
// '1' -> 1
function toNative(value) {
function toNative (value) {
if (typeof value === 'string') {
if (value === '' || value.trim() !== value) {
return value
@ -21,11 +21,11 @@ function toNative(value) {
}
// Return incremented id or uuid
function createId(coll) {
function createId (coll) {
if (_.isEmpty(coll)) {
return 1
} else {
var id = _.max(coll, function(doc) {
var id = _.max(coll, function (doc) {
return doc.id
}).id
@ -39,15 +39,14 @@ function createId(coll) {
}
}
// Returns document ids that have unsatisfied relations
// Example: a comment that references a post that doesn't exist
function getRemovable(db) {
function getRemovable (db) {
var removable = []
_(db).each(function(coll, collName) {
_(coll).each(function(doc) {
_(doc).each(function(value, key) {
_(db).each(function (coll, collName) {
_(coll).each(function (doc) {
_(doc).each(function (value, key) {
if (/Id$/.test(key)) {
var refName = _.pluralize(key.slice(0, -2))
// Test if table exists

View File

@ -1,14 +1,16 @@
var request = require('supertest')
var assert = require('assert')
var jsonServer = require('../src/')
var utils = require('../src/utils')
describe('Server', function() {
/* global beforeEach, describe, it */
describe('Server', function () {
var server
var router
var db
beforeEach(function() {
beforeEach(function () {
db = {}
db.posts = [
@ -40,8 +42,8 @@ describe('Server', function() {
server.use(router)
})
describe('GET /db', function() {
it('should respond with json and full database', function(done) {
describe('GET /db', function () {
it('should respond with json and full database', function (done) {
request(server)
.get('/db')
.expect('Content-Type', /json/)
@ -50,8 +52,8 @@ describe('Server', function() {
})
})
describe('GET /:resource', function() {
it('should respond with json and corresponding resources', function(done) {
describe('GET /:resource', function () {
it('should respond with json and corresponding resources', function (done) {
request(server)
.get('/posts')
.set('Origin', 'http://example.com')
@ -62,15 +64,15 @@ describe('Server', function() {
.expect(200, done)
})
it('should respond with 404 if resource is not found', function(done) {
it('should respond with 404 if resource is not found', function (done) {
request(server)
.get('/undefined')
.expect(404, done)
})
})
describe('GET /:resource?attr=&attr=', function() {
it('should respond with json and filter resources', function(done) {
describe('GET /:resource?attr=&attr=', function () {
it('should respond with json and filter resources', function (done) {
request(server)
.get('/comments?postId=1&published=true')
.expect('Content-Type', /json/)
@ -79,8 +81,8 @@ describe('Server', function() {
})
})
describe('GET /:resource?q=', function() {
it('should respond with json and make a full-text search', function(done) {
describe('GET /:resource?q=', function () {
it('should respond with json and make a full-text search', function (done) {
request(server)
.get('/tags?q=pho')
.expect('Content-Type', /json/)
@ -88,7 +90,7 @@ describe('Server', function() {
.expect(200, done)
})
it('should return an empty array when nothing is matched', function(done) {
it('should return an empty array when nothing is matched', function (done) {
request(server)
.get('/tags?q=nope')
.expect('Content-Type', /json/)
@ -97,8 +99,8 @@ describe('Server', function() {
})
})
describe('GET /:resource?_end=', function() {
it('should respond with a sliced array', function(done) {
describe('GET /:resource?_end=', function () {
it('should respond with a sliced array', function (done) {
request(server)
.get('/comments?_end=2')
.expect('Content-Type', /json/)
@ -109,8 +111,8 @@ describe('Server', function() {
})
})
describe('GET /:resource?sort=', function() {
it('should respond with json and sort on a field', function(done) {
describe('GET /:resource?sort=', function () {
it('should respond with json and sort on a field', function (done) {
request(server)
.get('/tags?_sort=body')
.expect('Content-Type', /json/)
@ -118,7 +120,7 @@ describe('Server', function() {
.expect(200, done)
})
it('should reverse sorting with _order=DESC', function(done) {
it('should reverse sorting with _order=DESC', function (done) {
request(server)
.get('/tags?_sort=body&_order=DESC')
.expect('Content-Type', /json/)
@ -126,7 +128,7 @@ describe('Server', function() {
.expect(200, done)
})
it('should sort on numerical field', function(done) {
it('should sort on numerical field', function (done) {
request(server)
.get('/posts?_sort=id&_order=DESC')
.expect('Content-Type', /json/)
@ -135,8 +137,8 @@ describe('Server', function() {
})
})
describe('GET /:resource?_start=&_end=', function() {
it('should respond with a sliced array', function(done) {
describe('GET /:resource?_start=&_end=', function () {
it('should respond with a sliced array', function (done) {
request(server)
.get('/comments?_start=1&_end=2')
.expect('Content-Type', /json/)
@ -147,8 +149,8 @@ describe('Server', function() {
})
})
describe('GET /:resource?_start=&_limit=', function() {
it('should respond with a limited array', function(done) {
describe('GET /:resource?_start=&_limit=', function () {
it('should respond with a limited array', function (done) {
request(server)
.get('/comments?_start=1&_limit=1')
.expect('Content-Type', /json/)
@ -159,22 +161,21 @@ describe('Server', function() {
})
})
describe('GET /:parent/:parentId/:resource', function() {
it('should respond with json and corresponding nested resources', function(done) {
describe('GET /:parent/:parentId/:resource', function () {
it('should respond with json and corresponding nested resources', function (done) {
request(server)
.get('/posts/1/comments')
.expect('Content-Type', /json/)
.expect([
db.comments[0],
db.comments[1],
db.comments[1]
])
.expect(200, done)
})
})
describe('GET /:resource/:id', function() {
it('should respond with json and corresponding resource', function(done) {
describe('GET /:resource/:id', function () {
it('should respond with json and corresponding resource', function (done) {
request(server)
.get('/posts/1')
.expect('Content-Type', /json/)
@ -182,7 +183,7 @@ describe('Server', function() {
.expect(200, done)
})
it('should support string id, respond with json and corresponding resource', function(done) {
it('should support string id, respond with json and corresponding resource', function (done) {
request(server)
.get('/refs/abcd-1234')
.expect('Content-Type', /json/)
@ -190,7 +191,7 @@ describe('Server', function() {
.expect(200, done)
})
it('should respond with 404 if resource is not found', function(done) {
it('should respond with 404 if resource is not found', function (done) {
request(server)
.get('/posts/9001')
.expect('Content-Type', /json/)
@ -199,17 +200,16 @@ describe('Server', function() {
})
})
describe('POST /:resource', function() {
describe('POST /:resource', function () {
it('should respond with json, create a resource and increment id',
function(done) {
function (done) {
request(server)
.post('/posts')
.send({body: 'foo', booleanValue: 'true', integerValue: '1'})
.expect('Content-Type', /json/)
.expect({id: 3, body: 'foo', booleanValue: true, integerValue: 1})
.expect(201)
.end(function(err, res) {
.end(function (err, res) {
if (err) return done(err)
assert.equal(db.posts.length, 3)
done()
@ -217,13 +217,13 @@ describe('Server', function() {
})
it('should respond with json, create a resource and generate string id',
function(done) {
function (done) {
request(server)
.post('/refs')
.send({url: 'http://foo.com', postId: '1'})
.expect('Content-Type', /json/)
.expect(201)
.end(function(err, res) {
.end(function (err, res) {
if (err) return done(err)
assert.equal(db.refs.length, 2)
done()
@ -231,15 +231,15 @@ describe('Server', function() {
})
})
describe('PUT /:resource/:id', function() {
it('should respond with json and update resource', function(done) {
describe('PUT /:resource/:id', function () {
it('should respond with json and update resource', function (done) {
request(server)
.put('/posts/1')
.send({id: 1, body: 'bar', booleanValue: 'true', integerValue: '1'})
.expect('Content-Type', /json/)
.expect({id: 1, body: 'bar', booleanValue: true, integerValue: 1})
.expect(200)
.end(function(err, res) {
.end(function (err, res) {
if (err) return done(err)
// assert it was created in database too
assert.deepEqual(db.posts[0], {id: 1, body: 'bar', booleanValue: true, integerValue: 1})
@ -247,7 +247,7 @@ describe('Server', function() {
})
})
it('should respond with 404 if resource is not found', function(done) {
it('should respond with 404 if resource is not found', function (done) {
request(server)
.put('/posts/9001')
.send({id: 1, body: 'bar', booleanValue: 'true', integerValue: '1'})
@ -257,15 +257,15 @@ describe('Server', function() {
})
})
describe('PATCH /:resource/:id', function() {
it('should respond with json and update resource', function(done) {
describe('PATCH /:resource/:id', function () {
it('should respond with json and update resource', function (done) {
request(server)
.patch('/posts/1')
.send({body: 'bar'})
.expect('Content-Type', /json/)
.expect({id: 1, body: 'bar'})
.expect(200)
.end(function(err, res) {
.end(function (err, res) {
if (err) return done(err)
// assert it was created in database too
assert.deepEqual(db.posts[0], {id: 1, body: 'bar'})
@ -273,7 +273,7 @@ describe('Server', function() {
})
})
it('should respond with 404 if resource is not found', function(done) {
it('should respond with 404 if resource is not found', function (done) {
request(server)
.patch('/posts/9001')
.send({body: 'bar'})
@ -283,13 +283,13 @@ describe('Server', function() {
})
})
describe('DELETE /:resource/:id', function() {
it('should respond with empty data, destroy resource and dependent resources', function(done) {
describe('DELETE /:resource/:id', function () {
it('should respond with empty data, destroy resource and dependent resources', function (done) {
request(server)
.del('/posts/1')
.expect({})
.expect(200)
.end(function(err, res) {
.end(function (err, res) {
if (err) return done(err)
assert.equal(db.posts.length, 1)
assert.equal(db.comments.length, 3)
@ -298,10 +298,10 @@ describe('Server', function() {
})
})
describe('Static routes', function() {
describe('Static routes', function () {
describe('GET /', function() {
it('should respond with html', function(done) {
describe('GET /', function () {
it('should respond with html', function (done) {
request(server)
.get('/')
.expect(/You're successfully running JSON Server/)
@ -309,8 +309,8 @@ describe('Server', function() {
})
})
describe('GET /stylesheets/style.css', function() {
it('should respond with css', function(done) {
describe('GET /stylesheets/style.css', function () {
it('should respond with css', function (done) {
request(server)
.get('/stylesheets/style.css')
.expect('Content-Type', /css/)
@ -320,8 +320,8 @@ describe('Server', function() {
})
describe('Database #object', function() {
it('should be accessible', function() {
describe('Database #object', function () {
it('should be accessible', function () {
assert(router.db.object)
})
})

View File

@ -1,11 +1,13 @@
var assert = require('assert')
var utils = require('../src/utils')
describe('utils', function() {
/* global describe, it */
describe('getRemovable', function() {
describe('utils', function () {
it('should return removable documents', function() {
describe('getRemovable', function () {
it('should return removable documents', function () {
var db = {
posts: [
@ -15,7 +17,7 @@ describe('utils', function() {
{id: 1, postId: 1},
// Comments below references a post that doesn't exist
{id: 2, postId: 2},
{id: 3, postId: 2},
{id: 3, postId: 2}
]
}
@ -29,9 +31,9 @@ describe('utils', function() {
})
})
describe('toNative', function() {
describe('toNative', function () {
it('should convert string to native type', function() {
it('should convert string to native type', function () {
// should convert
assert.strictEqual(utils.toNative('1'), 1)
assert.strictEqual(utils.toNative('true'), true)