Skip to content

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

@Lippiece

Description

@Lippiece

Environment

any

Reproduction

ofetch/src/types.ts

Lines 5 to 16 in dfbe3ca

export 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;
}

Describe the bug

As I noted in #380 (comment):

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions