kim-request.js 1.98 KB
Newer Older
duanledexianxianxian's avatar
duanledexianxianxian committed
1
import axios from 'axios';
duanledexianxianxian's avatar
duanledexianxianxian committed
2
import { string } from 'prop-types';
duanledexianxianxian's avatar
duanledexianxianxian committed
3 4 5 6 7 8 9 10

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

  getInsideConfig() {
duanledexianxianxian's avatar
duanledexianxianxian committed
11 12 13 14 15 16
    let headers = {};
    if (this.options.headers instanceof Function) {
      headers = this.options.headers();
    } else {
      headers = { ...this.options.headers };
    }
duanledexianxianxian's avatar
duanledexianxianxian committed
17
    const config = {
duanledexianxianxian's avatar
duanledexianxianxian committed
18 19 20 21
      baseURL: this.options.baseUrl, // baseURL
      timeout: 10000,
      responseType: 'json',
      withCredentials: false, // default
duanledexianxianxian's avatar
duanledexianxianxian committed
22 23
      headers: {
        'Access-Control-Allow-Origin': '*',
duanledexianxianxian's avatar
duanledexianxianxian committed
24
        'Content-Type': 'application/json',
duanledexianxianxian's avatar
duanledexianxianxian committed
25
        Accept: 'application/json',
duanledexianxianxian's avatar
duanledexianxianxian committed
26
        'X-Requested-With': 'XMLHttpRequest',
duanledexianxianxian's avatar
duanledexianxianxian committed
27
        ...headers,
duanledexianxianxian's avatar
duanledexianxianxian committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
      },
    };
    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
52
        // success
duanledexianxianxian's avatar
duanledexianxianxian committed
53
        this.destroy(url);
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
54 55 56
        if (options.successHandler instanceof Function) {
          options.successHandler({ queues: this.queues, res, options });
        }
duanledexianxianxian's avatar
duanledexianxianxian committed
57
        const { data, status } = res;
duanledexianxianxian's avatar
duanledexianxianxian committed
58 59 60 61 62
        return { data, status };
      },
      error => {
        // error
        this.destroy(url);
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
63 64
        if (options.errorHandler instanceof Function) {
          options.errorHandler({ queues: this.queues, error, options });
duanledexianxianxian's avatar
duanledexianxianxian committed
65 66
        }
        return Promise.reject(error);
duanledexianxianxian's avatar
duanledexianxianxian committed
67 68 69 70 71 72 73 74 75 76 77 78
      },
    );
  }

  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;