2017-01-21 02:40:28 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00: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 02:40:28 +00:00
|
|
|
*
|
|
|
|
* @providesModule convertRequestBody
|
|
|
|
* @flow
|
2018-01-26 17:06:14 +00:00
|
|
|
* @format
|
2017-01-21 02:40:28 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const binaryToBase64 = require('binaryToBase64');
|
|
|
|
|
2018-01-26 17:06:14 +00:00
|
|
|
const Blob = require('Blob');
|
2017-01-21 02:40:28 +00:00
|
|
|
const FormData = require('FormData');
|
|
|
|
|
|
|
|
export type RequestBody =
|
2018-01-26 17:06:14 +00:00
|
|
|
| string
|
|
|
|
| Blob
|
2017-01-21 02:40:28 +00:00
|
|
|
| FormData
|
|
|
|
| {uri: string}
|
|
|
|
| ArrayBuffer
|
2018-01-26 17:06:14 +00:00
|
|
|
| $ArrayBufferView;
|
2017-01-21 02:40:28 +00:00
|
|
|
|
|
|
|
function convertRequestBody(body: RequestBody): Object {
|
|
|
|
if (typeof body === 'string') {
|
|
|
|
return {string: body};
|
|
|
|
}
|
2018-01-26 17:06:14 +00:00
|
|
|
if (body instanceof Blob) {
|
|
|
|
return {blob: body.data};
|
|
|
|
}
|
2017-01-21 02:40:28 +00:00
|
|
|
if (body instanceof FormData) {
|
|
|
|
return {formData: body.getParts()};
|
|
|
|
}
|
|
|
|
if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) {
|
|
|
|
// $FlowFixMe: no way to assert that 'body' is indeed an ArrayBufferView
|
|
|
|
return {base64: binaryToBase64(body)};
|
|
|
|
}
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = convertRequestBody;
|