[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | import utils from '../utils.js';
|
---|
| 4 | import AxiosError from '../core/AxiosError.js';
|
---|
| 5 | import transitionalDefaults from './transitional.js';
|
---|
| 6 | import toFormData from '../helpers/toFormData.js';
|
---|
| 7 | import toURLEncodedForm from '../helpers/toURLEncodedForm.js';
|
---|
| 8 | import platform from '../platform/index.js';
|
---|
| 9 | import 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 | */
|
---|
| 21 | function 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 |
|
---|
| 36 | const defaults = {
|
---|
| 37 |
|
---|
| 38 | transitional: transitionalDefaults,
|
---|
| 39 |
|
---|
| 40 | adapter: ['xhr', 'http', 'fetch'],
|
---|
| 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 | utils.isReadableStream(data)
|
---|
| 63 | ) {
|
---|
| 64 | return data;
|
---|
| 65 | }
|
---|
| 66 | if (utils.isArrayBufferView(data)) {
|
---|
| 67 | return data.buffer;
|
---|
| 68 | }
|
---|
| 69 | if (utils.isURLSearchParams(data)) {
|
---|
| 70 | headers.setContentType('application/x-www-form-urlencoded;charset=utf-8', false);
|
---|
| 71 | return data.toString();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | let isFileList;
|
---|
| 75 |
|
---|
| 76 | if (isObjectPayload) {
|
---|
| 77 | if (contentType.indexOf('application/x-www-form-urlencoded') > -1) {
|
---|
| 78 | return toURLEncodedForm(data, this.formSerializer).toString();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | if ((isFileList = utils.isFileList(data)) || contentType.indexOf('multipart/form-data') > -1) {
|
---|
| 82 | const _FormData = this.env && this.env.FormData;
|
---|
| 83 |
|
---|
| 84 | return toFormData(
|
---|
| 85 | isFileList ? {'files[]': data} : data,
|
---|
| 86 | _FormData && new _FormData(),
|
---|
| 87 | this.formSerializer
|
---|
| 88 | );
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | if (isObjectPayload || hasJSONContentType ) {
|
---|
| 93 | headers.setContentType('application/json', false);
|
---|
| 94 | return stringifySafely(data);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | return data;
|
---|
| 98 | }],
|
---|
| 99 |
|
---|
| 100 | transformResponse: [function transformResponse(data) {
|
---|
| 101 | const transitional = this.transitional || defaults.transitional;
|
---|
| 102 | const forcedJSONParsing = transitional && transitional.forcedJSONParsing;
|
---|
| 103 | const JSONRequested = this.responseType === 'json';
|
---|
| 104 |
|
---|
| 105 | if (utils.isResponse(data) || utils.isReadableStream(data)) {
|
---|
| 106 | return data;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | if (data && utils.isString(data) && ((forcedJSONParsing && !this.responseType) || JSONRequested)) {
|
---|
| 110 | const silentJSONParsing = transitional && transitional.silentJSONParsing;
|
---|
| 111 | const strictJSONParsing = !silentJSONParsing && JSONRequested;
|
---|
| 112 |
|
---|
| 113 | try {
|
---|
| 114 | return JSON.parse(data);
|
---|
| 115 | } catch (e) {
|
---|
| 116 | if (strictJSONParsing) {
|
---|
| 117 | if (e.name === 'SyntaxError') {
|
---|
| 118 | throw AxiosError.from(e, AxiosError.ERR_BAD_RESPONSE, this, null, this.response);
|
---|
| 119 | }
|
---|
| 120 | throw e;
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | return data;
|
---|
| 126 | }],
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
| 129 | * A timeout in milliseconds to abort a request. If set to 0 (default) a
|
---|
| 130 | * timeout is not created.
|
---|
| 131 | */
|
---|
| 132 | timeout: 0,
|
---|
| 133 |
|
---|
| 134 | xsrfCookieName: 'XSRF-TOKEN',
|
---|
| 135 | xsrfHeaderName: 'X-XSRF-TOKEN',
|
---|
| 136 |
|
---|
| 137 | maxContentLength: -1,
|
---|
| 138 | maxBodyLength: -1,
|
---|
| 139 |
|
---|
| 140 | env: {
|
---|
| 141 | FormData: platform.classes.FormData,
|
---|
| 142 | Blob: platform.classes.Blob
|
---|
| 143 | },
|
---|
| 144 |
|
---|
| 145 | validateStatus: function validateStatus(status) {
|
---|
| 146 | return status >= 200 && status < 300;
|
---|
| 147 | },
|
---|
| 148 |
|
---|
| 149 | headers: {
|
---|
| 150 | common: {
|
---|
| 151 | 'Accept': 'application/json, text/plain, */*',
|
---|
| 152 | 'Content-Type': undefined
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | };
|
---|
| 156 |
|
---|
| 157 | utils.forEach(['delete', 'get', 'head', 'post', 'put', 'patch'], (method) => {
|
---|
| 158 | defaults.headers[method] = {};
|
---|
| 159 | });
|
---|
| 160 |
|
---|
| 161 | export default defaults;
|
---|