suhaotian / xior

A lite request lib based on fetch with plugin support and similar API to axios.
https://www.npmjs.com/package/xior
MIT License
114 stars 1 forks source link

Auth Refresh #11

Closed mgoodfellow closed 2 months ago

mgoodfellow commented 2 months ago

Hi,

First off, thank you for doing this brilliant port - we were stuck migrating to NextJS app router (SSR bits) because of our dependency on Axios. We generate our strongly typed axios clients via swagger-typescript-api. Although this could generate fetch clients, they would require substantial work to function with our existing stack which is axios-centric. We have converted generator that to generate xior clients via templating, so the last bit for us was making sure all our existing token refresh/login/logout stuff still worked.

Anyway, I digress, but long story short, your method for refreshing auth tokens we found didn't suit our use case, even with throttling and deduping, we would get a lot of refresh calls, and in addition, the failed requests (although they would retry) would show loads of errors while failing before retrying.

There is this brilliant library - https://github.com/Flyrell/axios-auth-refresh - which handles pausing the request pipeline, queuing them up, refreshing the token, and then letting the queue run afterwards which solves these problems.

I did a messy monkey patch to convert it to work with xior as a proof of concept, and so far it has slotted right in as an alternative and works (so far) exactly the way we found the axios version of the library to work for us.

Anyway, I just wanted to give you a heads up: https://github.com/Audiu/xior-auth-refresh

Once again, thank you so much for this library!

suhaotian commented 2 months ago

Cool!

Thanks create this plugin, and already add this plugin to README so more people can use the plugin !! 👍

And thanks for the feedback! 🙌


Maybe can add this to index.ts to fix TypeScript hint:

declare module 'xior' {
    interface XiorRequestConfig {
        skipAuthRefresh?: boolean;
    }
}
image
mgoodfellow commented 2 months ago

Thanks for the spot - I have tried to add it in a few different ways, but sadly can't seem to get it to export the module declaration on build/pack.

I'm by no means a typescript expert and I have adapted the existing project which uses webpack to build the output rather than just compiling via tsc/babel etc. which is adding to my confusion ;)

As a stop gap I have added a section in the readme to advise the user to add this declare module snippet to their project in their type definitions which will resolve the problem for them locally: https://github.com/Audiu/xior-auth-refresh?tab=readme-ov-file#typescript-usage

Thanks for the feedback!

suhaotian commented 2 months ago

@mgoodfellow Thanks, I create a PR to enhance types https://github.com/Audiu/xior-auth-refresh/pull/2