init
This commit is contained in:
36
lib/api.ts
Normal file
36
lib/api.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
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
|
||||
};
|
||||
Reference in New Issue
Block a user