keycloak/keycloak-nodejs-admin-client

How to abort a request?

netevolution opened this issue · 4 comments

Hello,

I use your library in React. I would need to stop an ongoing request before disconnecting the component. How can I achieve this?

Ideally I would need to use something like here:
https://developers.google.com/web/updates/2017/09/abortable-fetch

@netevolution we use axios as the transport layer it does have something for aborting requests we should expose this in the API

@edewit I also use Axios in my projects. Yes, you are right that there is such a possibility.
So can I expect this option to be available in your library?

Thank you for answer.

You can already do this by adding axios as a dependency to your project and when you create the admin client:

import axios from "axios";
//...
const source = axios.CancelToken.source();
const kcAdminClient = new KcAdminClient({ requestConfig: { cancelToken: source.token } });

// then when you want to cancel the requests
source.cancel('Operation canceled by the user.')

or if you have a shared KcAdminClient somewhere use the setConfig:

useEffect(() => {
  const source = axios.CancelToken.source();
  adminClient.setConfig({ requestConfig: { cancelToken: source.token } });

  try {
     const result = await adminClient.clients.find();
     setState(result);
   } catch(err) {
      if (!axios.isCancel(err)) {
        // handle error
      }
    }

  return () => {
    source.cancel();
  };
}