parse-body.js 794 B

12345678910111213141516171819202122232425
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const types_1 = require("./types");
  4. const parseBody = (response, responseType, parseJson, encoding) => {
  5. const { rawBody } = response;
  6. try {
  7. if (responseType === 'text') {
  8. return rawBody.toString(encoding);
  9. }
  10. if (responseType === 'json') {
  11. return rawBody.length === 0 ? '' : parseJson(rawBody.toString());
  12. }
  13. if (responseType === 'buffer') {
  14. return rawBody;
  15. }
  16. throw new types_1.ParseError({
  17. message: `Unknown body type '${responseType}'`,
  18. name: 'Error'
  19. }, response);
  20. }
  21. catch (error) {
  22. throw new types_1.ParseError(error, response);
  23. }
  24. };
  25. exports.default = parseBody;