Skip to content

Commit 75917f8

Browse files
committed
feat: Add _noDataNext option
1 parent 57af386 commit 75917f8

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/server/router/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ const singular = require('./singular')
1313
const mixins = require('../mixins')
1414

1515
module.exports = (db, opts) => {
16-
opts = Object.assign({ foreignKeySuffix: 'Id', _isFake: false }, opts)
16+
opts = Object.assign({
17+
foreignKeySuffix: 'Id',
18+
_isFake: false,
19+
_noDataNext: false,
20+
}, opts)
1721

1822
if (typeof db === 'string') {
1923
db = low(new FileSync(db))
@@ -50,7 +54,8 @@ module.exports = (db, opts) => {
5054
})
5155

5256
// Handle /:parent/:parentId/:resource
53-
router.use(nested(opts))
57+
const keys = Object.keys(db.value()).map(key => `/${key}`)
58+
opts._noDataNext && keys.length && router.use(keys, nested(opts));
5459

5560
// Create routes
5661
db.forEach((value, key) => {
@@ -76,15 +81,17 @@ module.exports = (db, opts) => {
7681
throw new Error(msg)
7782
}).value()
7883

79-
router.use((req, res) => {
84+
const render = (req, res) => {
8085
if (!res.locals.data) {
8186
res.status(404)
8287
res.locals.data = {}
8388
}
8489

8590
router.render(req, res)
86-
})
91+
}
8792

93+
opts._noDataNext && keys.length && router.use(keys, render)
94+
!opts._noDataNext && router.use(render);
8895
router.use((err, req, res, next) => {
8996
console.error(err.stack)
9097
res.status(500).send(err.stack)

0 commit comments

Comments
 (0)