Skip to content

Commit 3ac5e70

Browse files
raygesualdozackify
authored andcommitted
Abstract out fetch (#20)
* pulled fetch out from partial and request * added tests for fetch.js
1 parent d071f5d commit 3ac5e70

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

src/partial.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fetch from 'isomorphic-fetch'
1+
import fetch from './utilities/fetch'
22
import normalize from './utilities/normalize'
33

44
/*
@@ -12,12 +12,8 @@ export default (strings, ...vars) => {
1212
return (strings, ...vars) => {
1313
let finalData = normalize(strings, vars)
1414
let mergedOptions = { ...options, ...finalData.options }
15+
let finalUrl = finalData.url || url
1516

16-
return new Promise((resolve, reject) => {
17-
fetch(finalData.url || url, mergedOptions)
18-
.then(response => response.json())
19-
.then(json => resolve(json))
20-
.catch(error => reject(error))
21-
})
17+
return fetch(finalUrl, mergedOptions)
2218
}
2319
}

src/request.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
import fetch from 'isomorphic-fetch'
1+
import fetch from './utilities/fetch'
22
import normalize from './utilities/normalize'
33

44
export default (strings, ...vars) => {
55
let { options, url } = normalize(strings, vars)
66

7-
return new Promise((resolve, reject) => {
8-
fetch(url, options)
9-
.then(response => response.json())
10-
.then(json => resolve(json))
11-
.catch(error => reject(error))
12-
})
7+
return fetch(url, options)
138
}

src/utilities/fetch.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import fetch from 'isomorphic-fetch'
2+
3+
export default (url, options = {}) => {
4+
return new Promise((resolve, reject) => {
5+
fetch(url, options)
6+
.then(response => response.json())
7+
.then(json => resolve(json))
8+
.catch(error => reject(error))
9+
})
10+
}

tests/unit/fetch.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import fetch from '../../src/utilities/fetch'
2+
3+
describe('fetch', () => {
4+
it('returns value from response', async function () {
5+
let response = await fetch('https://freegeoip.net/json/github.com')
6+
7+
expect(response.country_code).to.equal('US')
8+
})
9+
})

0 commit comments

Comments
 (0)