OpenAPITools / OpenAPITools/openapi-generator
[REQ] [Javascript] Repeat or copy api request
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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