Last active
June 17, 2019 18:48
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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