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

[REQ][typescript-axios] Wrap AxiosPromise in Cancellable Requests #7896

Open
mattiasnixell opened this issue Nov 6, 2020 · 4 comments
Open

Comments

@mattiasnixell
Copy link

mattiasnixell commented Nov 6, 2020

First of all, thank you for the amazing work. The OpenAPI generator saves days of work and prevents stupid mistakes!

Is your feature request related to a problem? Please describe.

I believe it's common to need request cancellation in a modern app that is of considerable size. Especially useful in SPAs where one probably wants to cancel all on-going requests when navigating to a new page or when doing repetitive actions that should only yield the latest API result.

Describe the solution you'd like

I am imagining something similar to aws-sdk-js which returns request objects with a promise and abort/cancel function.

const request = petsApiClient.listPets();
request.promise() // promise() returns AxiosPromise
  .then((result) => {
    // handle result
  })
  .catch((error) => {
    // handle cancels or errors
  });

...

request.cancel(); // makes AxiosPromise reject with Cancel error

This can be achieved with axios.CancelToken inside the {{classname}}Fp function. It would be a breaking change, so should probably need to be configurable?

Describe alternatives you've considered

It's already now possible to cancel requests utilizing the axios.CancelToken.

const source = axios.CancelToken.source();
const request = petsApiClient.listPets(undefined, { cancelToken: source.token });
request
  .then((result) => {
    // handle result
  })
  .catch((error) => {
    // handle cancels or errors here
  });

...

source.cancel();

However, this approach becomes quite funny if you have an operation with many optional arguments.

const source = axios.CancelToken.source();
const request = apiClient.operationWithManyArgs(
  undefined,
  undefined,
  undefined,
  undefined,
  undefined,
  { cancelToken: source.token },
);

My goal with this feature request is to make the use of generated API clients/SDKs as simple as possible for the end-users. The typescript-axios template is already awesome, and I hope this can bring it to the next level.

Additional context

@auto-labeler
Copy link

auto-labeler bot commented Nov 6, 2020

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@mattiasnixell
Copy link
Author

And yes, I do know that there's also the option of using the rxjs template with cancellable observables. However, neither I nor my colleagues have any professional knowledge of observables. Making that switch would be a significant investment, which business will never agree to... :)

@Lomkex1
Copy link

Lomkex1 commented Nov 9, 2020

How is this still not implemented?

@VincentVanclef
Copy link

Is there still no way to have cancellation support in typescript-axios?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants