api-client-wrapper
===
An easy to use out of the box API wrapper that use axios in its core
This package was designed to wrap up the most common features of an api client implementation, in an effort to have a quick and stable tool for each project.
The present features in the package are:
-
Homogeneous response: Each request is a promise that resolve with an homogeneous response object: {success:Boolean, attempts:int, data:Object, info:string, error:Error}
. For more details refer to the Response Schema section on this readme.
-
Concurrent requests: All the requests could be made simultaneously. By default the limit is configured to 5 concurrent requests, but it could be changed at api.simultaneousCalls
-
Timeout and retry: In the case of a timeout response a request could be configured to try again with: api.maxAttemptsPerCall
the default value is 1. Each attempt will be executed transparently and it will be just one response so the code for a request with a single attempt will be the same as for a request with multiple attempts.
-
Masive requests: There is out of the box support for bulk calls that allows multiple requests in a single call. For more information refer to the How To use it section on this readme.
-
Vuex integration: The package came with Vuex store integration for state monitoring (it will work with any Flux implementation but further testing is needed). The store instance should be provided using: api.setStore(store_instance)
and this will result in the module APIwrapper
being registered with the next properties:
- Working: Indicating that is at least one request executing
store.state.APIwrapper.working
- Uploading: Indicating that is at least one request executing with a method different from
GET
store.state.APIwrapper.uploading
- Downloading: Indicating that is at least one request executing with a method equal to
GET
store.state.APIwrapper.downloading
- Request Count: Indicates the amount of requests being managed, executing requests + waiting for execution requests
store.state.APIwrapper.request_count
- Execute Count: Indicates the amount of requests being executed in a concurrent manner
store.state.APIwrapper.executing_count
Installing
npm install api-client-wrapper
How to use it
Performing a GET
request
import api from 'api-client-wrapper'
let result = await api.get('path');
api.get('path').then(result=>{});
Performing a POST
request
import api from 'api-client-wrapper'
let result = await api.post('path', {data});
api.post('path', {data}).then(result=>{});
The supported methods are:
get(path='', conf = {})
post(path='', data = {}, conf = {})
patch(path='', data = {}, conf = {})
put(path='', data = {}, conf = {})
delete(path='', conf = {})
call({request configuration})
Note that each method has a final argument that is a custom configuration for the request, this configuration takes precedence over the global configuration. For the supported properties please refer to the Request Configuration section.
Bulk calls
Each method has a bulk counterpart that allows for bulk calls
import api from 'api-client-wrapper'
let result = await api.bulkGet(['paths' or {configs}], continueWithFailure:Boolean, onProgress)
let result = await api.bulkPost(['paths' or {configs}], continueWithFailure:Boolean, onProgress)
let result = await api.bulkPatch(['paths' or {configs}], continueWithFailure:Boolean, onProgress)
let result = await api.bulkPut(['paths' or {configs}], continueWithFailure:Boolean, onProgress)
let result = await api.bulkDelete(['paths' or {configs}], continueWithFailure:Boolean, onProgress)
let result = await api.bulkCall([{configs}], continueWithFailure:Boolean, onProgress)
Params:
- [paths or configs]:[]-> An array containing the paths for each request or an array of request configuration objects (described next).
- continueWithFailure:Boolean-> Optional with the default to false.
false
The request will be considered failed when any of it subrequests fail and it will stop any further execution (there could be some sub-requests that never get executed if some other request failed before). true
All the requests are executed, it doesn´t matter if some of them failed. - onProgress:Function(progress:Number)-> Is an optional callback that receives progress between [0-1], the progress is computed with the next formula: (request-completed / total-amount-of-request-in-the-bulkCall)
Request Configuration
A configuration object for each request (with precedence over any global configuration) with the next scheme:
{
url:String,
method:String,
params:{},
data:{},
alias:String,
attempts:1,
timeout:10000
...
}
It is possible to add any property supported by axios (like headers or encoding).
Response Scheme
For any individual request the response schema is:
{
alias:String,
success:Boolean,
attempts:0,
data:{}
info:"",
error:null,
...
}
In the case of a bulk call the response data is an Array containing the response for each request (in the same order that the requests where passed)
let result = await anyBulkCall([{configs}]
result.data.forEach(response=>{
console.log (response)
})
let result = await anyBulkCall([{alias:'a',...},{alias:'b',...}]
console.log(result.data.a)
console.log(result.data.b)
Global Configuration
Configuration of the overall behaviour for the package
import api from 'api-client-wrapper'
api.baseURL = '';
api.maxAttemptsPerCall = 1;
api.simultaneousCalls = 5;
api.timeout = 10000;
api.setContentType(type = 'application/json')
api.setAuthorization(token, type = 'Bearer')
api.setHeader(name: string, value:string)
api.setStore(vuex_instance)