axios.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.CanceledError = require('./cancel/CanceledError');
  32. axios.CancelToken = require('./cancel/CancelToken');
  33. axios.isCancel = require('./cancel/isCancel');
  34. axios.VERSION = require('./env/data').version;
  35. axios.toFormData = require('./helpers/toFormData');
  36. // Expose AxiosError class
  37. axios.AxiosError = require('../lib/core/AxiosError');
  38. // alias for CanceledError for backward compatibility
  39. axios.Cancel = axios.CanceledError;
  40. // Expose all/spread
  41. axios.all = function all(promises) {
  42. return Promise.all(promises);
  43. };
  44. axios.spread = require('./helpers/spread');
  45. // Expose isAxiosError
  46. axios.isAxiosError = require('./helpers/isAxiosError');
  47. module.exports = axios;
  48. // Allow use of default import syntax in TypeScript
  49. module.exports.default = axios;