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

Feature Request: Ability to abort tagged operations #2349

Closed
SeanLMcCullough opened this issue Nov 23, 2021 · 1 comment
Closed

Feature Request: Ability to abort tagged operations #2349

SeanLMcCullough opened this issue Nov 23, 2021 · 1 comment

Comments

@SeanLMcCullough
Copy link

SeanLMcCullough commented Nov 23, 2021

Unsure if this is a closer fit for a bug or feature, as the direct SwaggerClient.http.http() method is documented to support AbortController signals.

Content & configuration

Swagger/OpenAPI definition:
https://petstore3.swagger.io/api/v3/openapi.json

Swagger-Client usage:

<html>
 <head>
   <script src="https://unpkg.com/swagger-client"></script>
   <script>
       const controller = new AbortController();
       const { signal } = controller;
       const timeout = setTimeout(() => {
         controller.abort();
       }, 1);

       (async () => {
         try {
           const client = await SwaggerClient({ url: 'https://petstore3.swagger.io/api/v3/openapi.json' })
           await client.apis.pet.getPetById({ petId: 1 }, { signal });
         } catch (error) {
           if (error.name === 'AbortError') {
             console.info('request was aborted');
             return;
           }
         } finally {
           clearTimeout(timeout);
         }
         console.error('request was NOT aborted');
       })();
   </script>
 </head>
 <body>
   check console in browser's dev. tools
 </body>
</html>

Is your feature request related to a problem?

I would like the ability to cancel requests made by the client.apis.{tag}.{operationId} API. This feature is present with SwaggerClient.http() but does not work with tagged operations. This would allow me as a to ensure that race conditions do not occur, by using a native browser feature.

Describe the solution you'd like

I would like to be able to supply an AbortController signal (and ideally any other init parameters to fetch) such that I can cancel requests.

Describe alternatives you've considered

A workaround for this problem could be achieved by using request/response interceptors or more complex flow control around the usage of the client - but I have not explored these in depth.

Additional context

AbortController is documented to be usable with SwaggerClient.http() and works as documented. This same parameter does not work with tagged API operations, possibly because buildRequest discards aditional init params.

@char0n char0n self-assigned this Dec 21, 2021
char0n added a commit that referenced this issue Dec 21, 2021
- support was added to HTTP Client for OAS operations
- support was added to Tags Interface

Older versions of swagger-client can use request
interceptors to inject abort signal into requests.

Refs #2349
@char0n
Copy link
Member

char0n commented Dec 21, 2021

Hi @SeanLMcCullough,

I've issued a PR #2380 which adds support for abort signals in HTTP Client for OAS operations as well as to Tags Interface. The feature will be release in couple of minutes as [email protected].

For older versions of swagger-client, there is workaround in using requestInterceptor to intercept the Request before it's made and inject he abort signal into the request.

const SwaggerClient = require('swagger-client');
const AbortController = require('abort-controller');

const controller = new AbortController();
const { signal } = controller;


const interfaceLevelRequestInterceptor = (req) => {
  req.signal = signal;
  return req;
};

new SwaggerClient({
  url: 'http://petstore.swagger.io/v2/swagger.json',
  requestInterceptor: topLevelRequestInterceptor,
}).then((client) =>
  client.apis.user.getUserByName(
    { username: 'username' },
    {
      requestInterceptor: interfaceLevelRequestInterceptor,
    }
  )
);

controller.abort();

char0n added a commit that referenced this issue Dec 21, 2021
- support was added to HTTP Client for OAS operations
- support was added to Tags Interface

Older versions of swagger-client can use request
interceptors to inject abort signal into requests.

Refs #2349
@char0n char0n closed this as completed Dec 21, 2021
swagger-bot pushed a commit that referenced this issue Dec 21, 2021
# [3.18.0](v3.17.0...v3.18.0) (2021-12-21)

### Features

* add support aborting requests in tags interface ([7b6bdc2](7b6bdc2)), closes [#2349](#2349)
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

2 participants