Axios.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. 'use strict';
  2. import utils from './../utils.js';
  3. import buildURL from '../helpers/buildURL.js';
  4. import InterceptorManager from './InterceptorManager.js';
  5. import dispatchRequest from './dispatchRequest.js';
  6. import mergeConfig from './mergeConfig.js';
  7. import buildFullPath from './buildFullPath.js';
  8. import validator from '../helpers/validator.js';
  9. import AxiosHeaders from './AxiosHeaders.js';
  10. const validators = validator.validators;
  11. /**
  12. * Create a new instance of Axios
  13. *
  14. * @param {Object} instanceConfig The default config for the instance
  15. *
  16. * @return {Axios} A new instance of Axios
  17. */
  18. class Axios {
  19. constructor(instanceConfig) {
  20. this.defaults = instanceConfig;
  21. this.interceptors = {
  22. request: new InterceptorManager(),
  23. response: new InterceptorManager()
  24. };
  25. }
  26. /**
  27. * Dispatch a request
  28. *
  29. * @param {String|Object} configOrUrl The config specific for this request (merged with this.defaults)
  30. * @param {?Object} config
  31. *
  32. * @returns {Promise} The Promise to be fulfilled
  33. */
  34. async request(configOrUrl, config) {
  35. try {
  36. return await this._request(configOrUrl, config);
  37. } catch (err) {
  38. if (err instanceof Error) {
  39. let dummy = {};
  40. Error.captureStackTrace ? Error.captureStackTrace(dummy) : (dummy = new Error());
  41. // slice off the Error: ... line
  42. const stack = dummy.stack ? dummy.stack.replace(/^.+\n/, '') : '';
  43. try {
  44. if (!err.stack) {
  45. err.stack = stack;
  46. // match without the 2 top stack lines
  47. } else if (stack && !String(err.stack).endsWith(stack.replace(/^.+\n.+\n/, ''))) {
  48. err.stack += '\n' + stack
  49. }
  50. } catch (e) {
  51. // ignore the case where "stack" is an un-writable property
  52. }
  53. }
  54. throw err;
  55. }
  56. }
  57. _request(configOrUrl, config) {
  58. /*eslint no-param-reassign:0*/
  59. // Allow for axios('example/url'[, config]) a la fetch API
  60. if (typeof configOrUrl === 'string') {
  61. config = config || {};
  62. config.url = configOrUrl;
  63. } else {
  64. config = configOrUrl || {};
  65. }
  66. config = mergeConfig(this.defaults, config);
  67. const {transitional, paramsSerializer, headers} = config;
  68. if (transitional !== undefined) {
  69. validator.assertOptions(transitional, {
  70. silentJSONParsing: validators.transitional(validators.boolean),
  71. forcedJSONParsing: validators.transitional(validators.boolean),
  72. clarifyTimeoutError: validators.transitional(validators.boolean)
  73. }, false);
  74. }
  75. if (paramsSerializer != null) {
  76. if (utils.isFunction(paramsSerializer)) {
  77. config.paramsSerializer = {
  78. serialize: paramsSerializer
  79. }
  80. } else {
  81. validator.assertOptions(paramsSerializer, {
  82. encode: validators.function,
  83. serialize: validators.function
  84. }, true);
  85. }
  86. }
  87. // Set config.allowAbsoluteUrls
  88. if (config.allowAbsoluteUrls !== undefined) {
  89. // do nothing
  90. } else if (this.defaults.allowAbsoluteUrls !== undefined) {
  91. config.allowAbsoluteUrls = this.defaults.allowAbsoluteUrls;
  92. } else {
  93. config.allowAbsoluteUrls = true;
  94. }
  95. validator.assertOptions(config, {
  96. baseUrl: validators.spelling('baseURL'),
  97. withXsrfToken: validators.spelling('withXSRFToken')
  98. }, true);
  99. // Set config.method
  100. config.method = (config.method || this.defaults.method || 'get').toLowerCase();
  101. // Flatten headers
  102. let contextHeaders = headers && utils.merge(
  103. headers.common,
  104. headers[config.method]
  105. );
  106. headers && utils.forEach(
  107. ['delete', 'get', 'head', 'post', 'put', 'patch', 'common'],
  108. (method) => {
  109. delete headers[method];
  110. }
  111. );
  112. config.headers = AxiosHeaders.concat(contextHeaders, headers);
  113. // filter out skipped interceptors
  114. const requestInterceptorChain = [];
  115. let synchronousRequestInterceptors = true;
  116. this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
  117. if (typeof interceptor.runWhen === 'function' && interceptor.runWhen(config) === false) {
  118. return;
  119. }
  120. synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
  121. requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);
  122. });
  123. const responseInterceptorChain = [];
  124. this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
  125. responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
  126. });
  127. let promise;
  128. let i = 0;
  129. let len;
  130. if (!synchronousRequestInterceptors) {
  131. const chain = [dispatchRequest.bind(this), undefined];
  132. chain.unshift.apply(chain, requestInterceptorChain);
  133. chain.push.apply(chain, responseInterceptorChain);
  134. len = chain.length;
  135. promise = Promise.resolve(config);
  136. while (i < len) {
  137. promise = promise.then(chain[i++], chain[i++]);
  138. }
  139. return promise;
  140. }
  141. len = requestInterceptorChain.length;
  142. let newConfig = config;
  143. i = 0;
  144. while (i < len) {
  145. const onFulfilled = requestInterceptorChain[i++];
  146. const onRejected = requestInterceptorChain[i++];
  147. try {
  148. newConfig = onFulfilled(newConfig);
  149. } catch (error) {
  150. onRejected.call(this, error);
  151. break;
  152. }
  153. }
  154. try {
  155. promise = dispatchRequest.call(this, newConfig);
  156. } catch (error) {
  157. return Promise.reject(error);
  158. }
  159. i = 0;
  160. len = responseInterceptorChain.length;
  161. while (i < len) {
  162. promise = promise.then(responseInterceptorChain[i++], responseInterceptorChain[i++]);
  163. }
  164. return promise;
  165. }
  166. getUri(config) {
  167. config = mergeConfig(this.defaults, config);
  168. const fullPath = buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls);
  169. return buildURL(fullPath, config.params, config.paramsSerializer);
  170. }
  171. }
  172. // Provide aliases for supported request methods
  173. utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
  174. /*eslint func-names:0*/
  175. Axios.prototype[method] = function(url, config) {
  176. return this.request(mergeConfig(config || {}, {
  177. method,
  178. url,
  179. data: (config || {}).data
  180. }));
  181. };
  182. });
  183. utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
  184. /*eslint func-names:0*/
  185. function generateHTTPMethod(isForm) {
  186. return function httpMethod(url, data, config) {
  187. return this.request(mergeConfig(config || {}, {
  188. method,
  189. headers: isForm ? {
  190. 'Content-Type': 'multipart/form-data'
  191. } : {},
  192. url,
  193. data
  194. }));
  195. };
  196. }
  197. Axios.prototype[method] = generateHTTPMethod();
  198. Axios.prototype[method + 'Form'] = generateHTTPMethod(true);
  199. });
  200. export default Axios;