1.4.1 • Published 6 years ago

redux-fetch-epic-builder v1.4.1

Weekly downloads
5
License
MIT
Repository
github
Last release
6 years ago

react-fetch-epic-builder

An utilitary lib for build "fetch actions" and generic EPICs handled by redux-observable.

Follow an example of use based on ducks structure.

import { createBuilder, successType, errorType } from 'utils/epic-builder';
import store from 'stores/configureStore';

const buildEpic = createBuilder({
    jwtGetter: () => store.getState().app.jwt,
});

// action types
const GET_COMMENTS = 'posts/GET_COMMENTS';
const GET_COMMENTS_SUCCESS = successType(GET_COMMENTS);
const GET_COMMENTS_ERROR = errorType(GET_COMMENTS);

const actionsType = {
    GET_COMMENTS,
    GET_COMMENTS_SUCCESS,
    GET_COMMENTS_ERROR,
}

// reducer example
function reducer(state = {}, action) {
    switch (action.type) {
        case GET_COMMENTS_SUCCESS:
            return {
                ...state,
                comments: action.result.data,
            }
        case GET_COMMENTS_ERROR:
            return {
                ...state,
                comments: [],
            }
    }

    return state;
}

//action creators

// get comments from a post
const getComments = (id, page = 1) => ({
    type: GET_COMMENTS, // (required) action type
    host: 'http://myblog.com', // (optional, if a default was configured)
    path: `/posts/${id}/comments`, // (optional) destination path
    query: { // (optional) query parameter object
        page,
    },
})

const actionCreators = {
    getComments,
}

// epics
const epics = [
    buildEpic(GET_COMMENTS),
]

export default reducer;

export {
    actionsType,
    actionCreators,
    epics,
}

posts.js

import { combineEpics } from 'redux-observable';
import { epics as posts } from 'ducks/posts';

const rootEpic = combineEpics(...[
    ...posts,
]);

export default rootEpic;

epics/index.js

1.4.1

6 years ago

1.4.0

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.1.0

6 years ago

6.1.2

6 years ago