Skip to content

Commit

Permalink
feat: Add _noDataNext option
Browse files Browse the repository at this point in the history
  • Loading branch information
wll8 committed May 27, 2022
1 parent 57af386 commit 75917f8
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/server/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ const singular = require('./singular')
const mixins = require('../mixins')

module.exports = (db, opts) => {
opts = Object.assign({ foreignKeySuffix: 'Id', _isFake: false }, opts)
opts = Object.assign({
foreignKeySuffix: 'Id',
_isFake: false,
_noDataNext: false,
}, opts)

if (typeof db === 'string') {
db = low(new FileSync(db))
Expand Down Expand Up @@ -50,7 +54,8 @@ module.exports = (db, opts) => {
})

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

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

router.use((req, res) => {
const render = (req, res) => {
if (!res.locals.data) {
res.status(404)
res.locals.data = {}
}

router.render(req, res)
})
}

opts._noDataNext && keys.length && router.use(keys, render)
!opts._noDataNext && router.use(render);
router.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).send(err.stack)
Expand Down

0 comments on commit 75917f8

Please sign in to comment.