[REQ] [Javascript] Repeat or copy api request
Created by: ybelenko
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