3.0.1 • Published 4 years ago

@k-ramel/driver-http v3.0.1

Weekly downloads
81
License
MIT
Repository
github
Last release
4 years ago

@k-ramel/driver-http

HTTP driver for k-ramel

Install it

yarn add @k-ramel/driver-http regeneratorRuntime

How to use it

In a reaction (See main documentation about listeners/reactions)

First you have to add it to define it to the store.

import { createStore, types } from 'k-ramel'
import http from '@k-ramel/driver-http'

const store = createStore(
  {
    data: {
      users: types.keyValue(),
    },
  },
  {
    drivers: {
      http,
    },
  },
)

Then you can use it in reactions and listeners

export const save = (action, store, drivers) => {
  const { http } = drivers
  const todo = store.data.todos.get(action.payload)

  // TODOS is the context for this request
  // You can then catch events like `/@@http\/TODOS>.*/
  http('TODOS')
    // It will
    // 1. trigger an event like `@@http/TODOS>POST>STARTED`
    // 2. serialize your todo
    // 3. add the header Content-Type to application/json
    // 4. use fetch to call your API
    // 5. trigger event like `@@http/TODOS>POST>FAILED` or `@@http/TODOS>POST>ENDED`
    .post('/api/todos', todo)
}

Context

Sometimes you need to remember why you did a request when you receive a terminal event (FAILED or ENDED):

In the previous example, if your API just returns 200 in case of success without any payload and you still want to let your user knows that the todo is actually saved (green border), then you don't know which todo to update!

In this cases you can use the context object:

export const save = (action, store, drivers) => {
  const { http } = drivers
  const todo = store.data.todos.get(action.payload)

  http('TODOS', { id: todo.id })
    .post('/api/todos', todo)
}

Then the FAILED or ENDED event will have a context field with the related todo id in it! You can use it to update your ui and give your user feedback!

// listeners.js
// ...
when('@@http/TODOS>POST>ENDED')(updateAfterSave),

// reactions.js
// ...
export const updateAfterSave = (action, store) => {
  const { context } = action
  store.data.todos.update({ id: context.id, saved: true, error: false })
}

Emitted events

event typewhen
@@http/MY_CONTEXT>POST>STARTEDdispatched just before the fetch is started
@@http/MY_CONTEXT>POST>ENDEDdispatched once the request is done without error (based on HTTP status)
@@http/MY_CONTEXT>POST>FAILEDdispatched once the request is done with error (based on HTTP status)

Actions

All emitted events have some data

fielddescriptiontype
typeevent typestring
fetchurl and fetch options, this can be used to retry a request that is on errorarray ([url, options])
statusHTTP status of the responsenumber
headersHTTP response headersobject (header name -> header value)
payloaddata of the response (if the content type is application/json, the data is already parsed)any
contextyour optional context added when use the driver. The context help you to recognize the FAILED or ENDED event and react with the right reaction.any
3.0.1

4 years ago

3.0.0

4 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.4.0

5 years ago

2.0.0-alpha.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.0

6 years ago

1.0.0-alpha.0

6 years ago

0.15.1

6 years ago

0.14.0

6 years ago

0.13.2

6 years ago

0.13.1

6 years ago

0.13.0

6 years ago

0.12.2

6 years ago

0.12.1

6 years ago

0.12.0

6 years ago

0.11.0

6 years ago

0.10.2

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.0

6 years ago

0.8.0

6 years ago