A redux middleware making api call, but with middlewares.
yarn add @abramstyle/redux-api
import {createStore, applyMiddleware } from 'redux';
import reduxApiGenerator from '@abramstyle/redux-api';
const reduxApi = reduxApiGenerator();
const middlewares = [reduxApi];
const store = createStore(reducers, applyMiddlewares(middlewares));
it returns a reduxMiddleware
an function that will transform fetchOptions before request.
you can do some common things for every request.
define global middleware just like callAPI.middleware
define global success checker just like callAPI.isSuccess
define global success callback just like callAPI.success
define global failure callback just like callAPI.failure
a api requesting action has [CALL_API] property. it specified request info.
import {CALL_API} from '@abramstyle/redux-api';
const action = {
[CALL_API]: {
url: 'https://api.domain.com',
types: [
'REQUEST_TYPE',
'SUCCESS_TYPE',
'FAILURE_TYPE'
],
}
};
one of omit, same-origin, or include. must be a string.
must be an object. specified request headers.
it will be append to url as query.
data should be sent to server. if it is an get method, it will be append to url as query.
request method, should be a valid method.
check if a request is success.
the resultPayload will be passed into the function.
return true
or false
to identity if result is succeed.
NOTE: It will override global options.isSuccess
this function will be called after request is success finished.
the result payload will be passed into the function.
NOTE: It will override global options.success
this function will be called after request is failure.
the result payload will be passed into the function.
NOTE: It will override global options.failure
an api request has these status: initial, requesting, success, failure.
redux-api will dispatch these actions.
once a call api action is dispatched, an promise is returned.
{
type: requestType,
payload: callAPI.data || {},
meta: callAPI.meta || {}
}
{
type: successType,
payload: parsedResponseData,
meta: callAPI.meta || {},
}
{
type: failureType,
payload: errorInstance,
meta: callAPI.meta,
error: true,
}
NOTE: payload is an error instance. http status code will be found as error.status. the error message responded from server will be found as error.data. the full response will be find as error.response.
- add tests for new features