# redux-cycles-http

> Default HTTP flow cycle to use with `redux-cycles`

Latest version **0.5.0** (published 2018-07-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install redux-cycles-http
pnpm add redux-cycles-http
yarn add redux-cycles-http
bun add redux-cycles-http
```

## 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.0 |
| Published | 2018-07-10 |
| First published | 2017-10-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=7.4.0 |
| Dependencies | 0 |
| Unpacked size | 139.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Kirill Kriuchkov |
| Maintainers | kirill_k |
| Keywords | redux, cycle.js, redux-cycles, http |

## Links

- npm: https://www.npmjs.com/package/redux-cycles-http
- Repository: https://github.com/kirill-kruchkov/redux-cycles-http
- Homepage: https://github.com/kirill-kruchkov/redux-cycles-http#readme
- Issues: https://github.com/kirill-kruchkov/redux-cycles-http/issues
- npm.io page: https://npm.io/package/redux-cycles-http

## Recent versions

- 0.5.0 (latest) — 2018-07-10
- 0.4.0 — 2018-07-07
- 0.3.0 — 2018-07-07
- 0.2.0 — 2018-03-10
- 0.1.7 — 2018-02-03
- 0.1.6 — 2017-10-22
- 0.1.5 — 2017-10-22
- 0.1.4 — 2017-10-22
- 0.1.3 — 2017-10-22
- 0.1.2 — 2017-10-22
- 0.1.1 — 2017-10-22
- 0.1.0 — 2017-10-22

## README

# redux-cycles-http

Default HTTP flow cycle to use with [redux-cycles](https://github.com/cyclejs-community/redux-cycles).

## Installation

```
yarn add redux-cycles-http
```

or

```
npm install --save redux-cycles-http
```

## Usage

### Setup

Setup `redux-cycles` as usual along with Cycle.js HTTP driver and add `httpCycle` to your cycles:

```
import { createCycleMiddleware } from 'redux-cycles'
import { run } from '@cycle/run'
import { makeHTTPDriver } from '@cycle/http'
import { combineCycles } from 'redux-cycles'
import httpCycle from 'redux-cycles-http'

const cycleMiddleware = createCycleMiddleware()
const { makeActionDriver } = cycleMiddleware
const middlewares = [cycleMiddleware]

export const cycles = combineCycles(
  httpCycle
)

run(cycles, {
  ACTION: makeActionDriver(),
  HTTP: makeHTTPDriver(),
})
```

### Dispatch actions

```
export function somedataFetch(id) {
  return {
    type: ACTION_TYPE,
    request: {
      url: `${API_ENDPOINT}/path/${encodeURIComponent(id)}`,
      method: 'GET',
    }
  }
}

```

### Match it in reducer

```
export default function reducer(state = INITIAL_STATE, action) {
  switch (action.type) {
    case success(ACTION_TYPE):
      return {
        ...state,
        response: action.payload,
      }
    default:
      return state
  }
}
```

Success action payload contains:
- `data` - response data,
- `request` - request matching this response,
- `initial` - initial action payload.

### Waiting for requests to finish

E.g. if you need it for server side rendering.

*NB!* This requires `redux-thunk` or similar middleware.

```
import { awaitable } from 'redux-cycles-http'

static async getInitialProps({ query, store, isServer }) {
  const { id } = query
  await store.dispatch(awaitable(somedataFetch(id)))
  return { isServer }
}

```

or


```
import { awaitable } from 'redux-cycles-http'

function getInitialActions() {
  const awaitableFetch = awaitable(somedataFetch())
  return store.dispatch(awaitableFetch).then(response => {
    console.log('My data:', response.body)
    return response
  })
}

```

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