I have an Typescript axios interceptor function file. This is mostly the generic Jhipster code with modification to support polling based on 202 HTTP status code in the response.
import axios, { type AxiosError } from 'axios';
const TIMEOUT = 1 * 60 * 1000;
axios.defaults.timeout = TIMEOUT;
axios.defaults.baseURL = SERVER_API_URL;
const setupAxiosInterceptors = onUnauthenticated => {
const onRequestSuccess = config => {
return config;
};
const onResponseSuccess = async response => {
if (response.status === 202) {
const pollingResponse = await startPolling(response.headers.location);
if (pollingResponse.data.status === 'ACCEPTED') {
throw new Error('Operation failed! ' + response.headers.location);
} else {
return pollingResponse;
}
}
return response;
};
const onResponseError = (err: AxiosError) => {
const status = err.status || (err.response ? err.response.status : 0);
if (status === 401) {
onUnauthenticated();
}
return Promise.reject(err);
};
axios.interceptors.request.use(onRequestSuccess);
axios.interceptors.response.use(onResponseSuccess, onResponseError);
};
async function startPolling(url) {
const pollingInterval = 100; // interval between polls in milliseconds
const maxPollingTime = 10000; // maximum polling time in milliseconds (15 seconds)
let result = null;
let stopPolling = false;
const startTime = Date.now();
while (!stopPolling) {
try {
const currentTime = Date.now();
const elapsedTime = currentTime - startTime;
if (elapsedTime >= maxPollingTime) {
console.warn('Polling stopped due to time limit exceeded');
break;
}
const response = await axios.get(url);
const status = response.data.entityStatus;
/* eslint-disable no-console */
console.log('Response status: ', status);
if (status === OK) {
stopPolling = true;
result = response;
/* eslint-disable no-console */
console.log('Pending job finished', status);
} else {
await new Promise(resolve => setTimeout(resolve, pollingInterval));
}
} catch (error) {
console.error(error);
}
}
return result;
}
export default setupAxiosInterceptors;
After adding polling functionality, standard JHipster tests related to axios started to fail.
import axios from 'axios';
import sinon from 'sinon';
import setupAxiosInterceptors from './axios-interceptor';
describe('Axios Interceptor', () => {
describe('setupAxiosInterceptors', () => {
const client = axios;
const onUnauthenticated = sinon.spy();
setupAxiosInterceptors(onUnauthenticated);
it('onRequestSuccess is called on fulfilled request', () => {
expect((client.interceptors.request as any).handlers[0].fulfilled({ data: 'foo', url: '/test' })).toMatchObject({
data: 'foo',
});
});
});
});
with an error:
SyntaxError: srcmainwebappappconfigaxios-interceptor.spec.ts: Unexpected token, expected "," (13:42)
11 |
12 | it('onRequestSuccess is called on fulfilled request', () => {
> 13 | expect((client.interceptors.request as any).handlers[0].fulfilled({ data: 'foo', url: '/test' })).toMatchObject({
| ^
14 | data: 'foo',
15 | });
16 | });
I`m mostly doing backend development and cannot figure out what I need to change to make it work.