Skip to content

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

Merged
pi0 merged 1 commit intonitrojs:mainfrom
didavid61202:fix-fetch-request-type-for-string
May 11, 2022
Merged

fix(types): update NitroFetchRequest type to support string type#226
pi0 merged 1 commit intonitrojs:mainfrom
didavid61202:fix-fetch-request-type-for-string

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 nitrojs: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