axios.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. import utils from './utils.js';
  3. import bind from './helpers/bind.js';
  4. import Axios from './core/Axios.js';
  5. import mergeConfig from './core/mergeConfig.js';
  6. import defaults from './defaults/index.js';
  7. import formDataToJSON from './helpers/formDataToJSON.js';
  8. import CanceledError from './cancel/CanceledError.js';
  9. import CancelToken from './cancel/CancelToken.js';
  10. import isCancel from './cancel/isCancel.js';
  11. import {VERSION} from './env/data.js';
  12. import toFormData from './helpers/toFormData.js';
  13. import AxiosError from './core/AxiosError.js';
  14. import spread from './helpers/spread.js';
  15. import isAxiosError from './helpers/isAxiosError.js';
  16. import AxiosHeaders from "./core/AxiosHeaders.js";
  17. import adapters from './adapters/adapters.js';
  18. import HttpStatusCode from './helpers/HttpStatusCode.js';
  19. /**
  20. * Create an instance of Axios
  21. *
  22. * @param {Object} defaultConfig The default config for the instance
  23. *
  24. * @returns {Axios} A new instance of Axios
  25. */
  26. function createInstance(defaultConfig) {
  27. const context = new Axios(defaultConfig);
  28. const instance = bind(Axios.prototype.request, context);
  29. // Copy axios.prototype to instance
  30. utils.extend(instance, Axios.prototype, context, {allOwnKeys: true});
  31. // Copy context to instance
  32. utils.extend(instance, context, null, {allOwnKeys: true});
  33. // Factory for creating new instances
  34. instance.create = function create(instanceConfig) {
  35. return createInstance(mergeConfig(defaultConfig, instanceConfig));
  36. };
  37. return instance;
  38. }
  39. // Create the default instance to be exported
  40. const axios = createInstance(defaults);
  41. // Expose Axios class to allow class inheritance
  42. axios.Axios = Axios;
  43. // Expose Cancel & CancelToken
  44. axios.CanceledError = CanceledError;
  45. axios.CancelToken = CancelToken;
  46. axios.isCancel = isCancel;
  47. axios.VERSION = VERSION;
  48. axios.toFormData = toFormData;
  49. // Expose AxiosError class
  50. axios.AxiosError = AxiosError;
  51. // alias for CanceledError for backward compatibility
  52. axios.Cancel = axios.CanceledError;
  53. // Expose all/spread
  54. axios.all = function all(promises) {
  55. return Promise.all(promises);
  56. };
  57. axios.spread = spread;
  58. // Expose isAxiosError
  59. axios.isAxiosError = isAxiosError;
  60. // Expose mergeConfig
  61. axios.mergeConfig = mergeConfig;
  62. axios.AxiosHeaders = AxiosHeaders;
  63. axios.formToJSON = thing => formDataToJSON(utils.isHTMLForm(thing) ? new FormData(thing) : thing);
  64. axios.getAdapter = adapters.getAdapter;
  65. axios.HttpStatusCode = HttpStatusCode;
  66. axios.default = axios;
  67. // this module should only have a default export
  68. export default axios