Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 7ee8e68

Browse files
committed
add retry interceptor impl
1 parent 222bfe0 commit 7ee8e68

File tree

7 files changed

+71
-48
lines changed

7 files changed

+71
-48
lines changed

media-store/app-src/.eslintrc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
}
3737
],
3838
"react/jsx-props-no-spreading": "off", // props spreading,
39-
"no-console": "off"
39+
"no-console": "off",
40+
"consistent-return": "off",
41+
"prefer-destructuring": "off"
4042
}
4143
}

media-store/app-src/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"scripts": {
66
"start": "./node_modules/.bin/webpack-dev-server --config ./webpack/webpack-dev-server.js",
77
"watch": "./node_modules/.bin/webpack -w --config ./webpack/webpack.dev.js",
8-
"build": "./node_modules/.bin/webpack --config ./webpack/webpack.prod.js",
8+
"build:dev": "./node_modules/.bin/webpack --config ./webpack/webpack.dev.js",
9+
"build:prod": "./node_modules/.bin/webpack --config ./webpack/webpack.prod.js",
910
"lint": "./node_modules/.bin/eslint"
1011
},
1112
"dependencies": {

media-store/app-src/src/api/axiosInstance.js

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ import axios from 'axios';
22
import { getUserFromLS, getLocaleFromLS } from '../util/localStorageService';
33
import { emitter } from '../util/EventEmitter';
44

5+
const TIMEOUT = 2000;
6+
const RETRY_COUNT = 1;
7+
58
/**
69
* This is axios instance
710
*/
811
const axiosInstance = axios.create({
912
baseURL: process.env.SERVICE_URL,
10-
timeout: 2000,
13+
timeout: TIMEOUT,
14+
retryDelay: TIMEOUT,
15+
retry: RETRY_COUNT,
1116
});
1217

1318
/**
@@ -41,15 +46,51 @@ function changeLocaleDefaults(locale) {
4146
}
4247

4348
/**
44-
* Initializing initial data
49+
* Init axios defaults
4550
*/
4651
const user = getUserFromLS();
4752
const locale = getLocaleFromLS();
4853
changeUserDefaults(user);
4954
changeLocaleDefaults(locale);
5055

5156
/**
52-
* Error interceptor for refresh tokens mechanism
57+
* Retry request if response time is too long
58+
* See link below
59+
* {@link https://github.com/axios/axios/issues/164#issuecomment-327837467 GitHub}
60+
* @param {*} err response error object
61+
*/
62+
function axiosRetryInterceptor(err) {
63+
const config = err.config;
64+
// If config does not exist or the retry option is not set, reject
65+
if (config && config.retry) {
66+
// Set the variable for keeping track of the retry count
67+
config.retryCount = config.retryCount || 0;
68+
69+
// Check if we've maxed out the total number of retries
70+
if (config.retryCount >= config.retry) {
71+
// Reject with the error
72+
return Promise.reject(err);
73+
}
74+
75+
// Increase the retry count
76+
config.retryCount += 1;
77+
78+
// Create new promise to handle exponential backoff
79+
const backoff = new Promise((resolve) => {
80+
setTimeout(() => {
81+
resolve();
82+
}, config.retryDelay || 1);
83+
});
84+
85+
// Return the promise in which recalls axios to retry the request
86+
return backoff.then(() => {
87+
return axios(config);
88+
});
89+
}
90+
}
91+
92+
/**
93+
* Things below needed for refresh tokens mechanism implementation
5394
*/
5495
let isRefreshing = false;
5596
let subscribers = [];
@@ -62,7 +103,14 @@ const refreshTokens = (refreshToken) => {
62103
}
63104
);
64105
};
65-
axiosInstance.interceptors.response.use(null, (error) => {
106+
107+
/**
108+
* Refresh tokens interceptor
109+
* See link below
110+
* {@link https://gist.github.com/mkjiau/650013a99c341c9f23ca00ccb213db1c#gistcomment-3536511 GitHub}
111+
* @param {*} error error response object
112+
*/
113+
function axiosRefreshTokensInterceptor(error) {
66114
const originalRequest = error.config;
67115
const user = getUserFromLS();
68116

@@ -109,8 +157,12 @@ axiosInstance.interceptors.response.use(null, (error) => {
109157
});
110158
});
111159
}
160+
}
112161

113-
return Promise.reject(error);
162+
axiosInstance.interceptors.response.use(null, (error) => {
163+
return (
164+
axiosRefreshTokensInterceptor(error) || axiosRetryInterceptor(error) || Promise.reject(error)
165+
);
114166
});
115167

116168
export { axiosInstance, changeLocaleDefaults, changeUserDefaults };

media-store/app-src/src/components/Header.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Menu, Badge, Spin } from 'antd';
2+
import { Menu, Badge, Spin, message } from 'antd';
33
import { isEmpty } from 'lodash';
44
import {
55
CreditCardOutlined,
@@ -13,7 +13,7 @@ import { setLocaleToLS } from '../util/localStorageService';
1313
import { changeLocaleDefaults } from '../api/axiosInstance';
1414
import { emitter } from '../util/EventEmitter';
1515
import './Header.css';
16-
import { requireEmployee, requireCustomer } from '../util/constants';
16+
import { requireEmployee, requireCustomer, MESSAGE_TIMEOUT } from '../util/constants';
1717

1818
const { SubMenu } = Menu;
1919

@@ -24,7 +24,7 @@ const RELOAD_LOCATION_NUMBER = 0;
2424
const Header = () => {
2525
const history = useHistory();
2626
const location = useLocation();
27-
const { user, invoicedItems, setInvoicedItems, locale, setLocale, loading } = useAppState();
27+
const { user, invoicedItems, locale, setLocale, loading } = useAppState();
2828
const currentKey = [keys.find((key) => key === location.pathname)];
2929
const haveInvoicedItems = !isEmpty(invoicedItems);
3030
const invoicedItemsLength = invoicedItems.length;
@@ -45,7 +45,11 @@ const Header = () => {
4545

4646
const onUserLogout = () => {
4747
emitter.emit('UPDATE_USER', undefined);
48-
history.go(0);
48+
message.warn(
49+
'Now you are not authenticated. Log in to use full functionality',
50+
MESSAGE_TIMEOUT
51+
);
52+
history.push('/');
4953
};
5054

5155
return (

media-store/app/.gitignore

Lines changed: 0 additions & 23 deletions
This file was deleted.

media-store/app/.vscode/launch.json

Lines changed: 0 additions & 13 deletions
This file was deleted.

media-store/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"private": true,
1111
"dependencies": {
1212
"@sap/cds": "^4.2.8",
13-
"@sap/hana-client": "^2.6.61",
1413
"bcryptjs": "^2.4.3",
1514
"express": "^4",
15+
"hdb": "^0.18.2",
1616
"jsonwebtoken": "^8.5.1",
1717
"moment": "^2.29.1",
1818
"passport": "^0.4.1"

0 commit comments

Comments
 (0)