Axios.js 4.1 KB

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