Skip to content

Commit

Permalink
Use nested routers approach
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbanham committed Sep 16, 2016
1 parent 84ac833 commit 6ab752f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 78 deletions.
37 changes: 22 additions & 15 deletions resource/src/routes/route.mustache
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}}
100 changes: 37 additions & 63 deletions resource/src/utils/router.mustache
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;
}
};

0 comments on commit 6ab752f

Please sign in to comment.