# redux-struct

> Toolset to keep and update async data state in Redux store when working with REST APIs

Latest version **1.3.1** (published 2018-05-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-struct
pnpm add redux-struct
yarn add redux-struct
bun add redux-struct
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2018-05-14 |
| First published | 2017-10-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | Alexander Tewpin |
| Maintainers | alextewpin, qlean-team |
| Keywords | redux, struct, rest, domain, data, storage |

## Links

- npm: https://www.npmjs.com/package/redux-struct
- Repository: https://github.com/Qlean/redux-struct
- Homepage: https://github.com/Qlean/redux-struct#readme
- Issues: https://github.com/Qlean/redux-struct/issues
- npm.io page: https://npm.io/package/redux-struct

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 1.3.1 (latest) — 2018-05-14
- 1.3.0 — 2018-05-13
- 1.2.1 — 2017-12-06
- 1.2.0 — 2017-10-23

## README

# redux-struct

`redux-struct` is a toolset to keep and update async data state in Redux store when working with REST APIs. It also drastically reduces boilerplate actions and reducers when working with such APIs.

One struct is a piece of store incapsulating fetching state, data and error from one endpoint. They are referred by string ids — for example, order with id `15` can be kept under struct id `order/15`. Structs are initiated automatically when the first action with given id is dispatched.

All structs have the same initial structure:
```js
{
  isFetching: false,
  data: null,
  error: null,
}
```

`redux-struct` provides set of action creators to start fetching data on struct with given id, stop it with result or error, update or reset it. There is also a selector to get struct by its id.

There is no built-in async middleware, `redux-struct` is just a data level abstraction to keep all async stuff organized.

## Installation
```
$ npm i redux-struct
```

## Usage
Add `redux-struct` reducer to your root reducer:
```js
import { combineReducers } from 'redux';
import { reducer as struct } from 'redux-struct';

const rootReducer = combineReducers({
  struct,
  // other reducers
});
```

Make an async wrapper for `redux-struct` actions. Implementation depends from tool you choose to maintain side effects in Redux. Here is example for `redux-saga`:
```js
import { put, call } from 'redux-saga/effects';
import { startStructFetch, stopStructFetch } from 'redux-struct';

import api from 'utils/api';

export function* fetchStruct(structId, url) {
  try {
    yield put(startStructFetch(structId));
    const result = yield call(api.get, url);
    yield put(stopStructFetch(structId, result));
    return { result };
  } catch (error) {
    yield put(stopStructFetch(structId, error));
    return { error };
  }
};
```

Call this async wrapper in other sagas or async action creators:
```js
import { call } from 'redux-saga/effects';

import { fetchStruct } from 'utils/sagas';

function* fetchUser(id) {
  const { result, error } = yield call(fetchStruct, `user/${id}`, `api/user/${id}`);
};
```

Get current state of struct from Redux store for usage in React component:
```js
import { getStruct } from 'redux-struct';

const mapStateToProps = (state, props) => {
  const { userId } = props;
  return {
    user: getStruct(userId)(state),
  };
}
```

## API

### `reducer()`
The struct reducer. Should be mounted to your Redux state at `struct`

### `getStruct(structId:String, [getStructState:Function])`, returns `state => struct:Object`
Selector, gets struct by name. Will return default struct if nothing was found. Has optional second argument `getStructState()` that is used to select the mount point of the `redux-struct` reducer from the root Redux reducer. It defaults to `state => state.struct`.

### `startStructFetch(structId:String)`
Action creator, sets structs `isFetching` to `true` and `error` to `null`. Ignores other fields.

### `stopStructFetch(structId:String, payload:any)`
Action creator, sets structs `isFetching` to `false`. If payload is instance of `Error`, sets `error` to payload and keep the `data`. Otherwise sets `data` to payload and `error` to `null`.

### `updateStruct(structId:String, payload:any)`
Action creator, merges struct with payload.

### `resetStruct(structId:String)`
Action creator, resets struct to its default state.

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