Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-helper

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-helper - npm Package Compare versions

Comparing version 0.5.0 to 1.0.0

.eslintrc.yml

7

async-helper.js

@@ -61,4 +61,3 @@ var _ = require('lodash');

*/
exports.callback = function(err, response) {
console.log(response);
var callback = function(err, response) {
var finalResponse = {};

@@ -71,3 +70,3 @@ finalResponse.code = 200;

if (_.isArray(response[i])) {
response[i] = this.callback(err, response[i]);
response[i] = callback(err, response[i]);
}

@@ -84,1 +83,3 @@ if (err && response[i].code) {

};
module.exports = callback;
{
"name": "async-helper",
"version": "0.5.0",
"version": "1.0.0",
"description": "This package will read an array of async function responses and create a final, single response with error or success to Express.",

@@ -21,4 +21,6 @@ "main": "async-helper.js",

"dependencies": {
"lodash": "^4.0"
"lodash": "^4.0",
"async": "^1.5",
"express": "^4"
}
}

@@ -1,81 +0,168 @@

var _ = require('lodash');
/**
* Callback response. It will read reach sequence of async function responses
* and create a final, single response with error or success.
* Escha function should set the err variable to true if a error occoured so
* this function will use it's response to create the HTML code and set the
* object that will be used by Express to send the response
* You should get this function response with Express and verify for error:
*
* response = asyncHelper.callback(err, response);
* res.status(response.code).send(response);
*
* @param {boolean} err Each async must set this argument to true
* if an error occoured
* @param {array} response Array with objects and arrays for each function
* executed using async
* Array example:
* [ // This array was generated in sequential functions
* {
* code: 400,
* errors: []
* },
* [ // This array was generate in parallell as the last sequential function
* {},
* {
* 'en-US': {
* mobletLabel: 'Fidelity Card',
* mobletHint: 'Create a fidelity card to your customers'
* },
* 'pt-BR': {
* mobletLabel: 'Cartão de Fidelidade',
* mobletHint: 'Crie um cartão de fidelidade para seus clientes'
* },
* {}
* ]
* ]
* @return {object} If any error occoured during the async execution,
* the response will have an HTML error code and an error. If no error occoured,
* the response will have a 200 (success) code and the messages given by each
* function
*
* Error example:
* {
* code: 405,
* error: 'Moblet already exists. Perform PUT to update'
* }
* Success example:
* {
* code: 200,
* errors: [],
* 'en-US': {
* mobletLabel: 'Fidelity Card',
* mobletHint: 'Create a fidelity card to your customers'
* },
* 'pt-BR': {
* mobletLabel: 'Cartão de Fidelidade',
* mobletHint: 'Crie um cartão de fidelidade para seus clientes'
* }
* }
# async-helper
This helper gets an array of _[async](https://www.npmjs.com/package/async)_ responses and create a final, single response to _[express](https://www.npmjs.com/package/express)_.
Each function in _async_ should set the err variable to true if an error occoured so async-helper will use it's response to create the HTML code and create the object that will be used by Express to send the response.
The default HTML code, if no error occoured is succes (200).
## Install
In your project root folder, run
```
$ npm install --save async-helper
```
## Usage
You should call async-helper after the execution of your async functions, for instance:
```javascript
var asyncHelper = require('async-helper');
/*
* START SERIAL FUNCTIONS
*/
exports.callback = function(err, response) {
console.log(response);
var finalResponse = {};
finalResponse.code = 200;
async.series([
// Validate arguments
function(callback) {
args.validate(
mobletId, gitUrl, mobletName, mobletVersion,
gitCheckout, function(err, response) {
callback(err, response);
});
},
// GIT Clone
function(callback) {
git.clone(
mobletId, gitUrl, gitCheckout,
mobletVersion, function(err, response) {
callback(err, response);
});
},
/*
* START PARALLELL FUNCTIONS
*/
function(callback) {
async.parallel([
// Test Moblet with Jasmine
function(callback) {
moblet.runTests(mobletId, function(err, response) {
callback(err, response);
});
},
// Get Moblet definition
function(callback) {
moblet.getDefinitions(mobletId, function(err, response) {
callback(err, response);
});
}
],
// Parallel callback function
function(err, response) {
callback(err, response);
});
}
],
function(err, response) {
/***************************************************************************
* ASYNC-HELPER CALL
***************************************************************************/
var expressResponse = asyncHelper(err, response);
/**************************************************************************/
res.status(expressResponse.code)
.send(expressResponse);
});
};
```
## Expected arguments
for (var i in response) {
// Iterate the parallel responses
if (!_.isEmpty(response[i])) {
if (_.isArray(response[i])) {
response[i] = this.callback(err, response[i]);
}
if (err && response[i].code) {
finalResponse = response[i];
finalResponse.code = response[i].code;
} else {
finalResponse = Object.assign(finalResponse, response[i]);
}
Async-helper expects a **boolean** (err) and an **array** (response).
As this function is called after the execution of _async_, the boolean (err) should be set after all functions and be set to false if any of the functions had an error.
The array (response) is the array generated by the async execution.
In the functions executed by async, if some error occour, you should set a "code" in the response object sent to the callback.
Example:
```javascript
module.exports = {
validate: function(gitCheckout, callback) {
var response = {};
var paramsMissing = [];
var err = false;
if (!gitCheckout) {
err = true;
paramsMissing.push('missing GIT checkout hash');
}
if (err === true) {
/*************************************************************************
* SET THE HTML ERROR CODE IN THE 'code' element
*************************************************************************/
response.code = 400;
/*************************************************************************
* SET OPTIONAL RESPONSES
*************************************************************************/
response.errors = paramsMissing;
}
callback(err, response);
}
return finalResponse;
};
```
After all the functions executions, you should get an array like this one:
```javascript
[ // This array was generated in sequential functions
{
code: 400,
errors: ['missing GIT checkout hash']
},
{},
{},
[ // This array was generate in parallell as the last sequential function
{},
{
'en-US': {
mobletLabel: 'Fidelity Card',
mobletHint: 'Create a fidelity card to your customers'
},
'pt-BR': {
mobletLabel: 'Cartão de Fidelidade',
mobletHint: 'Crie um cartão de fidelidade para seus clientes'
},
{}
]
]
```
## Function return
If some error occoured, you should get an object like this:
```javascript
{
code: 405,
error: 'Moblet already exists. Perform PUT to update'
}
```
In a success, something like this:
```javascript
Success example:
{
code: 200,
errors: [],
'en-US': {
mobletLabel: 'Fidelity Card',
mobletHint: 'Create a fidelity card to your customers'
},
'pt-BR': {
mobletLabel: 'Cartão de Fidelidade',
mobletHint: 'Crie um cartão de fidelidade para seus clientes'
}
}
```

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc