[d24f17c] | 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'],
|
---|
| 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 |
|
---|
| 152 | utils.forEach(['delete', 'get', 'head', 'post', 'put', 'patch'], (method) => {
|
---|
| 153 | defaults.headers[method] = {};
|
---|
| 154 | });
|
---|
| 155 |
|
---|
| 156 | export default defaults;
|
---|