Axios.js 4.3 KB

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