Permalink
Browse files

Added some documentation to the README about middleware

  • Loading branch information...
Max Thom Stahl
Max Thom Stahl committed Mar 30, 2018
1 parent 1f84999 commit daeb73369d019a11a7cd74629e9f0c4e02bb310f
Showing with 19 additions and 1 deletion.
  1. +19 −1 README.md
View
@@ -120,7 +120,7 @@ Devour uses a fully middleware based approach. This allows you to easily manipul
### Your First Middleware
Adding your own middleware is easy. It's just a simple JavaScript object that has a `name`, `req`, and/or `res` property. The `req` or `res` property is a function that receives a `payload`, which houses all the details of the request cycle _(inspect it for yourself to learn more)_. For async operations, your `req` or `res` methods can return a promise, which will need to resolve before the middleware chain continues. Otherwise, you may just manipulate the `payload` as needed and return it immediately.
Adding your own middleware is easy. It's just a simple JavaScript object that has a `name`, `req`, and/or `res` property. The `req` or `res` property is a function that receives a `payload`, which houses all the details of the request cycle _(see documentation below)_. For async operations, your `req` or `res` methods can return a promise, which will need to resolve before the middleware chain continues. Otherwise, you may just manipulate the `payload` as needed and return it immediately.
```js
let requestMiddleware = {
@@ -153,6 +153,24 @@ jsonApi.insertMiddlewareAfter('response', responseMiddleware)
jsonApi.replaceMiddleware('errors', errorMiddleware)
```
#### The payload object
The `payload` object that gets passed to your middleware function has the following shape:
* `data` - JSON data contained in request/response body
* `headers` - An object containing the headers for the request/response
* `method` - A string representing the HTTP verb used, e.g. `'GET'` or `'PATCH'`
* `model` - The model that initiated this request
* `params` - An object containing the keys/values passed in the query params of the request
* `url` - The URL to which the request was sent
The payload for response middleware contains these additional fields:
* `config` - An object of low-level config data
* `request` - The original XMLHttpRequest object used to make the request
* `status` - HTTP status code for the request, e.g. 200
* `statusText` - Text representation of the HTTP status, e.g. "OK", "Created"
### Your Second Middleware
This request middleware may be handy for live queries as it permits the last pending request to be cancelled (via Axios [request cancellation feature](https://github.com/axios/axios#cancellation)).

0 comments on commit daeb733

Please sign in to comment.