mande icon indicating copy to clipboard operation
mande copied to clipboard

feat: Add onSuccess and onError interceptors

Open Untel opened this issue 3 years ago • 2 comments

What kind of change does this PR introduce? (check at least one)

  • [ ] Bugfix
  • [x] Feature
  • [ ] Code style update
  • [ ] Refactor
  • [ ] Build-related changes
  • [ ] Other, please describe:

Does this PR introduce a breaking change? (check one)

  • [ ] Yes
  • [x] No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

  • [x] When resolving a specific issue, it's referenced in the PR's title (e.g. fix #xxx[,#xxx], where "xxx" is the issue number)
  • [x] All tests are passing
  • [x] New/updated tests are included

If adding a new feature, the PR's description includes:

  • [x] A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:

Hi 👋

I strongly needed a way to have common interceptors (like axios) accross my app to catch generic errors, and share logic across MandeInstances . I could overload the mande prototype or create a wrapper, but it seems ugly.

I did it really simple, only added onSuccess and onError callbacks, for response only. I could work on something more complex like axios interceptors (multiple request + response interceptors) if you see any interest in it. It will also come with the need to type the newly returned type. MandeInstance<InterceptedResponseType = { get: () => T, ... }> But i don't really know if it will be possible (axios don't allow it)

Untel avatar Aug 14 '22 06:08 Untel

How about supporting onRequest, onRequestError, onResponse, onResponseError events? inspiration by unjs/ofetch

zhiyuanzmj avatar Feb 27 '23 15:02 zhiyuanzmj

Yeah, something similar could be nice. I added an empty interceptors file to src if anybody wants to give it a try

posva avatar Jun 02 '23 09:06 posva