fix(nuxt): update asyncDataOptions and useFetchOptions type
🔗 Linked issue
resolves https://github.com/nuxt/nuxt/issues/29970
📚 Description
Add DataT for AsyncDataOptions and UseFetchOptions
And update useAsyncData error type.
Summary by CodeRabbit
Release Notes
-
New Features
- Updated documentation on creating a custom fetcher for external APIs in Nuxt 3, including a new section on handling authorization and response errors.
- Introduced new composable functions,
useFetchCustomanduseAsyncFetchCustom, for enhanced flexibility in data fetching.
-
Improvements
- Enhanced flexibility in handling default values for asynchronous data fetching and fetching options.
- Simplified error handling in
useAsyncDataanduseFetchfunctions, allowing for broader error type management.
-
Documentation
- Comprehensive updates to guides and examples related to custom fetchers and API handling in Nuxt 3.
Run & review this pull request in StackBlitz Codeflow.
Walkthrough
The pull request introduces updates to documentation and type definitions related to custom data fetching in Nuxt 3. Key changes include a new guide on creating a custom $fetch function, the addition of a useAPI composable for simplified API calls, and modifications to type parameters in the asyncData and fetch composables to enhance flexibility in default value handling. These adjustments aim to improve the usability and type safety of the data-fetching mechanisms within the framework.
Changes
| File Path | Change Summary |
|---|---|
docs/2.guide/4.recipes/3.custom-usefetch.md |
Updated to include a guide on creating a custom $fetch in Nuxt 3, added a new useAPI composable, and discussed error handling. |
packages/nuxt/src/app/composables/asyncData.ts |
Modified AsyncDataOptions interface to allow DefaultT to be DataT and updated useAsyncData function overloads accordingly. |
packages/nuxt/src/app/composables/fetch.ts |
Updated UseFetchOptions interface to allow DefaultT to be DataT and adjusted useFetch function overloads to reflect this change. |
test/fixtures/basic-types/types.ts |
Updated error handling types in useAsyncData and useFetch, and added tests for custom error types. |
test/fixtures/basic-types/composables/useFetchCustom.ts |
Introduced useFetchCustom and useAsyncFetchCustom functions for custom data fetching with enhanced error handling. |
test/fixtures/basic-types/plugins/fetchCustom.ts |
Added a Nuxt plugin for a custom fetch instance using $fetch.create(). |
Assessment against linked issues
| Objective | Addressed | Explanation |
|---|---|---|
Custom useFetch has incorrect type for default option (#29970) |
✅ |
Possibly related PRs
- #29756: This PR updates the documentation for creating a custom
$fetchinstance, which aligns with the main PR's focus on enhancing guidance for a custom fetcher in Nuxt 3. - #30006: This PR updates the documentation for
useFetch, which is directly relevant to the changes made in the main PR regarding theuseAPIfunction that wraps around$fetch. - #30073: This PR enhances the documentation for data fetching, which is closely related to the main PR's updates on custom fetchers and error handling in the context of data fetching in Nuxt.
Suggested labels
enhancement, documentation, test
Suggested reviewers
- danielroe
🐇 In the land of code, we hop and play,
Custom fetchers brighten the day!
WithuseAPInow in sight,
Fetching data feels just right.
Errors handled with grace,
In Nuxt's warm embrace! 🌟
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
🪧 Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
I pushed a fix in commit <commit_id>, please review it.Generate unit testing code for this file.Open a follow-up GitHub issue for this discussion.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai generate unit testing code for this file.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.@coderabbitai read src/utils.ts and generate unit testing code.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.@coderabbitai help me debug CodeRabbit configuration file.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (Invoked using PR comments)
@coderabbitai pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile to the root of your repository. - Please see the configuration documentation for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation:
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.
Would you be able to add some regression type tests (including for custom error type) in
test/fixtures/basic-types/types.ts?
I will try later for that. Maybe more difficult for me. 😅
When use custom fetch like docs
Its cause error, The type of the attribute "default" is incompatible.
I think its two questions.
- Options
typecould not beundefined - Options
defaulttype is not equal to fetch optionsdefaulttype.
Transform type incompatibility 😭. How could i transform this.
I will add some custom fetch test at Monday.
It's out of date. Close❤️