axios.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. var utils = require('./utils');
  3. var bind = require('./helpers/bind');
  4. var Axios = require('./core/Axios');
  5. var mergeConfig = require('./core/mergeConfig');
  6. var defaults = require('./defaults');
  7. /**
  8. * Create an instance of Axios
  9. *
  10. * @param {Object} defaultConfig The default config for the instance
  11. * @return {Axios} A new instance of Axios
  12. */
  13. function createInstance(defaultConfig) {
  14. var context = new Axios(defaultConfig);
  15. var instance = bind(Axios.prototype.request, context);
  16. // Copy axios.prototype to instance
  17. utils.extend(instance, Axios.prototype, context);
  18. // Copy context to instance
  19. utils.extend(instance, context);
  20. // Factory for creating new instances
  21. instance.create = function create(instanceConfig) {
  22. return createInstance(mergeConfig(defaultConfig, instanceConfig));
  23. };
  24. return instance;
  25. }
  26. // Create the default instance to be exported
  27. var axios = createInstance(defaults);
  28. // Expose Axios class to allow class inheritance
  29. axios.Axios = Axios;
  30. // Expose Cancel & CancelToken
  31. axios.Cancel = require('./cancel/Cancel');
  32. axios.CancelToken = require('./cancel/CancelToken');
  33. axios.isCancel = require('./cancel/isCancel');
  34. axios.VERSION = require('./env/data').version;
  35. // Expose all/spread
  36. axios.all = function all(promises) {
  37. return Promise.all(promises);
  38. };
  39. axios.spread = require('./helpers/spread');
  40. // Expose isAxiosError
  41. axios.isAxiosError = require('./helpers/isAxiosError');
  42. module.exports = axios;
  43. // Allow use of default import syntax in TypeScript
  44. module.exports.default = axios;