2015-06-09 19:25:24 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule FormData
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
type FormDataValue = any;
|
2015-07-30 02:53:07 +00:00
|
|
|
type FormDataNameValuePair = [string, FormDataValue];
|
|
|
|
|
|
|
|
type Headers = {[name: string]: string};
|
|
|
|
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]) => {
|
2015-07-30 02:53:07 +00:00
|
|
|
var contentDisposition = 'form-data; name="' + name + '"';
|
2016-01-08 19:41:30 +00:00
|
|
|
|
2015-12-19 01:03:59 +00:00
|
|
|
/* $FlowIssue(>=0.20.1) #9463928 */
|
2015-07-30 02:53:07 +00:00
|
|
|
var headers: Headers = {'content-disposition': contentDisposition};
|
|
|
|
|
|
|
|
// 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.)
|
2016-01-12 11:24:42 +00:00
|
|
|
if (typeof value === 'object') {
|
|
|
|
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;
|