Skip to content

Commit 3f29402

Browse files
committed
feat: add init options when plugin installed
1 parent 984f3e9 commit 3f29402

File tree

6 files changed

+185
-1042
lines changed

6 files changed

+185
-1042
lines changed

README.md

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,50 +29,101 @@ And in your entry file:
2929
import Vue from 'Vue'
3030
import VueAxiosPlugin from 'vue-axios-plugin'
3131

32-
const checkStatus = (response) => {
33-
if(response.status === 404){
34-
// do something
35-
}
36-
if(response.status === 500){
37-
//do something
38-
}
39-
40-
// after your uniform request handler, you must return response
41-
return response
42-
}
43-
4432
Vue.use(VueAxiosPlugin, {
45-
checkStatus: checkStatus
33+
// request interceptor handler
34+
reqHandleFunc: config => config,
35+
reqErrorFunc: error => Promise.reject(error),
36+
// response interceptor handler
37+
resHandleFunc: response => response,
38+
resErrorFunc: error => Promise.reject(error)
4639
})
4740
```
4841

42+
## Options
43+
44+
Except axios default [request options](https://github.com/axios/axios#request-config), `vue-axios-plugin` provide below request/response interceptors options:
45+
46+
|Name|Type|Default|Description|
47+
|:--:|:--:|:-----:|:----------|
48+
|**[`reqHandleFunc`](#)**|`{Function}`|`config => config`|The handler function for request, before request is sent|
49+
|**[`reqErrorFunc`](#)**|`{Function}`|`error => Promise.reject(error)`|The error function for request error|
50+
|**[`resHandleFunc`](#)**|`{Function}`|`response => response`|The handler function for response data|
51+
|**[`resErrorFunc`](#)**|`{Function}`|`error => Promise.reject(error)`| The error function for response error |
52+
4953
## Usage
5054

51-
default method in `$http` :
55+
default method in `$http`, it just contains get and post method:
5256

5357
```javascript
5458
this.$http.get(url, data, options).then((response) => {
55-
console.log(response.data)
59+
console.log(response)
5660
})
5761
this.$http.post(url, data, options).then((response) => {
58-
console.log(response.data)
62+
console.log(response)
5963
})
6064
```
6165

62-
use axios original method in `$axios`
66+
use axios original method in `$axios`, by this, you can use all allowed http methods: get,post,delete,put...
6367

6468
```javascript
65-
this.$axios.get(url).then((response) => {
66-
console.log(response.data)
69+
this.$axios.get(url, data, options).then((response) => {
70+
console.log(response)
71+
})
72+
73+
this.$axios.post(url, data, options).then((response) => {
74+
console.log(response)
6775
})
76+
```
77+
78+
## Notice!!!
6879

80+
When you send a request use `application/x-www-form-urlencoded` format, you need to use [qs]() library to transform post data, like below:
81+
82+
```js
6983
import qs from 'qs'
70-
this.$axios.post(url, qs.stringify(data).then((response) => {
71-
console.log(response.data)
84+
this.$http.post(url, qs.stringify(data), {
85+
headers: {
86+
'Content-Type': 'application/x-www-form-urlencoded',
87+
}
88+
}).then((response) => {
89+
console.log(response)
90+
})
91+
```
92+
93+
But if the `data` has properties who's type if object/array, you need convert these properties into JSON string:
94+
95+
```js
96+
import qs from 'qs'
97+
98+
function jsonProp (obj) {
99+
// type check
100+
if (!obj || (typeof obj !== 'object')) {
101+
return obj
102+
}
103+
Object.keys(obj).forEach((key) => {
104+
if ((typeof obj[key]) === 'object') {
105+
obj[key] = JSON.stringify(obj[key])
106+
}
107+
})
108+
return obj
109+
}
110+
111+
this.$http.post(url, qs.stringify(data), {
112+
headers: {
113+
'Content-Type': 'application/x-www-form-urlencoded',
114+
},
115+
transformRequest: [
116+
function (data) {
117+
// if data has object type properties, need JSON.stringify them.
118+
return qs.stringify(jsonProp(data))
119+
}
120+
]
121+
}).then((response) => {
122+
console.log(response)
72123
})
73124
```
74125

75-
More usage, go to [axios](https://github.com/mzabriskie/axios)
126+
More usage, view [axios](https://github.com/mzabriskie/axios)
76127

77128
## License
78129

0 commit comments

Comments
 (0)