2015-06-09 19:25:24 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-06-09 19:25:24 +00:00
|
|
|
*
|
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.
|
2015-06-09 19:25:24 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2015-06-09 19:25:24 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2015-06-09 19:25:24 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
type FormDataValue = any;
|
2015-07-30 02:53:07 +00:00
|
|
|
type FormDataNameValuePair = [string, FormDataValue];
|
|
|
|
|
|
|
|
type Headers = {[name: string]: string};
|
2018-05-11 02:06:46 +00:00
|
|
|
type FormDataPart =
|
|
|
|
| {
|
|
|
|
string: string,
|
|
|
|
headers: Headers,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
uri: string,
|
|
|
|
headers: Headers,
|
|
|
|
name?: string,
|
|
|
|
type?: string,
|
|
|
|
};
|
2015-06-09 19:25:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Polyfill for XMLHttpRequest2 FormData API, allowing multipart POST requests
|
|
|
|
* with mixed data (string, native files) to be submitted via XMLHttpRequest.
|
2015-07-30 02:53:07 +00:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* var photo = {
|
|
|
|
* uri: uriFromCameraRoll,
|
|
|
|
* type: 'image/jpeg',
|
|
|
|
* name: 'photo.jpg',
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* var body = new FormData();
|
|
|
|
* body.append('authToken', 'secret');
|
|
|
|
* body.append('photo', photo);
|
|
|
|
* body.append('title', 'A beautiful photo!');
|
|
|
|
*
|
|
|
|
* xhr.open('POST', serverURL);
|
|
|
|
* xhr.send(body);
|
2015-06-09 19:25:24 +00:00
|
|
|
*/
|
|
|
|
class FormData {
|
2015-07-30 02:53:07 +00:00
|
|
|
_parts: Array<FormDataNameValuePair>;
|
2015-06-09 19:25:24 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this._parts = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
append(key: string, value: FormDataValue) {
|
2015-10-21 22:11:47 +00:00
|
|
|
// The XMLHttpRequest spec doesn't specify if duplicate keys are allowed.
|
|
|
|
// MDN says that any new values should be appended to existing values.
|
|
|
|
// In any case, major browsers allow duplicate keys, so that's what we'll do
|
|
|
|
// too. They'll simply get appended as additional form data parts in the
|
|
|
|
// request body, leaving the server to deal with them.
|
|
|
|
this._parts.push([key, value]);
|
2015-06-09 19:25:24 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 02:53:07 +00:00
|
|
|
getParts(): Array<FormDataPart> {
|
2015-06-09 19:25:24 +00:00
|
|
|
return this._parts.map(([name, value]) => {
|
2018-05-10 22:44:52 +00:00
|
|
|
const contentDisposition = 'form-data; name="' + name + '"';
|
2016-01-08 19:41:30 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const headers: Headers = {'content-disposition': contentDisposition};
|
2015-07-30 02:53:07 +00:00
|
|
|
|
|
|
|
// The body part is a "blob", which in React Native just means
|
|
|
|
// an object with a `uri` attribute. Optionally, it can also
|
|
|
|
// have a `name` and `type` attribute to specify filename and
|
|
|
|
// content type (cf. web Blob interface.)
|
2017-02-10 21:26:14 +00:00
|
|
|
if (typeof value === 'object' && value) {
|
2016-01-12 11:24:42 +00:00
|
|
|
if (typeof value.name === 'string') {
|
|
|
|
headers['content-disposition'] += '; filename="' + value.name + '"';
|
|
|
|
}
|
|
|
|
if (typeof value.type === 'string') {
|
|
|
|
headers['content-type'] = value.type;
|
|
|
|
}
|
|
|
|
return {...value, headers, fieldName: name};
|
2015-06-09 19:25:24 +00:00
|
|
|
}
|
2016-04-06 16:00:43 +00:00
|
|
|
// Convert non-object values to strings as per FormData.append() spec
|
2016-01-12 11:24:42 +00:00
|
|
|
return {string: String(value), headers, fieldName: name};
|
2015-06-09 19:25:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = FormData;
|