forked from typicode/json-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (73 loc) · 2.07 KB
/
Copy pathindex.js
File metadata and controls
92 lines (73 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const express = require('express')
const methodOverride = require('method-override')
const _ = require('lodash')
const lodashId = require('lodash-id')
const low = require('lowdb')
const Memory = require('lowdb/adapters/Memory')
const FileSync = require('lowdb/adapters/FileSync')
const bodyParser = require('../body-parser')
const validateData = require('./validate-data')
const plural = require('./plural')
const nested = require('./nested')
const singular = require('./singular')
const mixins = require('../mixins')
module.exports = (db, opts = { foreignKeySuffix: 'Id' }) => {
if (typeof db === 'string') {
db = low(new FileSync(db))
} else if (!_.has(db, '__chain__') || !_.has(db, '__wrapped__')) {
db = low(new Memory()).setState(db)
}
// Create router
const router = express.Router()
// Add middlewares
router.use(methodOverride())
router.use(bodyParser)
validateData(db.getState())
// Add lodash-id methods to db
db._.mixin(lodashId)
// Add specific mixins
db._.mixin(mixins)
// Expose database
router.db = db
// Expose render
router.render = (req, res) => {
res.jsonp(res.locals.data)
}
// GET /db
router.get('/db', (req, res) => {
res.jsonp(db.getState())
})
// Handle /:parent/:parentId/:resource
router.use(nested(opts))
// Create routes
db.forEach((value, key) => {
if (_.isPlainObject(value)) {
router.use(`/${key}`, singular(db, key))
return
}
if (_.isArray(value)) {
router.use(`/${key}`, plural(db, key, opts))
return
}
var sourceMessage = ''
// if (!_.isObject(source)) {
// sourceMessage = `in ${source}`
// }
const msg =
`Type of "${key}" (${typeof value}) ${sourceMessage} is not supported. ` +
`Use objects or arrays of objects.`
throw new Error(msg)
}).value()
router.use((req, res) => {
if (!res.locals.data) {
res.status(404)
res.locals.data = {}
}
router.render(req, res)
})
router.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).send(err.stack)
})
return router
}