Skip to content

Commit 67cbafa

Browse files
committed
resolve race condition to ensure getting config is called upon request rather than when component mounts
1 parent b183995 commit 67cbafa

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

clients/shared/src/api/client.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,15 @@ export const createRequest = (
6868
};
6969

7070
export const useAPIClient = (): HttpClient => {
71-
const { getToken } = getConfig();
72-
const request = createRequest(getToken, getBaseUrl());
71+
// Lazily resolve config at request time so that setConfig()
72+
// can be called during app startup (e.g. in a useEffect)
73+
// before any API calls are executed.
74+
const request = async <T>(config: RequestConfig): Promise<T> => {
75+
const { getToken } = getConfig();
76+
const baseUrl = getBaseUrl();
77+
const doRequest = createRequest(getToken, baseUrl);
78+
return doRequest<T>(config);
79+
};
7380

7481
return {
7582
get: <T>(endpoint: string, params?: Record<string, any>) =>
@@ -86,7 +93,7 @@ export const useAPIClient = (): HttpClient => {
8693
};
8794

8895
export const getBaseUrl = (): string => {
89-
const url = getConfig().API_BASE_URL;
96+
const url = getConfig().API_BASE_URL;
9097
if (!url) {
9198
throw new Error("API_BASE_URL is not configured. Check your .env file.");
9299
}

0 commit comments

Comments
 (0)