2.13.2 • Published 8 years ago

async-fetch-helper v2.13.2

Weekly downloads
5
License
MIT
Repository
github
Last release
8 years ago

Async Fetch Helper 2.13.2

AsyncFetchHelper is wrapper for async that using unirest or soap to fetch data with less code.

Install

$ npm install async-fetch-helper --save

Usage

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]
})

AsyncFetchHelper initial - setting

  • settings - Object;

The options we have:

  • apiUrl - String; If your api have the same domain,your can setting this param
  • connectionPool - 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}
	}
};

AsyncFetchHelper initial - new function

var AsyncFetchHelper = require('async-fetch-helper');
var initSettings = {something : "setting at here, please view 'AsyncFetchHelper initial - setting'"};
var asyncFetchHelper = new AsyncFetchHelper(initSettings);

AsyncFetchHelper prototype Api

need(apiTypeList)

  • apiTypeList - ArrayList; api type (rest, soap, etc...)
need(['soap', 'rest'])

The method will return constructor of AsyncFetchHelper at follow.

then(callback)

  • 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()];
})

end(callback)

  • callback - Function; The function will get all api response using array
end(function(result){
	console.log(results);
	// [soapResult, restResult]
})

AsyncFetchHelper static Api

AsyncFetchHelper.register(apiType, handler)

  • apiType - String; Naming for handler
  • handler - Function; Other method for call api defined by user

    	This method will add your handler to method options of need, so you can call your handler by `need(['youApiTypeName'])`

AsyncFetchHelper.setConnectionPool(settings)

  • 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}
}

AsyncFetchHelper methods by function need()

rest(method, url, params, returnKey, restCallback)

  • method - String; Request type (GET, PUT, POST, etc...)
  • url - String; api url
  • params - Object; Request params, if request type is get or delete, it will be query uri, or it will be request body
  • headers - Object; Optional; Request headers, ex: {"Content-Type":"application/x-www-form-urlencoded"}
  • returnKey - String; Optional; Return value of key at first level in Result
  • restCallback - Function; Optional; This function will get current request result, and it is like function then that must return an array that about the api request settings

soap(url, soapCallback)

  • url - String; api url
  • soapCallback - Function; This function will get soap client, and method wrapper

    	### method wrapper(params, returnKey, methodCallback)
    
    	- `params` - _Object_; _Optional_; Request params
    	- `returnKey` - _String_; _Optional_; Return value of key at first level in Result
    	- `restCallback` - _Function_; _Optional_; This function will get current request result, and it is like function `then` that must return an array that about the api request settings
2.13.2

8 years ago

2.13.1

8 years ago

2.13.0

8 years ago

2.12.3

8 years ago

2.12.2

8 years ago

2.12.1

9 years ago

2.12.0

9 years ago

2.11.1

9 years ago

2.11.0

9 years ago

2.10.1

9 years ago

2.10.0

9 years ago

2.9.0

9 years ago

2.8.10

9 years ago

2.8.9

9 years ago

2.8.8

9 years ago

2.8.7

9 years ago

2.8.6

9 years ago

2.8.5

9 years ago

2.8.4

9 years ago

2.8.3

9 years ago

2.8.2

9 years ago

2.8.1

9 years ago

2.8.0

9 years ago

2.7.1

9 years ago

2.7.0

9 years ago

2.6.0

9 years ago

2.5.0

9 years ago

2.2.3

9 years ago

2.2.2

9 years ago

2.2.0

9 years ago

2.1.3

9 years ago

2.1.2

9 years ago

2.1.1

9 years ago

2.1.0

9 years ago

2.0.3

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago