Skip to content

Commit

Permalink
Clean up callback methods, support getting headers from response
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed Jan 20, 2017
1 parent 7b33ce5 commit 9b531fc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "legible",
"version": "0.2.6",
"version": "0.2.7",
"description": "cleanly code your api requests",
"main": "dist/index.js",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion src/utilities/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import fetch from 'isomorphic-fetch'
export default (url, options = {}) => {
return new Promise((resolve, reject) => {
fetch(url, options)
.then(response => response.json())
.then(response => {
if (options.onResponse) options.onResponse(response)
return response.json()
})
.then(json => resolve(json))
.catch(error => reject(error))
})
Expand Down
23 changes: 16 additions & 7 deletions src/utilities/normalize.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import processBody from './body'

/*
Methods that can be callbacks, ex:
headers: ${partialHeaders => }
*/
const callbackMethods = {
headers: ({ value, partial }) => value(partial),
url: ({ value, partial }) => value(partial.url)
}

/*
Take in raw query string and
return a fetch api compatible object
*/

const buildObjectFromTag = (strings, vars, partial) => {
const namespace = 'legible-request-var-'
return strings
Expand All @@ -31,14 +39,15 @@ const buildObjectFromTag = (strings, vars, partial) => {
if (!value.startsWith(namespace)) return [key, value]
// Get the index at the end of the namespaced string
const index = parseInt(value.replace(namespace, ''), 10)
// Return an array of the object key and replaced value from `vars`
if (partial.url && key === 'url' && typeof vars[index] === 'function') {
return [key, vars[index](partial.url)]
}

if (key === 'headers' && typeof vars[index] === 'function') {
return [key, vars[index](partial)]
// run through any callback methods
if (callbackMethods[key] && typeof vars[index] === 'function') {

This comment has been minimized.

Copy link
@zackify

zackify Jan 20, 2017

Author Member

I think this is way better than those if statements, thoughts, @raygesualdo ?

return [key, callbackMethods[key]({
value: vars[index],
partial
})]
}

return [key, vars[index]]
})
// Convert to object
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@ describe('request', () => {
`
expect(response.country_code).to.equal('US')
})

it('returns headers on response', async function () {
await request`
url: https://freegeoip.net/json/github.com
onResponse: ${response => {
expect(response.headers.get('content-type')).to.equal('application/json')
}}
`
})
})

0 comments on commit 9b531fc

Please sign in to comment.