Have ideas to improve npm?Join in the discussion! »

    great-axios

    1.0.4 • Public • Published

    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

    ChangeLog

    1. commit first version --2021/01/10 HLianfa
    2. reduce redundancy --2021/01/10 HLianfa
    3. add LICENSE --2021/01/10 HLianfa
    4. compiled into js --2021/01/19 HLianfa

    Install

    npm i great-axios

    DownloadsWeekly Downloads

    17

    Version

    1.0.4

    License

    MIT

    Unpacked Size

    34.3 kB

    Total Files

    18

    Last publish

    Collaborators

    • avatar