commons-generic-http
v1.0.10
Published
1. Axios
Downloads
155
Readme
HTTP Common module for http calls
Supported modules as of this version:
- Axios
Quick Start
Step 1: Set the Config (Skip if you are ok with default config)
// During the app initialization step (usually before anyapi call) call setConfig method of commons-generic-http
// If you want details for few apis, call this method again
const httpRequest = require('commons-generic-http')
httpRequest.setConfig({
retry: 3,
logAxiosDetails: false,
retryAfter: 10,
timeout: 15000,
logLevel: "info",
})
The default config is as below
| Config Key | Default Value | Other Examples | Description | | |-----------------|-------------------|---------------------------------------|-------------------------------------------------------------------------|---| | retry | 3 | any number | The number of times you want to repeat the api call in case of failure | | | logAxiosDetails | false | true | false | If set to true it will log axios in more details(for debugging purpose) | | | retryAfter | 10 | any number | Number of seconds to wait before retrying the failed api call | | | logLevel | log | debug | log | warn | error | none | Level of log you need to set | | | timeout | 15 | any number | The number of Milliseconds you need to wait for the server to respond back | |
Step 2: Call APIs anywhere in your app
const token = 'exyaukv93976kfds87s9'
const httpRequest = require('commons-generic-http')
const response = await httpRequest.httpCall({
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
},
url: 'http://api.some-endpoint.com',
proxy: false, // Pass in false in case of not using proxy
data: {}// data for POST method
})
// Required fields: url, method, proxy
httpCall method will accept an object of type AxiosRequestConfig
AxiosRequestConfig: This is supported by Axios official package so look for more details in their package here