Skip to content

Commit fe482c4

Browse files
author
Artur Charaev
committed
readme upd
1 parent 8a1b130 commit fe482c4

File tree

1 file changed

+56
-13
lines changed

1 file changed

+56
-13
lines changed

README.md

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
redux-callapi-middleware
22
====================
3-
Redux CallAPI Middleware to make API calls in generic and declarative way.
3+
Redux CallAPI Middleware to make API calls in generic and declarative way. Allows to batch multiple API calls and intercept responses.
44

55
## Contents
66

@@ -86,15 +86,29 @@ Otherwise it will dispatch *failure* FSA:
8686

8787
### Create middleware
8888

89-
Middleware exposes `createMiddleware` function which accepts object with `status` and `parse` functions.
90-
So you can customize status and parse functions on your own.
89+
Middleware exposes `createMiddleware` function which accepts object with `responseSuccess` and `responseFailure` functions.
90+
So you can customize response interceptors on your own.
9191

9292
```js
93-
import { createMiddleware } from 'redux-callapi-middleware';
94-
95-
const status = (response) => response;
96-
const parse = (response) => response;
97-
const apiMiddleware = createMiddleware({ status, response });
93+
import { createMiddleware, checkStatus, parseResponse } from 'redux-callapi-middleware';
94+
95+
const responseSuccess = (...args) => {
96+
// dont wish to parse response by default
97+
return Promise.resolve(...args).then(checkStatus);
98+
};
99+
100+
// it should return Promise reject to trigger errorType action.
101+
const responseFailure = (error) => {
102+
// wish to have parsed response in error
103+
return Promise.reject(error.response)
104+
.then(parseResponse)
105+
.then(response => {
106+
error.parsed = response;
107+
return error;
108+
});
109+
};
110+
111+
const apiMiddleware = createMiddleware({ responseSuccess, responseFailure });
98112
```
99113

100114
#### `checkStatus`
@@ -103,20 +117,44 @@ Small util to check if `response.ok` (status in the range 200-299) used as defau
103117

104118
#### `parseResponse`
105119

106-
Small util to check to parse typical response like json or text and used as default parse function. If unknow type it returns raw response (for instance images).
120+
Small util to check to parse typical response like json or text and used as default parse function. If unknown type it returns raw response (for instance images).
107121

108122
### Action creator
109123

110-
Action creator should return an object with `[CALL_API]` property with `endpoint`, `options` and `types` fields. See [example](#example).
124+
Action creator should return an object with `[CALL_API]` property with `batch`, `endpoint`, `options` and `types` fields. See [example](#example).
125+
126+
#### `[CALL_API].batch`
127+
128+
An API endpoints to batch call. `Array` of `Objects` contains `endpoint` and `options` fields in same format as `[CALL_API].endpoint` and `[CALL_API].options`.
129+
```js
130+
batch: [
131+
{ endpoint1, options1 },
132+
{ endpoint2, options2 },
133+
],
134+
```
111135

112136
#### `[CALL_API].endpoint`
113137

114-
An API endpoint to call. String or function which receives state and returns string.
138+
An API endpoint to call. Used if batch is not populated. String or function which receives state and returns string.
139+
```js
140+
endpoint: 'someurl',
141+
```
142+
```js
143+
// calculate url from state
144+
endpoint: (apiAction, state) => 'someurl',
145+
```
115146

116147
#### `[CALL_API].options`
117148

118-
Request options object. Object or function which receives state and returns object.
149+
Request options object. Used if batch is not populated. Object or function which receives state and returns object.
119150
It uses [`isomorphic-fetch`](https://github.com/matthew-andrews/isomorphic-fetch) under the hood, so any valid options for [fetch](https://fetch.spec.whatwg.org), like `body`, `credentials`, `headers` and etc.
151+
```js
152+
options: { 'method': 'PUT'},
153+
```
154+
```js
155+
// calculate options from state
156+
options: (apiAction, state) => { 'method': 'PUT'},
157+
```
120158

121159
#### `[CALL_API].types`
122160

@@ -260,12 +298,17 @@ Not supported, but might work with [redux-promise](https://github.com/acdlite/re
260298
2. It not dispatches "programmatic" errors, like errors on endpoint generation.
261299
3. It gives more control with functions as actions types
262300
4. Not supports promises, but take look to [redux-promise](https://github.com/acdlite/redux-promise).
301+
5. Allows to batch API calls
263302

264303
6. Want to have base URL?
265304

266305
Write a wrapper around your callApi action creator.
267306

268-
7. Want to check custom headers or custom parse response?
307+
7. Want to check custom headers or have custom parse response?
308+
309+
See [create middleware](#create-middleware)
310+
311+
8. Wish to have custom error handling?
269312

270313
See [create middleware](#create-middleware)
271314

0 commit comments

Comments
 (0)