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

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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