OpenAPITools / OpenAPITools/openapi-generator

[REQ] [Javascript] Repeat or copy api request

Open
#2,890 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: JavaScript/Node.js Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

Current Javascript generator based on Superagent npm package to make Api calls. It provides option to retry http request when server doesn't respond. Ref to guide https://visionmedia.github.io/superagent/#retrying-requests

But real nightmare starts when you need to refresh access token every 15 minutes. Let's say server responds with 401 status when your access token expired.

import { UsersApi } from './api-sdk/src';

const api = new UsersApi();
api.getCurrentUser()
  .then((data) => {
    // succes, everything great
  })
  .catch((error) => {
    if (error.status === 401) {
      // obtain new success token and try again
      api.refreshToken(accessToken, refreshToken, scope)
        .then((data) => {
          // now we have valid access token and need to repeat request
          api.getCurrentUser()
            .then((data) => {
              // finally user data retrieved
            })
            .catch((error) => {
              // something completely wrong
            })
        })
        .catch((error) => {
          // maybe api server is down
        })
    }
  });

Describe the solution you'd like

I think that error instance in reject callback should contain request instance or method to call the same endpoint again.
Maybe

api.getUser()
  .then((data) => {

  })
  .catch((error) => {
    const { apiRequest } = error;
    apiRequest.reset();
    apiRequest.call();
  });

Describe alternatives you've considered

or another example, but I don't know how to write it clean with promises:

const originalRequest = api.getUser();
originalRequest.call();

// when you need to repeat the same call
const clonedRequest = api.clone(originalRequest);
clonedRequest.call();

In a perfect world this issue requires some kind of middleware, I think. Middleware intercepts every failed request ended with 401 response, refreshes token and makes the same call again. Developer can write that middleware when he got option to recreate original request within error callback.

cc @jfiala @achew22 @jaypea

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

Start by inspecting the JavaScript generator's Superagent-based request implementation and the Superagent retrying-requests guide linked in the issue. Define the request lifecycle and middleware behavior needed to recreate or repeat a failed request after token refresh; done means the design is specified and covered by relevant generator tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.