@@ -10,8 +10,14 @@ export interface RunOptions<V, P> {
10
10
cloneResetValue ?: boolean | ( ( value : V ) => V )
11
11
}
12
12
13
- export type Run < V , R , P > = ( options ?: RunOptions < V , P > ) => Promise < R >
14
-
13
+ export type UseAxleExtra < V > = {
14
+ uploadProgress : Ref < number >
15
+ downloadProgress : Ref < number >
16
+ loading : Ref < boolean >
17
+ error : Ref < Error | undefined >
18
+ abort ( ) : void
19
+ resetValue ( options ?: ResetValueOptions < V > ) : void
20
+ }
15
21
export interface UseAxleRefs < V > {
16
22
value : Ref < V >
17
23
loading : Ref < boolean >
@@ -20,6 +26,10 @@ export interface UseAxleRefs<V> {
20
26
downloadProgress : Ref < number >
21
27
}
22
28
29
+ export type Run < V , R , P > = {
30
+ ( options ?: RunOptions < V , P > ) : Promise < R >
31
+ } & UseAxleExtra < V >
32
+
23
33
export interface UseAxleOptions < V = any , R = any , P = Record < string , any > > {
24
34
url : string | ( ( ) => string )
25
35
method : RunnerMethod
@@ -40,18 +50,7 @@ export interface ResetValueOptions<V> {
40
50
cloneResetValue ?: boolean | ( ( value : V ) => V )
41
51
}
42
52
43
- export type UseAxleInstance < V , R , P > = [
44
- value : Ref < V > ,
45
- run : Run < V , R , P > ,
46
- extra : {
47
- uploadProgress : Ref < number >
48
- downloadProgress : Ref < number >
49
- loading : Ref < boolean >
50
- error : Ref < Error | undefined >
51
- abort ( ) : void
52
- resetValue ( options ?: ResetValueOptions < V > ) : void
53
- }
54
- ]
53
+ export type UseAxleInstance < V , R , P > = [ value : Ref < V > , run : Run < V , R , P > , extra : UseAxleExtra < V > ]
55
54
56
55
export interface CreateUseAxleOptions {
57
56
axle : AxleInstance
@@ -104,75 +103,79 @@ export function createUseAxle(options: CreateUseAxleOptions) {
104
103
uploadProgress,
105
104
}
106
105
107
- let controller = new AbortController ( )
108
-
109
- const resetValue = ( options : ResetValueOptions < V > = { } ) => {
110
- const cloneResetValue = options . cloneResetValue ?? initialCloneResetValue ?? false
111
- const cloneFn =
112
- cloneResetValue === true
113
- ? ( v : V ) => ( v == null ? null : JSON . parse ( JSON . stringify ( v ) ) )
114
- : isFunction ( initialCloneResetValue )
115
- ? initialCloneResetValue
116
- : ( v : V ) => v
117
- value . value = cloneFn ( initialValue as V )
106
+ const extra : UseAxleExtra < V > = {
107
+ uploadProgress,
108
+ downloadProgress,
109
+ loading,
110
+ error,
111
+ abort,
112
+ resetValue,
118
113
}
119
114
120
- const run : Run < V , R , P > = async ( options : RunOptions < V , P > = { } ) => {
121
- if ( controller . signal . aborted ) {
122
- controller = new AbortController ( )
123
- }
115
+ let controller = new AbortController ( )
124
116
125
- const resetValueOption = options . resetValue ?? initialResetValue ?? false
126
- if ( resetValueOption === true ) {
127
- resetValue ( options )
128
- }
117
+ const run : Run < V , R , P > = Object . assign (
118
+ async ( options : RunOptions < V , P > = { } ) => {
119
+ if ( controller . signal . aborted ) {
120
+ controller = new AbortController ( )
121
+ }
129
122
130
- uploadProgress . value = 0
131
- downloadProgress . value = 0
123
+ const resetValueOption = options . resetValue ?? initialResetValue ?? false
124
+ if ( resetValueOption === true ) {
125
+ resetValue ( options )
126
+ }
132
127
133
- const url = options . url ?? normalizeValueGetter ( initialUrlOrGetter )
134
- const params = options . params ?? normalizeValueGetter ( initialParamsOrGetter )
135
- const config = options . config ?? normalizeValueGetter ( initialConfigOrGetter )
128
+ uploadProgress . value = 0
129
+ downloadProgress . value = 0
136
130
137
- onBefore ( refs )
131
+ const url = options . url ?? normalizeValueGetter ( initialUrlOrGetter )
132
+ const params = options . params ?? normalizeValueGetter ( initialParamsOrGetter )
133
+ const config = options . config ?? normalizeValueGetter ( initialConfigOrGetter )
138
134
139
- loading . value = true
135
+ onBefore ( refs )
140
136
141
- try {
142
- const response = await axle [ method ] ( url , params , {
143
- signal : controller . signal ,
137
+ loading . value = true
144
138
145
- onUploadProgress ( event ) {
146
- uploadProgress . value = event . progress ?? 0
147
- } ,
139
+ try {
140
+ const response = await axle [ method ] ( url , params , {
141
+ signal : controller . signal ,
148
142
149
- onDownloadProgress ( event ) {
150
- downloadProgress . value = event . progress ?? 0
151
- } ,
143
+ onUploadProgress ( event ) {
144
+ uploadProgress . value = event . progress ?? 0
145
+ } ,
152
146
153
- ...config ,
154
- } )
147
+ onDownloadProgress ( event ) {
148
+ downloadProgress . value = event . progress ?? 0
149
+ } ,
155
150
156
- value . value = await onTransform ( response as R , refs )
157
- error . value = undefined
158
- onSuccess ( response as R , refs )
159
- loading . value = false
160
- onAfter ( refs )
151
+ ...config ,
152
+ } )
161
153
162
- return response as R
163
- } catch ( responseError : any ) {
164
- error . value = responseError as Error
165
- onError ( responseError as Error , refs )
166
- loading . value = false
167
- onAfter ( refs )
154
+ value . value = await onTransform ( response as R , refs )
155
+ error . value = undefined
156
+ onSuccess ( response as R , refs )
157
+ loading . value = false
158
+ onAfter ( refs )
168
159
169
- throw responseError
170
- }
171
- }
160
+ return response as R
161
+ } catch ( responseError : any ) {
162
+ error . value = responseError as Error
163
+ onError ( responseError as Error , refs )
164
+ loading . value = false
165
+ onAfter ( refs )
172
166
173
- const abort = ( ) => {
174
- controller . abort ( )
175
- }
167
+ throw responseError
168
+ }
169
+ } ,
170
+ {
171
+ uploadProgress,
172
+ downloadProgress,
173
+ loading,
174
+ error,
175
+ abort,
176
+ resetValue,
177
+ }
178
+ )
176
179
177
180
if ( immediate ) {
178
181
run ( {
@@ -182,18 +185,22 @@ export function createUseAxle(options: CreateUseAxleOptions) {
182
185
} )
183
186
}
184
187
185
- return [
186
- value ,
187
- run ,
188
- {
189
- loading,
190
- error,
191
- uploadProgress,
192
- downloadProgress,
193
- abort,
194
- resetValue,
195
- } ,
196
- ]
188
+ function resetValue ( options : ResetValueOptions < V > = { } ) {
189
+ const cloneResetValue = options . cloneResetValue ?? initialCloneResetValue ?? false
190
+ const cloneFn =
191
+ cloneResetValue === true
192
+ ? ( v : V ) => ( v == null ? null : JSON . parse ( JSON . stringify ( v ) ) )
193
+ : isFunction ( initialCloneResetValue )
194
+ ? initialCloneResetValue
195
+ : ( v : V ) => v
196
+ value . value = cloneFn ( initialValue as V )
197
+ }
198
+
199
+ function abort ( ) {
200
+ controller . abort ( )
201
+ }
202
+
203
+ return [ value , run , extra ]
197
204
}
198
205
199
206
return useAxle
0 commit comments