<commit_before># API Middleware

The API middleware builds upon the middleware from the [redux real world example middleware](https://github.com/reactjs/redux/blob/ad33fa7314e5db852a306d9475be5cfe22bde180/examples/real-world/middleware/api.js).

| Feature                        | Redux Real World | React Production Starter |
|--------------------------------|------------------|--------------------------|
| Under-the-hood                 | Isomorphic Fetch | Axios                    |
| HTTP Verbs                     | `GET`            | All                      |
| Produces Flux Standard Actions | NO               | YES                      |

```javascript
import {
  LOAD_POSTS_REQUEST,
  LOAD_POSTS_SUCCESS,
  LOAD_POSTS_FAILURE
} from '../../constants';
import { CALL_API } from '../../middleware/api'

export function loadPosts(slug) {
  return {
    [CALL_API]: {
      // Types of actions to emit before and after
      types: [LOAD_POSTS_REQUEST, LOAD_POSTS_SUCCESS, LOAD_POSTS_FAILURE],

      // refer to axios documentation
      url: `/api/v0/posts/${slug}`,
      method: 'GET'
    }
  }
}
```

Inside of `CALL_API`, you must specify:
 - `types` and pass it 3 strings (representing request, success, and error outcomes).
 - `url` - the url endpoint
 - `method` - an HTTP Verb

For your reference, the middleware eventually calls `axios(options).then(...)`, thus **you are free to supply any/all axios API options**<commit_msg>example of `POST` request<commit_after># API Middleware

The API middleware builds upon the middleware from the [redux real world example middleware](https://github.com/reactjs/redux/blob/ad33fa7314e5db852a306d9475be5cfe22bde180/examples/real-world/middleware/api.js).

| Feature                        | Redux Real World | React Production Starter |
|--------------------------------|------------------|--------------------------|
| Under-the-hood                 | Isomorphic Fetch | Axios                    |
| HTTP Verbs                     | `GET`            | All                      |
| Produces Flux Standard Actions | NO               | YES                      |


Here is what a simple `GET` request would look like:

```javascript
// GET Request
import {
  LOAD_POSTS_REQUEST,
  LOAD_POSTS_SUCCESS,
  LOAD_POSTS_FAILURE
} from '../../constants';
import { CALL_API } from '../../middleware/api'

export function loadPosts(slug) {
  return {
    [CALL_API]: {
      // Types of actions to emit before and after
      types: [LOAD_POSTS_REQUEST, LOAD_POSTS_SUCCESS, LOAD_POSTS_FAILURE],

      // refer to axios documentation
      url: `/api/v0/posts/${slug}`,
      method: 'GET'

      // withCredentials
    }
  }
}
```

Example of a `POST` request:
```javascript
// POST Request
import {
  CREATE_POST_REQUEST,
  CREATE_POST_SUCCESS,
  CREATE_POST_FAILURE
} from '../../constants';
import { CALL_API } from '../../middleware/api'

export function createPost(title, body) {
  return {
    [CALL_API]: {
      // Types of actions to emit before and after
      types: [CREATE_POST_REQUEST, CREATE_POST_SUCCESS, CREATE_POST_FAILURE],

      // refer to axios documentation for all options
      url: '/api/v0/posts',
      method: 'POST'
      data: { title, body } // body of the post
      // withCredentials: true // depending on your cookie/session config
    }
  }
}
```

Inside of `CALL_API`, you must specify:
 - `types` and pass it 3 strings that representing request, success, and error actions to be emitted).
 - `url` - the url endpoint
 - `method` - an HTTP Verb

For your reference, the middleware eventually calls `axios(options).then(...)`, thus **you are free to supply any/all axios API options**
{'fraction_non_alphanumeric': 0.14620201470187857, 'fraction_numerical': 0.013885107541519195, 'mean_word_length': 2.150943396226415, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1512559', 'n_tokens_mistral': 1101, 'n_tokens_neox': 1042, 'n_words': 316}