2017-01-21 05:40:28 +03:00
|
|
|
/**
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-01-21 05:40:28 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-01-21 05:40:28 +03:00
|
|
|
*
|
|
|
|
* @flow
|
2018-01-26 20:06:14 +03:00
|
|
|
* @format
|
2017-01-21 05:40:28 +03:00
|
|
|
*/
|
2019-10-16 20:03:47 +03:00
|
|
|
|
2017-01-21 05:40:28 +03:00
|
|
|
'use strict';
|
|
|
|
|
2019-05-08 18:44:25 +03:00
|
|
|
const binaryToBase64 = require('../Utilities/binaryToBase64');
|
2017-01-21 05:40:28 +03:00
|
|
|
|
2019-05-08 18:44:25 +03:00
|
|
|
const Blob = require('../Blob/Blob');
|
|
|
|
const FormData = require('./FormData');
|
2017-01-21 05:40:28 +03:00
|
|
|
|
|
|
|
export type RequestBody =
|
2018-01-26 20:06:14 +03:00
|
|
|
| string
|
|
|
|
| Blob
|
2017-01-21 05:40:28 +03:00
|
|
|
| FormData
|
2019-11-21 20:38:13 +03:00
|
|
|
| {uri: string, ...}
|
2017-01-21 05:40:28 +03:00
|
|
|
| ArrayBuffer
|
2018-01-26 20:06:14 +03:00
|
|
|
| $ArrayBufferView;
|
2017-01-21 05:40:28 +03:00
|
|
|
|
|
|
|
function convertRequestBody(body: RequestBody): Object {
|
|
|
|
if (typeof body === 'string') {
|
|
|
|
return {string: body};
|
|
|
|
}
|
2018-01-26 20:06:14 +03:00
|
|
|
if (body instanceof Blob) {
|
|
|
|
return {blob: body.data};
|
|
|
|
}
|
2017-01-21 05:40:28 +03:00
|
|
|
if (body instanceof FormData) {
|
|
|
|
return {formData: body.getParts()};
|
|
|
|
}
|
|
|
|
if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) {
|
2021-04-01 04:19:54 +03:00
|
|
|
/* $FlowFixMe[incompatible-call] : no way to assert that 'body' is indeed
|
|
|
|
* an ArrayBufferView */
|
2017-01-21 05:40:28 +03:00
|
|
|
return {base64: binaryToBase64(body)};
|
|
|
|
}
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = convertRequestBody;
|