convertRequestBody.js 1000 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @flow
  8. * @format
  9. */
  10. 'use strict';
  11. const binaryToBase64 = require('../Utilities/binaryToBase64');
  12. const Blob = require('../Blob/Blob');
  13. const FormData = require('./FormData');
  14. export type RequestBody =
  15. | string
  16. | Blob
  17. | FormData
  18. | {uri: string, ...}
  19. | ArrayBuffer
  20. | $ArrayBufferView;
  21. function convertRequestBody(body: RequestBody): Object {
  22. if (typeof body === 'string') {
  23. return {string: body};
  24. }
  25. if (body instanceof Blob) {
  26. return {blob: body.data};
  27. }
  28. if (body instanceof FormData) {
  29. return {formData: body.getParts()};
  30. }
  31. if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) {
  32. // $FlowFixMe: no way to assert that 'body' is indeed an ArrayBufferView
  33. return {base64: binaryToBase64(body)};
  34. }
  35. return body;
  36. }
  37. module.exports = convertRequestBody;