Skip to content

Commit 3acc4ef

Browse files
Uploaded basic usage of axios interceptor, needed for extracting it to a declaration file.
1 parent 4c80b2b commit 3acc4ef

File tree

6 files changed

+62
-13
lines changed

6 files changed

+62
-13
lines changed

src/pages/generic/cta/Cta.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { Component, Vue, Prop, Watch, Emit } from 'vue-property-decorator';
22
import { requestGet } from '@/plugins/axios-handler';
3+
import { namespace } from 'vuex-class';
34
@Component({})
45
export default class Cta extends Vue {
6+
@namespace('home').Action('fetchImages') fetchImages!: Function;
57

68
private hello() {
7-
requestGet('aaaa', { x: 'a', y: 'b' });
9+
this.fetchImages();
810
}
911
}

src/plugins/axios-handler/index.d.ts

Whitespace-only changes.

src/plugins/axios-handler/index.ts

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,64 @@
11
// 默认利用axios的cancelToken进行防重复提交。
22
// 如需允许多个提交同时发出。则需要在请求配置config中增加 neverCancel 属性,并设置为true
3-
import axios from 'axios';
3+
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
44
import baseConfig from './axiosrc';
5-
import { Vue } from 'vue-property-decorator';
5+
66
// define AJAX params that should be a key value pair
7-
interface params {
7+
interface Params {
88
[key: string]: any
99
}
1010

11-
function createInstance (params ? : params, timeout ? : number) {
11+
function createInstance (params ? : Params, timeout ? : number) {
1212
if (params !== undefined) baseConfig.params = params;
1313
if (timeout !== undefined) baseConfig.timeout = timeout;
1414

15-
return axios.create(baseConfig);
15+
const instance = axios.create(baseConfig);
16+
requstInterceptor(instance);
17+
responseInterceptor(instance);
18+
19+
return instance;
1620
}
1721

18-
export function requestGet (url: string, params ? : params) {
19-
return createInstance(params).get(url);
22+
function requstInterceptor (instance: AxiosInstance) {
23+
instance.interceptors.request.use((config: AxiosRequestConfig) => {
24+
console.log(config);
25+
return config;
26+
});
2027
}
2128

22-
export function requestPost (url: string, params ? : params) {
23-
return createInstance(params).post(url);
29+
function responseInterceptor (instance: AxiosInstance) {
30+
instance.interceptors.response.use((response: AxiosResponse) => {
31+
console.log(response);
32+
return response;
33+
});
34+
}
35+
36+
export function requestGet (url: string, params ? : Params) {
37+
return createInstance(params)
38+
.get(url)
39+
.catch((error: Error) => {
40+
errorHandler(error);
41+
})
42+
.finally(() => {
43+
doFinally();
44+
});
45+
}
46+
47+
export function requestPost (url: string, params ? : Params) {
48+
return createInstance(params)
49+
.post(url)
50+
.catch((error: Error) => {
51+
errorHandler(error);
52+
})
53+
.finally(() => {
54+
doFinally();
55+
});
56+
}
57+
58+
function errorHandler (error: Error) {
59+
console.log(error);
60+
};
61+
62+
function doFinally () {
63+
console.log('finally');
2464
}

src/plugins/store/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ let opt: StoreOptions < any > = {
1818
modules: { app, home, activity },
1919

2020
// root states
21-
state: {},
21+
state: {
22+
user: 'user'
23+
},
2224
mutations: {},
2325
actions: {},
2426
getters: {}

src/plugins/store/modules/app/modules/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Store } from 'vuex';
22
import { State } from '@/plugins/store';
3-
3+
import { requestGet } from '@/plugins/axios-handler';
44
const actions = {
55

66
};
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { Store } from 'vuex';
22
import { State } from '@/plugins/store';
3+
import { requestGet } from '@/plugins/axios-handler';
34

45
const actions = {
5-
6+
fetchImages: function () {
7+
return requestGet('fetchImages', {
8+
size: '1024*720'
9+
});
10+
}
611
};
712

813
export default actions;

0 commit comments

Comments
 (0)