[英]Override retry-axios default config
Here is the code being used to retry failed attempts at a token verification endpoint:以下是用于在令牌验证端点重试失败尝试的代码:
const axios = require('axios')
const rax = require('retry-axios')
try {
const axiosInstance = axios.create();
const myRaxConfig = {
instance: axiosInstance,
retry: 4,
statusCodesToRetry: [[100, 199], [401, 429, 429], [500, 501, 502, 504, 599]],
httpMethodsToRetry: ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT', 'POST'],
retryDelay: 100,
noResponseRetries: 4,
onRetryAttempt: (err) => {
const cfg = rax.getConfig(err)
console.log(`Retry attempt #${cfg.currentRetryAttempt}`);
}
}
axiosInstance.defaults.raxConfig = myRaxConfig
const interceptorId = rax.attach(axiosInstance);
//const interceptorId = rax.attach()
console.log("DOING THIS ONE +++++++++~~~~~~~~~~~~~~~~~~~~~~~")
const tokenResult = await axiosInstance.post(`${process.env.URL}/api/token/verify`,
{
token
},
)
}
catch (err) {
logger.error('encounter error in verify: ', err)
//logger.error('encounter error in verify: ')
return false
}
However, the error logs show that the default config is being used - and since this is a POST request, no retries were attempted:但是,错误日志显示正在使用默认配置 - 由于这是一个 POST 请求,因此未尝试重试:
Error output:错误 output:
...
"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8","User-Agent":"axios/0.19.0","Content-Length":252},"transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,
"raxConfig":{"currentRetryAttempt":0,"retry":3,"retryDelay":100,"httpMethodsToRetry":["GET","HEAD","PUT","OPTIONS","DELETE"],"noResponseRetries":2,"statusCodesToRetry":[[100,199],[429,429],[500,599]]}}}
The axios request is using the default retry-axios config. axios 请求使用默认的 retry-axios 配置。
How can the default raxConfig be overwritten?如何覆盖默认的 raxConfig?
I went crazy to understand why my simply code like yours wasn't working.我疯狂地理解为什么像你这样的简单代码不起作用。
Well, I think there is a bug in the library.好吧,我认为库中存在错误。 This code which you can find in the npm library usage doesn't work properly:您可以在npm 库用法中找到此代码无法正常工作:
const myAxiosInstance = axios.create();
myAxiosInstance.defaults.raxConfig = {
instance: myAxiosInstance
};
const interceptorId = rax.attach(myAxiosInstance);
const res = await myAxiosInstance.get('https://test.local');
You have to add raxConfig object to your api call to make things work.您必须将 raxConfig object 添加到您的 api 调用中才能正常工作。 Something like this:像这样的东西:
const raxConfig = myAxiosInstance.defaults.raxConfig;
const res = await myAxiosInstance.get('https://test.local', { raxConfig } );
Don't forget to add "instance" property in it otherwise it doesn't work.不要忘记在其中添加“实例”属性,否则它不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.