forked from vueuse/vueuse
/
index.ts
218 lines (195 loc) · 6.55 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import type { Ref, ShallowRef } from 'vue-demi'
import { ref, shallowRef } from 'vue-demi'
import { isString, until } from '@vueuse/shared'
import type { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, CancelTokenSource } from 'axios'
import axios from 'axios'
export interface UseAxiosReturn<T, D> {
/**
* Axios Response
*/
response: ShallowRef<AxiosResponse<T, D> | undefined>
/**
* Axios response data
*/
data: Ref<T | undefined>
/**
* Indicates if the request has finished
*/
isFinished: Ref<boolean>
/**
* Indicates if the request is currently loading
*/
isLoading: Ref<boolean>
/**
* Indicates if the request was canceled
*/
isAborted: Ref<boolean>
/**
* Any errors that may have occurred
*/
error: ShallowRef<AxiosError<T> | undefined>
/**
* Aborts the current request
*/
abort: (message?: string | undefined) => void
/**
* isFinished alias
* @deprecated use `isFinished` instead
*/
finished: Ref<boolean>
/**
* isLoading alias
* @deprecated use `isLoading` instead
*/
loading: Ref<boolean>
/**
* isAborted alias
* @deprecated use `isAborted` instead
*/
aborted: Ref<boolean>
/**
* abort alias
*/
cancel: (message?: string | undefined) => void
/**
* isAborted alias
* @deprecated use `isCanceled` instead
*/
canceled: Ref<boolean>
/**
* isAborted alias
*/
isCanceled: Ref<boolean>
}
export interface StrictUseAxiosReturn<T, D> extends UseAxiosReturn<T, D> {
/**
* Manually call the axios request
*/
execute: (url?: string | AxiosRequestConfig<D>, config?: AxiosRequestConfig<D>) => PromiseLike<StrictUseAxiosReturn<T, D>>
}
export interface EasyUseAxiosReturn<T, D> extends UseAxiosReturn<T, D> {
/**
* Manually call the axios request
*/
execute: (url: string, config?: AxiosRequestConfig<D>) => PromiseLike<EasyUseAxiosReturn<T, D>>
}
export interface UseAxiosOptions {
/**
* Will automatically run axios request when `useAxios` is used
*
*/
immediate?: boolean
/**
* Use shallowRef.
*
* @default true
*/
shallow?: boolean
}
type OverallUseAxiosReturn<T, D> = StrictUseAxiosReturn<T, D> | EasyUseAxiosReturn<T, D>
export function useAxios<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>, options?: UseAxiosOptions): StrictUseAxiosReturn<T, D> & PromiseLike<StrictUseAxiosReturn<T, D>>
export function useAxios<T = any, D = any>(url: string, instance?: AxiosInstance, options?: UseAxiosOptions): StrictUseAxiosReturn<T, D> & PromiseLike<StrictUseAxiosReturn<T, D>>
export function useAxios<T = any, D = any>(url: string, config: AxiosRequestConfig<D>, instance: AxiosInstance, options?: UseAxiosOptions): StrictUseAxiosReturn<T, D> & PromiseLike<StrictUseAxiosReturn<T, D>>
export function useAxios<T = any, D = any>(config?: AxiosRequestConfig<D>): EasyUseAxiosReturn<T, D> & PromiseLike<EasyUseAxiosReturn<T, D>>
export function useAxios<T = any, D = any>(instance?: AxiosInstance): EasyUseAxiosReturn<T, D> & PromiseLike<EasyUseAxiosReturn<T, D>>
export function useAxios<T = any, D = any>(config?: AxiosRequestConfig<D>, instance?: AxiosInstance): EasyUseAxiosReturn<T, D> & PromiseLike<EasyUseAxiosReturn<T, D>>
/**
* Wrapper for axios.
*
* @see https://vueuse.org/useAxios
*/
export function useAxios<T = any, D = any>(...args: any[]): OverallUseAxiosReturn<T, D> & PromiseLike<OverallUseAxiosReturn<T, D>> {
const url: string | undefined = typeof args[0] === 'string' ? args[0] : undefined
const argsPlaceholder = isString(url) ? 1 : 0
let defaultConfig: AxiosRequestConfig<D> = {}
let instance: AxiosInstance = axios
let options: UseAxiosOptions = { immediate: !!argsPlaceholder, shallow: true }
const isAxiosInstance = (val: any) => !!val?.request
if (args.length > 0 + argsPlaceholder) {
/**
* Unable to use `instanceof` here because of (https://github.com/axios/axios/issues/737)
* so instead we are checking if there is a `request` on the object to see if it is an
* axios instance
*/
if (isAxiosInstance(args[0 + argsPlaceholder]))
instance = args[0 + argsPlaceholder]
else
defaultConfig = args[0 + argsPlaceholder]
}
if (args.length > 1 + argsPlaceholder) {
if (isAxiosInstance(args[1 + argsPlaceholder]))
instance = args[1 + argsPlaceholder]
}
if (
(args.length === 2 + argsPlaceholder && !isAxiosInstance(args[1 + argsPlaceholder]))
|| args.length === 3 + argsPlaceholder
)
options = args[args.length - 1]
const response = shallowRef<AxiosResponse<T>>()
const data = options.shallow ? shallowRef<T>() : ref<T>()
const isFinished = ref(false)
const isLoading = ref(false)
const isAborted = 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)
isAborted.value = true
isLoading.value = false
isFinished.value = false
}
const loading = (loading: boolean) => {
isLoading.value = loading
isFinished.value = !loading
}
const waitUntilFinished = () =>
new Promise<OverallUseAxiosReturn<T, D>>((resolve, reject) => {
until(isFinished).toBe(true)
// eslint-disable-next-line @typescript-eslint/no-use-before-define
.then(() => resolve(result))
.catch(reject)
})
const then: PromiseLike<OverallUseAxiosReturn<T, D>>['then'] = (onFulfilled, onRejected) =>
waitUntilFinished().then(onFulfilled, onRejected)
const execute: OverallUseAxiosReturn<T, D>['execute'] = (executeUrl: string | AxiosRequestConfig<D> | undefined = url, config: AxiosRequestConfig<D> = {}) => {
error.value = undefined
const _url = typeof executeUrl === 'string'
? executeUrl
: url ?? ''
loading(true)
instance(_url, { ...defaultConfig, ...typeof executeUrl === 'object' ? executeUrl : config, cancelToken: cancelToken.token })
.then((r: any) => {
response.value = r
data.value = r.data
})
.catch((e: any) => {
error.value = e
})
.finally(() => loading(false))
return { then }
}
if (options.immediate && url)
(execute as StrictUseAxiosReturn<T, D>['execute'])()
const result = {
response,
data,
error,
finished: isFinished,
loading: isLoading,
isFinished,
isLoading,
cancel: abort,
isAborted,
canceled: isAborted,
aborted: isAborted,
isCanceled: isAborted,
abort,
execute,
} as OverallUseAxiosReturn<T, D>
return {
...result,
then,
}
}