# reducers

Reducers are triggered by actions, and are the only way to modify the state. Let's see which reducers exist, and their effect on the state.

## REQUEST

Will add metadata corresponding to the normalized URL to the state.

```javascript
const state = {
  restEasy: {
    requests: {
      '[ACTION_NAME]:URL?QUERY': {
        resourceName: string,
        resourceId: any || null,
        startedAt: timestamp,
        endedAt: null,
        // Followings won't be overwritten is they already exist
        hasSucceeded: false,
        hasFailed: false,
        didInvalidate: false,
        fromCache: false,
      },
    },
  },
};
```

## RECEIVE

Will set the metadata to a success state, and store the resource.

```javascript
const state = {
  restEasy: {
    requests: {
      '[ACTION_NAME]:URL?QUERY': {
        endedAt: timestamp,
        hasSucceeded: true,
        hasFailed: false,
        didInvalidate: false,
        fromCache: false,
        payloadIds: {
          resource1: [any],
          ...,
        },
      },
    },
    resources: {
      resource1: {
        [id]: object,
        ...,
      },
      ...,
    },
  },
};
```

## FAIL

Will set the metadata to a an error state. `payloadIds` will remain unchanged.

```javascript
const state = {
  restEasy: {
    requests: {
      '[ACTION_NAME]:URL?QUERY': {
        endedAt: timestamp,
        hasSucceeded: false,
        hasFailed: true,
      },
    },
  },
};
```

## RECEIVE\_FROM\_CACHE

When requesting a couple of resource/id which is already in state and cache is still fresh, the request will be stored in a success state with the property `fromCache` to **true**.

```javascript
const state = {
  restEasy: {
    requests: {
      '[ACTION_NAME]:URL?QUERY': {
        resourceName: string,
        resourceId: any,
        startedAt: timestamp,
        endedAt: timestamp,
        hasSucceeded: true,
        hasFailed: false,
        didInvalidate: false,
        fromCache: true,
        payloadIds: {
          resource1: [any],
        },
      },
    },
  },
};
```

## INVALIDATE\_RESOURCE

Will set `didInvalidate` to **true** for each request related to the resource.

## INVALIDATE\_ID

Will set `didInvalidate` to **true** for each request related to the resource/id.

## INVALIDATE\_REQUEST

Will set `didInvalidate` to **true** for the related request.

## RESET\_RESOURCE

Will **remove** the resource and related requests from the state.

## RESET\_ALL

Will empty the state completely.


---

# 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/api/reducer-1/reducers.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.
