1 | 'use strict';
|
---|
2 |
|
---|
3 | import utils from './../utils.js';
|
---|
4 | import buildURL from '../helpers/buildURL.js';
|
---|
5 | import InterceptorManager from './InterceptorManager.js';
|
---|
6 | import dispatchRequest from './dispatchRequest.js';
|
---|
7 | import mergeConfig from './mergeConfig.js';
|
---|
8 | import buildFullPath from './buildFullPath.js';
|
---|
9 | import validator from '../helpers/validator.js';
|
---|
10 | import AxiosHeaders from './AxiosHeaders.js';
|
---|
11 |
|
---|
12 | const validators = validator.validators;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Create a new instance of Axios
|
---|
16 | *
|
---|
17 | * @param {Object} instanceConfig The default config for the instance
|
---|
18 | *
|
---|
19 | * @return {Axios} A new instance of Axios
|
---|
20 | */
|
---|
21 | class Axios {
|
---|
22 | constructor(instanceConfig) {
|
---|
23 | this.defaults = instanceConfig;
|
---|
24 | this.interceptors = {
|
---|
25 | request: new InterceptorManager(),
|
---|
26 | response: new InterceptorManager()
|
---|
27 | };
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Dispatch a request
|
---|
32 | *
|
---|
33 | * @param {String|Object} configOrUrl The config specific for this request (merged with this.defaults)
|
---|
34 | * @param {?Object} config
|
---|
35 | *
|
---|
36 | * @returns {Promise} The Promise to be fulfilled
|
---|
37 | */
|
---|
38 | async request(configOrUrl, config) {
|
---|
39 | try {
|
---|
40 | return await this._request(configOrUrl, config);
|
---|
41 | } catch (err) {
|
---|
42 | if (err instanceof Error) {
|
---|
43 | let dummy;
|
---|
44 |
|
---|
45 | Error.captureStackTrace ? Error.captureStackTrace(dummy = {}) : (dummy = new Error());
|
---|
46 |
|
---|
47 | // slice off the Error: ... line
|
---|
48 | const stack = dummy.stack ? dummy.stack.replace(/^.+\n/, '') : '';
|
---|
49 | try {
|
---|
50 | if (!err.stack) {
|
---|
51 | err.stack = stack;
|
---|
52 | // match without the 2 top stack lines
|
---|
53 | } else if (stack && !String(err.stack).endsWith(stack.replace(/^.+\n.+\n/, ''))) {
|
---|
54 | err.stack += '\n' + stack
|
---|
55 | }
|
---|
56 | } catch (e) {
|
---|
57 | // ignore the case where "stack" is an un-writable property
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | throw err;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | _request(configOrUrl, config) {
|
---|
66 | /*eslint no-param-reassign:0*/
|
---|
67 | // Allow for axios('example/url'[, config]) a la fetch API
|
---|
68 | if (typeof configOrUrl === 'string') {
|
---|
69 | config = config || {};
|
---|
70 | config.url = configOrUrl;
|
---|
71 | } else {
|
---|
72 | config = configOrUrl || {};
|
---|
73 | }
|
---|
74 |
|
---|
75 | config = mergeConfig(this.defaults, config);
|
---|
76 |
|
---|
77 | const {transitional, paramsSerializer, headers} = config;
|
---|
78 |
|
---|
79 | if (transitional !== undefined) {
|
---|
80 | validator.assertOptions(transitional, {
|
---|
81 | silentJSONParsing: validators.transitional(validators.boolean),
|
---|
82 | forcedJSONParsing: validators.transitional(validators.boolean),
|
---|
83 | clarifyTimeoutError: validators.transitional(validators.boolean)
|
---|
84 | }, false);
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (paramsSerializer != null) {
|
---|
88 | if (utils.isFunction(paramsSerializer)) {
|
---|
89 | config.paramsSerializer = {
|
---|
90 | serialize: paramsSerializer
|
---|
91 | }
|
---|
92 | } else {
|
---|
93 | validator.assertOptions(paramsSerializer, {
|
---|
94 | encode: validators.function,
|
---|
95 | serialize: validators.function
|
---|
96 | }, true);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | // Set config.method
|
---|
101 | config.method = (config.method || this.defaults.method || 'get').toLowerCase();
|
---|
102 |
|
---|
103 | // Flatten headers
|
---|
104 | let contextHeaders = headers && utils.merge(
|
---|
105 | headers.common,
|
---|
106 | headers[config.method]
|
---|
107 | );
|
---|
108 |
|
---|
109 | headers && utils.forEach(
|
---|
110 | ['delete', 'get', 'head', 'post', 'put', 'patch', 'common'],
|
---|
111 | (method) => {
|
---|
112 | delete headers[method];
|
---|
113 | }
|
---|
114 | );
|
---|
115 |
|
---|
116 | config.headers = AxiosHeaders.concat(contextHeaders, headers);
|
---|
117 |
|
---|
118 | // filter out skipped interceptors
|
---|
119 | const requestInterceptorChain = [];
|
---|
120 | let synchronousRequestInterceptors = true;
|
---|
121 | this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
|
---|
122 | if (typeof interceptor.runWhen === 'function' && interceptor.runWhen(config) === false) {
|
---|
123 | return;
|
---|
124 | }
|
---|
125 |
|
---|
126 | synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
|
---|
127 |
|
---|
128 | requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);
|
---|
129 | });
|
---|
130 |
|
---|
131 | const responseInterceptorChain = [];
|
---|
132 | this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
|
---|
133 | responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
|
---|
134 | });
|
---|
135 |
|
---|
136 | let promise;
|
---|
137 | let i = 0;
|
---|
138 | let len;
|
---|
139 |
|
---|
140 | if (!synchronousRequestInterceptors) {
|
---|
141 | const chain = [dispatchRequest.bind(this), undefined];
|
---|
142 | chain.unshift.apply(chain, requestInterceptorChain);
|
---|
143 | chain.push.apply(chain, responseInterceptorChain);
|
---|
144 | len = chain.length;
|
---|
145 |
|
---|
146 | promise = Promise.resolve(config);
|
---|
147 |
|
---|
148 | while (i < len) {
|
---|
149 | promise = promise.then(chain[i++], chain[i++]);
|
---|
150 | }
|
---|
151 |
|
---|
152 | return promise;
|
---|
153 | }
|
---|
154 |
|
---|
155 | len = requestInterceptorChain.length;
|
---|
156 |
|
---|
157 | let newConfig = config;
|
---|
158 |
|
---|
159 | i = 0;
|
---|
160 |
|
---|
161 | while (i < len) {
|
---|
162 | const onFulfilled = requestInterceptorChain[i++];
|
---|
163 | const onRejected = requestInterceptorChain[i++];
|
---|
164 | try {
|
---|
165 | newConfig = onFulfilled(newConfig);
|
---|
166 | } catch (error) {
|
---|
167 | onRejected.call(this, error);
|
---|
168 | break;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | try {
|
---|
173 | promise = dispatchRequest.call(this, newConfig);
|
---|
174 | } catch (error) {
|
---|
175 | return Promise.reject(error);
|
---|
176 | }
|
---|
177 |
|
---|
178 | i = 0;
|
---|
179 | len = responseInterceptorChain.length;
|
---|
180 |
|
---|
181 | while (i < len) {
|
---|
182 | promise = promise.then(responseInterceptorChain[i++], responseInterceptorChain[i++]);
|
---|
183 | }
|
---|
184 |
|
---|
185 | return promise;
|
---|
186 | }
|
---|
187 |
|
---|
188 | getUri(config) {
|
---|
189 | config = mergeConfig(this.defaults, config);
|
---|
190 | const fullPath = buildFullPath(config.baseURL, config.url);
|
---|
191 | return buildURL(fullPath, config.params, config.paramsSerializer);
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | // Provide aliases for supported request methods
|
---|
196 | utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
|
---|
197 | /*eslint func-names:0*/
|
---|
198 | Axios.prototype[method] = function(url, config) {
|
---|
199 | return this.request(mergeConfig(config || {}, {
|
---|
200 | method,
|
---|
201 | url,
|
---|
202 | data: (config || {}).data
|
---|
203 | }));
|
---|
204 | };
|
---|
205 | });
|
---|
206 |
|
---|
207 | utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
|
---|
208 | /*eslint func-names:0*/
|
---|
209 |
|
---|
210 | function generateHTTPMethod(isForm) {
|
---|
211 | return function httpMethod(url, data, config) {
|
---|
212 | return this.request(mergeConfig(config || {}, {
|
---|
213 | method,
|
---|
214 | headers: isForm ? {
|
---|
215 | 'Content-Type': 'multipart/form-data'
|
---|
216 | } : {},
|
---|
217 | url,
|
---|
218 | data
|
---|
219 | }));
|
---|
220 | };
|
---|
221 | }
|
---|
222 |
|
---|
223 | Axios.prototype[method] = generateHTTPMethod();
|
---|
224 |
|
---|
225 | Axios.prototype[method + 'Form'] = generateHTTPMethod(true);
|
---|
226 | });
|
---|
227 |
|
---|
228 | export default Axios;
|
---|