{"id":21520525,"url":"https://github.com/mrnkr/redux-saga-toolbox","last_synced_at":"2026-05-04T16:36:17.702Z","repository":{"id":57128447,"uuid":"184932013","full_name":"mrnkr/redux-saga-toolbox","owner":"mrnkr","description":"General use saga builders","archived":false,"fork":false,"pushed_at":"2020-08-08T22:26:42.000Z","size":147,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-09T09:36:10.930Z","etag":null,"topics":["helpers-library","redux","redux-saga","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@mrnkr/redux-saga-toolbox","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mrnkr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-04T18:41:14.000Z","updated_at":"2020-08-08T22:26:44.000Z","dependencies_parsed_at":"2022-08-26T23:02:37.240Z","dependency_job_id":null,"html_url":"https://github.com/mrnkr/redux-saga-toolbox","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrnkr%2Fredux-saga-toolbox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrnkr%2Fredux-saga-toolbox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrnkr%2Fredux-saga-toolbox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrnkr%2Fredux-saga-toolbox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrnkr","download_url":"https://codeload.github.com/mrnkr/redux-saga-toolbox/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244075637,"owners_count":20393979,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["helpers-library","redux","redux-saga","typescript"],"created_at":"2024-11-24T01:01:43.032Z","updated_at":"2026-05-04T16:36:12.643Z","avatar_url":"https://github.com/mrnkr.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Redux-Saga Toolbox\n\n[![NPM version][npm-image]][npm-url]\n[![Downloads][downloads-image]][npm-url]\n[![Build Status](https://travis-ci.com/mrnkr/redux-saga-toolbox.svg?branch=master)](https://travis-ci.com/mrnkr/redux-saga-toolbox)\n[![codecov](https://codecov.io/gh/mrnkr/redux-saga-toolbox/branch/master/graph/badge.svg)](https://codecov.io/gh/mrnkr/redux-saga-toolbox)\n[![License][license]][npm-url]\n\n[npm-image]:http://img.shields.io/npm/v/@mrnkr/redux-saga-toolbox.svg\n[npm-url]:https://npmjs.org/package/@mrnkr/redux-saga-toolbox\n[downloads-image]:http://img.shields.io/npm/dm/@mrnkr/redux-saga-toolbox.svg\n[license]:https://img.shields.io/github/license/mrnkr/redux-saga-toolbox\n\nA set of utilities meant to have you write less and do more, better.\n\n### Motivation\n\nSince I started using redux-saga (which I use alongside reduxsauce) I noticed something. Not only was I writing the same logic over and over again but I was also seeing all the devs in the project do things their own way, for better or for worse. We were following no structure nor were we reutilizing logic in any way. How does one solve such a problem? Taking the best from ngrx/entity and creating factory functions whose output are the saga handlers. I like fractal as far as file structures are concerned, by auto generating sagas I can keep the logic within one file without the file getting too big, hence, I can use fractal and be happy :)\n\n### Installation\n\n```zsh\nyarn add @mrnkr/redux-saga-toolbox\n```\n\n### Quick start\n\nThere is a lot going on here, starting won't be too quick... Sorry hehe. Let's go through all possible use cases (at least the ones I could come up with).\n\n#### Single event sagas (the ones that are not observables)\n\n##### The most basic of basic sagas\n\nWhat did I consider a basic saga? One which is triggered by a request action and dispatches a loading, a commit and a success action. It may dispatch an error action if it needs to. The way to make one of those is the following:\n\n```typescript\nimport { createSingleEventSaga } from '@mrnkr/redux-saga-toolbox';\nimport APIClient from 'my-api';\n\nconst watcher = createSingleEventSaga({\n  takeEvery: 'REQUEST',\n  loadingAction: () =\u003e ({ type: 'LOADING' }),\n  commitAction: payload =\u003e ({ payload, type: 'COMMIT' }),\n  successAction: payload =\u003e ({ payload, type: 'SUCCESS' }),\n  errorAction: error =\u003e ({ error, type: 'ERROR' }),\n  action: APIClient.getMeThatData,\n});\n```\n\nBasically, just specify the action you want to use as the request action in the `takeEvery` property. Then specify the action creators for the saga to `put` and lastly specify some function to execute as the long running action that represents the center of it all, like... your API call!\n\nAlso, you may fancy using an alternative way of providing your takeEvery parameter. An equivalent way of listening to the same action is `takeEvery: action =\u003e action.type === 'REQUEST'`. Why is this useful? Well, say you have multiple actions and they should all trigger the same saga, this can be achieved in the following fashion: `takeEvery: action =\u003e action.type === actionType1 || action.type === actionType2`. This I added because I needed it, hope some of you will also find it useful!\n\n##### Task cancellation\n\nTake the same basic saga. Say the download is taking longer than expected and you want to cancel it. Just dispatch the cancel action you specified in the configuration like so:\n\n```typescript\nconst watcher = createSingleEventSaga({\n  ...restOfTheConfiguration,\n  cancelActionType: 'CANCEL',\n});\n```\n\n##### Uploading data with optimistic feedback\n\nDo you enjoy using Google apps? Do you like how they allow you to undo actions instead of making you confirm everything before it gets done? Well, that undo mechanism can easily be implemented with redux-saga-toolbox.\n\n```typescript\nconst watcher = createSingleEventSaga({\n  ...restOfTheConfiguration,\n  runAfterCommit: true,\n  undoThreshold: 5000,\n  undoActionType: 'REQUEST_UNDO',\n  undoAction: (payload) =\u003e ({ payload, type: 'UNDO' }),\n  undoPayloadBuilder?: (args) =\u003e { return someProcessedVersionOfTheArgs; },\n});\n```\n\nLet us dive a bit more into detail... By setting `runAfterCommit` to `true` we're saying that the `commitAction()` should be dispatched with the action payload and before the action gets run, hence, before the data gets processed by the API. The `undoThreshold` is the time the user has to undo the action before it is made definitive, in this example I set it to 5 seconds. Last but not least, `undoActionType` is the action which we will have to dispatch in order to trigger the undoing of the action which will cut the execution of it and also dispatch the `undoAction` with a payload equal to the return value of `undoPayloadBuilder`. You don't need to provide the last function, it defaults to the identity function (returns what it receives). Also, you should know it is a generator function, it returns stuff (if it doesn't it breaks stuff), but it is still a generator function.\n\nSomething else you may find useful is that if you define an `undoAction` you may set the `undoOnError` flag to `true` and that will allow you to not have your redux store get corrupt in case of failures.\n\n##### Processing the action payload and the API call result\n\nI wanted to give up on as little flexibility as I possibly could. That means I wanted to be able to process the payload received from the action that triggered the saga and also I wanted to process the result the API gave me. To do that I exposed two hooks which are generator functions which are expected to return the processed payload or result.\n\nThis is how they're meant to be used:\n\n```typescript\nconst watcher = createSingleEventSaga({\n  ...restOfTheConfiguration,\n  beforeAction: (args) =\u003e { return processedArgs; },\n  afterAction: (res, args) =\u003e { return processedResult; },\n});\n```\n\nSome details to take into consideration: `beforeAction` gets run before commit regardless of whether the action runs before or after it. If `beforeAction` is provided and `runAfterCommit` is set to `true` then `commitAction()` will have the processed payload instead of the one it received at first. `afterAction` receives the result of the API call and as a second (optional) parameter it receives the payload (as returned by `beforeAction`).\n\n##### Crappy API? Retry!\n\nRedux saga lets us easily retry stuff, I did not use that but I still offer the same possiblity in this library. If you set `retry` to any number greater than 0 (0 is the default value) you will have let your API fail on you without you giving up on it... Good guy you!\n\n```typescript\nconst watcher = createSingleEventSaga({\n  ...restOfTheConfiguration,\n  retry: 3,\n});\n```\n\n##### Taking too long to respond? Timeout\n\nSame logic as above. Nice thing about this? You can use the `timeout` property in conjunction with the `retry` property and give each try a maximum time to complete. Effortlessly, by the way 😍\n\n```typescript\n// try 3 times at most but don't let\n// each try take longer than 800ms\nconst watcher = createSingleEventSaga({\n  ...restOfTheConfiguration,\n  retry: 3,\n  timeout: 800,\n});\n```\n\n### Changelog\n\n* 1.0.0 - First release, had some trouble with config files. That's why the actual first release was 1.0.2 😬\n* 1.0.3 - If you were one of the few amazing people that downloaded the library as soon as I released it you may have noticed inconsistencies in the documentation... I tried to fix all the problems I could find in this version... Sorry!!\n* 1.0.8 - Updated the documentation to fix some discrepancies and fixed the forms reducer so that it does not re-register a form.\n* 1.0.9 - Updated selectors to be memoized.\n* 1.0.10 - Updated saga generator typings to support predicates as takeEvery and subscribe actions. If you're like me you wanted this to trigger the same saga with multiple actions.\n* 1.0.11 - Added support for initial values in the forms module.\n* 1.0.12 - Added support for not clearing forms on submit.\n* 1.0.13 - Added support for multiple submissions on the same form (sorry if you had to deal with this error 😞)\n* 1.0.14 - Fixed bug in addAll in entity adapter (both sorted and unsorted) (having bugs there means that I did not copy the whole thing carelessly, just remade it carelessly 😛)\n* 1.0.15 - Fixed bug that when a form had an error the saga stopped running.\n* 1.0.16 - Fixed bug in form saga - stops listening when the form is cleared.\n* 1.0.18 - Deprecated entity module and forms module. To use entity I encourage you find a way to do it yourself, to manage forms I recommend formik.\n* 2.0.0 - Removed deprecated modules\n\n### The boy scout rule\n\nIt's not enough to write code well. The code has to be kept clean over time. We've all seen code rot and degrade as time passes. So we must take an active role\nin preventing that degradation.\n\nThe boy scouts of America have a simple rule that we can apply to our profession.\n\nLeave the campground better than you found it.\n\nIf we all checked-in out code a little cleaner than we checked it out, the code simply could not rot. The cleanup doesn't have to be something big. Change one\nvariable name for the better, break up one function that's a little too large, eliminate one small bit of duplication, clean up one composite if statement.\n\nCan you imagine working on a project where the code simply got better as time passed? Do you believe that any other option is professional? Indeed, isn't continuous\nimprovement an intrinsic part of professionalism?\n\nRobert C. Martin - from the book Clean Code (he says the took this from another book but I didn't take note which one)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrnkr%2Fredux-saga-toolbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrnkr%2Fredux-saga-toolbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrnkr%2Fredux-saga-toolbox/lists"}