From 0c69f7b5ce206ca5c48a8d4c3977384971da8a9d Mon Sep 17 00:00:00 2001 From: Tung Date: Thu, 15 Sep 2016 18:35:08 +0700 Subject: [PATCH 1/3] resolve body with state when body is a function --- src/middleware.js | 20 ++++++++++++++++++-- test/index.js | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/middleware.js b/src/middleware.js index 0df4014..74ea4dc 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -39,8 +39,8 @@ function apiMiddleware({ getState }) { // Parse the validated RSAA action const callAPI = action[CALL_API]; - var { endpoint, headers } = callAPI; - const { method, body, credentials, bailout, types } = callAPI; + var { endpoint, body, headers } = callAPI; + const { method, credentials, bailout, types } = callAPI; const [requestType, successType, failureType] = normalizeTypeDescriptors(types); // Should we bail out? @@ -76,6 +76,22 @@ function apiMiddleware({ getState }) { } } + // Process [CALL_API].endpoint function + if (typeof body === 'function') { + try { + body = body(getState()); + } catch (e) { + return next(await actionWith( + { + ...requestType, + payload: new RequestError('[CALL_API].body function failed'), + error: true + }, + [action, getState()] + )); + } + } + // Process [CALL_API].headers function if (typeof headers === 'function') { try { diff --git a/test/index.js b/test/index.js index 95765ed..5db201d 100644 --- a/test/index.js +++ b/test/index.js @@ -1231,6 +1231,30 @@ test('apiMiddleware must use an [CALL_API].endpoint function when present', (t) actionHandler(anAction); }); +test('apiMiddleware must use an [CALL_API].body function when present', (t) => { + const api = nock('http://127.0.0.1') + .get('/api/users/1') + .reply(200); + const anAction = { + [CALL_API]: { + endpoint: 'http://127.0.0.1/api/users/1', + body: () => { + t.pass('[CALL_API].body function called'); + return 'body'; + }, + method: 'GET', + types: ['REQUEST', 'SUCCESS', 'FAILURE'] + } + }; + const doGetState = () => {}; + const nextHandler = apiMiddleware({ getState: doGetState }); + const doNext = (action) => {}; + const actionHandler = nextHandler(doNext); + + t.plan(1); + actionHandler(anAction); +}); + test('apiMiddleware must use an [CALL_API].headers function when present', (t) => { const api = nock('http://127.0.0.1') .get('/api/users/1') From 4c98205b6ffb3cb52c6dd33c30a27a178117b429 Mon Sep 17 00:00:00 2001 From: Tung Date: Thu, 15 Sep 2016 18:35:46 +0700 Subject: [PATCH 2/3] fix typo --- src/middleware.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/middleware.js b/src/middleware.js index 74ea4dc..40e8be1 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -76,7 +76,7 @@ function apiMiddleware({ getState }) { } } - // Process [CALL_API].endpoint function + // Process [CALL_API].body function if (typeof body === 'function') { try { body = body(getState()); From 8db491c63d44b2c97bc6611154a01fe9bb804e7e Mon Sep 17 00:00:00 2001 From: Tung Date: Thu, 15 Sep 2016 20:45:04 +0700 Subject: [PATCH 3/3] add test case for failed body function --- test/index.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/index.js b/test/index.js index 5db201d..e30d6de 100644 --- a/test/index.js +++ b/test/index.js @@ -1042,6 +1042,55 @@ test('apiMiddleware must dispatch an error request FSA when [CALL_API].endpoint actionHandler(anAction); }); +test('apiMiddleware must dispatch an error request FSA when [CALL_API].body fails', (t) => { + const anAction = { + [CALL_API]: { + endpoint: 'http://127.0.0.1/api/users/1', + body: () => { + throw new Error + }, + method: 'GET', + types: [ + { + type: 'REQUEST', + payload: 'ignoredPayload', + meta: 'someMeta' + }, + 'SUCCESS', + 'FAILURE' + ] + } + }; + const doGetState = () => {}; + const nextHandler = apiMiddleware({ getState: doGetState }); + const doNext = (action) => { + t.pass('next handler called'); + t.equal( + action.type, + 'REQUEST', + 'dispatched FSA has correct type property' + ); + t.equal( + action.payload.message, + '[CALL_API].body function failed', + 'dispatched FSA has correct payload property' + ); + t.equal( + action.meta, + 'someMeta', + 'dispatched FSA has correct meta property' + ); + t.ok( + action.error, + 'dispatched FSA has correct error property' + ); + }; + const actionHandler = nextHandler(doNext); + + t.plan(5); + actionHandler(anAction); +}); + test('apiMiddleware must dispatch an error request FSA when [CALL_API].headers fails', (t) => { const anAction = { [CALL_API]: {