This repository has been archived by the owner on Jun 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multipart/form-data mime support
- Loading branch information
Showing
4 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2014 the original author or authors | ||
* @license MIT, see LICENSE.txt for details | ||
* | ||
* @author Michael Jackson | ||
*/ | ||
|
||
/* global FormData, File, Blob */ | ||
|
||
(function (define) { | ||
'use strict'; | ||
|
||
define(function (/* require */) { | ||
|
||
function isFormElement(object) { | ||
return object && | ||
object.nodeType === 1 && // Node.ELEMENT_NODE | ||
object.tagName === 'FORM'; | ||
} | ||
|
||
function createFormDataFromObject(object) { | ||
var formData = new FormData(); | ||
|
||
var value; | ||
for (var property in object) { | ||
if (object.hasOwnProperty(property)) { | ||
value = object[property]; | ||
|
||
if (value instanceof File) { | ||
formData.append(property, value, value.name); | ||
} else if (value instanceof Blob) { | ||
formData.append(property, value); | ||
} else { | ||
formData.append(property, String(value)); | ||
} | ||
} | ||
} | ||
|
||
return formData; | ||
} | ||
|
||
return { | ||
|
||
write: function (object) { | ||
if (typeof FormData === 'undefined') { | ||
throw new Error('The multipart/form-data mime serializer requires FormData support'); | ||
} | ||
|
||
// Support FormData directly. | ||
if (object instanceof FormData) { | ||
return object; | ||
} | ||
|
||
// Support <form> elements. | ||
if (isFormElement(object)) { | ||
return new FormData(object); | ||
} | ||
|
||
// Support plain objects, may contain File/Blob as value. | ||
if (typeof object === 'object' && object !== null) { | ||
return createFormDataFromObject(object); | ||
} | ||
|
||
throw new Error('Unable to create FormData from object ' + object); | ||
} | ||
|
||
}; | ||
}); | ||
|
||
}( | ||
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); } | ||
// Boilerplate for AMD and Node | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2014 the original author or authors | ||
* @license MIT, see LICENSE.txt for details | ||
* | ||
* @author Michael Jackson | ||
*/ | ||
|
||
(function (buster, define) { | ||
'use strict'; | ||
|
||
var assert, refute; | ||
|
||
assert = buster.assertions.assert; | ||
refute = buster.assertions.refute; | ||
|
||
define('rest/mime/type/multipart/form-data-test', function (require) { | ||
|
||
var encoder = require('rest/mime/type/multipart/form-data'); | ||
|
||
buster.testCase('rest/mime/type/multipart/form-data', { | ||
'': { | ||
requiresSupportFor: { FormData: 'FormData' in window }, | ||
'should pass a FormData object through unmodified': function () { | ||
var data = new FormData(); | ||
assert.same(encoder.write(data), data); | ||
}, | ||
'should encode a <form> element as FormData': function () { | ||
var form = document.createElement('form'); | ||
assert.hasPrototype(encoder.write(form), FormData.prototype); | ||
}, | ||
'should encode a plain object as FormData': function () { | ||
assert.hasPrototype(encoder.write({ a: 'string', b: 5 }), FormData.prototype); | ||
}, | ||
'should throw when given a non-object': function () { | ||
assert.exception(function () { | ||
encoder.write('hello world'); | ||
}, 'Error'); | ||
} | ||
} | ||
}); | ||
|
||
}); | ||
|
||
}( | ||
this.buster || require('buster'), | ||
typeof define === 'function' && define.amd ? define : function (id, factory) { | ||
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..'); | ||
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot; | ||
factory(function (moduleId) { | ||
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId); | ||
}); | ||
} | ||
// Boilerplate for AMD and Node | ||
)); |