/
index.ts
137 lines (116 loc) · 3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Ref, ref, shallowRef } from 'vue-demi'
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse, CancelTokenSource, AxiosInstance } from 'axios'
export interface UseAxiosReturn<T> {
/**
* Axios Response
*/
response: Ref<AxiosResponse<T> | undefined>
/**
* Axios response data
*/
data: Ref<T | undefined>
/**
* @deprecated use isFinished instead
*/
finished: Ref<boolean>
/**
* @deprecated use isLoading instead
*/
loading: Ref<boolean>
/**
* Indicates if the request has finished
*/
isFinished: Ref<boolean>
/**
* Indicates if the request is currently loading
*/
isLoading: Ref<boolean>
/**
* @deprecated use aborted instead
*/
canceled: Ref<boolean>
/**
* Indicates if the request was canceled
*/
aborted: Ref<boolean>
/**
* Any erros that may have occurred
*/
error: Ref<AxiosError<T> | undefined>
/**
* @deprecated use abort instead
*/
cancel: (message?: string | undefined) => void
/**
* Aborts the current request
*/
abort: (message?: string | undefined) => void
}
export function useAxios<T = any>(url: string, config?: AxiosRequestConfig): UseAxiosReturn<T>
export function useAxios<T = any>(url: string, instance?: AxiosInstance): UseAxiosReturn<T>
export function useAxios<T = any>(url: string, config: AxiosRequestConfig, instance: AxiosInstance): UseAxiosReturn<T>
/**
* Wrapper for axios.
*
* @see https://vueuse.org/useAxios
* @param url
* @param config
*/
export function useAxios<T = any>(url: string, ...args: any[]) {
let config: AxiosRequestConfig = {}
let instance: AxiosInstance = axios
if (args.length > 0) {
/**
* Unable to use `instanceof` here becuase of (https://github.com/axios/axios/issues/737)
* so instead we are checking if there is a `requset` on the object to see if it is an
* axios instance
*/
if ('request' in args[0])
instance = args[0]
else
config = args[0]
}
if (args.length > 1) {
if ('request' in args[1])
instance = args[1]
}
const response = shallowRef<AxiosResponse<T>>()
const data = shallowRef<T>()
const isFinished = ref(false)
const isLoading = ref(true)
const aborted = ref(false)
const error = shallowRef<AxiosError<T>>()
const cancelToken: CancelTokenSource = axios.CancelToken.source()
const abort = (message?: string) => {
if (isFinished.value || !isLoading.value) return
cancelToken.cancel(message)
aborted.value = true
isLoading.value = false
isFinished.value = false
}
instance(url, { ...config, cancelToken: cancelToken.token })
.then((r: AxiosResponse<T>) => {
response.value = r
data.value = r.data
})
.catch((e) => {
error.value = e
})
.finally(() => {
isLoading.value = false
isFinished.value = true
})
return {
response,
data,
error,
finished: isFinished,
loading: isLoading,
isFinished,
isLoading,
cancel: abort,
canceled: aborted,
aborted,
abort,
}
}