You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.6 KiB

  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. }));
  68. };
  69. });
  70. utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
  71. /*eslint func-names:0*/
  72. Axios.prototype[method] = function(url, data, config) {
  73. return this.request(mergeConfig(config || {}, {
  74. method: method,
  75. url: url,
  76. data: data
  77. }));
  78. };
  79. });
  80. module.exports = Axios;