Skip to content

Commit 5b73a6c

Browse files
Extract axios handler to declaration file, make it abstract.
1 parent 3acc4ef commit 5b73a6c

File tree

9 files changed

+125
-77
lines changed

9 files changed

+125
-77
lines changed

src/pages/generic/cta/Cta.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Component, Vue, Prop, Watch, Emit } from 'vue-property-decorator';
2-
import { requestGet } from '@/plugins/axios-handler';
32
import { namespace } from 'vuex-class';
43
@Component({})
54
export default class Cta extends Vue {

src/pages/generic/cta/Cta.vue

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<template>
2-
<v-btn fab
3-
fixed
4-
bottom
5-
right
6-
color="secondary"
7-
@click="hello()"
8-
target="_blank"
9-
rel="noopener">
2+
<v-btn
3+
fab
4+
fixed
5+
bottom
6+
right
7+
color="secondary"
8+
@click="hello()"
9+
target="_blank"
10+
rel="noopener"
11+
>
1012
<v-icon>mdi-download</v-icon>
1113
</v-btn>
1214
</template>

src/pages/generic/nav_bar/NavBar.vue

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
<template>
2-
<v-app-bar app color="primary" dark>
2+
<v-app-bar
3+
app
4+
color="primary"
5+
dark
6+
>
37
<div class="d-flex align-center">
4-
<v-img alt="Vuetify Logo" class="shrink mr-2" contain src="https://cdn.vuetifyjs.com/images/logos/vuetify-logo-dark.png" transition="scale-transition" width="40" />
8+
<v-img
9+
alt="Vuetify Logo"
10+
class="shrink mr-2"
11+
contain
12+
src="https://cdn.vuetifyjs.com/images/logos/vuetify-logo-dark.png"
13+
transition="scale-transition"
14+
width="40"
15+
/>
516

6-
<v-img alt="Vuetify Name" class="shrink mt-1 hidden-sm-and-down" contain min-width="100" src="https://cdn.vuetifyjs.com/images/logos/vuetify-name-dark.png" width="100" />
17+
<v-img
18+
alt="Vuetify Name"
19+
class="shrink mt-1 hidden-sm-and-down"
20+
contain
21+
min-width="100"
22+
src="https://cdn.vuetifyjs.com/images/logos/vuetify-name-dark.png"
23+
width="100"
24+
/>
725
</div>
826

927
<v-spacer></v-spacer>
1028

1129
<router-link to="/login">
12-
<samll-btn displayWording="LOGIN NOW!" :iconType="iconType.Person" />
30+
<samll-btn
31+
displayWording="LOGIN NOW!"
32+
:iconType="iconType.Person"
33+
/>
1334
</router-link>
1435
</v-app-bar>
1536
</template>

src/pages/generic/toolbar/Toolbar.vue

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
11
<template>
2-
<v-app-bar app
3-
flat>
4-
<v-app-bar-nav-icon class="hidden-md-and-up"
5-
@click="toggleDrawer" />
2+
<v-app-bar
3+
app
4+
flat
5+
>
6+
<v-app-bar-nav-icon
7+
class="hidden-md-and-up"
8+
@click="toggleDrawer"
9+
/>
610
<v-container class="mx-auto py-0">
711
<v-row>
8-
<v-img :src="require('@/assets/logo.png')"
9-
contain
10-
class="mr-5"
11-
height="48"
12-
width="48"
13-
max-width="48"
14-
@click="$vuetify.goTo(0)" />
15-
<v-btn v-for="(link, i) in links"
16-
:key="i"
17-
:to="link.to"
18-
class="ml-2 hidden-sm-and-down"
19-
tile
20-
text
21-
@click="onClickButton($event, link)">
12+
<v-img
13+
:src="require('@/assets/logo.png')"
14+
contain
15+
class="mr-5"
16+
height="48"
17+
width="48"
18+
max-width="48"
19+
@click="$vuetify.goTo(0)"
20+
/>
21+
<v-btn
22+
v-for="(link, i) in links"
23+
:key="i"
24+
:to="link.to"
25+
class="ml-2 hidden-sm-and-down"
26+
tile
27+
text
28+
@click="onClickButton($event, link)"
29+
>
2230
{{ link.text }}
2331
</v-btn>
2432
<v-spacer />
25-
<v-text-field append-icon="mdi-magnify"
26-
flat
27-
hide-details
28-
solo-inverted
29-
style="max-width: 300px;" />
33+
<v-text-field
34+
append-icon="mdi-magnify"
35+
flat
36+
hide-details
37+
solo-inverted
38+
style="max-width: 300px;"
39+
/>
3040
</v-row>
3141
</v-container>
3242
</v-app-bar>
File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface Params<T> {
2+
[key: string]: T;
3+
}
4+
5+
export interface AxiosHandlerStatic<T> {
6+
requestGet ( url: string, params?: Params<T> ): Function;
7+
requestPost ( url: string, params?: Params<T> ): Function;
8+
}
9+
10+
declare const AxiosHandler: AxiosHandlerStatic;
11+
export default AxiosHandler;

src/plugins/axios-handler/index.ts

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

6-
// define AJAX params that should be a key value pair
7-
interface Params {
8-
[key: string]: any
6+
// define request params that should be a key value pair
7+
interface Params<T> {
8+
[key: string]: T;
99
}
1010

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

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

1919
return instance;
2020
}
2121

22-
function requstInterceptor (instance: AxiosInstance) {
23-
instance.interceptors.request.use((config: AxiosRequestConfig) => {
24-
console.log(config);
22+
function requstInterceptor ( instance: AxiosInstance ) {
23+
instance.interceptors.request.use( ( config: AxiosRequestConfig ) => {
24+
console.log( config );
2525
return config;
26-
});
26+
} );
2727
}
2828

29-
function responseInterceptor (instance: AxiosInstance) {
30-
instance.interceptors.response.use((response: AxiosResponse) => {
31-
console.log(response);
29+
function responseInterceptor ( instance: AxiosInstance ) {
30+
instance.interceptors.response.use( ( response: AxiosResponse ) => {
31+
console.log( response );
3232
return response;
33-
});
33+
} );
3434
}
3535

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(() => {
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( () => {
4343
doFinally();
44-
});
44+
} );
4545
}
4646

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(() => {
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( () => {
5454
doFinally();
55-
});
55+
} );
5656
}
57-
58-
function errorHandler (error: Error) {
59-
console.log(error);
57+
function errorHandler ( error: Error ) {
58+
console.log( error );
6059
};
6160

6261
function doFinally () {
63-
console.log('finally');
62+
console.log( 'finally' );
6463
}
64+
65+
const axiosHandler: any = {};
66+
67+
axiosHandler.requestGet = requestGet;
68+
axiosHandler.requestPost = requestPost;
69+
70+
export default axiosHandler;

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

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

65
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Store } from 'vuex';
22
import { State } from '@/plugins/store';
3-
import { requestGet } from '@/plugins/axios-handler';
3+
import AxiosHandler from '@/plugins/axios-handler';
44

55
const actions = {
66
fetchImages: function () {
7-
return requestGet('fetchImages', {
7+
return AxiosHandler.requestGet('fetchImages', {
88
size: '1024*720'
99
});
1010
}

0 commit comments

Comments
 (0)