# resource-store-redux

> Resource store for async resources

Latest version **0.4.5** (published 2019-10-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install resource-store-redux
pnpm add resource-store-redux
yarn add resource-store-redux
bun add resource-store-redux
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.5 |
| Published | 2019-10-11 |
| First published | 2019-06-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 125.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Aras Can Akin |
| Maintainers | aacanakin |

## Links

- npm: https://www.npmjs.com/package/resource-store-redux
- Repository: https://github.com/aacanakin/resource-store-redux
- Issues: https://github.com/aacanakin/resource-store-redux/issues
- npm.io page: https://npm.io/package/resource-store-redux

## Recent versions

- 0.4.5 (latest) — 2019-10-11
- 0.4.4 — 2019-10-11
- 0.4.3 — 2019-10-01
- 0.4.2 — 2019-10-01
- 0.4.1 — 2019-10-01
- 0.4.0 — 2019-10-01
- 0.3.4 — 2019-10-01
- 0.3.2 — 2019-10-01
- 0.3.1 — 2019-10-01
- 0.3.0 — 2019-10-01
- 0.2.2 — 2019-07-15
- 0.2.1 — 2019-07-15
- 0.2.0 — 2019-07-14
- 0.1.3 — 2019-07-02
- 0.1.2 — 2019-07-02
- … 2 more at https://npm.io/package/resource-store-redux/versions

## README

### resource-store-redux
[![Build Status](https://travis-ci.org/aacanakin/resource-store-redux.svg?branch=master)](https://travis-ci.org/aacanakin/resource-store-redux) [![Test Coverage](https://api.codeclimate.com/v1/badges/50ceb23e52dda93a7b60/test_coverage)](https://codeclimate.com/github/aacanakin/resource-store-redux/test_coverage)

An async generic resource store for redux

This is a resource store implementation for handling redux state shape for async resources. The async resources can be fetched from http or websocket or even just promises. It contains only the reducer, action implementation. It doesn't have any saga, thunk library. This library is going to stay generic.

It's a remake of [this](https://github.com/oplog/resource-redux) library but without the saga stuff and contains improvements with the use of `typesafe-actions` library.

**Notice:** This package doesn't have functionality to perform request but it handles the state shape changes of async resources 

**Notice:** There will be a separate package based on thunk middleware that maps these actions into `axios` or `fetch` requests

The state shape is the following;

```ts
export interface Resource {
  readonly isBusy: boolean;
  readonly error?: Error;
  readonly data?: any;
}

export interface ResourceStoreState {
  [key: string]: Resource;
}
```

The actions that can be applied to state shape is the following;

```ts
export enum ResourceActionTypes {
  RESOURCE_REQUEST = "@RESOURCE/REQUEST",
  RESOURCE_SUCCESS = "@RESOURCE/SUCCESS",
  RESOURCE_FAILURE = "@RESOURCE/FAILURE",
  RESOURCE_CANCEL = "@RESOURCE/CANCEL",
}
```

The `key` can be described as the resource key. Here's a sample of meaningful resource keys;

```ts
enum ResourceKeys {
    CreateUser = "createUser"
    ListUsers = "listUsers"
    DeleteUser = "deleteUser"
    UpdateUser = "updateUser"
}
```

To wire the reducer into the root reducer;

```ts
import { combineReducers } from 'redux'
import { resourceStore } from 'resource-store-redux'

const resourceStore = resourceStore({
    keys: Object.values(ResourceKeys)
})

export default combineReducers({
    resources: resourceStore.reducer,
    // And your other reducers
})
```
After this, the initial state would be generated for each resource key

To dispatch actions in your container definition;

```ts
import { resourceActions } from 'resource-store-redux'

const mapDispatchToProps = dispatch => ({

  onStuffHappens: () => {
    // request starts
    dispatch(resourceActions.request(ResourceKeys.CreateUser, requestParams)),

    // request ended with 20x
    dispatch(resourceActions.success(ResourceKeys.CreateUser, data))

    // request fails
    dispatch(resourceActions.failure(ResourceKeys.CreateUser, error))

    // cancel request
    dispatch(resourceActions.cancel(ResourceKeys.CreateUser, error))

    // Notice: These actions doesn't make any request but just handles the redux state of requests
  }
})
```

There are also builtin selectors for resources for mapping your state to props;

```ts
import { getResource, getData, isBusy, getError, hasError } from 'resource-store-redux'

const resource = getResource(state, ResourceKeys.CreateUser)
// returns the complete state shape for resource

const data =  getData(state, ResourceKeys.CreateUser)
// returns response body of request

const isResourceBusy = isBusy(state, ResourceKeys.CreateUser)
// returns if the resource key is busy (between request and response)

const error = getError(state, ResourceKeys.CreateUser)
// returns the error on the resource key

const hasResourceError = hasError(state, ResourceKeys.CreateUser)
// returns if the resource key has error
```

#### Roadmap
- [ ] Get rid of tslint, integrate eslint

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