Skip to content

Fix NitroFetchRequest type to support string type #225

Closed

Description

@didavid61202

Environment

  • Node version: 16.13.0
  • Nitro version: 0.4.3

Reproduction

git clone https://github.com/unjs/nitro.git
cd nitro
pnpm install
pnpm dev
// after .nitro folder is created, go to any playground's route (.ts) and add following code
const someDynamicUrl: string = 'https://test.com'
$fetch(someDynamicUrl) // Error: Argument of type 'string' is not assignable to parameter of type 'NitroFetchRequest'

Describe the bug

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

  • updated type NitroFetchRequest

Corresponding PR: #226

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