source: node_modules/axios/lib/core/Axios.js@ 3a74959

Last change on this file since 3a74959 was 57e58a3, checked in by ste08 <sjovanoska@…>, 4 months ago

Initial commit

  • Property mode set to 100644
File size: 6.4 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';
11
12const 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 */
21class 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 validator.assertOptions(config, {
101 baseUrl: validators.spelling('baseURL'),
102 withXsrfToken: validators.spelling('withXSRFToken')
103 }, true);
104
105 // Set config.method
106 config.method = (config.method || this.defaults.method || 'get').toLowerCase();
107
108 // Flatten headers
109 let contextHeaders = headers && utils.merge(
110 headers.common,
111 headers[config.method]
112 );
113
114 headers && utils.forEach(
115 ['delete', 'get', 'head', 'post', 'put', 'patch', 'common'],
116 (method) => {
117 delete headers[method];
118 }
119 );
120
121 config.headers = AxiosHeaders.concat(contextHeaders, headers);
122
123 // filter out skipped interceptors
124 const requestInterceptorChain = [];
125 let synchronousRequestInterceptors = true;
126 this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
127 if (typeof interceptor.runWhen === 'function' && interceptor.runWhen(config) === false) {
128 return;
129 }
130
131 synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
132
133 requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);
134 });
135
136 const responseInterceptorChain = [];
137 this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
138 responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
139 });
140
141 let promise;
142 let i = 0;
143 let len;
144
145 if (!synchronousRequestInterceptors) {
146 const chain = [dispatchRequest.bind(this), undefined];
147 chain.unshift.apply(chain, requestInterceptorChain);
148 chain.push.apply(chain, responseInterceptorChain);
149 len = chain.length;
150
151 promise = Promise.resolve(config);
152
153 while (i < len) {
154 promise = promise.then(chain[i++], chain[i++]);
155 }
156
157 return promise;
158 }
159
160 len = requestInterceptorChain.length;
161
162 let newConfig = config;
163
164 i = 0;
165
166 while (i < len) {
167 const onFulfilled = requestInterceptorChain[i++];
168 const onRejected = requestInterceptorChain[i++];
169 try {
170 newConfig = onFulfilled(newConfig);
171 } catch (error) {
172 onRejected.call(this, error);
173 break;
174 }
175 }
176
177 try {
178 promise = dispatchRequest.call(this, newConfig);
179 } catch (error) {
180 return Promise.reject(error);
181 }
182
183 i = 0;
184 len = responseInterceptorChain.length;
185
186 while (i < len) {
187 promise = promise.then(responseInterceptorChain[i++], responseInterceptorChain[i++]);
188 }
189
190 return promise;
191 }
192
193 getUri(config) {
194 config = mergeConfig(this.defaults, config);
195 const fullPath = buildFullPath(config.baseURL, config.url);
196 return buildURL(fullPath, config.params, config.paramsSerializer);
197 }
198}
199
200// Provide aliases for supported request methods
201utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
202 /*eslint func-names:0*/
203 Axios.prototype[method] = function(url, config) {
204 return this.request(mergeConfig(config || {}, {
205 method,
206 url,
207 data: (config || {}).data
208 }));
209 };
210});
211
212utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
213 /*eslint func-names:0*/
214
215 function generateHTTPMethod(isForm) {
216 return function httpMethod(url, data, config) {
217 return this.request(mergeConfig(config || {}, {
218 method,
219 headers: isForm ? {
220 'Content-Type': 'multipart/form-data'
221 } : {},
222 url,
223 data
224 }));
225 };
226 }
227
228 Axios.prototype[method] = generateHTTPMethod();
229
230 Axios.prototype[method + 'Form'] = generateHTTPMethod(true);
231});
232
233export default Axios;
Note: See TracBrowser for help on using the repository browser.