-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement "bindAction" in router, and registerActionMiddleware
- Loading branch information
Showing
4 changed files
with
112 additions
and
30 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
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Sails.prototype.registerActionMiddleware() | ||
* | ||
* Register an action middleware with Sails. | ||
* | ||
* Action middleware runs before the action or actions | ||
* specified by the middleware key. | ||
* | ||
* @param {fn} middleware The function to register | ||
* @param {string} actions The identity of the action or actions that this middleware should apply to. | ||
* Use * at the end for a wildcard; e.g. `user.*` will apply to any actions | ||
* whose identity begins with `user.`. | ||
* | ||
* | ||
* @api public | ||
*/ | ||
module.exports = function registerAction(middleware, actions) { | ||
|
||
// TODO -- update machine-as-action with a response type that calls `next`, | ||
// so machine defs can be registered as middleware? | ||
if (_.isFunction(middleware)) { | ||
throw new Error('Attempted to register middleware for `' + actions + '` but the provided middleware was not a function (it was a ' + typeof(middleware) + ').'); | ||
} | ||
|
||
// Get or create the array for this middleware key. | ||
var middlewareForKey = this._actionMiddleware[actions] || []; | ||
|
||
// Add this middleware to the array. | ||
middlewareForKey.push(middleware); | ||
|
||
// Assign the array back to the dictionary. | ||
this._actionMiddleware[actions] = middlewareForKey; | ||
|
||
}; |
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