Skip to content

Commit c68784c

Browse files
authored
Merge pull request #28 from braska/fix_for_axios_0_13
Fix for compatibility with Axios 0.13
2 parents b8ee931 + 1ed73ae commit c68784c

File tree

5 files changed

+52
-54
lines changed

5 files changed

+52
-54
lines changed

README.md

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ npm i -S redux-axios-middleware
1212

1313
### Use middleware
1414

15-
By default you only need to import middleware from package and add it to redux middlewares
16-
and execute it with first argument being with axios instance. second optional argument are middleware
17-
options for customizing
15+
By default you only need to import middleware from package and add it to redux middlewares and execute it with first argument being with axios instance. second optional argument are middleware options for customizing
1816

1917
```js
2018
import {createStore, applyMiddleware} from 'redux';
@@ -39,12 +37,11 @@ let store = createStore(
3937

4038
### Dispatch action
4139

42-
Every action which have `payload.request` defined will be handled by middleware. There are two possible type
43-
definitions.
40+
Every action which have `payload.request` defined will be handled by middleware. There are two possible type definitions.
4441

4542
- use `action.type` with string name
46-
action with type will be dispatched on start, and then followed by type suffixed with underscore and
47-
success suffix on success, or error suffix on error
43+
- action with type will be dispatched on start, and then followed by type suffixed with underscore and
44+
- success suffix on success, or error suffix on error
4845

4946
defaults: success suffix = "_SUCCESS" error suffix = "_FAIL"
5047

@@ -62,7 +59,7 @@ export function loadCategories() {
6259
```
6360

6461
- use `action.types` with array of types `[REQUEST,SUCCESS,FAILURE]`
65-
`REQUEST` will be dispatched on start, then `SUCCESS` or `FAILURE` after request result
62+
- `REQUEST` will be dispatched on start, then `SUCCESS` or `FAILURE` after request result
6663

6764
```javascript
6865
export function loadCategories() {
@@ -110,7 +107,7 @@ Promise.all([
110107

111108
### Request complete
112109

113-
After request complete, middleware will dispatch new action,
110+
After request complete, middleware will dispatch new action,
114111

115112
#### on success
116113

@@ -131,7 +128,7 @@ Error action is same as success action with one difference, there's no key `payl
131128
```js
132129
{
133130
type:'OH_NO',
134-
error: { ... }, //axios error response object with data status headers etc.
131+
error: { ... }, //axios error object with message, code, config and response fields
135132
meta: {
136133
previousAction: { ... } //action which triggered request
137134
}
@@ -143,10 +140,10 @@ Error action is same as success action with one difference, there's no key `payl
143140
```js
144141
{
145142
type:'OH_NO',
146-
error: {
143+
error: {
147144
status: 0,
148145
... //rest of axios error response object
149-
},
146+
},
150147
meta: {
151148
previousAction: { ... } //action which triggered request
152149
}
@@ -155,8 +152,7 @@ Error action is same as success action with one difference, there's no key `payl
155152

156153
### Multiple clients
157154

158-
If you are using more than one different APIs, you can define those clients and put them to middleware. All you have to change is import
159-
of middleware, which is passed to redux createStore function and defined those clients.
155+
If you are using more than one different APIs, you can define those clients and put them to middleware. All you have to change is import of middleware, which is passed to redux createStore function and defined those clients.
160156

161157
```
162158
import { multiClientMiddleware } from 'redux-axios-middleware';
@@ -169,26 +165,29 @@ createStore(
169165
)
170166
```
171167

