Skip to content

kvzhuang/async-fetch-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Async Fetch Helper

[![License][npm-license]][license-url] [![Downloads][npm-downloads]][npm-url]

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');

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 Api

AsyncFetchHelper.need(apiTypeList)

  • apiTypeList - ArrayList; api type (rest, soap, etc...)

    The method will return constructor of AsyncFetchHelper.

    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.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.setting(settings)

  • settings - Object;

The options we have:

  • apiUrl - String; If your api have the same domain,your can setting this param

example : if you have two apis that url is [rest] http://api.com.tw/api1 and [soap] http://api.com.tw/api2

// settings
settings = {
	apiUrl : 'http://api.com.tw'
};

// call api
rest('get', '/api1', params),soap('/api2', (client)=>{})

AsyncFetchHelper method

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

  • method - String; Request type (GET, PUT, POST, etc...)
  • url - String; api url
  • params - Object; Optional; Request params, if request type is get or delete, it will be query uri, or it will be request body
  • 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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published