@@ -2,7 +2,7 @@ import type { MaybeRef, Ref } from '#imports'
2
2
import type { NitroFetchRequest } from 'nitro'
3
3
import type { AsyncData , AsyncDataOptions , AsyncDataRequestStatus , NuxtError } from 'nuxt/app'
4
4
import type { FetchContext , FetchMethod , FetchResponse , HTTPConfig , Interceptors , KeysOf , PickFrom } from './type'
5
- import { createError , reactive , ref , toValue , unref , useAsyncData , useNuxtApp } from '#imports'
5
+ import { createError , getCurrentScope , onScopeDispose , reactive , ref , toValue , unref , useAsyncData , useNuxtApp , useRequestFetch , watch } from '#imports'
6
6
import { hash , serialize } from 'ohash'
7
7
8
8
type CustomFetchReturnValue < DataT , ErrorT > = AsyncData < PickFrom < DataT , KeysOf < DataT > > | null , ( ErrorT extends Error | NuxtError < unknown > ? ErrorT : NuxtError < ErrorT > ) | null >
@@ -171,7 +171,7 @@ export class CustomFetch {
171
171
_cachedController . get ( key ) ?. abort ?.( )
172
172
}
173
173
174
- return $fetch ( url as string , {
174
+ return useRequestFetch ( ) ( url as string , {
175
175
signal : _cachedController . get ( key ) ?. signal ,
176
176
...defaultOptions ,
177
177
..._config
@@ -192,6 +192,8 @@ export class CustomFetch {
192
192
pending : Ref < boolean >
193
193
error : Ref < ( ErrorT extends Error | NuxtError < unknown > ? ErrorT : NuxtError < ErrorT > ) | null >
194
194
status : Ref < AsyncDataRequestStatus >
195
+ refresh ?: ( ) => Promise < DataT >
196
+ execute ?: ( ) => Promise < DataT >
195
197
} = {
196
198
data : ref ( null ) ,
197
199
pending : ref ( true ) ,
@@ -235,6 +237,18 @@ export class CustomFetch {
235
237
asyncData . pending . value = false
236
238
_cachedController . delete ( key )
237
239
} )
240
+
241
+ asyncData . refresh = asyncData . execute = ( ) => _handler ( ) . then ( result => asyncData . data . value = result )
242
+ const hasScope = getCurrentScope ( )
243
+ if ( options . watch ) {
244
+ const unsub = watch ( options . watch , async ( ) => {
245
+ asyncData . refresh ! ( )
246
+ } )
247
+ if ( hasScope ) {
248
+ onScopeDispose ( unsub )
249
+ }
250
+ }
251
+
238
252
return promise as any
239
253
}
240
254
0 commit comments