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

feat: Add onSuccess and onError interceptors #340

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

Untel
Copy link

@Untel Untel commented Aug 14, 2022

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

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

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

  • Yes
  • No

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

The PR fulfills these requirements:

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

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

  • 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)

Copy link
Owner

@posva posva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is interesting but it only allows one callback (including globals). I think a lengthier solution would be nice but as a plugin so it doesn't increase mande base size. I will have to take a deeper look another time

@zhiyuanzmj
Copy link

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

@posva
Copy link
Owner

posva commented Jun 2, 2023

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

@posva posva mentioned this pull request Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants