config-req
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -1,6 +0,5 @@ | ||
# config-req | ||
![npm](https://img.shields.io/npm/v/config-req) | ||
[![CircleCI](https://circleci.com/gh/kevinccbsg/config-req/tree/master.svg?style=svg)](https://circleci.com/gh/kevinccbsg/config-req/tree/master) | ||
[![CircleCI](https://circleci.com/gh/BRIKEV/config-req/tree/master.svg?style=svg)](https://circleci.com/gh/BRIKEV/config-req/tree/master) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/kevinccbsg/config-req/badge.svg)](https://snyk.io/test/github/kevinccbsg/config-req) [![Greenkeeper badge](https://badges.greenkeeper.io/kevinccbsg/config-req.svg)](https://greenkeeper.io/) | ||
@@ -12,4 +11,7 @@ | ||
This modules gives you some axios request methods based on a config so that you don't need to worry about method and URL and how to handle their changes on each environments. | ||
This module allows to set up a programmatic HTTP client based on axios. To set it up, it just the url and the HTTP | ||
method per environment to be setup ¡et voilà!, a new and shiny HTTP client is ready to be used. | ||
Axios options can be found [here](https://axios-http.com/docs/instance); | ||
### Install package | ||
@@ -73,3 +75,3 @@ | ||
## Advance example | ||
## Customizer request parameters | ||
@@ -89,2 +91,7 @@ ```js | ||
}, | ||
withBasicAuth: { // This will affect each call to this endpoint | ||
url: 'http://localhost:5000/v1/account/:id/activate', | ||
method: 'get', | ||
auth: { password: 'pwd', username: 'nickname' }, | ||
} | ||
}, | ||
@@ -97,3 +104,3 @@ }; | ||
body: { example: 'example' }, // this is how to send body params | ||
params: { example: 'example' }, // this is how to send query params | ||
query: { example: 'example' }, // this is how to send query params | ||
headers: { Authorization: 'Bearer example' }, // this is how to send header params | ||
@@ -110,5 +117,5 @@ }) | ||
body: { example: 'example' }, // this is how to send body params | ||
params: { example: 'example' }, // this is how to send query params | ||
query: { example: 'example' }, // this is how to send query params | ||
headers: { Authorization: 'Bearer example' }, // this is how to send header params | ||
urlParams: { id: 'urlParam' }, | ||
params: { id: 'urlParam' }, | ||
}) | ||
@@ -118,7 +125,22 @@ .then(response => { | ||
}); | ||
// Basic auth | ||
api.advanced.withURLParams({ | ||
body: { example: 'example' }, // this is how to send body params | ||
query: { example: 'example' }, // this is how to send query params | ||
headers: { Authorization: 'Bearer example' }, // this is how to send header params | ||
params: { id: 'urlParam' }, // This is how to send path params | ||
auth: { password: 'pwd', username: 'nickname' }, // this is how you add basic auth for each request | ||
}) | ||
.then(response => { | ||
console.log(response); // Axios response | ||
}); | ||
```` | ||
## Use with Req complete object | ||
## Intercepting requests | ||
You can send a complete req object like the one express.js uses. *Note* for now, it needs to be extended with a flag for version 1.1.0. For version 2.0.0 this will not be needed. | ||
In some scenarios, it might be needed to have a fine-grained control on request or the responses. For example, to | ||
refresh a token when it is expired or to handle errors in a specific way. This can be achieved by using | ||
the [interceptors](https://axios-http.com/docs/interceptors) option provided by Axios. These interceptors can be set-up | ||
in the following way: | ||
@@ -128,49 +150,30 @@ ```js | ||
const options = { | ||
withURLParams: { | ||
url: 'http://localhost:5000/v1/account/:id/activate', | ||
method: 'get', | ||
// Your env configuration | ||
const config = { | ||
activateAccount: { | ||
url: 'http://localhost:5000/v1/account/activate', | ||
method: 'post', | ||
}, | ||
}; | ||
const api = request(options); | ||
const reqObject = { | ||
body: { example: 'example' }, // this is how to send body params | ||
params: { example: 'example' }, // this is how to send query params | ||
headers: { Authorization: 'Bearer example' }, // this is how to send header params | ||
}; | ||
// V1 version | ||
api.withURLParams({ | ||
...reqObject, | ||
fullRequest: true, | ||
}) | ||
.then(response => { | ||
console.log(response); // Axios response | ||
}); | ||
const api = request(config, { | ||
interceptors: { | ||
request: (config) => { | ||
// Do something before request is sent | ||
return config; | ||
}, | ||
response: { | ||
success: (response) => { | ||
// Do something with response data | ||
return response; | ||
}, | ||
error: (error) => { | ||
// Do something with response error | ||
return Promise.reject(error); | ||
} | ||
} | ||
} | ||
}); | ||
// V2 version | ||
api.withURLParams(reqObject) | ||
.then(response => { | ||
console.log(response); // Axios response | ||
}); | ||
```` | ||
This `fullRequest: true` needs to be added to support in V1 a mix of query params and URL params from a req Object request with the old behaviour. | ||
If you want to change it from V2 you must change this cases: | ||
```js | ||
// if you mix params and urlParams in V2 it won't work as expected | ||
{ | ||
params: { example: 'example' }, | ||
urlParams: { id: 'urlParam' }, | ||
body: { example: 'this is an example' } | ||
} | ||
// just remove it from your code to work with V2 | ||
{ | ||
params: { example: 'example' }, | ||
urlParams: { id: 'urlParam' } | ||
} | ||
``` | ||
@@ -177,0 +180,0 @@ |
@@ -38,5 +38,11 @@ const axios = require('axios'); | ||
const options = (routesConfig, instanceConfig) => { | ||
const options = (routesConfig, instanceOptions = {}) => { | ||
const { interceptors = { response: { } }, ...instanceConfig } = instanceOptions; | ||
debug('CREATING INSTANCE USING CONFIG', routesConfig); | ||
const instance = createInstance(instanceConfig); | ||
debug('SETTING UP INTERCEPTORS FROM CONFIG', interceptors); | ||
if (interceptors) { | ||
instance.interceptors.request.use(interceptors.request); | ||
instance.interceptors.response.use(interceptors.response.success, interceptors.response.error); | ||
} | ||
debug('INSTANCE REQUEST CONFIG', instanceConfig); | ||
@@ -43,0 +49,0 @@ return requestMap(instance, routesConfig); |
{ | ||
"name": "config-req", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Axios wrapper based on a config file", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,1 @@ | ||
# config-req | ||
@@ -12,4 +11,7 @@ | ||
This modules gives you some axios request methods based on a config so that you don't need to worry about method and URL and how to handle their changes on each environments. | ||
This module allows to set up a programmatic HTTP client based on axios. To set it up, it just the url and the HTTP | ||
method per environment to be setup ¡et voilà!, a new and shiny HTTP client is ready to be used. | ||
Axios options can be found [here](https://axios-http.com/docs/instance); | ||
### Install package | ||
@@ -73,3 +75,3 @@ | ||
## Advance example | ||
## Customizer request parameters | ||
@@ -126,3 +128,3 @@ ```js | ||
headers: { Authorization: 'Bearer example' }, // this is how to send header params | ||
params: { id: 'urlParam' }, | ||
params: { id: 'urlParam' }, // This is how to send path params | ||
auth: { password: 'pwd', username: 'nickname' }, // this is how you add basic auth for each request | ||
@@ -135,2 +137,42 @@ }) | ||
## Intercepting requests | ||
In some scenarios, it might be needed to have a fine-grained control on request or the responses. For example, to | ||
refresh a token when it is expired or to handle errors in a specific way. This can be achieved by using | ||
the [interceptors](https://axios-http.com/docs/interceptors) option provided by Axios. These interceptors can be set-up | ||
in the following way: | ||
```js | ||
const request = require('config-req'); | ||
// Your env configuration | ||
const config = { | ||
activateAccount: { | ||
url: 'http://localhost:5000/v1/account/activate', | ||
method: 'post', | ||
}, | ||
}; | ||
const api = request(config, { | ||
interceptors: { | ||
request: (config) => { | ||
// Do something before request is sent | ||
return config; | ||
}, | ||
response: { | ||
success: (response) => { | ||
// Do something with response data | ||
return response; | ||
}, | ||
error: (error) => { | ||
// Do something with response error | ||
return Promise.reject(error); | ||
} | ||
} | ||
} | ||
}); | ||
``` | ||
## How to contribute | ||
@@ -137,0 +179,0 @@ |
Sorry, the diff of this file is not supported yet
23536
15
290
191