Files
twimg-frontend/lib/api.ts
Logan Cusano 7d3f08cae9 init
2025-07-13 19:03:13 -04:00

36 lines
1.1 KiB
TypeScript

const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://127.0.0.1:8000';
const apiRequest = async (endpoint, options = {}) => {
const { method = 'GET', body = null, token = null } = options;
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
};
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
const config = {
method,
headers,
};
if (body) {
config.body = JSON.stringify(body);
}
const response = await fetch(`${API_URL}${endpoint}`, config);
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 (response.headers.get("content-type")?.includes("application/json")) {
return response.json();
}
return response; // For non-json responses like video streams
};