Axios.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /**
  8. * Create a new instance of Axios
  9. *
  10. * @param {Object} instanceConfig The default config for the instance
  11. */
  12. function Axios(instanceConfig) {
  13. this.defaults = instanceConfig;
  14. this.interceptors = {
  15. request: new InterceptorManager(),
  16. response: new InterceptorManager()
  17. };
  18. }
  19. /**
  20. * Dispatch a request
  21. *
  22. * @param {Object} config The config specific for this request (merged with this.defaults)
  23. */
  24. Axios.prototype.request = function request(config) {
  25. /*eslint no-param-reassign:0*/
  26. // Allow for axios('example/url'[, config]) a la fetch API
  27. if (typeof config === 'string') {
  28. config = arguments[1] || {};
  29. config.url = arguments[0];
  30. } else {
  31. config = config || {};
  32. }
  33. config = mergeConfig(this.defaults, config);
  34. // Set config.method
  35. if (config.method) {
  36. config.method = config.method.toLowerCase();
  37. } else if (this.defaults.method) {
  38. config.method = this.defaults.method.toLowerCase();
  39. } else {
  40. config.method = 'get';
  41. }
  42. // Hook up interceptors middleware
  43. var chain = [dispatchRequest, undefined];
  44. var promise = Promise.resolve(config);
  45. this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
  46. chain.unshift(interceptor.fulfilled, interceptor.rejected);
  47. });
  48. this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
  49. chain.push(interceptor.fulfilled, interceptor.rejected);
  50. });
  51. while (chain.length) {
  52. promise = promise.then(chain.shift(), chain.shift());
  53. }
  54. return promise;
  55. };
  56. Axios.prototype.getUri = function getUri(config) {
  57. config = mergeConfig(this.defaults, config);
  58. return buildURL(config.url, config.params, config.paramsSerializer).replace(/^\?/, '');
  59. };
  60. // Provide aliases for supported request methods
  61. utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
  62. /*eslint func-names:0*/
  63. Axios.prototype[method] = function(url, config) {
  64. return this.request(mergeConfig(config || {}, {
  65. method: method,
  66. url: url,
  67. data: (config || {}).data
  68. }));
  69. };
  70. });
  71. utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
  72. /*eslint func-names:0*/
  73. Axios.prototype[method] = function(url, data, config) {
  74. return this.request(mergeConfig(config || {}, {
  75. method: method,
  76. url: url,
  77. data: data
  78. }));
  79. };
  80. });
  81. module.exports = Axios;