1.0.3 • Published 7 years ago

sails-hook-cb-async-controller v1.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

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

  1. 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'
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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

References

  1. Hooks | Sails.js Documentation
  2. node.js - Using ES6 Generator functions with SailsJS - Stack Overflow
  3. ICodeMyOwnLife/learn-sails-async-await
1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.1

7 years ago