From dd858c6972383218d43d3d8a00b2347735d2bede Mon Sep 17 00:00:00 2001 From: Andrew Luca Date: Thu, 8 Feb 2018 17:26:46 +0200 Subject: [PATCH 1/6] Add response handler --- src/middleware.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/middleware.js b/src/middleware.js index d9b1a89..5f18d7f 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -3,14 +3,14 @@ import { isRSAA, validateRSAA } from './validation'; import { InvalidRSAA, RequestError } from './errors'; import { normalizeTypeDescriptors, actionWith } from './util'; -/** - * A Redux middleware that processes RSAA actions. - * - * @type {ReduxMiddleware} - * @access public - */ -function apiMiddleware({ getState }) { - return next => action => { +const defaults = { + responseOk: res => res.ok +}; + +export function createMiddleware(options = {}) { + const middlewareOptions = Object.assign({}, defaults, options); + + return ({ getState }) => next => action => { // Do not process actions without an [RSAA] property if (!isRSAA(action)) { return next(action); @@ -176,7 +176,7 @@ function apiMiddleware({ getState }) { } // Process the server response - if (res.ok) { + if (middlewareOptions.responseOk(res)) { return next(await actionWith(successType, [action, getState(), res])); } else { return next( @@ -193,4 +193,14 @@ function apiMiddleware({ getState }) { }; } +/** + * A Redux middleware that processes RSAA actions. + * + * @type {ReduxMiddleware} + * @access public + */ +function apiMiddleware({ getState }) { + return createMiddleware()(...arguments); +} + export { apiMiddleware }; From e38e5c80d2cee6b299f609fc7783a09efe04bc71 Mon Sep 17 00:00:00 2001 From: Andrew Luca Date: Thu, 8 Feb 2018 17:38:47 +0200 Subject: [PATCH 2/6] Keep same signature --- src/middleware.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/middleware.js b/src/middleware.js index 5f18d7f..7a3ba41 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -200,7 +200,7 @@ export function createMiddleware(options = {}) { * @access public */ function apiMiddleware({ getState }) { - return createMiddleware()(...arguments); + return createMiddleware()({ getState }); } export { apiMiddleware }; From db7261732d2e2d3b43e721072d6125545cbcda81 Mon Sep 17 00:00:00 2001 From: Andrew Luca Date: Thu, 8 Feb 2018 17:45:53 +0200 Subject: [PATCH 3/6] Export createMiddleware --- src/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 680ac14..3472323 100644 --- a/src/index.js +++ b/src/index.js @@ -33,7 +33,7 @@ import RSAA from './RSAA'; import { isRSAA, validateRSAA, isValidRSAA } from './validation'; import { InvalidRSAA, InternalError, RequestError, ApiError } from './errors'; import { getJSON } from './util'; -import { apiMiddleware } from './middleware'; +import { apiMiddleware, createMiddleware } from './middleware'; export { // Alias RSAA to CALL_API to smooth v1 - v2 migration @@ -48,5 +48,6 @@ export { RequestError, ApiError, getJSON, - apiMiddleware + apiMiddleware, + createMiddleware }; From 3d65cc907d57e628ff5683ce5e83d491afe07fad Mon Sep 17 00:00:00 2001 From: Andrew Luca Date: Thu, 8 Feb 2018 17:59:15 +0200 Subject: [PATCH 4/6] Export defaults into separate file --- src/defaults.js | 5 +++++ src/middleware.js | 5 +---- 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 src/defaults.js diff --git a/src/defaults.js b/src/defaults.js new file mode 100644 index 0000000..9f68c3d --- /dev/null +++ b/src/defaults.js @@ -0,0 +1,5 @@ +const defaults = { + responseOk: res => res.ok +}; + +export default defaults; diff --git a/src/middleware.js b/src/middleware.js index 7a3ba41..7540093 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -2,10 +2,7 @@ import RSAA from './RSAA'; import { isRSAA, validateRSAA } from './validation'; import { InvalidRSAA, RequestError } from './errors'; import { normalizeTypeDescriptors, actionWith } from './util'; - -const defaults = { - responseOk: res => res.ok -}; +import defaults from './defaults'; export function createMiddleware(options = {}) { const middlewareOptions = Object.assign({}, defaults, options); From be8637551055352222b8783dd669662d5b49c846 Mon Sep 17 00:00:00 2001 From: Andrew Luca Date: Fri, 9 Feb 2018 15:08:17 +0200 Subject: [PATCH 5/6] Explicit export createMiddleware --- src/index.js | 4 ++-- src/middleware.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 3472323..0c2f082 100644 --- a/src/index.js +++ b/src/index.js @@ -48,6 +48,6 @@ export { RequestError, ApiError, getJSON, - apiMiddleware, - createMiddleware + createMiddleware, + apiMiddleware }; diff --git a/src/middleware.js b/src/middleware.js index 7540093..adc0b0a 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -4,7 +4,7 @@ import { InvalidRSAA, RequestError } from './errors'; import { normalizeTypeDescriptors, actionWith } from './util'; import defaults from './defaults'; -export function createMiddleware(options = {}) { +function createMiddleware(options = {}) { const middlewareOptions = Object.assign({}, defaults, options); return ({ getState }) => next => action => { @@ -200,4 +200,4 @@ function apiMiddleware({ getState }) { return createMiddleware()({ getState }); } -export { apiMiddleware }; +export { createMiddleware, apiMiddleware }; From a7cb9963468dc5336fef84939cf311ce924147d6 Mon Sep 17 00:00:00 2001 From: Andrew Luca Date: Wed, 7 Mar 2018 12:07:38 +0200 Subject: [PATCH 6/6] Add local ok handler, take fetch from createMiddleware Now user can provide a global ok/fetch handler or a local ok/fetch handler Local ok/fetch handler will take priority --- src/defaults.js | 3 ++- src/middleware.js | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/defaults.js b/src/defaults.js index 9f68c3d..93302a6 100644 --- a/src/defaults.js +++ b/src/defaults.js @@ -1,5 +1,6 @@ const defaults = { - responseOk: res => res.ok + ok: res => res.ok, + fetch }; export default defaults; diff --git a/src/middleware.js b/src/middleware.js index adc0b0a..5b37c35 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -39,7 +39,8 @@ function createMiddleware(options = {}) { body, headers, options = {}, - fetch: doFetch = fetch + fetch: doFetch = middlewareOptions.fetch, + ok = middlewareOptions.ok } = callAPI; const { method, credentials, bailout, types } = callAPI; const [requestType, successType, failureType] = normalizeTypeDescriptors( @@ -173,7 +174,7 @@ function createMiddleware(options = {}) { } // Process the server response - if (middlewareOptions.responseOk(res)) { + if (ok(res)) { return next(await actionWith(successType, [action, getState(), res])); } else { return next(