Skip to content

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

Open
@mattiasnixell

Description

@mattiasnixell

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions