Skip to content

feat: add NitroFetchRequest type and enhance $fetch's request type#209

Merged
pi0 merged 3 commits intonitrojs:mainfrom
didavid61202:enhance-$fetch-request-type
May 10, 2022
Merged

feat: add NitroFetchRequest type and enhance $fetch's request type#209
pi0 merged 3 commits intonitrojs:mainfrom
didavid61202:enhance-$fetch-request-type

Conversation

@didavid61202
Copy link
Contributor

@didavid61202 didavid61202 commented May 7, 2022

Enhance the $fetch request's type by inferring the server route's string literal (InternalApi interface). supporting auto-complete and showing type hints.

🔗 Linked issue

resolves nuxt/framework#208

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Add a new type NitroFetchRequest to Enhance $fetch's request type by inferring the generated server route's string literal (InternalApi interface).

This will provide better DX and prevent typos while $fetch by supporting auto-complete and showing type hints when entering the request. And should still be able to pass other valid request URLs like https://example.com/api or Request object like new Request('/api/test')

This will also provide Nuxt 3's useFetch and useLazyFetch to also have type hints on request parameters with little type changes. see issue nuxt/nuxt#13934 and PR nuxt/framework#4825

Usage scenario

// assume the project contains routes/api/test.ts
const result = await $fetch( /** IDE should show hint: '/api/test' */ )

const request: NitroFetchRequest = /** IDE should show hint: '/api/test' */
$fetch('/api/test') // ok
$fetch('https://example.com/api') // ok
$fetch(new Request('api/test')) // ok

// assume no routes/other.ts
$fetch('other') // type error: Argument of type '"other"' is not assignable to parameter of type 'FetchRequestUrl | Ref<FetchRequestUrl> | (() => FetchRequestUrl)'.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Enhance the $fetch request's type by inferring server route's string literal (InternalApi interface). supporting auto-complete and showing type hints.
Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love it! ❤️

@pi0
Copy link
Member

pi0 commented May 10, 2022

Thanks @didavid61202 @danielroe 💚

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants