sails-hook-responders
This is a installable sailsjs hook that attaches policy like methods, called responders, to controller actions.
Installation
npm install sails-hook-responders --save
Usage
api/responders
, with the signature
Create your responders, in /** * Create users API token and add it to the response * * @param * @param * @param * @param * @param */module { // Do something // Add any data you want sent back to the user to data object // For example, // data.response = 'Hi, this message was generated in a responder'; // Set the type of response you want to be used // options.method = 'ok'; // options is optional. By default it is set to `serverError` if there was an error, // else to `json` // If there is an error, call next with the error // next(err); var token; // Generate token datatoken = token; ;};
config/responders.js
Map responders to controller actions in /** * Mapping responders to a controller action * @type */moduleexportsresponders = UserController: // responders should be put in an array, in the order they are meant to be run create: 'generateApiToken' 'sendWelcomeEmail' // though if you only have one responder for an action, you can just use a string delete: 'sendGoodbyeEmail' ... ...;
Return the responder in your controller action
A method, respond
, is attached to the res
object. It runs all the responders attached to a controller action
and sends the response back to the user. If no responder has been attached, it will simply return res.ok
/** * User controller */ /** * The user controller * @type */moduleexports = { // create user var data = id: userid name: username ; // options is optional, and so is data return res; } { // delete the user var data = id: deletedUserid name: deletedUsername message: 'Sorry to see you go ' + deletedUsername ; // You can pass a custom callback to `res.respond`, which will be called after the responders have been run return res; } ... ...;
This project is licensed under the Apache 2.0 License.
For contributing, please check the contributing guidelines.
Made with ❤️ by Postman