Skip to content

Commit f161a5b

Browse files
committed
readme up
1 parent 8d79129 commit f161a5b

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

README.md

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,38 +86,33 @@ Otherwise it will dispatch *failure* FSA:
8686

8787
### Create middleware
8888

89-
Middleware exposes `createMiddleware` function which accepts object with `responseSuccess` and `responseFailure` functions.
90-
So you can customize response interceptors on your own.
89+
Middleware exposes `createMiddleware` function which accepts object with `callApi` function.
90+
So you can pass any fetch implementation you wish with any response interceptors.
9191

9292
```js
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 });
93+
import { createMiddleware } from 'redux-callapi-middleware';
94+
import fetch from 'isomorphic-fetch';
95+
96+
const apiMiddleware = createMiddleware({ callApi: fetch });
11297
```
11398

114-
#### `checkStatus`
99+
Or with interceptors
100+
101+
```js
102+
import { createMiddleware } from 'redux-callapi-middleware';
103+
import fetch from 'isomorphic-fetch';
115104

116-
Small util to check if `response.ok` (status in the range 200-299) used as default status check. If not it throws error with response attached to it in `error.response`. Failure handler will receive this error.
105+
const onSuccess = (response) => {
106+
if (!response.ok) {
107+
throw new Error('Error');
108+
}
109+
return response;
110+
}
117111

118-
#### `parseResponse`
112+
const callApi = (url, options) => fetch(url, options).then(onSuccess);
119113

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).
114+
const apiMiddleware = createMiddleware({ callApi });
115+
```
121116

122117
### Action creator
123118

0 commit comments

Comments
 (0)