From 9b0cfa6a3915656771712b2edf90f0ff0044ac88 Mon Sep 17 00:00:00 2001 From: Agustin Vargas Date: Tue, 7 Feb 2017 14:28:46 -0500 Subject: [PATCH 1/2] add support for fetch api JSON representation --- src/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 2ecf558..b199691 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,11 @@ function isPromise(val) { return val && typeof val.then === 'function'; } +function isFetchRequest(request) { + if (!fetch) return request; + return request.bodyUsed === undefined ? request : request.json(); +} + export default function promiseMiddleware({ dispatch }) { return next => action => { if (!isFSA(action)) { @@ -14,7 +19,7 @@ export default function promiseMiddleware({ dispatch }) { return isPromise(action.payload) ? action.payload.then( - result => dispatch({ ...action, payload: result }), + result => dispatch({ ...action, payload: isFetchRequest(request) }), error => { dispatch({ ...action, payload: error, error: true }); return Promise.reject(error); From 18ed68c76f1dfcc93742bc68b21e53bd40342829 Mon Sep 17 00:00:00 2001 From: Agustin Vargas Date: Tue, 7 Feb 2017 15:49:06 -0500 Subject: [PATCH 2/2] make indent changes, change var, correctly reference globa obj --- src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index b199691..350d3e6 100644 --- a/src/index.js +++ b/src/index.js @@ -5,8 +5,8 @@ function isPromise(val) { } function isFetchRequest(request) { - if (!fetch) return request; - return request.bodyUsed === undefined ? request : request.json(); + if (!global.fetch) return request; + return request.bodyUsed === undefined ? request : request.json(); } export default function promiseMiddleware({ dispatch }) { @@ -19,7 +19,7 @@ export default function promiseMiddleware({ dispatch }) { return isPromise(action.payload) ? action.payload.then( - result => dispatch({ ...action, payload: isFetchRequest(request) }), + result => dispatch({ ...action, payload: isFetchRequest(result) }), error => { dispatch({ ...action, payload: error, error: true }); return Promise.reject(error);