AsyncFetchHelper is wrapper for async that using unirest or soap to fetch data with less code.
$ npm install async-fetch-helper --save
var AsyncFetchHelper = require('async-fetch-helper');
var initSettings = {};
var asyncFetchHelper = new AsyncFetchHelper(initSettings);
asyncFetchHelper.need(['soap', 'rest']).then(function(soap, rest){
return [
soap('http://soapApi.com', function(client){
client.method(paramObj);
}),
rest('get', 'http://restApi.com', params)
];
}).end(function(results){
console.log(results);
// [soapResult, restResult]
})
settings
- Object;
The options we have:
apiUrl
- String; If your api have the same domain,your can setting this paramconnectionPool
- Object; Set http agent object here
example : if you have two apis that url is [rest] http://api.com.tw/api1
and [soap] http://api.com.tw/api2
// settings
var initSettings = {
"apiUrl" : "http://api.com.tw",
"connectionPool": {
"rest" : { "keepAlive": true, "keepAliveMsecs": 600000 , "maxSockets":5, "maxFreeSockets": 5},
"soap" : { "keepAlive": true, "keepAliveMsecs": 600000 , "maxSockets":5, "maxFreeSockets": 5}
}
};
var AsyncFetchHelper = require('async-fetch-helper');
var initSettings = {something : "setting at here, please view 'AsyncFetchHelper initial - setting'"};
var asyncFetchHelper = new AsyncFetchHelper(initSettings);
apiTypeList
- ArrayList; api type (rest, soap, etc...)
need(['soap', 'rest'])
callback
- Function; The function "then" will apply the api type you need, and it must return an array that about the api request settings
then(function(rest, soap){
return [rest(),soap()];
})
callback
- Function; The function will get all api response using array
end(function(result){
console.log(results);
// [soapResult, restResult]
})
-
apiType
- String; Naming for handler -
handler
- Function; Other method for call api defined by userThis method will add your handler to method options of need, so you can call your handler by
need(['youApiTypeName'])
settings
- Object; Set http agent object here
{
rest : { keepAlive: true, keepAliveMsecs: 600000 , maxSockets:5, maxFreeSockets: 5},
soap : { keepAlive: true, keepAliveMsecs: 600000 , maxSockets:5, maxFreeSockets: 5}
}
method
- String; Request type (GET, PUT, POST, etc...)url
- String; api urlparams
- Object; Request params, if request type is get or delete, it will be query uri, or it will be request bodyheaders
- Object; Optional; Request headers, ex: {"Content-Type":"application/x-www-form-urlencoded"}returnKey
- String; Optional; Return value of key at first level in ResultrestCallback
- Function; Optional; This function will get current request result, and it is like functionthen
that must return an array that about the api request settings
-
url
- String; api url -
soapCallback
- Function; This function will get soap client, andmethod wrapper
params
- Object; Optional; Request paramsreturnKey
- String; Optional; Return value of key at first level in ResultrestCallback
- Function; Optional; This function will get current request result, and it is like functionthen
that must return an array that about the api request settings