Skip to content

Instantly share code, notes, and snippets.

@lukebrandonfarrell
Last active June 17, 2019 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukebrandonfarrell/0fa13c21d28b0adc1e340c470f30e1d1 to your computer and use it in GitHub Desktop.
Save lukebrandonfarrell/0fa13c21d28b0adc1e340c470f30e1d1 to your computer and use it in GitHub Desktop.
A abstract saga for composing request for the redux-saga network layer pattern.
import { put, call } from "redux-saga";
export function* AbstractAPISaga(
action,
baseUrl,
api,
type,
) {
try {
const payload = { ...action.payload };
const meta = { ...action.meta };
const data = yield call(api, baseUrl, payload, meta);
yield put({
type,
payload: { data },
error: false
});
} catch (e) {
const data = _get(e, "response.data", null) || {};
yield put({ type, payload: { ...data }, error: true });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment