Skip to content

Latest commit

 

History

History
88 lines (76 loc) · 2.28 KB

API.md

File metadata and controls

88 lines (76 loc) · 2.28 KB

API

TOC

Actions

All the action creators are factories for the FETCH action. The saga then uses the FETCH action to call the Fetch API. The FETCH action look like this:

{
  type: '@@redux-saga-fetch/FETCH',
  payload: {
    method: 'GET' | 'POST' | 'DELETE',
    success: (response: Response) => Promise<Action> | Action,
    fail: (error: Error) => Object,
    ...options: Object
  }
}

The success callback takes a Response, and can return either a Promise<Action> or an Action directly. Action will get converted to Promise<Action> with Promise.resolve. The Action will then get dispatched when the Promise completes.

The fail callback takes an Error and can only return with an Action directly.

get()

get(
  url: string,
  config: {
    success: (response: Response) => Promise | Object,
    fail: (error: Error) => Promise | Object
  }
): FetchAction

This sets the method to 'GET'.

post()

post(
  url: string,
  config: {
    body: Object | Blob | string,
    success: (response: Response) => Promise | Object,
    fail: (error: Error) => Promise | Object,
    ...options: Object
  }
): FetchAction

This sets the method to 'POST'.

del()

del(
  url: string,
  config: {
    success: (response: Response) => Promise | Object,
    fail: (error: Error) => Promise | Object
  }
): FetchAction

This sets the method to 'DELETE'.

.json()

post.json(
  url: string,
  config: {
    body: Object,
    success: (response: Response) => Promise | Object,
    fail: (error: Error) => Promise | Object,
    ...options: Object
  }
): FetchAction

This is a factory action that only accepts an Object for the body (if any). It also adds the { 'Content-Type': 'application/json' } header and automatically parses a response as JSON. If the response is not valid JSON, the fail action is dispatched.