Skip to content

Commit

Permalink
turn off base url
Browse files Browse the repository at this point in the history
  • Loading branch information
Precious-Macaulay committed Oct 29, 2024
1 parent 57daf9c commit 760c7cf
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/services/apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import axios, { AxiosInstance, AxiosResponse, AxiosError } from 'axios';

const BASE_URL = "https://redesigned-space-potato-qg9pj9g9g45299x5-5000.app.github.dev/"
// Set base URL conditionally based on an environment variable
const BASE_URL = process.env.USE_BASE_URL === 'true'
? "https://redesigned-space-potato-qg9pj9g9g45299x5-5000.app.github.dev/"
: undefined;

const apiClient: AxiosInstance = axios.create({
baseURL: BASE_URL,
Expand All @@ -11,7 +14,7 @@ const apiClient: AxiosInstance = axios.create({
},
});

// multipart form data api client2
// Multipart form data client
const apiClient2: AxiosInstance = axios.create({
baseURL: BASE_URL,
headers: {
Expand All @@ -27,6 +30,7 @@ interface ApiResponse<T> {
statusText: string;
}

// GET request
export const get = async <T>(url: string, params?: object): Promise<ApiResponse<T>> => {
try {
const response: AxiosResponse<T> = await apiClient.get(url, { params });
Expand All @@ -41,6 +45,7 @@ export const get = async <T>(url: string, params?: object): Promise<ApiResponse<
}
};

// POST request
export const post = async <T>(url: string, data: object): Promise<ApiResponse<T>> => {
try {
const response: AxiosResponse<T> = await apiClient.post(url, data);
Expand All @@ -55,6 +60,7 @@ export const post = async <T>(url: string, data: object): Promise<ApiResponse<T>
}
};

// Error handler
const handleApiError = (error: AxiosError): void => {
if (error.response) {
console.error('API Error:', error.response.status, error.response.data);
Expand All @@ -65,4 +71,4 @@ const handleApiError = (error: AxiosError): void => {
}
};

export { apiClient, apiClient2 };
export { apiClient, apiClient2 };

0 comments on commit 760c7cf

Please sign in to comment.