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

Prototype for v19 support #82

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions .babelrc
@@ -1,3 +1,5 @@
{
"presets": ["es2015"]
}
"presets": [
"@babel/preset-env"
]
}
2 changes: 1 addition & 1 deletion .eslintrc
Expand Up @@ -9,7 +9,7 @@
"trailingComma": "none",
"printWidth": 100,
"semi": true,
"parser": "babylon"
"parser": "babel"
}
],
"jasmine/no-spec-dupes": ["warn", "branch"],
Expand Down
11 changes: 0 additions & 11 deletions README.md
Expand Up @@ -42,17 +42,6 @@ client.get('/test') // The first request fails and the second returns 'ok'
.then(result => {
result.data; // 'ok'
});

// Allows request-specific configuration
client
.get('/test', {
'axios-retry': {
retries: 0
}
})
.catch(error => { // The first request fails
error !== undefined
});
```

**Note:** Unless `shouldResetTimeout` is set, the plugin interprets the request timeout as a global value, so it is not used for each retry but for the whole request lifecycle.
Expand Down
16 changes: 8 additions & 8 deletions es/index.js
@@ -1,6 +1,6 @@
import isRetryAllowed from 'is-retry-allowed';

const namespace = 'axios-retry';
const namespace = Symbol('axios-retry');

/**
* @param {Error} error
Expand Down Expand Up @@ -82,13 +82,13 @@ export function exponentialDelay(retryNumber = 0) {

/**
* Initializes and returns the retry state for the given request/config
* @param {AxiosRequestConfig} config
* @param {AxiosRequestConfig} axios
* @return {Object}
*/
function getCurrentState(config) {
const currentState = config[namespace] || {};
function getCurrentState(axios) {
const currentState = axios[namespace] || {};
currentState.retryCount = currentState.retryCount || 0;
config[namespace] = currentState;
axios[namespace] = currentState;
return currentState;
}

Expand Down Expand Up @@ -172,13 +172,13 @@ function fixConfig(axios, config) {
*/
export default function axiosRetry(axios, defaultOptions) {
axios.interceptors.request.use(config => {
const currentState = getCurrentState(config);
const currentState = getCurrentState(axios);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this not use a single state per whole axios instance? Will that not make requesting separate resources with the same axios instance impossible, because their retry states will be the same instead of independent?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. But it does not cause too much trouble I guess... I personally tend to create as many custom Axios instances as microservices (apis) my app talks to, e.g.:

const todosApiClient = axios.create({ baseURL: 'https://todos-api.com' });
const todosApiClientWithRetry = axiosRetry(todosApiClient, { ... });

const usersApiClient = axios.create({ baseURL: 'https://users-api.com' });
const usersApiWithLogger = axiosLogger(usersApiClient, { ... });

// etc.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While that is true, and it is nice that it works for you, it is certainly not standard and very much unexpected behavior for anyone who reuses axios instance for different MS. Or for example has axios instance for a single domain but the domain is API gateway and so different resources can have vastly different availability.

Right now this change would make safe axios instance reuse impossible.

Keeping retry state in config.adapter as seen in https://github.com/softonic/axios-retry/pull/76/files would not work?

Copy link
Contributor

@mawrkus mawrkus Jun 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API gateway is an interesting use case, we should indeed find a way to support it.
On one side, I'm not super keen to follow the config.adapter approach as we shouldn't modify what we don't own. On the other side, I don't see how we could pass the axios-retry state from one retry to the next in a reliable way...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...unless we decorate Axios request method in order to store the axios-retry state in a local "cache", the cache could be a WeakMap and the key, the config passed to the request method.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WeakMap idea sounds pretty good to me, compatibility with ie < 11 aside. Either with the request object itself as key, as you say, or on some id that is generated in an interceptor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One solution could come from Axios itself axios/axios#2207

currentState.lastRequestTime = Date.now();
return config;
});

axios.interceptors.response.use(null, error => {
const config = error.config;
const { config } = error;

// If we have no information to retry the request
if (!config) {
Expand All @@ -192,7 +192,7 @@ export default function axiosRetry(axios, defaultOptions) {
shouldResetTimeout = false
} = getRequestOptions(config, defaultOptions);

const currentState = getCurrentState(config);
const currentState = getCurrentState(axios);

const shouldRetry = retryCondition(error) && currentState.retryCount < retries;

Expand Down