File tree Expand file tree Collapse file tree 5 files changed +54
-2
lines changed
Expand file tree Collapse file tree 5 files changed +54
-2
lines changed Original file line number Diff line number Diff line change 3333 "babel-preset-latest" : " ^6.16.0" ,
3434 "babel-register" : " ^6.18.0" ,
3535 "chai" : " ^3.5.0" ,
36+ "form-data" : " ^2.1.2" ,
3637 "mocha" : " ^3.2.0" ,
3738 "standard" : " ^8.6.0"
3839 },
4344 "globals" : [
4445 " describe" ,
4546 " it" ,
46- " expect"
47+ " expect" ,
48+ " FormData"
4749 ]
4850 }
4951}
Original file line number Diff line number Diff line change 1+ export default ( body ) => {
2+ // Handle empty case
3+ if ( ! body ) return null
4+ // Handle FormData
5+ if ( body instanceof FormData ) return body
6+ try {
7+ // Handle already stringified JSON
8+ JSON . parse ( body )
9+ return body
10+ } catch ( err ) {
11+ // Handle plain object
12+ return JSON . stringify ( body )
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ import processBody from './body'
2+
13/*
24 Take in raw query string and
35 return a fetch api compatible object
@@ -45,7 +47,7 @@ export default (strings, vars) => {
4547 url,
4648 options : {
4749 method : method || 'GET' ,
48- body : body ? JSON . stringify ( body ) : null ,
50+ body : processBody ( body ) ,
4951 ...options
5052 }
5153 }
Original file line number Diff line number Diff line change 11import 'babel-polyfill'
2+ import FormData from 'form-data'
23import { expect } from 'chai'
34
45global . expect = expect
6+ global . FormData = FormData
Original file line number Diff line number Diff line change 1+ import processBody from '../../src/utilities/body'
2+
3+ describe ( 'processBody' , ( ) => {
4+ it ( 'returns null when no value is passed' , ( ) => {
5+ const body = processBody ( )
6+ expect ( body ) . to . equal ( null )
7+ } )
8+
9+ it ( 'returns null when empty string is passed' , ( ) => {
10+ const body = processBody ( '' )
11+ expect ( body ) . to . equal ( null )
12+ } )
13+
14+ it ( 'returns input when FormData is passed' , ( ) => {
15+ const fd = new FormData ( )
16+ const body = processBody ( fd )
17+ expect ( body ) . to . equal ( fd )
18+ } )
19+
20+ it ( 'returns input when stringified data is passed' , ( ) => {
21+ const data = JSON . stringify ( { value : 'test' } )
22+ const body = processBody ( data )
23+ expect ( body ) . to . equal ( data )
24+ } )
25+
26+ it ( 'returns stringified data when object is passed' , ( ) => {
27+ const rawData = { value : 'test' }
28+ const data = JSON . stringify ( rawData )
29+ const body = processBody ( rawData )
30+ expect ( body ) . to . equal ( data )
31+ } )
32+ } )
You can’t perform that action at this time.
0 commit comments