index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 'use strict';
  2. var utils = require('../utils');
  3. var normalizeHeaderName = require('../helpers/normalizeHeaderName');
  4. var AxiosError = require('../core/AxiosError');
  5. var transitionalDefaults = require('./transitional');
  6. var toFormData = require('../helpers/toFormData');
  7. var DEFAULT_CONTENT_TYPE = {
  8. 'Content-Type': 'application/x-www-form-urlencoded'
  9. };
  10. function setContentTypeIfUnset(headers, value) {
  11. if (!utils.isUndefined(headers) && utils.isUndefined(headers['Content-Type'])) {
  12. headers['Content-Type'] = value;
  13. }
  14. }
  15. function getDefaultAdapter() {
  16. var adapter;
  17. if (typeof XMLHttpRequest !== 'undefined') {
  18. // For browsers use XHR adapter
  19. adapter = require('../adapters/xhr');
  20. } else if (typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]') {
  21. // For node use HTTP adapter
  22. adapter = require('../adapters/http');
  23. }
  24. return adapter;
  25. }
  26. function stringifySafely(rawValue, parser, encoder) {
  27. if (utils.isString(rawValue)) {
  28. try {
  29. (parser || JSON.parse)(rawValue);
  30. return utils.trim(rawValue);
  31. } catch (e) {
  32. if (e.name !== 'SyntaxError') {
  33. throw e;
  34. }
  35. }
  36. }
  37. return (encoder || JSON.stringify)(rawValue);
  38. }
  39. var defaults = {
  40. transitional: transitionalDefaults,
  41. adapter: getDefaultAdapter(),
  42. transformRequest: [function transformRequest(data, headers) {
  43. normalizeHeaderName(headers, 'Accept');
  44. normalizeHeaderName(headers, 'Content-Type');
  45. if (utils.isFormData(data) ||
  46. utils.isArrayBuffer(data) ||
  47. utils.isBuffer(data) ||
  48. utils.isStream(data) ||
  49. utils.isFile(data) ||
  50. utils.isBlob(data)
  51. ) {
  52. return data;
  53. }
  54. if (utils.isArrayBufferView(data)) {
  55. return data.buffer;
  56. }
  57. if (utils.isURLSearchParams(data)) {
  58. setContentTypeIfUnset(headers, 'application/x-www-form-urlencoded;charset=utf-8');
  59. return data.toString();
  60. }
  61. var isObjectPayload = utils.isObject(data);
  62. var contentType = headers && headers['Content-Type'];
  63. var isFileList;
  64. if ((isFileList = utils.isFileList(data)) || (isObjectPayload && contentType === 'multipart/form-data')) {
  65. var _FormData = this.env && this.env.FormData;
  66. return toFormData(isFileList ? {'files[]': data} : data, _FormData && new _FormData());
  67. } else if (isObjectPayload || contentType === 'application/json') {
  68. setContentTypeIfUnset(headers, 'application/json');
  69. return stringifySafely(data);
  70. }
  71. return data;
  72. }],
  73. transformResponse: [function transformResponse(data) {
  74. var transitional = this.transitional || defaults.transitional;
  75. var silentJSONParsing = transitional && transitional.silentJSONParsing;
  76. var forcedJSONParsing = transitional && transitional.forcedJSONParsing;
  77. var strictJSONParsing = !silentJSONParsing && this.responseType === 'json';
  78. if (strictJSONParsing || (forcedJSONParsing && utils.isString(data) && data.length)) {
  79. try {
  80. return JSON.parse(data);
  81. } catch (e) {
  82. if (strictJSONParsing) {
  83. if (e.name === 'SyntaxError') {
  84. throw AxiosError.from(e, AxiosError.ERR_BAD_RESPONSE, this, null, this.response);
  85. }
  86. throw e;
  87. }
  88. }
  89. }
  90. return data;
  91. }],
  92. /**
  93. * A timeout in milliseconds to abort a request. If set to 0 (default) a
  94. * timeout is not created.
  95. */
  96. timeout: 0,
  97. xsrfCookieName: 'XSRF-TOKEN',
  98. xsrfHeaderName: 'X-XSRF-TOKEN',
  99. maxContentLength: -1,
  100. maxBodyLength: -1,
  101. env: {
  102. FormData: require('./env/FormData')
  103. },
  104. validateStatus: function validateStatus(status) {
  105. return status >= 200 && status < 300;
  106. },
  107. headers: {
  108. common: {
  109. 'Accept': 'application/json, text/plain, */*'
  110. }
  111. }
  112. };
  113. utils.forEach(['delete', 'get', 'head'], function forEachMethodNoData(method) {
  114. defaults.headers[method] = {};
  115. });
  116. utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
  117. defaults.headers[method] = utils.merge(DEFAULT_CONTENT_TYPE);
  118. });
  119. module.exports = defaults;