1.1.0 • Published 1 year ago

great-axios v1.1.0

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

great-axios

Introduction

Lightly packaged based on Axios, HTTP library that can be used for browsers and node. Support jsonp request and Axios configuration and Axios function, it is more convenient to cancel the request.

Get Started

npm install great-axios --save

Language

  • Typescript

Example

Here have some examples, hopefully could give you an idea of how to use it:

// GreatAxiosInstance
const greatAxios = new GreatAxios({
  reqOptions: {
    ignoreStatus: true, // onyly return serverdatas, default true
    errorKey: "code", // default
    jsonpTimeout: 3000, // jsonp request timeout, default 5000
    catchError: false, // use reject to return an error message, default false
  },
  // AxiosRequestConfig
  reqConfig: {
    headers: { "X-Requested-With": "XMLHttpRequest" },
  },
});
// interceptor
const interceptor = greatAxios.interceptors4Request((config) => {
  config.withCredentials = false;
});

// do somthing...

// remove request interceptor
greatAxios.removeInterceptor(interceptor);

greatAxios.interceptors4Response((rsp) => {
  // do someting with response
});
// greatAxios.jsonp(url[,config])
greatAxios
  .jsonp("//xxx/example?param=test", {
    reqOptions: {
      jsonpTimeout: 5000,
    },
  })
  .then((value) => {
    const { code } = value; // code: default errorKey
    if (code && code !== -1) {
      // jspnp request success
    } else {
      // jspnp request failed
    }
  });
// greatAxios.get(url[,config])
greatAxios
  .get("/example?param=test", {
    // AxiosRequestConfig
    reqConfig: {
      withCredentials: true,
      timeout: 5000,
    },
    reqOptions: {
      catchError: true,
    },
  })
  .then((value) => {
    // get request success
  })
  .catch((error) => {
    // get request failed
  });
let postCancelID = "";

// greatAxios.post(url[, data[, config]])
greatAxios
  .post(
    "/example",
    {
      params: "test",
    },
    {
      reqOptions: {
        ignoreStatus: false,
      },
      getInnerAttributes: (config) => {
        postCancelID = config.cancelId; // used to cancel this request
        // config.axiosInstance // you can also get an axios instance
      },
    }
  )
  .then((value) => {
    const { status, data } = value;
    if (status === 200) {
      // post request success
    } else {
      // post request failed
    }
  });
// if you want to cancel a pending request
greatAxios.cancel(postCancelID);

License

MIT

1.1.0

1 year ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago