Socket
Book a DemoInstallSign in
Socket

redux-saga-fetch-actions

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redux-saga-fetch-actions

Saga for consistent fetch api actions

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

redux-saga-fetch-actions

CI Status npm version

Saga for consistent async actions.

Dispatch an action in FSA format with your desired prefix (default is "FETCH_").

dispatch({
    type: "FETCH_WIDGETS",
    payload: {
        request: () => Promise.resolve([1, 2, 3, 4, 5]),
        onSuccess: (response) => {
            alert(response)
        },
        onFailure: (error) => {
            alert(error)
        }
    },
    meta: "Some data"
})

The saga will then dispatch a request action, and either a success or failure action, both in FSA format. The meta property will be passed through to both. If no meta is provided, it will be set to be the request function.

{
    type: "WIDGETS_SUCCESS",
    payload: [1,2,3,4,5] // The request response,
    meta: "Some data"
}
{
    type: "WIDGETS_FAILED",
    payload: "Something went wrong!", // The exception error,
    meta: "Some data"
    error: true
}

Installation

npm install redux-saga-fetch-actions
import { all } from "redux-saga/effects";
import createSagaMiddleware from "@redux-saga/core";
import { fetchSaga } from "redux-saga-fetch-actions";
import { applyMiddleware, createStore } from "redux";
import { rootReducer } from "../reducers";

const sagaMiddleware = createSagaMiddleware();

export const store = createStore(rootReducer, applyMiddleware(sagaMiddleware));

export function* rootSaga() {
    yield all([fetchSaga("FETCH_")]);
}

sagaMiddleware.run(rootSaga);

Saga Props

PropTypeDescription
prefix
(default = "FETCH_")
stringPrefix to trigger the fetch saga

Action Payload Props

PropTypeDescription
request
(required)
() => PromiseFetch request
onSuccess(response: any) => voidFunction called if the request was successful
onFailure(error: Error) => voidFunction called if the request failed

Keywords

react

FAQs

Package last updated on 28 Jan 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts