Skip to content

Commit 2bd431d

Browse files
raygesualdozackify
authored andcommitted
Added utility to handle various body formats (#14)
* added utility to handle various body formats * fixed typos
1 parent 58d77ac commit 2bd431d

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
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
},
@@ -43,7 +44,8 @@
4344
"globals": [
4445
"describe",
4546
"it",
46-
"expect"
47+
"expect",
48+
"FormData"
4749
]
4850
}
4951
}

src/utilities/body.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}

src/utilities/normalize.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
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
}

tests/setup.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import 'babel-polyfill'
2+
import FormData from 'form-data'
23
import { expect } from 'chai'
34

45
global.expect = expect
6+
global.FormData = FormData

tests/unit/body.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
})

0 commit comments

Comments
 (0)