-
Notifications
You must be signed in to change notification settings - Fork 158
FetchOptions don't use the first parameter that is supposed to indicate response data #559
Copy link
Copy link
Labels
bugSomething isn't workingSomething isn't working
Description
Environment
any
Reproduction
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working