`FetchOptions` don't use the first parameter that is supposed to indicate response data
Nobody has claimed this yet.
- 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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