[d24f17c] | 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 |
|
---|
| 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 | }
|
---|
| 57 |
|
---|
| 58 | throw err;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | _request(configOrUrl, config) {
|
---|
| 63 | /*eslint no-param-reassign:0*/
|
---|
| 64 | // Allow for axios('example/url'[, config]) a la fetch API
|
---|
| 65 | if (typeof configOrUrl === 'string') {
|
---|
| 66 | config = config || {};
|
---|
| 67 | config.url = configOrUrl;
|
---|
| 68 | } else {
|
---|
| 69 | config = configOrUrl || {};
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | config = mergeConfig(this.defaults, config);
|
---|
| 73 |
|
---|
| 74 | const {transitional, paramsSerializer, headers} = config;
|
---|
| 75 |
|
---|
| 76 | if (transitional !== undefined) {
|
---|
| 77 | validator.assertOptions(transitional, {
|
---|
| 78 | silentJSONParsing: validators.transitional(validators.boolean),
|
---|
| 79 | forcedJSONParsing: validators.transitional(validators.boolean),
|
---|
| 80 | clarifyTimeoutError: validators.transitional(validators.boolean)
|
---|
| 81 | }, false);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | if (paramsSerializer != null) {
|
---|
| 85 | if (utils.isFunction(paramsSerializer)) {
|
---|
| 86 | config.paramsSerializer = {
|
---|
| 87 | serialize: paramsSerializer
|
---|
| 88 | }
|
---|
| 89 | } else {
|
---|
| 90 | validator.assertOptions(paramsSerializer, {
|
---|
| 91 | encode: validators.function,
|
---|
| 92 | serialize: validators.function
|
---|
| 93 | }, true);
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | // Set config.method
|
---|
| 98 | config.method = (config.method || this.defaults.method || 'get').toLowerCase();
|
---|
| 99 |
|
---|
| 100 | // Flatten headers
|
---|
| 101 | let contextHeaders = headers && utils.merge(
|
---|
| 102 | headers.common,
|
---|
| 103 | headers[config.method]
|
---|
| 104 | );
|
---|
| 105 |
|
---|
| 106 | headers && utils.forEach(
|
---|
| 107 | ['delete', 'get', 'head', 'post', 'put', 'patch', 'common'],
|
---|
| 108 | (method) => {
|
---|
| 109 | delete headers[method];
|
---|
| 110 | }
|
---|
| 111 | );
|
---|
| 112 |
|
---|
| 113 | config.headers = AxiosHeaders.concat(contextHeaders, headers);
|
---|
| 114 |
|
---|
| 115 | // filter out skipped interceptors
|
---|
| 116 | const requestInterceptorChain = [];
|
---|
| 117 | let synchronousRequestInterceptors = true;
|
---|
| 118 | this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
|
---|
| 119 | if (typeof interceptor.runWhen === 'function' && interceptor.runWhen(config) === false) {
|
---|
| 120 | return;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | synchronousRequestInterceptors = synchronousRequestInterceptors && interceptor.synchronous;
|
---|
| 124 |
|
---|
| 125 | requestInterceptorChain.unshift(interceptor.fulfilled, interceptor.rejected);
|
---|
| 126 | });
|
---|
| 127 |
|
---|
| 128 | const responseInterceptorChain = [];
|
---|
| 129 | this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
|
---|
| 130 | responseInterceptorChain.push(interceptor.fulfilled, interceptor.rejected);
|
---|
| 131 | });
|
---|
| 132 |
|
---|
| 133 | let promise;
|
---|
| 134 | let i = 0;
|
---|
| 135 | let len;
|
---|
| 136 |
|
---|
| 137 | if (!synchronousRequestInterceptors) {
|
---|
| 138 | const chain = [dispatchRequest.bind(this), undefined];
|
---|
| 139 | chain.unshift.apply(chain, requestInterceptorChain);
|
---|
| 140 | chain.push.apply(chain, responseInterceptorChain);
|
---|
| 141 | len = chain.length;
|
---|
| 142 |
|
---|
| 143 | promise = Promise.resolve(config);
|
---|
| 144 |
|
---|
| 145 | while (i < len) {
|
---|
| 146 | promise = promise.then(chain[i++], chain[i++]);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | return promise;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | len = requestInterceptorChain.length;
|
---|
| 153 |
|
---|
| 154 | let newConfig = config;
|
---|
| 155 |
|
---|
| 156 | i = 0;
|
---|
| 157 |
|
---|
| 158 | while (i < len) {
|
---|
| 159 | const onFulfilled = requestInterceptorChain[i++];
|
---|
| 160 | const onRejected = requestInterceptorChain[i++];
|
---|
| 161 | try {
|
---|
| 162 | newConfig = onFulfilled(newConfig);
|
---|
| 163 | } catch (error) {
|
---|
| 164 | onRejected.call(this, error);
|
---|
| 165 | break;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | try {
|
---|
| 170 | promise = dispatchRequest.call(this, newConfig);
|
---|
| 171 | } catch (error) {
|
---|
| 172 | return Promise.reject(error);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | i = 0;
|
---|
| 176 | len = responseInterceptorChain.length;
|
---|
| 177 |
|
---|
| 178 | while (i < len) {
|
---|
| 179 | promise = promise.then(responseInterceptorChain[i++], responseInterceptorChain[i++]);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | return promise;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | getUri(config) {
|
---|
| 186 | config = mergeConfig(this.defaults, config);
|
---|
| 187 | const fullPath = buildFullPath(config.baseURL, config.url);
|
---|
| 188 | return buildURL(fullPath, config.params, config.paramsSerializer);
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | // Provide aliases for supported request methods
|
---|
| 193 | utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
|
---|
| 194 | /*eslint func-names:0*/
|
---|
| 195 | Axios.prototype[method] = function(url, config) {
|
---|
| 196 | return this.request(mergeConfig(config || {}, {
|
---|
| 197 | method,
|
---|
| 198 | url,
|
---|
| 199 | data: (config || {}).data
|
---|
| 200 | }));
|
---|
| 201 | };
|
---|
| 202 | });
|
---|
| 203 |
|
---|
| 204 | utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
|
---|
| 205 | /*eslint func-names:0*/
|
---|
| 206 |
|
---|
| 207 | function generateHTTPMethod(isForm) {
|
---|
| 208 | return function httpMethod(url, data, config) {
|
---|
| 209 | return this.request(mergeConfig(config || {}, {
|
---|
| 210 | method,
|
---|
| 211 | headers: isForm ? {
|
---|
| 212 | 'Content-Type': 'multipart/form-data'
|
---|
| 213 | } : {},
|
---|
| 214 | url,
|
---|
| 215 | data
|
---|
| 216 | }));
|
---|
| 217 | };
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | Axios.prototype[method] = generateHTTPMethod();
|
---|
| 221 |
|
---|
| 222 | Axios.prototype[method + 'Form'] = generateHTTPMethod(true);
|
---|
| 223 | });
|
---|
| 224 |
|
---|
| 225 | export default Axios;
|
---|