kim-request.js 1.94 KB
Newer Older
duanledexianxianxian's avatar
duanledexianxianxian committed
1 2 3 4 5 6 7 8 9
import axios from 'axios';

class HttpRequest {
  constructor(options) {
    this.options = options;
    this.queues = {};
  }

  getInsideConfig() {
duanledexianxianxian's avatar
duanledexianxianxian committed
10 11 12 13 14 15
    let headers = {};
    if (this.options.headers instanceof Function) {
      headers = this.options.headers();
    } else {
      headers = { ...this.options.headers };
    }
duanledexianxianxian's avatar
duanledexianxianxian committed
16
    const config = {
duanledexianxianxian's avatar
duanledexianxianxian committed
17 18 19 20
      baseURL: this.options.baseUrl, // baseURL
      timeout: 10000,
      responseType: 'json',
      withCredentials: false, // default
duanledexianxianxian's avatar
duanledexianxianxian committed
21 22
      headers: {
        'Access-Control-Allow-Origin': '*',
duanledexianxianxian's avatar
duanledexianxianxian committed
23
        'Content-Type': 'application/json',
duanledexianxianxian's avatar
duanledexianxianxian committed
24
        Accept: 'application/json',
duanledexianxianxian's avatar
duanledexianxianxian committed
25
        'X-Requested-With': 'XMLHttpRequest',
duanledexianxianxian's avatar
duanledexianxianxian committed
26
        ...headers,
duanledexianxianxian's avatar
duanledexianxianxian committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
      },
    };
    return config;
  }

  destroy(url) {
    delete this.queues[url];
  }

  interceptors(instance, url, options) {
    // 请求拦ζˆͺ
    instance.interceptors.request.use(
      config => {
        this.queues[url] = true;
        if (options.beforeRequest instanceof Function) {
          options.beforeRequest({ queues: this.queues, config, options });
        }
        return config;
      },
      error => Promise.reject(error),
    );
    // 响应拦ζˆͺ
    instance.interceptors.response.use(
      res => {
duanledexianxianxian's avatar
duanledexianxianxian committed
51
        // success
duanledexianxianxian's avatar
duanledexianxianxian committed
52
        this.destroy(url);
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
53 54 55
        if (options.successHandler instanceof Function) {
          options.successHandler({ queues: this.queues, res, options });
        }
duanledexianxianxian's avatar
duanledexianxianxian committed
56
        const { data, status } = res;
duanledexianxianxian's avatar
duanledexianxianxian committed
57 58 59 60 61
        return { data, status };
      },
      error => {
        // error
        this.destroy(url);
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
62 63
        if (options.errorHandler instanceof Function) {
          options.errorHandler({ queues: this.queues, error, options });
duanledexianxianxian's avatar
duanledexianxianxian committed
64 65
        }
        return Promise.reject(error);
duanledexianxianxian's avatar
duanledexianxianxian committed
66 67 68 69 70 71 72 73 74 75 76 77
      },
    );
  }

  request(options) {
    const instance = axios.create();
    const ops = Object.assign(this.getInsideConfig(), options);
    this.interceptors(instance, ops.url, ops);
    return instance(ops);
  }
}
export default HttpRequest;