nuxt icon indicating copy to clipboard operation
nuxt copied to clipboard

fix(nuxt): update asyncDataOptions and useFetchOptions type

Open xjccc opened this issue 1 year ago • 6 comments

🔗 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, useFetchCustom and useAsyncFetchCustom, for enhanced flexibility in data fetching.
  • Improvements

    • Enhanced flexibility in handling default values for asynchronous data fetching and fetching options.
    • Simplified error handling in useAsyncData and useFetch functions, allowing for broader error type management.
  • Documentation

    • Comprehensive updates to guides and examples related to custom fetchers and API handling in Nuxt 3.

xjccc avatar Nov 27 '24 10:11 xjccc

Review PR in StackBlitz Codeflow 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 $fetch instance, 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 the useAPI function 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!
With useAPI now 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?

❤️ Share
🪧 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 @coderabbitai in 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 @coderabbitai in 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 pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file 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.

coderabbitai[bot] avatar Nov 27 '24 10:11 coderabbitai[bot]

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. 😅

xjccc avatar Nov 27 '24 13:11 xjccc

When use custom fetch like docs

Image

Its cause error, The type of the attribute "default" is incompatible.

I think its two questions.

  1. Options type could not be undefined
  2. Options default type is not equal to fetch options default type.

xjccc avatar Nov 28 '24 06:11 xjccc

Transform type incompatibility 😭. How could i transform this.

xjccc avatar Nov 29 '24 07:11 xjccc

I will add some custom fetch test at Monday.

xjccc avatar Nov 30 '24 04:11 xjccc

It's out of date. Close❤️

xjccc avatar Apr 30 '25 02:04 xjccc