Skip to content
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

fix(types): update NitroFetchRequest type to support string type #226

Merged
merged 1 commit into from
May 11, 2022

Conversation

didavid61202
Copy link
Contributor

@didavid61202 didavid61202 commented May 11, 2022

πŸ”— Linked issue

fix #225

❓ 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

As mention in PR comment, with the new NitroFetchRequest type for request (added in #209), user currently can't do:

 const someDynamicUrl: string = 'https://test.com'
$fetch(someDynamicUrl) // Error: Argument of type 'string' is not assignable to parameter of type 'NitroFetchRequest'

Proposed solution

This can be fix by adding string & {} to NitroFetchRequest in types.fetch.ts as:

type NitroFetchRequest = keyof InternalApi | Exclude<FetchRequest, string> | string & {}

the only small downside is losing type hint when quotes are already typed,

$fetch( /** cursor here */ ) // shows routes hints
$fetch('/** cursor here */') // won't show hints, might have to tap into typescript hints using extendtion to support this?

we could also create a helper type and let users import it if they need (another RP)

// types/fetch.ts
export type NitroApiRoutes = keyof InternalApi | CovertRouteToMatcher<keyof InternalApi> & {}

// other.ts
import { NitroApiRoutes } from 'nitropack'
const url: NitroApiRoutes = "/user/:userId/post/:postId" // support best type hints (/w fussy search)

πŸ“ Checklist

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

@pi0 pi0 changed the title fix: update NitroFetchRequest type to support string type fix(types): update NitroFetchRequest type to support string type May 11, 2022
@pi0 pi0 merged commit 3542b61 into unjs:main May 11, 2022
@pi0
Copy link
Member

pi0 commented May 11, 2022

Thanks!

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.

Fix NitroFetchRequest type to support string type
2 participants