You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.5 KiB

  1. 'use strict';
  2. var utils = require('./utils');
  3. var normalizeHeaderName = require('./helpers/normalizeHeaderName');
  4. var DEFAULT_CONTENT_TYPE = {
  5. 'Content-Type': 'application/x-www-form-urlencoded'
  6. };
  7. function setContentTypeIfUnset(headers, value) {
  8. if (!utils.isUndefined(headers) && utils.isUndefined(headers['Content-Type'])) {
  9. headers['Content-Type'] = value;
  10. }
  11. }
  12. function getDefaultAdapter() {
  13. var adapter;
  14. if (typeof XMLHttpRequest !== 'undefined') {
  15. // For browsers use XHR adapter
  16. adapter = require('./adapters/xhr');
  17. } else if (typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]') {
  18. // For node use HTTP adapter
  19. adapter = require('./adapters/http');
  20. }
  21. return adapter;
  22. }
  23. var defaults = {
  24. adapter: getDefaultAdapter(),
  25. transformRequest: [function transformRequest(data, headers) {
  26. normalizeHeaderName(headers, 'Accept');
  27. normalizeHeaderName(headers, 'Content-Type');
  28. if (utils.isFormData(data) ||
  29. utils.isArrayBuffer(data) ||
  30. utils.isBuffer(data) ||
  31. utils.isStream(data) ||
  32. utils.isFile(data) ||
  33. utils.isBlob(data)
  34. ) {
  35. return data;
  36. }
  37. if (utils.isArrayBufferView(data)) {
  38. return data.buffer;
  39. }
  40. if (utils.isURLSearchParams(data)) {
  41. setContentTypeIfUnset(headers, 'application/x-www-form-urlencoded;charset=utf-8');
  42. return data.toString();
  43. }
  44. if (utils.isObject(data)) {
  45. setContentTypeIfUnset(headers, 'application/json;charset=utf-8');
  46. return JSON.stringify(data);
  47. }
  48. return data;
  49. }],
  50. transformResponse: [function transformResponse(data) {
  51. /*eslint no-param-reassign:0*/
  52. if (typeof data === 'string') {
  53. try {
  54. data = JSON.parse(data);
  55. } catch (e) { /* Ignore */ }
  56. }
  57. return data;
  58. }],
  59. /**
  60. * A timeout in milliseconds to abort a request. If set to 0 (default) a
  61. * timeout is not created.
  62. */
  63. timeout: 0,
  64. xsrfCookieName: 'XSRF-TOKEN',
  65. xsrfHeaderName: 'X-XSRF-TOKEN',
  66. maxContentLength: -1,
  67. maxBodyLength: -1,
  68. validateStatus: function validateStatus(status) {
  69. return status >= 200 && status < 300;
  70. }
  71. };
  72. defaults.headers = {
  73. common: {
  74. 'Accept': 'application/json, text/plain, */*'
  75. }
  76. };
  77. utils.forEach(['delete', 'get', 'head'], function forEachMethodNoData(method) {
  78. defaults.headers[method] = {};
  79. });
  80. utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
  81. defaults.headers[method] = utils.merge(DEFAULT_CONTENT_TYPE);
  82. });
  83. module.exports = defaults;