defaults.js 3.5 KB

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