Skip to content

enhance $fetch's request type infer from generated InternalApi interface #208

Closed

Description

@didavid61202

✨ Enhancement proposal

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

  • Implement type NitroFetchRequest
  • update type $Fetch

Corresponding PR: nuxt/nuxt#10920

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions