0.1.3 • Published 4 years ago

@ytkj/redux-axios-middleware v0.1.3

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

Redux Axios Middleware

This repository is related to npm package @ytkj/redux-axios-middleware.

Installation

npm install -S @ytkj/redux-axios-middleware

Usage

  1. Create your store applying raMiddleware with thunkMiddleware.

    import { createStore, applyMiddleware } from 'redux';
    import thunkMiddleware from 'redux-thunk';
    import { AxiosError } from 'axios';
    
    import { raAction, createRAMiddleware, RAAction } from '@ytkj/redux-axios-middleware';
    
    const raMiddleware = createRAMiddleware({
        actionBeforeFetch: { type: 'FOO_ACTION'},
        actionAfterFetch: { type: 'BAR_ACTION'},
        actionCreatorOnFail: (e: AxiosError) => ({type: 'ERROR', payload: e.response.data}),
    });
    store = createStore(reducer, applyMiddleware(thunkMiddleware, raMiddleware));
  2. dispatch raAction from inside your ThunkAction.

    import { ThunkAction, ThunkDispatch }  from 'redux-thunk';
    
    const FETCH_MESSAGE: 'FETCH_MESSAGE' = 'FETCH_MESSAGE';
    interface FetchMessage extends Action<typeof FETCH_MESSAGE> {
        payload: string;
    }
    
    function fetchMessage(
        url: string
    ): ThunkAction<Promise<RAAction>, State, void, RAAction|FetchMessage> {
    
        let thunkAction = async(
            dispatch: ThunkDispatch<State, void, RAAction|FetchMessage>,
            getState: () => State
        ): Promise<RAAction> => {
    
            return dispatch(raAction({
                url: url,
                method: 'GET',
                successActionType: FETCH_MESSAGE,
            }));
        }
    
        return thunkAction;
    }
    
    store.dispatch(fetchMessage('/api/hello/world'));

API

createRAMiddleware()

middleware factory function.

arguments

typedescription
CreateRAMiddlewareOption1st argument

return

typedescription
RAMiddlewarecreated raMiddleware

CreateRAMiddlewareOption

propertytypedescription
actionBeforeFetch?Action\|Action[]action that should be dispatched before sending Ajax request.
actionAfterFetch?Action\|Action[]action that should be dispatched after receiving Ajax response, whether success or fail.
actionCreatorOnFail?(e: AxiosError) => Action \| Array<(e: AxiosError) => Action>function that shold be called after receiving failure response. AxiosError object will be passed to this function.
axiosClient?AxiosInstanceaxios instance that shold be used to call Ajax request; default to axios global object.

raAction()

action creator for RAAction.

argument

typedescription
RAActionPayload1st argument.

return

typedescription
RAActiondispatched and will trigger some events in raMiddleware

RAActionPayload

propertytypedescription
urlstringrequest URL.
method'GET'\|'POST'\|'PUT'requst HTTP method.
successActionTypeanyaction.type string.
requestBody?anyrequest body (only for 'POST' and 'PUT').
requestConfig?AxiosRequestConfigrequest config for axios.
successChainAction?Action\|TunkAction\|Action[]\|ThunkAction[]action that shold be dispatced after receiving Ajax response only if succeed.
successChainActionCreator?ActionCreator<Action\|ThunkAction> \| Array<ActionCreator<Action>\|ActionCreator<TunkAction>>action creator that shold be dispatched after receiving Ajax response only if succeed. response content(res.data) will be passed as argument.
0.1.3

4 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.1

5 years ago