[d24f17c] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | import transformData from './transformData.js';
|
---|
| 4 | import isCancel from '../cancel/isCancel.js';
|
---|
| 5 | import defaults from '../defaults/index.js';
|
---|
| 6 | import CanceledError from '../cancel/CanceledError.js';
|
---|
| 7 | import AxiosHeaders from '../core/AxiosHeaders.js';
|
---|
| 8 | import adapters from "../adapters/adapters.js";
|
---|
| 9 |
|
---|
| 10 | /**
|
---|
| 11 | * Throws a `CanceledError` if cancellation has been requested.
|
---|
| 12 | *
|
---|
| 13 | * @param {Object} config The config that is to be used for the request
|
---|
| 14 | *
|
---|
| 15 | * @returns {void}
|
---|
| 16 | */
|
---|
| 17 | function throwIfCancellationRequested(config) {
|
---|
| 18 | if (config.cancelToken) {
|
---|
| 19 | config.cancelToken.throwIfRequested();
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | if (config.signal && config.signal.aborted) {
|
---|
| 23 | throw new CanceledError(null, config);
|
---|
| 24 | }
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Dispatch a request to the server using the configured adapter.
|
---|
| 29 | *
|
---|
| 30 | * @param {object} config The config that is to be used for the request
|
---|
| 31 | *
|
---|
| 32 | * @returns {Promise} The Promise to be fulfilled
|
---|
| 33 | */
|
---|
| 34 | export default function dispatchRequest(config) {
|
---|
| 35 | throwIfCancellationRequested(config);
|
---|
| 36 |
|
---|
| 37 | config.headers = AxiosHeaders.from(config.headers);
|
---|
| 38 |
|
---|
| 39 | // Transform request data
|
---|
| 40 | config.data = transformData.call(
|
---|
| 41 | config,
|
---|
| 42 | config.transformRequest
|
---|
| 43 | );
|
---|
| 44 |
|
---|
| 45 | if (['post', 'put', 'patch'].indexOf(config.method) !== -1) {
|
---|
| 46 | config.headers.setContentType('application/x-www-form-urlencoded', false);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | const adapter = adapters.getAdapter(config.adapter || defaults.adapter);
|
---|
| 50 |
|
---|
| 51 | return adapter(config).then(function onAdapterResolution(response) {
|
---|
| 52 | throwIfCancellationRequested(config);
|
---|
| 53 |
|
---|
| 54 | // Transform response data
|
---|
| 55 | response.data = transformData.call(
|
---|
| 56 | config,
|
---|
| 57 | config.transformResponse,
|
---|
| 58 | response
|
---|
| 59 | );
|
---|
| 60 |
|
---|
| 61 | response.headers = AxiosHeaders.from(response.headers);
|
---|
| 62 |
|
---|
| 63 | return response;
|
---|
| 64 | }, function onAdapterRejection(reason) {
|
---|
| 65 | if (!isCancel(reason)) {
|
---|
| 66 | throwIfCancellationRequested(config);
|
---|
| 67 |
|
---|
| 68 | // Transform response data
|
---|
| 69 | if (reason && reason.response) {
|
---|
| 70 | reason.response.data = transformData.call(
|
---|
| 71 | config,
|
---|
| 72 | config.transformResponse,
|
---|
| 73 | reason.response
|
---|
| 74 | );
|
---|
| 75 | reason.response.headers = AxiosHeaders.from(reason.response.headers);
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | return Promise.reject(reason);
|
---|
| 80 | });
|
---|
| 81 | }
|
---|