a9ea2f96c1b640ae5605c6f05a2614a0e9deb57680f0f30b17908a6dd65ef260a5d88f71a370daf2030bd54ce66b6c3d9ab18be5f439d9c42b55e4e0816827 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 'use strict';
  2. var utils = require('./../utils');
  3. var buildURL = require('../helpers/buildURL');
  4. var InterceptorManager = require('./InterceptorManager');
  5. var dispatchRequest = require('./dispatchRequest');
  6. var mergeConfig = require('./mergeConfig');
  7. var buildFullPath = require('./buildFullPath');
  8. var validator = require('../helpers/validator');
  9. var validators = validator.validators;
  10. /**
  11. * Create a new instance of Axios
  12. *
  13. * @param {Object} instanceConfig The default config for the instance
  14. */
  15. function Axios(instanceConfig) {
  16. this.defaults = instanceConfig;
  17. this.interceptors = {
  18. request: new InterceptorManager(),
  19. response: new InterceptorManager()
  20. };
  21. }
  22. /**
  23. * Dispatch a request
  24. *
  25. * @param {String|Object} configOrUrl The config specific for this request (merged with this.defaults)
  26. * @param {?Object} config
  27. */
  28. Axios.prototype.request = function request(configOrUrl, config) {
  29. /*eslint no-param-reassign:0*/
  30. // Allow for axios('example/url'[, config]) a la fetch API
  31. if (typeof configOrUrl === 'string') {
  32. config = config || {};
  33. config.url = configOrUrl;
  34. } else {
  35. config = configOrUrl || {};
  36. }
  37. config = mergeConfig(this.defaults, config);
  38. // Set config.method
  39. if (config.method) {
  40. config.method = config.method.toLowerCase();
  41. } else if (this.defaults.method) {
  42. config.method = this.defaults.method.toLowerCase();
  43. } else {
  44. config.method = 'get';
  45. }
  46. var transitional = config.transitional;
  47. if (transitional !== undefined) {
  48. validator.assertOptions(transitional, {
  49. silentJSONParsing: validators.transitional(validators.boolean),
  50. forcedJSONParsing: validators.transitional(validators.boolean),
  51. clarifyTimeoutError: validators.transitional(validators.boolean)
  52. }, false);
  53. }
  54. var paramsSerializer = config.paramsSerializer;
  55. if (paramsSerializer !== undefined) {
  56. validator.assertOptions(paramsSerializer, {
  57. encode: validators.function,
  58. serialize: validators.function
  59. }, true);
  60. }
  61. utils.isFunction(paramsSerializer) && (config.paramsSerializer = {serialize: paramsSerializer});
  62. // filter out skipped interceptors
  63. var requestInterceptorChain = [];
  64. var synchronousRequestInterceptors = true;
  65. this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
  66. if (typeof interceptor.runWhen === 'function' && interceptor.runWhen(config) === false) {
  67. return;
  68. }
  69. synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
  70. requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);
  71. });
  72. var responseInterceptorChain = [];
  73. this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
  74. responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
  75. });
  76. var promise;
  77. if (!synchronousRequestInterceptors) {
  78. var chain = [dispatchRequest, undefined];
  79. Array.prototype.unshift.apply(chain, requestInterceptorChain);
  80. chain = chain.concat(responseInterceptorChain);
  81. promise = Promise.resolve(config);
  82. while (chain.length) {
  83. promise = promise.then(chain.shift(), chain.shift());
  84. }
  85. return promise;
  86. }
  87. var newConfig = config;
  88. while (requestInterceptorChain.length) {
  89. var onFulfilled = requestInterceptorChain.shift();
  90. var onRejected = requestInterceptorChain.shift();
  91. try {
  92. newConfig = onFulfilled(newConfig);
  93. } catch (error) {
  94. onRejected(error);
  95. break;
  96. }
  97. }
  98. try {
  99. promise = dispatchRequest(newConfig);
  100. } catch (error) {
  101. return Promise.reject(error);
  102. }
  103. while (responseInterceptorChain.length) {
  104. promise = promise.then(responseInterceptorChain.shift(), responseInterceptorChain.shift());
  105. }
  106. return promise;
  107. };
  108. Axios.prototype.getUri = function getUri(config) {
  109. config = mergeConfig(this.defaults, config);
  110. var fullPath = buildFullPath(config.baseURL, config.url);
  111. return buildURL(fullPath, config.params, config.paramsSerializer);
  112. };
  113. // Provide aliases for supported request methods
  114. utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
  115. /*eslint func-names:0*/
  116. Axios.prototype[method] = function(url, config) {
  117. return this.request(mergeConfig(config || {}, {
  118. method: method,
  119. url: url,
  120. data: (config || {}).data
  121. }));
  122. };
  123. });
  124. utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
  125. /*eslint func-names:0*/
  126. function generateHTTPMethod(isForm) {
  127. return function httpMethod(url, data, config) {
  128. return this.request(mergeConfig(config || {}, {
  129. method: method,
  130. headers: isForm ? {
  131. 'Content-Type': 'multipart/form-data'
  132. } : {},
  133. url: url,
  134. data: data
  135. }));
  136. };
  137. }
  138. Axios.prototype[method] = generateHTTPMethod();
  139. Axios.prototype[method + 'Form'] = generateHTTPMethod(true);
  140. });
  141. module.exports = Axios;