index.js 4.9 KB

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