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