axios.js 3.5 KB
Newer Older
M
Matt Zabriskie 已提交
1
var Promise = require('es6-promise').Promise;
2
var defaults = require('./defaults');
3
var utils = require('./utils');
4
var InterceptorManager = require('./helpers/InterceptorManager');
M
Matt Zabriskie 已提交
5

M
Matt Zabriskie 已提交
6 7
var axios = module.exports = function axios(config) {
  config = utils.merge({
M
Matt Zabriskie 已提交
8
    method: 'get',
M
mzabriskie 已提交
9
    headers: {},
M
Matt Zabriskie 已提交
10 11
    transformRequest: defaults.transformRequest,
    transformResponse: defaults.transformResponse
M
Matt Zabriskie 已提交
12
  }, config);
M
Matt Zabriskie 已提交
13

M
Matt Zabriskie 已提交
14
  // Don't allow overriding defaults.withCredentials
M
Matt Zabriskie 已提交
15
  config.withCredentials = config.withCredentials || defaults.withCredentials;
M
Matt Zabriskie 已提交
16

17
  function dispatchRequest(config) {
J
Jason Dobry 已提交
18 19 20 21 22 23 24 25 26 27 28 29
    return new Promise(function (resolve, reject) {
      try {
        // For browsers use XHR adapter
        if (typeof window !== 'undefined') {
          require('./adapters/xhr')(resolve, reject, config);
        }
        // For node use HTTP adapter
        else if (typeof process !== 'undefined') {
          require('./adapters/http')(resolve, reject, config);
        }
      } catch (e) {
        reject(e);
30
      }
J
Jason Dobry 已提交
31 32
    });
  };
M
Matt Zabriskie 已提交
33

34 35 36 37 38 39 40 41 42 43 44
  function deprecatedMethod(method, instead, docs) {
    try {
      console.warn(
        'DEPRECATED method `' + method + '`.' +
        (instead ? ' Use `' + instead + '` instead.' : '') +
        ' This method will be removed in a future release.');

      if (docs) {
        console.warn('For more information about usage see ' + docs);
      }
    } catch (e) {}
45
  };
46

47 48
  // Hook up interceptors middleware
  var chain = [dispatchRequest, undefined];
J
Jason Dobry 已提交
49 50
  var promise = Promise.resolve(config);

51 52
  axios.interceptors.request.forEach(function (interceptor) {
    chain.unshift(interceptor.fulfilled, interceptor.rejected);
J
Jason Dobry 已提交
53 54
  });

55 56
  axios.interceptors.response.forEach(function (interceptor) {
    chain.push(interceptor.fulfilled, interceptor.rejected);
J
Jason Dobry 已提交
57 58 59
  });

  while (chain.length) {
60
    promise = promise.then(chain.shift(), chain.shift());
J
Jason Dobry 已提交
61 62
  }

63 64
  // Provide alias for success
  promise.success = function success(fn) {
65 66
    deprecatedMethod('success', 'then', 'https://github.com/mzabriskie/axios/blob/master/README.md#response-api');

M
Matt Zabriskie 已提交
67
    promise.then(function(response) {
M
mzabriskie 已提交
68
      fn(response.data, response.status, response.headers, response.config);
M
Matt Zabriskie 已提交
69 70 71 72
    });
    return promise;
  };

73 74
  // Provide alias for error
  promise.error = function error(fn) {
75 76
    deprecatedMethod('error', 'catch', 'https://github.com/mzabriskie/axios/blob/master/README.md#response-api');

M
Matt Zabriskie 已提交
77
    promise.then(null, function(response) {
M
mzabriskie 已提交
78
      fn(response.data, response.status, response.headers, response.config);
M
Matt Zabriskie 已提交
79 80 81 82 83 84 85
    });
    return promise;
  };

  return promise;
};

86 87
// Expose defaults
axios.defaults = defaults;
M
Matt Zabriskie 已提交
88

89
// Expose all/spread
M
mzabriskie 已提交
90 91 92
axios.all = function (promises) {
  return Promise.all(promises);
};
M
mzabriskie 已提交
93
axios.spread = require('./helpers/spread');
94

95
// Expose interceptors
J
Jason Dobry 已提交
96
axios.interceptors = {
97 98
  request: new InterceptorManager(),
  response: new InterceptorManager()
J
Jason Dobry 已提交
99 100
};

101 102 103
// Provide aliases for supported request methods
createShortMethods('delete', 'get', 'head');
createShortMethodsWithData('post', 'put', 'patch');
M
Matt Zabriskie 已提交
104 105

function createShortMethods() {
106
  utils.forEach(arguments, function (method) {
M
Matt Zabriskie 已提交
107 108
    axios[method] = function (url, config) {
      return axios(utils.merge(config || {}, {
M
Matt Zabriskie 已提交
109 110 111 112 113 114 115 116
        method: method,
        url: url
      }));
    };
  });
}

function createShortMethodsWithData() {
117
  utils.forEach(arguments, function (method) {
M
Matt Zabriskie 已提交
118 119
    axios[method] = function (url, data, config) {
      return axios(utils.merge(config || {}, {
M
Matt Zabriskie 已提交
120 121 122 123 124 125
        method: method,
        url: url,
        data: data
      }));
    };
  });
J
Jason Dobry 已提交
126
}