172-
`clients` object should be map of
168+
`clients` object should be map of
169+
173170
```
174-
{
171+
{
175172
client: axiosInstance, // instance of axios client created by axios.create(...)
176173
options: {} //optional key for passing specific client options
177174
}
178175
```
176+
179177
For example:
178+
180179
```
181180
{
182181
default: {
183182
client: axios.create({
184183
baseURL:'http://localhost:8080/api',
185-
responseType: 'json'
184+
responseType: 'json'
186185
})
187186
},
188187
googleMaps: {
189188
client: axios.create({
190189
baseURL:'https://maps.googleapis.com/maps/api',
191-
responseType: 'json'
190+
responseType: 'json'
192191
})
193192
}
194193
}
@@ -209,45 +208,40 @@ export function loadCategories() {
209208
}
210209
}
211210
```
212-
If you don't define client, default value will be used. You can change default client name in middleware options.
213-
214211

212+
If you don't define client, default value will be used. You can change default client name in middleware options.
215213

216214
### Middleware options
217215

218216
Options can be changed on multiple levels. They are merged in following direction:
217+
219218
```
220-
default middleware values < middleware config < client config < action config
219+
default middleware values < middleware config < client config < action config
221220
```
222-
All values except interceptors are overriden, interceptors are merged in same order.
223-
Some values are changeable only on certain level (can be seen in change level column).
224221

225-
Legend:
226-
`M` - middleware config
227-
`C` - client config
228-
`A` - action config
222+
All values except interceptors are overriden, interceptors are merged in same order. Some values are changeable only on certain level (can be seen in change level column).
229223

224+
Legend: `M` - middleware config `C` - client config `A` - action config
230225

231-
| key | type | default value | change level | description |
232-
| --- | ---- | ------------- | ------------ | ----------- |
233-
| successSuffix | string | SUCCESS | `M` `C` `A` | default suffix added to success action, for example `{type:"READ"}` will be `{type:"READ_SUCCESS"}`|
234-
| errorSuffix | string | FAIL | `M` `C` `A`| same as above |
235-
| onSuccess | function | described above | `M` `C` `A` | function called if axios resolve with success |
236-
| onError | function | described above | `M` `C` `A` | function called if axios resolve with error |
237-
| onComplete | function | described above | `M` `C` `A`| function called after axios resolve |
238-
| returnRejectedPromiseOnError | bool | false| `M` `C` `A` | if `true`, axios onError handler will return `Promise.reject(newAction)` instead of `newAction` |
239-
| isAxiosRequest | function | function check if action contain `action.payload.request` | `M` | check if action is axios request, this is connected to `getRequestConfig` |
240-
| getRequestConfig | function | return content of `action.payload.request` | `M` `C` `A` | if `isAxiosRequest` returns true, this function get axios request config from action |
241-
| getClientName | function | returns `action.payload.client` OR `'default'` | `M` `C` `A` | attempts to resolve used client name or use defaultClientName |
242-
| defaultClientName | every possible object key type | `'default'` | `M` | key which define client used if `getClienName` returned false value |
243-
| getRequestOptions | function | return `action.payload.options` | `M` `C` | returns options object from action to override some values |
244-
| interceptors | object `{request: [], response: []}` | | `M` `C` | You can pass axios request and response interceptors. Take care, first argument of interceptor is different from default axios interceptor, first received argument is object with `getState`, `dispatch` and `action` keys |
226+
key | type | default value | change level | description
227+
---------------------------- | ------------------------------------ | --------------------------------------------------------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
228+
successSuffix | string | SUCCESS | `M` `C` `A` | default suffix added to success action, for example `{type:"READ"}` will be `{type:"READ_SUCCESS"}`
229+
errorSuffix | string | FAIL | `M` `C` `A` | same as above
230+
onSuccess | function | described above | `M` `C` `A` | function called if axios resolve with success
231+
onError | function | described above | `M` `C` `A` | function called if axios resolve with error
232+
onComplete | function | described above | `M` `C` `A` | function called after axios resolve
233+
returnRejectedPromiseOnError | bool | false | `M` `C` `A` | if `true`, axios onError handler will return `Promise.reject(newAction)` instead of `newAction`
234+
isAxiosRequest | function | function check if action contain `action.payload.request` | `M` | check if action is axios request, this is connected to `getRequestConfig`
235+
getRequestConfig | function | return content of `action.payload.request` | `M` `C` `A` | if `isAxiosRequest` returns true, this function get axios request config from action
236+
getClientName | function | returns `action.payload.client` OR `'default'` | `M` `C` `A` | attempts to resolve used client name or use defaultClientName
237+
defaultClientName | every possible object key type | `'default'` | `M` | key which define client used if `getClienName` returned false value
238+
getRequestOptions | function | return `action.payload.options` | `M` `C` | returns options object from action to override some values
239+
interceptors | object `{request: [], response: []}` | | `M` `C` | You can pass axios request and response interceptors. Take care, first argument of interceptor is different from default axios interceptor, first received argument is object with `getState`, `dispatch` and `action` keys
245240

