-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84ac833
commit 6ab752f
Showing
2 changed files
with
59 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,33 @@ | ||
{{#isUser}} | ||
const { Router } = require('express'); | ||
{{/isUser}} | ||
const { createRouter } = require('../utils/router'); | ||
const {{camelCasePlural}} = require('../controllers/{{snakeCasePlural}}'); | ||
|
||
{{#isUser}} | ||
const serialize = {{camelCase}} => { | ||
const data = Object.assign({}, {{camelCase}}); | ||
{{#isUser}} | ||
delete data.password; | ||
{{/isUser}} | ||
return data; | ||
}; | ||
|
||
{{#isUser}} | ||
const template = { | ||
post: { | ||
'/signin': function *(req, res) { | ||
const { user, token } = yield {{camelCasePlural}}.signin(req.body.email, req.body.password); | ||
res.json({ token, user: serialize(user) }); | ||
}, | ||
'/signout': function *(req, res) { | ||
res.json(yield {{camelCasePlural}}.signout(req.body)); | ||
} | ||
} | ||
}; | ||
const customRouter = new Router() | ||
.post('/signin', function *(req, res) { | ||
const { user, token } = yield {{camelCasePlural}}.signin(req.body.email, req.body.password); | ||
res.json({ token, user: serialize(user) }); | ||
}) | ||
.post('/signout', function *(req, res) { | ||
res.json(yield {{camelCasePlural}}.signout(req.body)); | ||
}); | ||
|
||
const defaultRouter = createRouter({{camelCasePlural}}, serialize); | ||
customRouter.socket = defaultRouter.socket; | ||
|
||
customRouter.use(defaultRouter); | ||
|
||
module.exports = customRouter; | ||
{{/isUser}} | ||
|
||
module.exports = createRouter({{camelCasePlural}}, serialize{{#isUser}}, template{{/isUser}}); | ||
{{^isUser}} | ||
module.exports = createRouter({{camelCasePlural}}); | ||
{{/isUser}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,49 @@ | ||
const { nullToUndefined } = require('./helpers'); | ||
const { Router } = require('express'); | ||
const { merge } = require('lodash'); | ||
|
||
const defaultSerializer = record => Object.assign({}, record); | ||
|
||
exports.createRouter = (controller, serialize = defaultSerializer, template = {}) => { | ||
const router = merge({ | ||
get: { | ||
'': function *(req, res) { | ||
const list = yield controller.all(req.query); | ||
res.json(list.map(serialize)); | ||
}, | ||
'/:id': function *(req, res) { | ||
const record = yield controller.find(req.params.id); | ||
res.json(serialize(record)); | ||
} | ||
}, | ||
post: { | ||
'': function *(req, res) { | ||
const record = yield controller.create(req.body); | ||
res.status(201).json(serialize(record)); | ||
} | ||
}, | ||
put: { | ||
'/:id': function *(req, res) { | ||
const record = yield controller.replace(req.params.id, req.body); | ||
res.json(serialize(record)); | ||
} | ||
}, | ||
patch: { | ||
'/:id': function *(req, res) { | ||
const replaced = nullToUndefined(req.body); | ||
const record = yield controller.update(req.params.id, replaced); | ||
res.json(serialize(record)); | ||
} | ||
}, | ||
delete: { | ||
'/:id': function *(req, res) { | ||
const record = yield controller.delete(req.params.id); | ||
res.json(serialize(record)); | ||
} | ||
}, | ||
*socket(socket, req) { | ||
const { feed, count } = yield controller.watch(req.query); | ||
socket.emit('metadata', { count }); | ||
|
||
feed.listen((err, event, record) => { | ||
if (err) throw err; | ||
socket.emit(event, serialize(record)); | ||
}); | ||
|
||
return feed; | ||
} | ||
}, template); | ||
exports.createRouter = (controller, serialize = defaultSerializer) => { | ||
const router = new Router(); | ||
return objToRouter(router); | ||
}; | ||
router | ||
.get('', function *(req, res) { | ||
const list = yield controller.all(req.query); | ||
res.json(list.map(serialize)); | ||
}) | ||
.get('/:id', function *(req, res) { | ||
const record = yield controller.find(req.params.id); | ||
res.json(serialize(record)); | ||
}) | ||
.post('', function *(req, res) { | ||
const record = yield controller.create(req.body); | ||
res.status(201).json(serialize(record)); | ||
}) | ||
.put('/:id', function *(req, res) { | ||
const record = yield controller.replace(req.params.id, req.body); | ||
res.json(serialize(record)); | ||
}) | ||
.patch('/:id', function *(req, res) { | ||
const replaced = nullToUndefined(req.body); | ||
const record = yield controller.update(req.params.id, replaced); | ||
res.json(serialize(record)); | ||
}) | ||
.delete('/:id', function *(req, res) { | ||
const record = yield controller.delete(req.params.id); | ||
res.json(serialize(record)); | ||
}); | ||
|
||
function objToRouter(routerTemplate) { | ||
const router = new Router(); | ||
router.socket = function *(socket, req) { | ||
const { feed, count } = yield controller.watch(req.query); | ||
socket.emit('metadata', { count }); | ||
|
||
Object.keys(routerTemplate).forEach(method => { | ||
if (method === 'socket') return; | ||
Object.keys(routerTemplate[method]).forEach(route => { | ||
const handler = routerTemplate[method][route]; | ||
if (!handler) return; | ||
router[method](route, handler); | ||
feed.listen((err, event, record) => { | ||
if (err) throw err; | ||
socket.emit(event, serialize(record)); | ||
}); | ||
}); | ||
|
||
router.socket = routerTemplate.socket; | ||
return feed; | ||
}; | ||
|
||
return router; | ||
} | ||
}; |