source: frontend/node_modules/axios/lib/core/Axios.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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