index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. 'use strict';
  2. import utils from '../utils.js';
  3. import AxiosError from '../core/AxiosError.js';
  4. import transitionalDefaults from './transitional.js';
  5. import toFormData from '../helpers/toFormData.js';
  6. import toURLEncodedForm from '../helpers/toURLEncodedForm.js';
  7. import platform from '../platform/index.js';
  8. import formDataToJSON from '../helpers/formDataToJSON.js';
  9. import methodList from '../core/methodList.js';
  10. const own = (obj, key) => (obj != null && utils.hasOwnProp(obj, key) ? obj[key] : undefined);
  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. return (encoder || JSON.stringify)(rawValue);
  33. }
  34. const defaults = {
  35. transitional: transitionalDefaults,
  36. adapter: ['xhr', 'http', 'fetch'],
  37. transformRequest: [
  38. function transformRequest(data, headers) {
  39. const contentType = headers.getContentType() || '';
  40. const hasJSONContentType = contentType.indexOf('application/json') > -1;
  41. const isObjectPayload = utils.isObject(data);
  42. if (isObjectPayload && utils.isHTMLForm(data)) {
  43. data = new FormData(data);
  44. }
  45. const isFormData = utils.isFormData(data);
  46. if (isFormData) {
  47. return hasJSONContentType ? JSON.stringify(formDataToJSON(data)) : data;
  48. }
  49. if (
  50. utils.isArrayBuffer(data) ||
  51. utils.isBuffer(data) ||
  52. utils.isStream(data) ||
  53. utils.isFile(data) ||
  54. utils.isBlob(data) ||
  55. utils.isReadableStream(data)
  56. ) {
  57. return data;
  58. }
  59. if (utils.isArrayBufferView(data)) {
  60. return data.buffer;
  61. }
  62. if (utils.isURLSearchParams(data)) {
  63. headers.setContentType('application/x-www-form-urlencoded;charset=utf-8', false);
  64. return data.toString();
  65. }
  66. let isFileList;
  67. if (isObjectPayload) {
  68. const formSerializer = own(this, 'formSerializer');
  69. if (contentType.indexOf('application/x-www-form-urlencoded') > -1) {
  70. return toURLEncodedForm(data, formSerializer).toString();
  71. }
  72. if (
  73. (isFileList = utils.isFileList(data)) ||
  74. contentType.indexOf('multipart/form-data') > -1
  75. ) {
  76. const env = own(this, 'env');
  77. const _FormData = env && env.FormData;
  78. return toFormData(
  79. isFileList ? { 'files[]': data } : data,
  80. _FormData && new _FormData(),
  81. formSerializer
  82. );
  83. }
  84. }
  85. if (isObjectPayload || hasJSONContentType) {
  86. headers.setContentType('application/json', false);
  87. return stringifySafely(data);
  88. }
  89. return data;
  90. },
  91. ],
  92. transformResponse: [
  93. function transformResponse(data) {
  94. const transitional = own(this, 'transitional') || defaults.transitional;
  95. const forcedJSONParsing = transitional && transitional.forcedJSONParsing;
  96. const responseType = own(this, 'responseType');
  97. const JSONRequested = responseType === 'json';
  98. if (utils.isResponse(data) || utils.isReadableStream(data)) {
  99. return data;
  100. }
  101. if (
  102. data &&
  103. utils.isString(data) &&
  104. ((forcedJSONParsing && !responseType) || JSONRequested)
  105. ) {
  106. const silentJSONParsing = transitional && transitional.silentJSONParsing;
  107. const strictJSONParsing = !silentJSONParsing && JSONRequested;
  108. try {
  109. return JSON.parse(data, own(this, 'parseReviver'));
  110. } catch (e) {
  111. if (strictJSONParsing) {
  112. if (e.name === 'SyntaxError') {
  113. throw AxiosError.from(e, AxiosError.ERR_BAD_RESPONSE, this, null, own(this, 'response'));
  114. }
  115. throw e;
  116. }
  117. }
  118. }
  119. return data;
  120. },
  121. ],
  122. /**
  123. * A timeout in milliseconds to abort a request. If set to 0 (default) a
  124. * timeout is not created.
  125. */
  126. timeout: 0,
  127. xsrfCookieName: 'XSRF-TOKEN',
  128. xsrfHeaderName: 'X-XSRF-TOKEN',
  129. maxContentLength: -1,
  130. maxBodyLength: -1,
  131. env: {
  132. FormData: platform.classes.FormData,
  133. Blob: platform.classes.Blob,
  134. },
  135. validateStatus: function validateStatus(status) {
  136. return status >= 200 && status < 300;
  137. },
  138. headers: {
  139. common: {
  140. Accept: 'application/json, text/plain, */*',
  141. 'Content-Type': undefined,
  142. },
  143. },
  144. };
  145. utils.forEach(methodList, (method) => {
  146. defaults.headers[method] = {};
  147. });
  148. export default defaults;