New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chain beforeFetch/afterFetch/onFetchError from createFetch and useFetch #1310
Comments
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
The chainCallbacks function does not work well with async beforeFetch functions. While it does execute them with an await, chainCallbacks itself never returns a promise and as such is not awaited before the actual fetch happens. This bug cost me quite a while to find, unfortunately, and resulted in strange behavior. |
Indeed, @brease-colin seems right here. type ContextFunction<T = BeforeFetchContext | AfterFetchContext> = (ctx: T) =>
ReturnType<Exclude<UseFetchOptions[T extends BeforeFetchContext ? 'beforeFetch' : 'afterFetch'], undefined>>
const mergeContextHandlers = <T = BeforeFetchContext | AfterFetchContext> (...handlers: (ContextFunction<T> | undefined)[]) => {
return async (ctx: T) => {
for (const handler of handlers) {
if (handler !== undefined) {
ctx = { ...ctx, ...(await handler(ctx)) }
}
}
return ctx
}
} And it doesn't use a |
Fixed by #2231 |
Clear and concise description of the problem
Currently, a handler defined in
createFetch
can be overwritten inuseFetch
.For example, we can define a
createFetch
with a globalbeforeFetch
option to set a bearer token, and in theuseFetch
call, add abeforeFetch
option to set some other needed headers.Suggested solution
useFetchFactory should be able to chain beforeFetch/afterFetch/onFetchError instead of overwriting them.
Alternative
No response
Additional context
Manually chain for now:
Validations
The text was updated successfully, but these errors were encountered: