OpenAPITools / OpenAPITools/openapi-generator

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

Open
#7,896 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The request targets the typescript-axios template, specifically the {{classname}}Fp function. Start by reading that function and the linked Axios cancellation context, then determine how a configurable, non-breaking design would fit. Done means the generated request supports the requested promise and cancellation behavior with coverage for the chosen configuration.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.