Environment
any
Reproduction
|
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
Environment
any
Reproduction
ofetch/src/types.ts
Lines 5 to 16 in dfbe3ca
Describe the bug
As I noted in #380 (comment):
As far as I can see, the type parameter doesn't apply to
FetchContextfor both instances and the mainofetchfunction.Here, I specify
Data, but it doesn't apply toFetchContext, thereforeFetchResponsealways hasanyas a parameter:`ofetch.create()`
`ofetch`
This is because the parameter isn't even used for these contexts:
ofetch's `index.d.mts`
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
useAxiosfrom VueUse.Logs