mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 12:43:18 +08:00
style update to standard v7
This commit is contained in:
@ -4,7 +4,6 @@ var run = require('./run')
|
||||
var pkg = require('../../package.json')
|
||||
|
||||
module.exports = function () {
|
||||
|
||||
updateNotifier({ pkg: pkg }).notify()
|
||||
|
||||
var argv = yargs
|
||||
|
@ -74,7 +74,6 @@ function createApp (source, object, routes, argv) {
|
||||
}
|
||||
|
||||
module.exports = function (argv) {
|
||||
|
||||
var source = argv._[0]
|
||||
var app
|
||||
var server
|
||||
@ -98,7 +97,6 @@ module.exports = function (argv) {
|
||||
|
||||
// Load JSON, JS or HTTP database
|
||||
load(source, function (err, data) {
|
||||
|
||||
if (err) throw err
|
||||
|
||||
// Load additional routes
|
||||
@ -125,7 +123,6 @@ module.exports = function (argv) {
|
||||
|
||||
// Start server
|
||||
start(function () {
|
||||
|
||||
// Snapshot
|
||||
console.log(
|
||||
chalk.gray(' Type s + enter at any time to create a snapshot of the database')
|
||||
@ -162,7 +159,6 @@ module.exports = function (argv) {
|
||||
.on('change', function (file) {
|
||||
if (file === source) {
|
||||
if (is.JSON(file)) {
|
||||
console.log(file, fs.readFileSync(file))
|
||||
var obj = JSON.parse(fs.readFileSync(file))
|
||||
// Compare .json file content with in memory database
|
||||
var isDatabaseDifferent = !_.eq(obj, app.db.getState())
|
||||
|
@ -8,14 +8,11 @@ module.exports = function (source, cb) {
|
||||
var data
|
||||
|
||||
if (is.URL(source)) {
|
||||
|
||||
request({ url: source, json: true }, function (err, response) {
|
||||
if (err) return cb(err)
|
||||
cb(null, response.body)
|
||||
})
|
||||
|
||||
} else if (is.JS(source)) {
|
||||
|
||||
var filename = path.resolve(source)
|
||||
delete require.cache[filename]
|
||||
var dataFn = require(filename)
|
||||
@ -26,15 +23,10 @@ module.exports = function (source, cb) {
|
||||
|
||||
data = dataFn()
|
||||
cb(null, data)
|
||||
|
||||
} else if (is.JSON(source)) {
|
||||
|
||||
data = low(source, { storage: fileAsync }).getState()
|
||||
cb(null, data)
|
||||
|
||||
} else {
|
||||
|
||||
throw new Error('Unsupported source ' + source)
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -10,9 +10,9 @@ var objectAssign = require('object-assign')
|
||||
module.exports = function (opts) {
|
||||
var userDir = path.join(process.cwd(), 'public')
|
||||
var defaultDir = path.join(__dirname, 'public')
|
||||
var staticDir = fs.existsSync(userDir) ?
|
||||
userDir :
|
||||
defaultDir
|
||||
var staticDir = fs.existsSync(userDir)
|
||||
? userDir
|
||||
: defaultDir
|
||||
|
||||
opts = objectAssign({ logger: true, static: staticDir }, opts)
|
||||
|
||||
|
@ -4,7 +4,6 @@ module.exports = function (routes) {
|
||||
var router = express.Router()
|
||||
|
||||
Object.keys(routes).forEach(function (route) {
|
||||
|
||||
if (route.indexOf(':') !== -1) {
|
||||
router.all(route, function (req, res, next) {
|
||||
// Rewrite target url using params
|
||||
@ -22,7 +21,6 @@ module.exports = function (routes) {
|
||||
next()
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
return router
|
||||
|
@ -11,7 +11,6 @@ var singular = require('./singular')
|
||||
var mixins = require('../mixins')
|
||||
|
||||
module.exports = function (source) {
|
||||
|
||||
// Create router
|
||||
var router = express.Router()
|
||||
|
||||
|
@ -3,7 +3,6 @@ var pluralize = require('pluralize')
|
||||
var utils = require('../utils')
|
||||
|
||||
module.exports = function () {
|
||||
|
||||
var router = express.Router()
|
||||
|
||||
// Rewrite URL (/:resource/:id/:nested -> /:nested) and request query
|
||||
|
@ -4,7 +4,6 @@ var pluralize = require('pluralize')
|
||||
var utils = require('../utils')
|
||||
|
||||
module.exports = function (db, name) {
|
||||
|
||||
// Create router
|
||||
var router = express.Router()
|
||||
|
||||
@ -40,7 +39,6 @@ module.exports = function (db, name) {
|
||||
// GET /name?_start=&_end=&
|
||||
// GET /name?_embed=&_expand=
|
||||
function list (req, res, next) {
|
||||
|
||||
// Resource chain
|
||||
var chain = db.get(name)
|
||||
|
||||
@ -82,7 +80,6 @@ module.exports = function (db, name) {
|
||||
})
|
||||
|
||||
if (q) {
|
||||
|
||||
// Full-text search
|
||||
q = q.toLowerCase()
|
||||
|
||||
@ -94,7 +91,6 @@ module.exports = function (db, name) {
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
Object.keys(req.query).forEach(function (key) {
|
||||
@ -228,9 +224,9 @@ module.exports = function (db, name) {
|
||||
var id = utils.toNative(req.params.id)
|
||||
var chain = db.get(name)
|
||||
|
||||
chain = req.method === 'PATCH' ?
|
||||
chain.updateById(id, req.body) :
|
||||
chain.replaceById(id, req.body)
|
||||
chain = req.method === 'PATCH'
|
||||
? chain.updateById(id, req.body)
|
||||
: chain.replaceById(id, req.body)
|
||||
|
||||
var resource = chain.value()
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
var express = require('express')
|
||||
|
||||
module.exports = function (db, name) {
|
||||
|
||||
var router = express.Router()
|
||||
|
||||
function show (req, res, next) {
|
||||
@ -37,5 +36,4 @@ module.exports = function (db, name) {
|
||||
.patch(update)
|
||||
|
||||
return router
|
||||
|
||||
}
|
||||
|
@ -8,9 +8,11 @@ module.exports = {
|
||||
// '1' -> 1
|
||||
function toNative (value) {
|
||||
if (typeof value === 'string') {
|
||||
if (value === ''
|
||||
|| value.trim() !== value
|
||||
|| (value.length > 1 && value[0] === '0')) {
|
||||
if (
|
||||
value === '' ||
|
||||
value.trim() !== value ||
|
||||
(value.length > 1 && value[0] === '0')
|
||||
) {
|
||||
return value
|
||||
} else if (value === 'true' || value === 'false') {
|
||||
return value === 'true'
|
||||
|
@ -24,7 +24,6 @@ function cli (args) {
|
||||
/* global beforeEach, afterEach, describe, it */
|
||||
|
||||
describe('cli', function () {
|
||||
|
||||
var child
|
||||
var request
|
||||
var dbFile
|
||||
@ -34,7 +33,7 @@ describe('cli', function () {
|
||||
dbFile = tempWrite.sync(JSON.stringify({
|
||||
posts: [
|
||||
{ id: 1 },
|
||||
{_id: 2 }
|
||||
{ _id: 2 }
|
||||
]
|
||||
}), 'db.json')
|
||||
|
||||
@ -51,7 +50,6 @@ describe('cli', function () {
|
||||
})
|
||||
|
||||
describe('db.json', function () {
|
||||
|
||||
beforeEach(function (done) {
|
||||
child = cli([dbFile])
|
||||
serverReady(PORT, done)
|
||||
@ -84,7 +82,6 @@ describe('cli', function () {
|
||||
})
|
||||
|
||||
describe('seed.js', function () {
|
||||
|
||||
beforeEach(function (done) {
|
||||
child = cli(['fixtures/seed.js'])
|
||||
serverReady(PORT, done)
|
||||
@ -93,11 +90,9 @@ describe('cli', function () {
|
||||
it('should support JS file', function (done) {
|
||||
request.get('/posts').expect(200, done)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('http://localhost:8080/db', function () {
|
||||
|
||||
beforeEach(function (done) {
|
||||
var fakeServer = express()
|
||||
fakeServer.get('/db', function (req, res) {
|
||||
@ -112,11 +107,9 @@ describe('cli', function () {
|
||||
it('should support URL file', function (done) {
|
||||
request.get('/posts').expect(200, done)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('db.json -r routes.json -i _id --read-only', function () {
|
||||
|
||||
beforeEach(function (done) {
|
||||
child = cli([dbFile, '-r', routesFile, '-i', '_id', '--read-only'])
|
||||
serverReady(PORT, done)
|
||||
@ -129,11 +122,9 @@ describe('cli', function () {
|
||||
it('should allow only GET requests', function (done) {
|
||||
request.post('/blog/posts').expect(403, done)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('db.json -d 1000', function () {
|
||||
|
||||
beforeEach(function (done) {
|
||||
child = cli([dbFile, '-d', 1000])
|
||||
serverReady(PORT, done)
|
||||
@ -146,11 +137,9 @@ describe('cli', function () {
|
||||
done(end - start > 1000 ? err : new Error('Request wasn\'t delayed'))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('db.json -s fixtures/public -S /some/path/snapshots', function () {
|
||||
|
||||
var snapshotsDir = path.join(osTmpdir(), 'snapshots')
|
||||
var publicDir = 'fixtures/public'
|
||||
|
||||
@ -172,11 +161,9 @@ describe('cli', function () {
|
||||
it('should save a snapshot in snapshots dir', function () {
|
||||
assert.equal(fs.readdirSync(snapshotsDir).length, 1)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('db.json --no-cors=true', function () {
|
||||
|
||||
beforeEach(function (done) {
|
||||
child = cli(['fixtures/seed.js', '--no-cors=true'])
|
||||
serverReady(PORT, done)
|
||||
@ -198,11 +185,9 @@ describe('cli', function () {
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('db.json --no-gzip=true', function () {
|
||||
|
||||
beforeEach(function (done) {
|
||||
child = cli(['fixtures/seed.js', '--no-gzip=true'])
|
||||
serverReady(PORT, done)
|
||||
@ -224,11 +209,9 @@ describe('cli', function () {
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('db.json --no-gzip=false', function () {
|
||||
|
||||
beforeEach(function (done) {
|
||||
child = cli(['fixtures/seed.js', '--no-gzip=false'])
|
||||
serverReady(PORT, done)
|
||||
@ -250,13 +233,9 @@ describe('cli', function () {
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
// FIXME test fails on OS X and maybe on Windows
|
||||
// But manually updating db.json works...
|
||||
describe('--watch db.json -r routes.json', function () {
|
||||
|
||||
beforeEach(function (done) {
|
||||
child = cli(['--watch', dbFile, '-r', routesFile])
|
||||
serverReady(PORT, done)
|
||||
@ -277,7 +256,5 @@ describe('cli', function () {
|
||||
request.get('/api/posts').expect(200, done)
|
||||
}, 9000)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
|
@ -6,11 +6,8 @@ var mixins = require('../../src/server/mixins')
|
||||
/* global describe, it */
|
||||
|
||||
describe('mixins', function () {
|
||||
|
||||
describe('getRemovable', function () {
|
||||
|
||||
it('should return removable documents', function () {
|
||||
|
||||
var db = {
|
||||
posts: [
|
||||
{id: 1, comment: 1}
|
||||
@ -32,7 +29,6 @@ describe('mixins', function () {
|
||||
_.mixin(mixins)
|
||||
|
||||
assert.deepEqual(_.getRemovable(db), expected)
|
||||
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -4,9 +4,7 @@ var request = require('supertest')
|
||||
var jsonServer = require('../../src/server')
|
||||
|
||||
/* global beforeEach, describe, it */
|
||||
|
||||
describe('Server', function () {
|
||||
|
||||
var server
|
||||
var router
|
||||
var db
|
||||
@ -533,7 +531,6 @@ describe('Server', function () {
|
||||
})
|
||||
|
||||
describe('Static routes', function () {
|
||||
|
||||
describe('GET /', function () {
|
||||
it('should respond with html', function (done) {
|
||||
request(server)
|
||||
@ -551,7 +548,6 @@ describe('Server', function () {
|
||||
.expect(200, done)
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('Database state', function () {
|
||||
@ -561,7 +557,6 @@ describe('Server', function () {
|
||||
})
|
||||
|
||||
describe('Responses', function () {
|
||||
|
||||
it('should have no cache headers (for IE)', function (done) {
|
||||
request(server)
|
||||
.get('/db')
|
||||
@ -570,11 +565,9 @@ describe('Server', function () {
|
||||
.expect('Expires', '-1')
|
||||
.end(done)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('Rewriter', function () {
|
||||
|
||||
it('should rewrite using prefix', function (done) {
|
||||
request(server)
|
||||
.get('/api/posts/1')
|
||||
@ -588,11 +581,9 @@ describe('Server', function () {
|
||||
.expect(db.posts[0])
|
||||
.end(done)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('router.render', function (done) {
|
||||
|
||||
beforeEach(function () {
|
||||
router.render = function (req, res) {
|
||||
res.jsonp({
|
||||
@ -608,11 +599,9 @@ describe('Server', function () {
|
||||
.expect({ data: db.posts[0] })
|
||||
.expect(200, done)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('router.db._.id', function (done) {
|
||||
|
||||
beforeEach(function () {
|
||||
router.db.setState({
|
||||
posts: [
|
||||
@ -639,6 +628,5 @@ describe('Server', function () {
|
||||
.expect({_id: 2, body: 'hello'})
|
||||
.expect(201, done)
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
|
@ -2,9 +2,7 @@ var request = require('supertest')
|
||||
var jsonServer = require('../../src/server')
|
||||
|
||||
/* global beforeEach, describe, it */
|
||||
|
||||
describe('Server', function () {
|
||||
|
||||
var server
|
||||
var router
|
||||
var db
|
||||
@ -63,5 +61,4 @@ describe('Server', function () {
|
||||
.expect(200, done)
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
@ -4,9 +4,7 @@ var utils = require('../../src/server/utils')
|
||||
/* global describe, it */
|
||||
|
||||
describe('utils', function () {
|
||||
|
||||
describe('toNative', function () {
|
||||
|
||||
it('should convert string to native type', function () {
|
||||
// should convert
|
||||
assert.strictEqual(utils.toNative('1'), 1)
|
||||
@ -22,6 +20,5 @@ describe('utils', function () {
|
||||
assert.strictEqual(utils.toNative(1), 1)
|
||||
assert.strictEqual(utils.toNative(true), true)
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user