Axios.js 4.5 KB

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