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

Interceptor support? #19

Closed
ariefsn opened this issue Oct 20, 2021 · 6 comments
Closed

Interceptor support? #19

ariefsn opened this issue Oct 20, 2021 · 6 comments

Comments

@ariefsn
Copy link

ariefsn commented Oct 20, 2021

Hi,
is this have interceptors?

Thanks,

@psycongaroo
Copy link

up

it will be so useful, could use it instead of axios

@ushironoko
Copy link

In Nuxt3, it is no longer possible to pass arbitrary fetches to useFetch. This will be an obstacle to migration for users who used to use @nuxtjs/composition-api's useFetch in combination with axios.

It would be nice if something like the axios interceptor could be implemented in ohmyfetch as well.

@danielroe
Copy link
Member

@ushironoko The version that allows you to pass arbitrary fetches is called useAsyncData.

@pi0 pi0 closed this as completed in 1bf2dd9 Dec 21, 2021
@pi0
Copy link
Member

pi0 commented Dec 21, 2021

Just added a basic set of interceptors. Docs. ideas more than welcome to improve.

@ushironoko
Copy link

Looks like a very flexible API. Thanks!

@engenes
Copy link

engenes commented Aug 4, 2022

@pi0

Just added a basic set of interceptors. Docs . ideas more than welcome to improve.

How can I catch only specific error and return a success response?

I want to intercept "not found error".
I make this:

Call function in component

const fetchOnClick = async () => {
 const data = await $fetch('bad_url', {
   onResponseError(ctx) {
     if (ctx.response.status === 404) {
       console.warn('Not found');

       return {
         status: 'error',
         error: {
           message: 'not found',
         },
       };

       return  Promise.reject(ctx)
     }
   },
 });

 console.log(data);
};

After response I see Not found, but data does not appear in log
That is, with such an error, I still want to get the correct message and clear the error.
So that only data that does not pass the condition enters the error

The documentation shows how to log errors, but it doesn't say how to intercept

I would like to process only those errors that match the condition and skip the rest further as in axios.

Now I see that you are suggesting to handle all errors
await $fetch(...).catch((error) => error.data)

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

No branches or pull requests

6 participants