-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
feat: add a factory function for useFetch
and useAsyncData
#32300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
@nuxt/kit
nuxt
@nuxt/rspack-builder
@nuxt/schema
@nuxt/vite-builder
@nuxt/webpack-builder
commit: |
CodSpeed Performance ReportMerging #32300 will not alter performanceComparing Summary
|
@cernymatej It would also be great to support creating a custom |
@maximepvrt that is already possible in using https://nuxt.com/docs/guide/recipes/custom-usefetch#custom-fetch |
@cernymatej Yes, you're right. Tt's possible via It would be great if we could also easily create a custom export const useClientFetch = createUseFetch({ fetch: $api }) or export const useClientFetch = createUseFetch($api) |
You can already register the custom // utils/myFetch.ts
export const $myFetch = $fetch.create() // will be auto-imported as for providing a custom // composables/myUseFetch.ts
import { $myFetch } from '../utils/myFetch'
export const myUseFetch = createUseFetch({ $fetch: $myFetch }) |
Perfect 🥰🥰 |
Warning
This is an initial draft of the PR. Treat it as a proof of concept and a starting point for discussions about the ideal API. None of the points mentioned below are final - everything is very much a work in progress.
🔗 Linked issue
resolves #14736
resolves #14936
resolves #28792
resolves #28485
resolves #24157
fixes #26275
📚 Description
This PR introduces a way to create custom
useFetch
anduseAsyncData
functions with user-defined default options.These custom instances can be configured with either static options or a dynamic getter, which can be used to access values from
runtimeConfig
, for example.How it works
Both composables rely on automatic key injection. For this reason, it was necessary to create a mechanism to automatically register these new custom instances to
optimization.keyedComposables
. This registration occurs during Nuxt initialization, where thecomposables
folder is scanned for usages of these factory functions. Each export is processed, and the corresponding composable identifier is registered for key injection.The factory functions should be treated more like compiler macros. Because of this, there are only a few places where they can be used - specifically, as exports from files in the
composables
directory. To warn users about incorrect usage, the runtime version of these factory functions simply throws an error. Only when they are correctly recognized and processed during compilation are they replaced with the actual factory functions (which are prefixed with an underscore, e.g.,_createUseFetch()
).I plan to create a guide for manual creation later on. I believe it's important to ensure this works with auto-imports fully disabled.
🔖 Additional changes
This PR also allows modules to extend the
keyedComposables
config, which wasn't possible before. (#26275) With this change, I believe there's a greater need to introduce a more fine-grained approach to defining keyed composables. Modules could potentially add many keyed functions, which might collide with user-defined functions or those from other modules.I propose that the
source
option - already available forkeyedComposables
(though never documented anywhere) - should be treated as the path to the file from which the composable originates. This would allow the build plugin to target only the specific functions. I would even consider making this option required to prevent accidental errors, and possibly removing the option to pass a regex to it - since the function will always be defined in a single file, so I don’t really see the need for a regex - in my opition, it only makes the definition “less specific”. (This hasn’t been implemented yet, since it would be considered a breaking change… maybe we can include it in Nuxt 4? The extension of the string comparison - which has been implemented here - shouldn’t break anything, though)💬 Additional points to discuss
onRequest
completely override the defaults when calling the composable, or should they merge with them? We could potentially separate these two strategies under different keys in the factory configuration object. Currently, everything is grouped underdefaults
, but I'm considering introducing a second key (e.g.,merge
) that would merge the options. This would allow users to define anonRequest
handler when calling the composable, while still executing the one defined in the factory function. (similar issue Allow having interceptos from both defaults and per request unjs/ofetch#319)@nuxt/kit
?source
option in thekeyedComposables
object required and remove the possibility to pass a regex?useFetch
pass cookies from the server to the client (by default?). Is this something we want to tackle in this PR, or should we revisit it later? (Simplify Set-Cookie header forwarding #32306)key
option is set #28169UseFetchOptions
extendable? This would allow users to add custom configuration keys to theiruseFetch
instances, which they could then handle in the dynamic getter version of the factory function (by applying different logic in request hooks, etc.) - related: Add aresetAfter
option touseFetch
to automatically reset the state after a specified time. #28792🚧 TODO
@nuxt/kit
utilities for creating factoriesuseFetch
utilities likegenerateOptionSegments
for better customizationuseAsyncData()