# redux-sleep

> Convention over configuration approach to redux facing RESTful endpoints.

Latest version **0.5.2** (published 2018-05-13) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.2 |
| Published | 2018-05-13 |
| First published | 2018-02-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 25.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Jack Scott |
| Maintainers | jackrobertscott |
| Keywords | redux, thunk, actions, restful, rest |

## Links

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

## Dependencies (4)

- [pluralize](https://npm.io/package/pluralize.md) ^7.0.0
- [change-case](https://npm.io/package/change-case.md) ^3.0.1
- [redux-actions](https://npm.io/package/redux-actions.md) ^2.2.1
- [babel-polyfill](https://npm.io/package/babel-polyfill.md) ^6.26.0

## Recent versions

- 0.5.2 (latest) — 2018-05-13
- 0.5.1 — 2018-05-12
- 0.5.0 — 2018-05-12
- 0.4.4 — 2018-05-07
- 0.4.3 — 2018-04-27
- 0.4.2 — 2018-03-27
- 0.4.1 — 2018-03-18
- 0.4.0 — 2018-03-17
- 0.3.4 — 2018-03-14
- 0.3.3 — 2018-03-10
- 0.3.2 — 2018-03-10
- 0.3.1 — 2018-03-10
- 0.3.0 — 2018-03-10
- 0.2.0 — 2018-02-23
- 0.1.2 — 2018-02-20
- … 2 more at https://npm.io/package/redux-sleep/versions

## README

# redux-sleep

> Simple structure to redux reducers to help speed up development.

[![Build Status](https://travis-ci.org/jackrobertscott/redux-sleep.svg?branch=master)](https://travis-ci.org/jackrobertscott/redux-sleep)

Convention over configuration approach to redux reducers which face RESTful endpoints.

## Install

Get started by installing hobson (and mongoose, if you haven't already).

```sh
npm install --save redux redux-sleep
```

To use the thunk functions; you must also install redux-thunk.

```sh
npm install --save redux-thunk
```

## Usage

Create the resource.

```js
const listingResource = new Resource({
  scope: 'appName',
  name: 'listing',
  key: '_id', // id key on the items
  state: {
    person: 'fred',
    extra: 'options',
    initial: ['state'],
  },
});

// code...

export default listingResource;
```

Define custom methods.

```js
listingResource.addMethod('changePerson', (state, { payload = null }) => ({
  ...state,
  person: payload,
}))
```

Define custom thunks.

```js
listingResource.addThunk('getPeople', () => resource => async (dispatch, getState) => {
  const { token } = getState().user.auth;
  const person = await apiGetRandomPerson(token);
  dispatch(resource.action('changePerson')(person));
  return { person };
});
```

Add the reducers to the redux state.

```js
const reducers = {
  listing: listingResource.reducer,
};
```

Get the actions and thunks from the resource.

```js
const mapDispatchToProps = {
  patchListing: listingResource.action('patch'),
  getListings: listingResource.thunk('getListings'),
};
```

Use the functions!

```js
this.props.patchListing({ hello: 'there' });
```

## Initial State

Initial state which is provided.

```js
{
  [pluralName]: [], // e.g. listings: []
  [singularName]: null, // e.g. listing: null
  problem: null, // will be filled with error object
  loading: false,
  success: null,
}
```

## Methods Provided

List of the methods which the resource provides by default.

| Method          | Description      |
|-----------------|------------------|
| `reset`         | Resets the state back to the initial state. |
| `loading`       | Sets the loading state option. |
| `success`       | Sets the success state option. |
| `errored`       | Sets the errored state option, usually with object. |
| `set`           | Sets the `manyName` array e.g. `listings`. |
| `replace`       | Replaces an item in the `manyName` array. |
| `remove`        | Removes an item from the `manyName` array. |
| `add`           | Adds an item to the `manyName` array. |
| `current`       | Sets the `singleName` object, usually for editing or previewing. |

## Maintainers

- [Jack Scott](https://github.com/jackrobertscott)

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