# actions

This process defines the set of steps performed by `redux-rest-easy` actions, from the moment the request is performed to the moment we receive the payload.

See [createResource documentation](/redux-rest-easy/docs/api/createresource-1.md#arguments) for the list of action parameters.

## Dispatch the REQUEST action

Dispatching the **REQUEST** action will trigger the corresponding reducer, and create a key in the store corresponding to the normalized URL of the request, with metadata useful to cache control and selectors. See [reducers documentation](/redux-rest-easy/docs/principles/reducers.md#request).

## Execute the beforeHook

Specifying an *optional* function `action.beforeHook` when defining an action will allow executing this function everytime the preflight checks are passed for this action. If it returns a non-falsy value, the return value will be used as the body for the principal request. Useful to perform series of calls.

## Perform the request

The request will be performed using `fetch`, using the URL `action.url` specified when defining the action, and the query parameters/body specified when dispatching the action.

## Receive and treat the payload of the request

The status code of the answer will be handled, then the payload will be processed to JSON.

## Normalize the payload

The payload will be normalized using *optional* function `action.normalizer` if there is one (and if the request succeeded). The payload will otherwise be stored as-is.

The payload metadata will be extracted using *optional* function `action.metadataNormalizer` if there is one (and if the request succeeded).

## Dispatch the RECEIVE / FAIL action

Dispatching the **RECEIVE** action will trigger the corresponding reducer and store the normalized data in the state. See [reducers documentation](/redux-rest-easy/docs/principles/reducers.md#receive).

Dispatching the **FAIL** action will trigger the corresponding reducer and update the normalized URL metadata. See [reducers documentation](/redux-rest-easy/docs/principles/reducers.md#fail).

## Execute the afterHook

Specifying an *optional* function `action.afterHook` when defining an action will allow executing this function every time a request has successfully performed.

## Execute the onSuccess/onFailure

Last but not least, specifying a `onSuccess` / `onFailure` parameter when dispatching an action (e.g. from a component) will provide a way to specify callbacks at the component level, and to adapt the UI accordingly. See [actions documentation](/redux-rest-easy/docs/api/createresource/actions.md#actions).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://brigad.gitbook.io/redux-rest-easy/docs/principles/actions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
