http request library based axios for the browser.
npm install axios-base-request --save
yarn add axios-base-request
Once the package is installed, you can import
the library using import:
import axiosBaseRequest from 'axios-base-request';
or you can use script tag:
<script src="axios.base.request.js"></script>
Use middleware function
Send http request.
The config
param is same
as axios
https://github.com/axios/axios#request-config
// process request data
function signMiddleware(context, next) {
const { options } = context;
options.headers = { 'x-sign': '123456' };
return next();
}
// process response data
function transformResponseMiddleware(context, next) {
return next()
.then(() => {
const { response } = context;
const data = response && response.data;
const status = response && response.status;
response.data = {
status,
data,
};
return context;
});
}
// use middleware
axiosBaseRequest.use(signMiddleware);
axiosBaseRequest.use(transformResponseMiddleware);
// request
axiosBaseRequest.request({
url: '/api/users',
method: 'get'
}).then((response) => {
console.log(response)
}).catch((error) => {
console.log(error);
})
MIT