246241
## License
247242

248243
This project is licensed under the MIT license, Copyright (c) 2016 Michal Svrček. For more information see `LICENSE.md`.
249244

250245
## Acknowledgements
251246

252-
[Dan Abramov](https://github.com/gaearon) for Redux
253-
[Matt Zabriskie](https://github.com/mzabriskie) for [Axios](https://github.com/mzabriskie/axios). A Promise based HTTP client for the browser and node.js
247+
[Dan Abramov](https://github.com/gaearon) for Redux [Matt Zabriskie](https://github.com/mzabriskie) for [Axios](https://github.com/mzabriskie/axios). A Promise based HTTP client for the browser and node.js

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
},
2222
"homepage": "https://github.com/svrcekmichal/redux-axios-middleware",
2323
"peerDependencies": {
24-
"axios": ">= 0.5"
24+
"axios": ">= 0.13"
2525
},
2626
"devDependencies": {
27-
"axios": "^0.11.1",
27+
"axios": "^0.13.1",
2828
"axios-mock-adapter": "^1.3.1",
2929
"babel-cli": "^6.5.1",
3030
"babel-eslint": "^6.0.2",

src/defaults.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const onSuccess = ({ action, next, response }, options) => {
2626

2727
export const onError = ({ action, next, error }, options) => {
2828
let errorObject;
29-
if (error instanceof Error) {
29+
if (!error.response) {
3030
errorObject = {
3131
data: error.message,
3232
status: 0

test/defaults.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,13 @@ describe('defaults', () => {
9191
let resultAction = null;
9292
let next = (action) => resultAction = action;
9393
const error = {
94-
data: ['Error1','Error2'],
95-
status:400,
96-
statusTest:'BAD_REQUEST',
97-
headers:{},
98-
config: {}
94+
response: {
95+
data: ['Error1','Error2'],
96+
status:400,
97+
statusTest:'BAD_REQUEST',
98+
headers:{},
99+
config: {}
100+
}
99101
};
100102

101103
beforeEach(() => {

test/middleware.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('middleware', () => {
2525
});
2626

2727
it('should dispatch _SUCCESS', () => {
28-
mockAxiosClient.onGet(`${BASE_URL}/test`).reply(200, 'response');
28+
mockAxiosClient.onGet('/test').reply(200, 'response');
2929

3030
const expectActions = [{
3131
type: 'LOAD',
@@ -47,7 +47,7 @@ describe('middleware', () => {
4747
});
4848
});
4949
it('should dispatch _FAIL', () => {
50-
mockAxiosClient.onGet(`${BASE_URL}/test`).reply(404);
50+
mockAxiosClient.onGet('/test').reply(404);
5151
const expectActions = [{
5252
type: 'LOAD',
5353
payload: {
@@ -59,7 +59,9 @@ describe('middleware', () => {
5959
}, {
6060
type: 'LOAD_FAIL',
6161
error: {
62-
status: 404
62+
response: {
63+
status: 404
64+
}
6365
}
6466
}];
6567
const store = mockStore();

0 commit comments

Comments
 (0)