source: frontend/node_modules/axios/lib/defaults/index.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.9 KB
RevLine 
[9af201e]1'use strict';
2
3import utils from '../utils.js';
4import AxiosError from '../core/AxiosError.js';
5import transitionalDefaults from './transitional.js';
6import toFormData from '../helpers/toFormData.js';
7import toURLEncodedForm from '../helpers/toURLEncodedForm.js';
8import platform from '../platform/index.js';
9import formDataToJSON from '../helpers/formDataToJSON.js';
10
11const own = (obj, key) => (obj != null && utils.hasOwnProp(obj, key) ? obj[key] : undefined);
12
13/**
14 * It takes a string, tries to parse it, and if it fails, it returns the stringified version
15 * of the input
16 *
17 * @param {any} rawValue - The value to be stringified.
18 * @param {Function} parser - A function that parses a string into a JavaScript object.
19 * @param {Function} encoder - A function that takes a value and returns a string.
20 *
21 * @returns {string} A stringified version of the rawValue.
22 */
23function stringifySafely(rawValue, parser, encoder) {
24 if (utils.isString(rawValue)) {
25 try {
26 (parser || JSON.parse)(rawValue);
27 return utils.trim(rawValue);
28 } catch (e) {
29 if (e.name !== 'SyntaxError') {
30 throw e;
31 }
32 }
33 }
34
35 return (encoder || JSON.stringify)(rawValue);
36}
37
38const defaults = {
39 transitional: transitionalDefaults,
40
41 adapter: ['xhr', 'http', 'fetch'],
42
43 transformRequest: [
44 function transformRequest(data, headers) {
45 const contentType = headers.getContentType() || '';
46 const hasJSONContentType = contentType.indexOf('application/json') > -1;
47 const isObjectPayload = utils.isObject(data);
48
49 if (isObjectPayload && utils.isHTMLForm(data)) {
50 data = new FormData(data);
51 }
52
53 const isFormData = utils.isFormData(data);
54
55 if (isFormData) {
56 return hasJSONContentType ? JSON.stringify(formDataToJSON(data)) : data;
57 }
58
59 if (
60 utils.isArrayBuffer(data) ||
61 utils.isBuffer(data) ||
62 utils.isStream(data) ||
63 utils.isFile(data) ||
64 utils.isBlob(data) ||
65 utils.isReadableStream(data)
66 ) {
67 return data;
68 }
69 if (utils.isArrayBufferView(data)) {
70 return data.buffer;
71 }
72 if (utils.isURLSearchParams(data)) {
73 headers.setContentType('application/x-www-form-urlencoded;charset=utf-8', false);
74 return data.toString();
75 }
76
77 let isFileList;
78
79 if (isObjectPayload) {
80 const formSerializer = own(this, 'formSerializer');
81 if (contentType.indexOf('application/x-www-form-urlencoded') > -1) {
82 return toURLEncodedForm(data, formSerializer).toString();
83 }
84
85 if (
86 (isFileList = utils.isFileList(data)) ||
87 contentType.indexOf('multipart/form-data') > -1
88 ) {
89 const env = own(this, 'env');
90 const _FormData = env && env.FormData;
91
92 return toFormData(
93 isFileList ? { 'files[]': data } : data,
94 _FormData && new _FormData(),
95 formSerializer
96 );
97 }
98 }
99
100 if (isObjectPayload || hasJSONContentType) {
101 headers.setContentType('application/json', false);
102 return stringifySafely(data);
103 }
104
105 return data;
106 },
107 ],
108
109 transformResponse: [
110 function transformResponse(data) {
111 const transitional = own(this, 'transitional') || defaults.transitional;
112 const forcedJSONParsing = transitional && transitional.forcedJSONParsing;
113 const responseType = own(this, 'responseType');
114 const JSONRequested = responseType === 'json';
115
116 if (utils.isResponse(data) || utils.isReadableStream(data)) {
117 return data;
118 }
119
120 if (
121 data &&
122 utils.isString(data) &&
123 ((forcedJSONParsing && !responseType) || JSONRequested)
124 ) {
125 const silentJSONParsing = transitional && transitional.silentJSONParsing;
126 const strictJSONParsing = !silentJSONParsing && JSONRequested;
127
128 try {
129 return JSON.parse(data, own(this, 'parseReviver'));
130 } catch (e) {
131 if (strictJSONParsing) {
132 if (e.name === 'SyntaxError') {
133 throw AxiosError.from(e, AxiosError.ERR_BAD_RESPONSE, this, null, own(this, 'response'));
134 }
135 throw e;
136 }
137 }
138 }
139
140 return data;
141 },
142 ],
143
144 /**
145 * A timeout in milliseconds to abort a request. If set to 0 (default) a
146 * timeout is not created.
147 */
148 timeout: 0,
149
150 xsrfCookieName: 'XSRF-TOKEN',
151 xsrfHeaderName: 'X-XSRF-TOKEN',
152
153 maxContentLength: -1,
154 maxBodyLength: -1,
155
156 env: {
157 FormData: platform.classes.FormData,
158 Blob: platform.classes.Blob,
159 },
160
161 validateStatus: function validateStatus(status) {
162 return status >= 200 && status < 300;
163 },
164
165 headers: {
166 common: {
167 Accept: 'application/json, text/plain, */*',
168 'Content-Type': undefined,
169 },
170 },
171};
172
173utils.forEach(['delete', 'get', 'head', 'post', 'put', 'patch', 'query'], (method) => {
174 defaults.headers[method] = {};
175});
176
177export default defaults;
Note: See TracBrowser for help on using the repository browser.