fix undefined url

This commit is contained in:
Logan Cusano
2025-07-13 21:05:11 -04:00
parent 10c298a3ea
commit 5896165cfd

View File

@@ -1,9 +1,17 @@
const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://127.0.0.1:8000'; // We still export this for use in components where the raw URL is needed,
// like the <video> src attribute.
export const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://127.0.0.1:8000';
const apiRequest = async (endpoint, options = {}) => { type ApiRequestOptions = {
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
body?: any;
token?: string | null;
};
export const apiRequest = async (endpoint: string, options: ApiRequestOptions = {}) => {
const { method = 'GET', body = null, token = null } = options; const { method = 'GET', body = null, token = null } = options;
const headers = { const headers: HeadersInit = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Accept': 'application/json', 'Accept': 'application/json',
}; };
@@ -12,7 +20,7 @@ const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://127.0.0.1:8000';
headers['Authorization'] = `Bearer ${token}`; headers['Authorization'] = `Bearer ${token}`;
} }
const config = { const config: RequestInit = {
method, method,
headers, headers,
}; };
@@ -21,6 +29,7 @@ const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://127.0.0.1:8000';
config.body = JSON.stringify(body); config.body = JSON.stringify(body);
} }
// Use the API_URL variable to construct the full URL inside the function
const response = await fetch(`${API_URL}${endpoint}`, config); const response = await fetch(`${API_URL}${endpoint}`, config);
if (!response.ok) { if (!response.ok) {
@@ -28,9 +37,12 @@ const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://127.0.0.1:8000';
throw new Error(errorData.detail || `HTTP error! status: ${response.status}`); throw new Error(errorData.detail || `HTTP error! status: ${response.status}`);
} }
if (response.headers.get("content-type")?.includes("application/json")) { // Handle cases where the response might not be JSON
const contentType = response.headers.get("content-type");
if (contentType?.includes("application/json")) {
return response.json(); return response.json();
} }
return response; // For non-json responses like video streams // For non-json responses (like video streams or simple text), return the raw response
}; return response;
};