Skip to content

Commit

Permalink
Bruce/857 axios retry (#211)
Browse files Browse the repository at this point in the history
* feat: adding retry logic to axios requests #857

* feat: fixing interceptor creator to after config is set #857

* feat: updated axios with response inceptors (#857)

---------

Co-authored-by: Fran McDade <franmcdade@Frans-MacBook-Pro.local>
  • Loading branch information
BruceRodrigues and Fran McDade authored Feb 21, 2024
1 parent c7c0007 commit 49a89e2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function getSupportRequestOptions(
* Upload file to add as attachment to request.
* @param url - Upload URL.
* @param file - File.
* @returns upload response.
*/
export async function uploadAttachment<T extends File>(
url: string,
Expand All @@ -81,6 +82,7 @@ export async function uploadAttachment<T extends File>(
* Execute fetch, throwing error on non-200 response.
* @param input - Request URL.
* @param options - Request options.
* @returns response.
*/
async function fetchWithErrorRejection(
input: RequestInfo | URL,
Expand Down
2 changes: 2 additions & 0 deletions packages/data-explorer-ui/src/entity/api/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export const fetchSummary = async (
* Fetch summary from given URL.
* @param path - URL.
* @param accessToken - Auth token.
* @returns summary response.
*/
export const fetchSummaryFromURL = async (
path: string,
Expand All @@ -160,6 +161,7 @@ export const fetchSummaryFromURL = async (
/**
* Fetch system status from given URL.
* @param URL - URL.
* @returns system status.
*/
export const fetchSystemStatusFromURL = async <R>(URL: string): Promise<R> => {
const res = await api().get<AzulSummaryResponse>(URL);
Expand Down
31 changes: 30 additions & 1 deletion packages/data-explorer-ui/src/entity/common/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
import axios, { AxiosInstance } from "axios";
import axios, {
AxiosError,
AxiosInstance,
AxiosResponse,
HttpStatusCode,
} from "axios";
import { getURL } from "../../shared/utils";

let axiosInstance: AxiosInstance | null = null;

/**
* Adding response interceptors to axios instances.
* @param api - AxiosInstance.
*/
export const configureInterceptors = (api: AxiosInstance): void => {
api.interceptors.response.use(
(response: AxiosResponse) => response,
(error: AxiosError) => {
const { config, response } = error;

if (response?.status === HttpStatusCode.ServiceUnavailable && config) {
const retryAfterValue = response.headers["Retry-After"];
const waitingTime = retryAfterValue ? +retryAfterValue : 0;
return new Promise((resolve) => {
setTimeout(() => resolve(api(config)), waitingTime);
});
}

Promise.reject(error);
}
);
};

/**
* Returns a singleton Axios instance configured for making HTTP requests to a specified base URL.
* @param baseURL - The base URL to use for the AxiosInstance.
Expand All @@ -14,6 +42,7 @@ export const api = (baseURL = getURL()): AxiosInstance => {
baseURL,
timeout: 20 * 1000,
});
configureInterceptors(axiosInstance);
}
return axiosInstance;
};

0 comments on commit 49a89e2

Please sign in to comment.