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
How to extract catcher functions with TypeScript #226
Labels
Comments
Hey @jsardev, sorry for the late answer. The following code seems to work on my end: import wretch, { Wretch, WretchError, WretchErrorCallback } from './dist';
async function renewCredentials<T, C, R extends undefined>(error: WretchError, req: T & Wretch<T, C, R>) {
// Renew credentials
const token = await wretch('/renewtoken').get().text();
// storeToken(token);
// Replay the original request with new credentials
return req
.auth(token)
.get()
.unauthorized(err => {
throw err;
})
.json();
};
const client = wretch('/api/v1').resolve((chain) =>
chain.unauthorized(renewCredentials).forbidden(renewCredentials)
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've been trying to extract some of my catcher functions to reuse them in multiple places but can't make TypeScript satisfied.
I tried to create an infer type to get the right
WretchErrorCallback
to be used in all of the catcher functions, but without luck. I always end up with this error:I think you'll get the idea from the example code above. I'd very appreciate any help 🙏
The code above in a live environment: https://stackblitz.com/edit/vitejs-vite-effanv?file=src%2Fmain.ts
The text was updated successfully, but these errors were encountered: