unjs / unjs/ofetch

`FetchOptions` don't use the first parameter that is supposed to indicate response data

Open
#559 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
5.4k
Forks
195
PR merge metrics
No merged PRs in 30d

Description

Environment

any

Reproduction

https://github.com/unjs/ofetch/blob/dfbe3ca4ef8a22fc023fca5a5ef530e525f5e523/src/types.ts#L5-L16

Describe the bug

As I noted in https://github.com/unjs/ofetch/issues/380#issuecomment-4173095023:

As far as I can see, the type parameter doesn't apply to FetchContext for both instances and the main ofetch function.

Here, I specify Data, but it doesn't apply to FetchContext, therefore FetchResponse always has any as a parameter:

`ofetch.create()`

  const instance = ofetch.create(defaultOptions)

  instance<Data>("", {
      // `context` is:
      // FetchContext<any, "json"> & {
      //     response: FetchResponse<any>;
      // }
    onResponse: context => {
      context.response // is `FetchResponse<any>`
    }
  })

`ofetch`

  ofetch<Data>("",{onResponse: context => {
      // `context` is:
      // FetchContext<any, "json"> & {
      //     response: FetchResponse<any>;
      // }
    context.response // is `FetchResponse<any>`
  }})

This is because the parameter isn't even used for these contexts:

ofetch's `index.d.mts`

interface $Fetch {
  <T = any, R extends ResponseType = "json">(request: FetchRequest, options?: FetchOptions<R>): Promise<MappedResponseType<R, T>>;
  raw<T = any, R extends ResponseType = "json">(request: FetchRequest, options?: FetchOptions<R>): Promise<FetchResponse<MappedResponseType<R, T>>>;
  native: Fetch;
  create(defaults: FetchOptions, globalOptions?: CreateFetchOptions): $Fetch;
}

Additional context

Looking forward to finally use ergonomic tools such as this. I've stumbled upon this when I tried to write an ofetch interpretation of useAxios from VueUse.

Logs

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the referenced definitions in src/types.ts and the generated index.d.mts declarations, then reproduce the two examples for ofetch.create() and ofetch(). Check how the generic Data parameter reaches FetchContext and FetchResponse. Done means the callback response types preserve Data for both call forms and the declarations type-check.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.