source: imaps-frontend/node_modules/axios/lib/core/dispatchRequest.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.1 KB
Line 
1'use strict';
2
3import transformData from './transformData.js';
4import isCancel from '../cancel/isCancel.js';
5import defaults from '../defaults/index.js';
6import CanceledError from '../cancel/CanceledError.js';
7import AxiosHeaders from '../core/AxiosHeaders.js';
8import 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 */
17function 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 */
34export 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}
Note: See TracBrowser for help on using the repository browser.