sails-hook-cb-async-controller
v1.0.3
Published
This hook helps you write controllers using generator functions or async functions
Downloads
4
Maintainers
Readme
sails-hook-cb-async-controller
Installation
npm i -S sails-hook-cb-async-controller
Usage
UserController.js - See this example
module.exports = {
// Will crash app due to headers sent twice error
callbackCrash(req, res) {
setTimeout(() => res.ok({value: Math.round(Math.random() * 10)}), 200);
},
// Set response.isHandled on to not send a response twice
callbackNotCrash(req, res) {
res.isHandled = true;
setTimeout(() => res.ok({value: Math.round(Math.random() * 10)}), 200);
},
async async(req, res) {
const users = await User.find();
return {value: users.length};
},
async asyncHandled(req, res) {
const users = await User.find();
return res.ok({value: users.length});
},
async throwAsync(req, res) {
const users = await User.find();
throw {message: 'An error', type: 'warn'};
},
* generator(req, res) {
const users = yield User.find();
return {value: users.length};
},
* generatorHandled(req, res) {
const users = yield User.find();
return res.ok({value: users.length});
},
* throwGenerator(req, res) {
const users = yield User.find();
throw {errorCode: 1001};
},
promise(req, res) {
return User.find()
.then((users) => { return {value: users.length}; });
},
promiseHandled(req, res) {
return User.find()
.then((users) => { return res.ok({value: users.length}); });
},
throwPromise(req, res) {
return User.find()
.then((users) => { throw Error('Error rejected'); });
},
sync(req, res) {
return {value: Math.round(Math.random() * 100)};
},
syncHandled(req, res) {
return res.ok({value: Math.round(Math.random() * 10)});
},
throwSync(req, res) {
throw {errorCode: 1002, message: 'Error thrown', logMessage: '[throwSync] Bad error'};
}
};
Tests
Add users
curl -X POST 'http://localhost:11111/users'-H 'content-type: application/x-www-form-urlencoded' -d 'name=Ren&birthyear=2005' curl -X POST 'http://localhost:11111/users' -H 'content-type: application/x-www-form-urlencoded' -d 'name=Sou&birthyear=2002'
Test controllers with async functions
curl -X GET http://localhost:11111/users/async curl -X GET http://localhost:11111/users/asyncHandled curl -X GET http://localhost:11111/users/throwAsync
Test controllers with generator functions
curl -X GET http://localhost:11111/users/generator curl -X GET http://localhost:11111/users/generatorHandled curl -X GET http://localhost:11111/users/throwGenerator
Test controllers with functions returning promise
curl -X GET http://localhost:11111/users/promise curl -X GET http://localhost:11111/users/promiseHandled curl -X GET http://localhost:11111/users/throwPromise
Test controllers with normal functions
curl -X GET http://localhost:11111/users/sync curl -X GET http://localhost:11111/users/syncHandled curl -X GET http://localhost:11111/users/throwSync
Test controllers with callbacks
Caution: This request will crash the app
curl -X GET http://localhost:11111/users/callbackCrash
curl -X GET http://localhost:11111/users/callbackNotCrash