Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export {
UserUpdate,
UserUpdatePassword,
} from './users/types';
export { testAxiosObject } from './utils/axiosSetup';
export { testAxiosObject, cancelAllRequests } from './utils/axiosSetup';
export { Pages } from './utils/pages/pages';
export { DeltaSyncParams } from './utils/params/deltaSyncParams';
export { RequestParams, RequestParamsBuilder } from './utils/params/requestParams';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/axiosSetup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('axiosSetup', () => {
mock.onGet(readPath).timeout();
await api.tasks.read();
} catch (error) {
expect(error).toMatchObject({ message: 'timeout of 30000ms exceeded' });
expect(error).toMatchObject({ message: 'canceled' });
}

expect.assertions(1);
Expand Down
8 changes: 8 additions & 0 deletions src/utils/axiosSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ export type InterceptorParams = {
authentication: AuthenticationEndpoint;
};

const abortController = new AbortController();

export const cancelAllRequests = () => {
abortController.abort();
};

const requestInterceptor = (config: AxiosRequestConfig): InternalAxiosRequestConfig<unknown> => {
// axios.defaults do not automatically apply here, so set timeout manually
config.signal = abortController.signal;
config.timeout = axios.defaults.timeout;
config.headers = config.headers ?? {};
return config as InternalAxiosRequestConfig<unknown>;
Expand Down Expand Up @@ -82,6 +89,7 @@ export const createResponseRejectedInterceptor = (interceptorParams: Interceptor
const status = error.response?.status;
if (status === 497 || status === 404 || status === 503 || status === 400) {
if (interceptorParams.config.settings.onTokenRefreshFailed != null) {
cancelAllRequests();
interceptorParams.config.settings.onTokenRefreshFailed();
}
}
Expand Down