Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/spoonx/request-helpers
A collection of request helpers. Usable with express js (and consequently, sails.js).
https://github.com/spoonx/request-helpers
Last synced: about 1 month ago
JSON representation
A collection of request helpers. Usable with express js (and consequently, sails.js).
- Host: GitHub
- URL: https://github.com/spoonx/request-helpers
- Owner: SpoonX
- License: mit
- Created: 2014-08-30T15:01:36.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-03-24T12:36:13.000Z (almost 9 years ago)
- Last Synced: 2024-11-16T13:06:26.029Z (about 1 month ago)
- Language: JavaScript
- Size: 282 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Request helpers
A collection of request helpers. Usable with express js (and consequently, sails.js).## Installation
`npm install --save request-helpers`## Methods
Currently only 1.### secureParameters (params, req|object[, forceCollect = false])
Allows you to create a `Parameters` instance from the params sent with the request.
This method allows you to easily populate the parameters, making sure all required params were sent.**Note:** This method also supports the use of a simple object in stead of using `req`.
This can be useful when you wish to secureParameters from parseCriteria (sails.js).**Example:**
```javascript
var paramBlueprint = [
'username', // Required
'password', // Required.
{param: 'name', required: false}, // Optional. Default: null
{param: 'nickname', default: 'anon'}, // Optional. Default: anon
{param: 'enable', cast: 'boolean'} // Cast value to boolean
];var params = helpers.secureParameters(paramBlueprint, req);
params.isValid(); // true
params.asObject(); // Returns POJO of parameters
params.get('key'); // Returns value for parameter `key`
params.getMissing(); // Returns an array of missing parameters
params.getMissingParameter(); // Returns the first missing parameter.// With forceCollect (third argument) you tell secureParameters to not stop collecting when it finds a missing parameter.
Defaults to false.
params = helpers.secureParameters(paramBlueprint, req);
```## Contributing
Go for it. Submit a PR, with the tests, and watch the magic.