Skip to content

Commit 7d83645

Browse files
Bind VUEX with AXIOS to make it reusable, change function names to Enums.
1 parent 5b73a6c commit 7d83645

File tree

18 files changed

+139
-114
lines changed

18 files changed

+139
-114
lines changed

src/pages/activity/photos/Photos.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ import { Component, Vue } from 'vue-property-decorator';
22
import Feed from '@/components/activity/feed/Feed.vue';
33

44
@Component({ components: { Feed } })
5-
export default class Photos extends Vue {};
5+
export default class Photos extends Vue {
6+
7+
};

src/pages/generic/cta/Cta.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Component, Vue, Prop, Watch, Emit } from 'vue-property-decorator';
22
import { namespace } from 'vuex-class';
3+
import { ActionType } from '@/plugins/store/modules/app';
4+
35
@Component({})
46
export default class Cta extends Vue {
5-
@namespace('home').Action('fetchImages') fetchImages!: Function;
7+
@namespace('app').Action(ActionType.FETCH_IMAGES) fetchImages!: Function;
68

7-
private hello() {
9+
private hello () {
810
this.fetchImages();
911
}
1012
}

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

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/plugins/axios-handler/index.ts

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,75 @@
1-
// 默认利用axios的cancelToken进行防重复提交。
2-
// 如需允许多个提交同时发出。则需要在请求配置config中增加 neverCancel 属性,并设置为true
31
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
42
import baseConfig from './config/axiosrc';
53

64
// define request params that should be a key value pair
7-
interface Params<T> {
5+
export interface Params<T> {
86
[key: string]: T;
97
}
108

11-
function createInstance ( params?: Params<object>, timeout?: number ) {
12-
if ( params !== undefined ) baseConfig.params = params;
13-
if ( timeout !== undefined ) baseConfig.timeout = timeout;
9+
function createInstance (params?: Params<object>, timeout?: number): AxiosInstance {
10+
if (params !== undefined) baseConfig.params = params;
11+
if (timeout !== undefined) baseConfig.timeout = timeout;
1412

15-
const instance = axios.create( baseConfig );
16-
requstInterceptor( instance );
17-
responseInterceptor( instance );
13+
const instance = axios.create(baseConfig);
14+
requstInterceptor(instance);
15+
responseInterceptor(instance);
1816

1917
return instance;
2018
}
2119

22-
function requstInterceptor ( instance: AxiosInstance ) {
23-
instance.interceptors.request.use( ( config: AxiosRequestConfig ) => {
24-
console.log( config );
20+
function requstInterceptor (instance: AxiosInstance): void {
21+
instance.interceptors.request.use((config: AxiosRequestConfig) => {
22+
console.log(config);
2523
return config;
26-
} );
24+
});
2725
}
2826

29-
function responseInterceptor ( instance: AxiosInstance ) {
30-
instance.interceptors.response.use( ( response: AxiosResponse ) => {
31-
console.log( response );
27+
function responseInterceptor (instance: AxiosInstance): void {
28+
instance.interceptors.response.use((response: AxiosResponse) => {
29+
console.log(response);
3230
return response;
33-
} );
31+
});
3432
}
3533

36-
function requestGet ( url: string, params?: Params<object> ) {
37-
return createInstance( params )
38-
.get( url )
39-
.catch( ( error: Error ) => {
40-
errorHandler( error );
41-
} )
42-
.finally( () => {
34+
function requestGet (url: string, params?: Params<object>): Promise<void | AxiosResponse<any>> {
35+
return createInstance(params)
36+
.get(url)
37+
.catch((error: Error) => {
38+
errorHandler(error);
39+
})
40+
.finally(() => {
4341
doFinally();
44-
} );
42+
});
4543
}
4644

47-
function requestPost ( url: string, params?: Params<object> ) {
48-
return createInstance( params )
49-
.post( url )
50-
.catch( ( error: Error ) => {
51-
errorHandler( error );
52-
} )
53-
.finally( () => {
45+
function requestPost (url: string, params?: Params<object>): Promise<void | AxiosResponse<any>> {
46+
return createInstance(params)
47+
.post(url)
48+
.catch((error: Error) => {
49+
errorHandler(error);
50+
})
51+
.finally(() => {
5452
doFinally();
55-
} );
53+
});
5654
}
57-
function errorHandler ( error: Error ) {
58-
console.log( error );
55+
56+
function errorHandler (error: Error): void {
57+
console.log(error);
5958
};
6059

61-
function doFinally () {
62-
console.log( 'finally' );
60+
function doFinally (): void {
61+
console.log('finally');
6362
}
6463

65-
const axiosHandler: any = {};
64+
export interface AxiosHandlerStatic {
65+
requestGet (url: string, params?: Params<any>): Promise<void | AxiosResponse<any>>;
66+
requestPost (url: string, params?: Params<any>): Promise<void | AxiosResponse<any>>;
67+
}
6668

67-
axiosHandler.requestGet = requestGet;
68-
axiosHandler.requestPost = requestPost;
69+
const axiosHandler: AxiosHandlerStatic = {
70+
requestGet: requestGet,
71+
requestPost: requestPost
72+
};
6973

74+
export { AxiosResponse };
7075
export default axiosHandler;

src/plugins/store/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ import Vuex, { StoreOptions } from 'vuex';
33
import app from './modules/app';
44
import home from './modules/home';
55
import activity from './modules/activity';
6+
67
// register VUEX
78
Vue.use(Vuex);
89

9-
export interface State {
10-
[key: string]: any
11-
}
12-
13-
let opt: StoreOptions < any > = {
10+
let opt: StoreOptions<any> = {
1411
// strict mode
1512
strict: true,
1613

@@ -21,10 +18,16 @@ let opt: StoreOptions < any > = {
2118
state: {
2219
user: 'user'
2320
},
21+
2422
mutations: {},
2523
actions: {},
2624
getters: {}
2725

2826
};
2927

28+
export interface State<T> {
29+
[key: string]: T;
30+
}
31+
32+
export { Action, ActionContext, MutationTree, Mutation, Getter } from 'vuex';
3033
export default new Vuex.Store(opt);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Store } from 'vuex';
22
import { State } from '@/plugins/store';
33

44
const actions = {
5-
65
};
76

87
export default actions;

src/plugins/store/modules/activity/modules/getters.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export interface Category {
66
to: string;
77
}
88

9-
const getters: GetterTree < State, Getter < string, any > > = {
10-
categories: (state: State) => {
9+
const getters: GetterTree<State<any>, Getter<string, any>> = {
10+
categories: (state: State<any>) => {
1111
const categories: Category[] = [];
1212

1313
for (const photo of state.photos) {
@@ -26,7 +26,7 @@ const getters: GetterTree < State, Getter < string, any > > = {
2626

2727
return categories.sort().slice(0, 4);
2828
},
29-
links: (state: State, getters: any) => {
29+
links: (state: State<any>, getters: any) => {
3030
return state.items.concat(getters.categories);
3131
}
3232
};

src/plugins/store/modules/activity/modules/mutations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { State } from '@/plugins/store';
22
import { MutationTree, Mutation } from 'vuex';
33

44
const mutations: MutationTree < Mutation < any > > = {
5-
setDrawer: (state: State, payload: boolean) => (state.drawer = payload),
6-
toggleDrawer: (state: State) => (state.drawer = !state.drawer)
5+
setDrawer: (state: State<any>, payload: boolean) => (state.drawer = payload),
6+
toggleDrawer: (state: State<any>) => (state.drawer = !state.drawer)
77
};
88

99
export default mutations;
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
import { articles } from '@/data/articles';
22
import { State } from '@/plugins/store';
33

4-
export interface Item {
5-
[key: string]: string
6-
}
7-
8-
let items: Item[] = [{
9-
text: 'Home',
10-
to: '/'
11-
},
12-
{
13-
text: 'About',
14-
href: '#about'
15-
}
16-
];
17-
18-
const state: State = {
4+
const state: State<any> = {
195
photos: articles,
20-
drawer: false,
21-
items: items
6+
sponsorId: ''
227
};
238

249
export default state;
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import state from './modules/state';
2-
import mutations from './modules/mutations';
3-
import actions from './modules/actions';
4-
import getters from './modules/getters';
1+
import state, { StateType } from './modules/state';
2+
import mutations, { MutationType } from './modules/mutations';
3+
import actions, { ActionType } from './modules/actions';
4+
import getters, { GetterType } from './modules/getters';
55

66
const app = {
77
namespaced: true, // 成為帶名稱空間的模組
@@ -11,4 +11,5 @@ const app = {
1111
getters: getters
1212
};
1313

14+
export { StateType, MutationType, ActionType, GetterType };
1415
export default app;

0 commit comments

Comments
 (0)