| 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(config, config.transformRequest);
|
|---|
| 41 |
|
|---|
| 42 | if (['post', 'put', 'patch'].indexOf(config.method) !== -1) {
|
|---|
| 43 | config.headers.setContentType('application/x-www-form-urlencoded', false);
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | const adapter = adapters.getAdapter(config.adapter || defaults.adapter, config);
|
|---|
| 47 |
|
|---|
| 48 | return adapter(config).then(
|
|---|
| 49 | function onAdapterResolution(response) {
|
|---|
| 50 | throwIfCancellationRequested(config);
|
|---|
| 51 |
|
|---|
| 52 | // Expose the current response on config so that transformResponse can
|
|---|
| 53 | // attach it to any AxiosError it throws (e.g. on JSON parse failure).
|
|---|
| 54 | // We clean it up afterwards to avoid polluting the config object.
|
|---|
| 55 | config.response = response;
|
|---|
| 56 | try {
|
|---|
| 57 | response.data = transformData.call(config, config.transformResponse, response);
|
|---|
| 58 | } finally {
|
|---|
| 59 | delete config.response;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | response.headers = AxiosHeaders.from(response.headers);
|
|---|
| 63 |
|
|---|
| 64 | return response;
|
|---|
| 65 | },
|
|---|
| 66 | function onAdapterRejection(reason) {
|
|---|
| 67 | if (!isCancel(reason)) {
|
|---|
| 68 | throwIfCancellationRequested(config);
|
|---|
| 69 |
|
|---|
| 70 | // Transform response data
|
|---|
| 71 | if (reason && reason.response) {
|
|---|
| 72 | config.response = reason.response;
|
|---|
| 73 | try {
|
|---|
| 74 | reason.response.data = transformData.call(
|
|---|
| 75 | config,
|
|---|
| 76 | config.transformResponse,
|
|---|
| 77 | reason.response
|
|---|
| 78 | );
|
|---|
| 79 | } finally {
|
|---|
| 80 | delete config.response;
|
|---|
| 81 | }
|
|---|
| 82 | reason.response.headers = AxiosHeaders.from(reason.response.headers);
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | return Promise.reject(reason);
|
|---|
| 87 | }
|
|---|
| 88 | );
|
|---|
| 89 | }
|
|---|