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,36 +1,48 @@
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.
const apiRequest = async (endpoint, options = {}) => { export const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://127.0.0.1:8000';
const { method = 'GET', body = null, token = null } = options;
type ApiRequestOptions = {
const headers = { method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
'Content-Type': 'application/json', body?: any;
'Accept': 'application/json', token?: string | null;
}; };
if (token) { export const apiRequest = async (endpoint: string, options: ApiRequestOptions = {}) => {
headers['Authorization'] = `Bearer ${token}`; const { method = 'GET', body = null, token = null } = options;
}
const headers: HeadersInit = {
const config = { 'Content-Type': 'application/json',
method, 'Accept': 'application/json',
headers, };
};
if (token) {
if (body) { headers['Authorization'] = `Bearer ${token}`;
config.body = JSON.stringify(body); }
}
const config: RequestInit = {
const response = await fetch(`${API_URL}${endpoint}`, config); method,
headers,
if (!response.ok) { };
const errorData = await response.json().catch(() => ({ detail: 'An unknown error occurred' }));
throw new Error(errorData.detail || `HTTP error! status: ${response.status}`); if (body) {
} config.body = JSON.stringify(body);
}
if (response.headers.get("content-type")?.includes("application/json")) {
return response.json(); // Use the API_URL variable to construct the full URL inside the function
} const response = await fetch(`${API_URL}${endpoint}`, config);
return response; // For non-json responses like video streams if (!response.ok) {
}; const errorData = await response.json().catch(() => ({ detail: 'An unknown error occurred' }));
throw new Error(errorData.detail || `HTTP error! status: ${response.status}`);
}
// Handle cases where the response might not be JSON
const contentType = response.headers.get("content-type");
if (contentType?.includes("application/json")) {
return response.json();
}
// For non-json responses (like video streams or simple text), return the raw response
return response;
};