Skip to content
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

Open
jsardev opened this issue Mar 24, 2024 · 1 comment
Open

How to extract catcher functions with TypeScript #226

jsardev opened this issue Mar 24, 2024 · 1 comment
Labels

Comments

@jsardev
Copy link

jsardev commented Mar 24, 2024

I've been trying to extract some of my catcher functions to reuse them in multiple places but can't make TypeScript satisfied.

import wretch, { Wretch, WretchError, WretchErrorCallback } from 'wretch';

// Here I want `error` and `req` be properly typed
const renewCredentials: ClientErrorCallback = async (error, req) => {
  // 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: WretchError) => {
      throw err;
    })
    .json();
};

const client = wretch('/api/v1').resolve((chain) =>
  chain.unauthorized(renewCredentials).forbidden(renewCredentials)
);

type InferClientErrorCallback<Client> =
  Client extends Wretch<infer T, infer C, infer R>
    ? WretchErrorCallback<T, C, R>
    : never;

type ClientErrorCallback = InferClientErrorCallback<typeof client>

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:

CleanShot 2024-03-24 at 10 09 53

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

@elbywan
Copy link
Owner

elbywan commented May 11, 2024

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
Labels
Projects
None yet
Development

No branches or pull requests

2 participants