1.0.5 • Published 1 year ago

axios-base-request v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

axiosBaseRequest

NPM version Continuous Integration Test coverage npm download

http request library based axios for the browser.

Installing

Using npm:

npm install axios-base-request --save

Using yarn:

yarn add axios-base-request 

Usage

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>

API

axiosBaseRequest.use(fn)

Use middleware function

axiosBaseRequest.request(config)

Send http request.

The config param is same as axios https://github.com/axios/axios#request-config

Example

// 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);
})

License

MIT

1.0.5

1 year ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago