async-helper
Advanced tools
Comparing version 0.5.0 to 1.0.0
@@ -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" | ||
} | ||
} |
239
readme.md
@@ -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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
7677
5
2
169
3
+ Addedasync@^1.5
+ Addedexpress@^4
+ Addedaccepts@1.3.8(transitive)
+ Addedarray-flatten@1.1.1(transitive)
+ Addedasync@1.5.2(transitive)
+ Addedbody-parser@1.20.3(transitive)
+ Addedbytes@3.1.2(transitive)
+ Addedcall-bind-apply-helpers@1.0.1(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addedcontent-disposition@0.5.4(transitive)
+ Addedcontent-type@1.0.5(transitive)
+ Addedcookie@0.7.1(transitive)
+ Addedcookie-signature@1.0.6(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addeddepd@2.0.0(transitive)
+ Addeddestroy@1.2.0(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedee-first@1.1.1(transitive)
+ Addedencodeurl@1.0.22.0.0(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.0.0(transitive)
+ Addedescape-html@1.0.3(transitive)
+ Addedetag@1.8.1(transitive)
+ Addedexpress@4.21.2(transitive)
+ Addedfinalhandler@1.3.1(transitive)
+ Addedforwarded@0.2.0(transitive)
+ Addedfresh@0.5.2(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.7(transitive)
+ Addedget-proto@1.0.1(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedipaddr.js@1.9.1(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedmedia-typer@0.3.0(transitive)
+ Addedmerge-descriptors@1.0.3(transitive)
+ Addedmethods@1.1.2(transitive)
+ Addedmime@1.6.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedms@2.0.02.1.3(transitive)
+ Addednegotiator@0.6.3(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedon-finished@2.4.1(transitive)
+ Addedparseurl@1.3.3(transitive)
+ Addedpath-to-regexp@0.1.12(transitive)
+ Addedproxy-addr@2.0.7(transitive)
+ Addedqs@6.13.0(transitive)
+ Addedrange-parser@1.2.1(transitive)
+ Addedraw-body@2.5.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsend@0.19.0(transitive)
+ Addedserve-static@1.16.2(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedside-channel@1.1.0(transitive)
+ Addedside-channel-list@1.0.0(transitive)
+ Addedside-channel-map@1.0.1(transitive)
+ Addedside-channel-weakmap@1.0.2(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedtoidentifier@1.0.1(transitive)
+ Addedtype-is@1.6.18(transitive)
+ Addedunpipe@1.0.0(transitive)
+ Addedutils-merge@1.0.1(transitive)
+ Addedvary@1.1.2(transitive)