Redux api middleware native
Api middleware for redux compatible with native and web apps.
Install
npm install --save redux-api-middleware-native
Adding the middleware to redux store
;;; const reducer = ;const initialState = {}; const store = ;
Usage
; { return CALL_API: endpoint: 'http://www.example.com/resource' method: "POST" headers: 'Content-Type': 'application/json' body: 'username' : 'npm-user' 'password' : 'test' types: 'SUCCESS' 'FAILURE' 'ERROR' meta: id: 'Data to reducer' }
Custom payload / meta handler
; { return CALL_API: endpoint: 'http://www.example.com/resource' method: "POST" headers: 'Content-Type': 'application/json' body: 'username' : 'npm-user' 'password' : 'test' types: type: 'SUCCESS' { return res; } { return actionmeta; } 'FAILURE' 'ERROR' meta: id: 'Data to reducer' }
Responses
SUCCESS
Action type = types0 payload = JSON parsed response error = false meta = Any data that you sent
FAILURE
Type failure means your request not get HTTP status code 200 without any other errors.
Action type = types1 payload = JSON parsed response error = true meta = Any data that you sent
ERROR
Type error means we got exception on some point of code (ex. response parsing).
Action type = types2 payload = ERROR object error = true meta = Any data that you sent