From 9b531fc452cf8aa67229fac4f96b8dbcd236aba4 Mon Sep 17 00:00:00 2001 From: Zach Silveira Date: Thu, 19 Jan 2017 19:58:34 -0500 Subject: [PATCH] Clean up callback methods, support getting headers from response --- package.json | 2 +- src/utilities/fetch.js | 5 ++++- src/utilities/normalize.js | 23 ++++++++++++++++------- tests/unit/request.js | 9 +++++++++ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 430d75e..425452d 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/utilities/fetch.js b/src/utilities/fetch.js index 97ff4df..75f64fc 100644 --- a/src/utilities/fetch.js +++ b/src/utilities/fetch.js @@ -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)) }) diff --git a/src/utilities/normalize.js b/src/utilities/normalize.js index b1e8d6a..9d2967c 100644 --- a/src/utilities/normalize.js +++ b/src/utilities/normalize.js @@ -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 @@ -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') { + return [key, callbackMethods[key]({ + value: vars[index], + partial + })] } + return [key, vars[index]] }) // Convert to object diff --git a/tests/unit/request.js b/tests/unit/request.js index 695145d..0e564fa 100644 --- a/tests/unit/request.js +++ b/tests/unit/request.js @@ -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') + }} + ` + }) })