orval icon indicating copy to clipboard operation
orval copied to clipboard

Customizing error type (TError) in SWR integration

Open movahedan opened this issue 3 years ago • 1 comments
trafficstars

I'm using orval with SWR, and it's working fine, but the error type is always unknown unless I provide them at the usage level each time I want to use an API call. It's a good idea to type the error somehow in the customInstance or in orval.config.js. Is it possible?

movahedan avatar Jun 10 '22 14:06 movahedan

Hey @movahedan, you can export an ErrorType type in the mutator which should do the job. You have an example here

anymaniax avatar Jun 11 '22 11:06 anymaniax

@anymaniax customInstance is only receiving one generic argument, which is the response. Is there any way to adapt customInstance to support two generic arguments and leave orval do the rest? Like:

export const getUsers = (options?: SecondParameter<typeof customInstance>) => {
  return customInstance<Users, UsersError>({ url: `/api/users`, method: 'get' }, options);
};

By having this, we would avoid casting all the derived created react query hooks (for example).

vitorcamachoo avatar Aug 22 '23 11:08 vitorcamachoo

Hey @movahedan, you can export an ErrorType type in the mutator which should do the job. You have an example here

Yes, that's exactly what I did previously in my project. Seems a bit tricky though. I agree with @vitorcamachoo, that would be even nicer.

movahedan avatar Aug 22 '23 12:08 movahedan

@movahedan

Hi, have all your questions about this issue been resolved? Is there anything I can do to help?

soartec-lab avatar Dec 30 '23 01:12 soartec-lab

@melloware

Hi, I will watch this issue, so could you assign it to me? Or, since it seems like the question has already been resolved, you may want to close it.

soartec-lab avatar Jan 01 '24 01:01 soartec-lab

I will close it for now as it seems like it was answered.

melloware avatar Jan 01 '24 01:01 melloware