Skip to content

Commit 9b531fc

Browse files
committed
Clean up callback methods, support getting headers from response
1 parent 7b33ce5 commit 9b531fc

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "legible",
3-
"version": "0.2.6",
3+
"version": "0.2.7",
44
"description": "cleanly code your api requests",
55
"main": "dist/index.js",
66
"scripts": {

src/utilities/fetch.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import fetch from 'isomorphic-fetch'
33
export default (url, options = {}) => {
44
return new Promise((resolve, reject) => {
55
fetch(url, options)
6-
.then(response => response.json())
6+
.then(response => {
7+
if (options.onResponse) options.onResponse(response)
8+
return response.json()
9+
})
710
.then(json => resolve(json))
811
.catch(error => reject(error))
912
})

src/utilities/normalize.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import processBody from './body'
22

3+
/*
4+
Methods that can be callbacks, ex:
5+
headers: ${partialHeaders => }
6+
*/
7+
const callbackMethods = {
8+
headers: ({ value, partial }) => value(partial),
9+
url: ({ value, partial }) => value(partial.url)
10+
}
11+
312
/*
413
Take in raw query string and
514
return a fetch api compatible object
615
*/
7-
816
const buildObjectFromTag = (strings, vars, partial) => {
917
const namespace = 'legible-request-var-'
1018
return strings
@@ -31,14 +39,15 @@ const buildObjectFromTag = (strings, vars, partial) => {
3139
if (!value.startsWith(namespace)) return [key, value]
3240
// Get the index at the end of the namespaced string
3341
const index = parseInt(value.replace(namespace, ''), 10)
34-
// Return an array of the object key and replaced value from `vars`
35-
if (partial.url && key === 'url' && typeof vars[index] === 'function') {
36-
return [key, vars[index](partial.url)]
37-
}
3842

39-
if (key === 'headers' && typeof vars[index] === 'function') {
40-
return [key, vars[index](partial)]
43+
// run through any callback methods
44+
if (callbackMethods[key] && typeof vars[index] === 'function') {
45+
return [key, callbackMethods[key]({
46+
value: vars[index],
47+
partial
48+
})]
4149
}
50+
4251
return [key, vars[index]]
4352
})
4453
// Convert to object

tests/unit/request.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@ describe('request', () => {
77
`
88
expect(response.country_code).to.equal('US')
99
})
10+
11+
it('returns headers on response', async function () {
12+
await request`
13+
url: https://freegeoip.net/json/github.com
14+
onResponse: ${response => {
15+
expect(response.headers.get('content-type')).to.equal('application/json')
16+
}}
17+
`
18+
})
1019
})

0 commit comments

Comments
 (0)