# redux-create-fsa-reducer

> Redux reducer factory that is designed for use with Flux Standard Actions (FSA).

Latest version **2.0.1** (published 2019-02-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install redux-create-fsa-reducer
pnpm add redux-create-fsa-reducer
yarn add redux-create-fsa-reducer
bun add redux-create-fsa-reducer
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2019-02-18 |
| First published | 2017-11-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Martin Jurča |
| Maintainers | jurca |
| Keywords | redux, reducer, factory, fsa |

## Links

- npm: https://www.npmjs.com/package/redux-create-fsa-reducer
- Repository: https://github.com/jurca/redux-create-fsa-reducer
- Homepage: https://github.com/jurca/redux-create-fsa-reducer#readme
- Issues: https://github.com/jurca/redux-create-fsa-reducer/issues
- npm.io page: https://npm.io/package/redux-create-fsa-reducer

## Recent versions

- 2.0.1 (latest) — 2019-02-18
- 2.0.0 — 2019-02-08
- 1.0.0 — 2017-11-09

## README

# redux-create-fsa-reducer

Redux reducer factory that is designed for use with Flux Standard Actions (FSA).

This package provides the same functionality as the
[redux-create-reducer](https://www.npmjs.com/package/redux-create-reducer)
(originally based on
[this code](https://redux.js.org/docs/recipes/ReducingBoilerplate.html#generating-reducers)),
except that the action handler methods will receive the action's payload
directly, thus reducing a small amount of boilerplate code. This, however,
requires all actions to follow the
[Flux Standard Action](https://github.com/acdlite/flux-standard-action)
pattern.

## Usage

```typescript
import createReducer from 'redux-create-fsa-reducer'

enum Action {
    ADD_TODO = "ADD_TODO",
    FETCH_TODO_LIST_DONE = "FETCH_TODO_LIST_DONE",
}

/*
Alternative way of declaring action types enum:

interface IActionEnum {
    ADD_TODO: "ADD_TODO"
    FETCH_TODO_LIST_DONE: "FETCH_TODO_LIST_DONE"
}
// The ": IActionEnum" is necessary to make the enum constants unique symbols
const Action: IActionEnum = {
    ADD_TODO: "ADD_TODO",
    FETCH_TODO_LIST_DONE: "FETCH_TODO_LIST_DONE",
}
*/

interface IState {
    items: string[]
    lastError: null | Error,
    lastErrorDetails: any
}
const DEFAULT_STATE: IState = {
    items: [],
    lastError: null,
    lastErrorDetails: null,
}

// An interface cannot be used here because interfaces, unlike type literals,
// can be extended.
type Reducer = {
    [Action.ADD_TODO]?(state: IState, payload: string): IState,
    [Action.FETCH_TODO_LIST_DONE]?(state: IState, payload: string[]): IState,
    [Action.FETCH_TODO_LIST_DONE]?(state: IState, payload: void, error: Error): IState,
}

export default createReducer<IState, Reducer>(DEFAULT_STATE, {
  [Action.ADD_TODO](state: IState, text: string): IState {
    return {
      ...state,
      items: [...state.items, text.trim()],
    }
  },
  [Action.FETCH_TODO_LIST_DONE](
      state: IState,
      todoList: void | string[],
      error?: Error,
      meta?: any,
  ): IState {
    if (!todoList) {
      return {
        ...state,
        lastError: error,
        lastErrorDetails: meta
      }
    }
    
    return {
      ...state,
      items: todoList,
    }
  },
})
```

---
_Source: https://npm.io/package/redux-create-fsa-reducer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
