transformData.js 778 B

12345678910111213141516171819202122232425262728
  1. 'use strict';
  2. import utils from './../utils.js';
  3. import defaults from '../defaults/index.js';
  4. import AxiosHeaders from '../core/AxiosHeaders.js';
  5. /**
  6. * Transform the data for a request or a response
  7. *
  8. * @param {Array|Function} fns A single function or Array of functions
  9. * @param {?Object} response The response object
  10. *
  11. * @returns {*} The resulting transformed data
  12. */
  13. export default function transformData(fns, response) {
  14. const config = this || defaults;
  15. const context = response || config;
  16. const headers = AxiosHeaders.from(context.headers);
  17. let data = context.data;
  18. utils.forEach(fns, function transform(fn) {
  19. data = fn.call(config, data, headers.normalize(), response ? response.status : undefined);
  20. });
  21. headers.normalize();
  22. return data;
  23. }