UNPKG

2.46 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 HttpStatusCode from './helpers/HttpStatusCode.js';
19
20/**
21 * Create an instance of Axios
22 *
23 * @param {Object} defaultConfig The default config for the instance
24 *
25 * @returns {Axios} A new instance of Axios
26 */
27function createInstance(defaultConfig) {
28 const context = new Axios(defaultConfig);
29 const instance = bind(Axios.prototype.request, context);
30
31 // Copy axios.prototype to instance
32 utils.extend(instance, Axios.prototype, context, {allOwnKeys: true});
33
34 // Copy context to instance
35 utils.extend(instance, context, null, {allOwnKeys: true});
36
37 // Factory for creating new instances
38 instance.create = function create(instanceConfig) {
39 return createInstance(mergeConfig(defaultConfig, instanceConfig));
40 };
41
42 return instance;
43}
44
45// Create the default instance to be exported
46const axios = createInstance(defaults);
47
48// Expose Axios class to allow class inheritance
49axios.Axios = Axios;
50
51// Expose Cancel & CancelToken
52axios.CanceledError = CanceledError;
53axios.CancelToken = CancelToken;
54axios.isCancel = isCancel;
55axios.VERSION = VERSION;
56axios.toFormData = toFormData;
57
58// Expose AxiosError class
59axios.AxiosError = AxiosError;
60
61// alias for CanceledError for backward compatibility
62axios.Cancel = axios.CanceledError;
63
64// Expose all/spread
65axios.all = function all(promises) {
66 return Promise.all(promises);
67};
68
69axios.spread = spread;
70
71// Expose isAxiosError
72axios.isAxiosError = isAxiosError;
73
74// Expose mergeConfig
75axios.mergeConfig = mergeConfig;
76
77axios.AxiosHeaders = AxiosHeaders;
78
79axios.formToJSON = thing => formDataToJSON(utils.isHTMLForm(thing) ? new FormData(thing) : thing);
80
81axios.HttpStatusCode = HttpStatusCode;
82
83axios.default = axios;
84
85// this module should only have a default export
86export default axios