-
-
Notifications
You must be signed in to change notification settings - Fork 298
Description
Description
Summary
When using composable: 'useAsyncData' in the Nuxt client, the watch option should be supported to enable reactive re-fetching when specified dependencies change.
Current Behavior
Currently, when using the composable pattern with composable: 'useAsyncData', the generated Options type does not include the watch property, making it impossible to enable reactive data fetching directly through the API function options.
Current workaround:
// Must manually wrap the API call in useAsyncData
const { data } = await useAsyncData(
() => `key-${selectedClubId.value}`,
() => clubsControllerGetClubUsers({
path: { clubId: selectedClubId.value },
}),
{ watch: [selectedClubId] }, // watch option here
);Desired Behavior
The watch option should be supported directly in the API function call when using composable: 'useAsyncData', allowing for cleaner code:
// Direct API call with watch option
const { data } = await clubsControllerGetClubUsers({
composable: 'useAsyncData',
key: 'club-users',
path: { clubId: selectedClubId.value },
watch: [selectedClubId], // should be supported
});Benefits
- Cleaner API: No need to manually wrap API calls in
useAsyncData - Type Safety: The
watchoption would be properly typed - Consistency: Matches the pattern of using
keyand other composable options - Less Boilerplate: Reduces code duplication across Nuxt pages
- Developer Experience: More intuitive for developers familiar with Nuxt composition API
Use Case
In a multi-tenant or multi-resource application where the same API endpoint is called with different parameters based on a reactive value (like a selected club ID), it's natural to want to reactively refetch the data when that dependency changes. The current pattern requires manually wrapping each such call in useAsyncData, which is verbose and error-prone.
Implementation Notes
The watch option would need to be:
- Added to the
Optionstype definition whencomposable: 'useAsyncData' - Passed through to the underlying
useAsyncDatacall in the client implementation - Properly typed to accept
WatchSource<any>[]or similar