UNPKG

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