Infrastructure: Implement master/dev/vX branching, save git path, and fix username casing
This commit is contained in:
110
frontend/lib/api.ts
Normal file
110
frontend/lib/api.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import axios from 'axios';
|
||||
|
||||
export const getBackendUrl = () => {
|
||||
if (typeof window === 'undefined') return 'http://localhost:8000';
|
||||
|
||||
const host = window.location.hostname;
|
||||
|
||||
// If we are on HTTPS (Proxy/Mobile mode), we use port 3002 for the backend
|
||||
if (window.location.protocol === 'https:') {
|
||||
if (host.includes('.loca.lt')) {
|
||||
return 'https://inventory-ai-api.loca.lt';
|
||||
}
|
||||
return `https://${host}:3002`;
|
||||
}
|
||||
|
||||
return `http://${host}:8000`;
|
||||
};
|
||||
|
||||
export const inventoryApi = {
|
||||
getItems: async () => {
|
||||
const res = await axios.get(`${getBackendUrl()}/items/`);
|
||||
return res.data;
|
||||
},
|
||||
|
||||
syncBulkOperations: async (userId: number, operations: any[]) => {
|
||||
const url = `${getBackendUrl()}/operations/bulk-sync`;
|
||||
try {
|
||||
const res = await axios.post(url, {
|
||||
user_id: userId,
|
||||
operations: operations
|
||||
});
|
||||
return res.data;
|
||||
} catch (err: any) {
|
||||
console.error("Sync API Error at:", url, err);
|
||||
if (err.response?.status === 404) {
|
||||
throw new Error(`404: Endpoint not found at ${url}`);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
|
||||
analyzeLabel: async (formData: FormData) => {
|
||||
const res = await axios.post(`${getBackendUrl()}/items/extract-label`, formData, {
|
||||
headers: { 'Content-Type': 'multipart/form-data' }
|
||||
});
|
||||
return res.data;
|
||||
},
|
||||
|
||||
createItem: async (userId: number, itemData: any) => {
|
||||
const res = await axios.post(`${getBackendUrl()}/items/`, itemData, {
|
||||
params: { user_id: userId }
|
||||
});
|
||||
return res.data;
|
||||
},
|
||||
|
||||
updateItem: async (itemId: number, itemData: any) => {
|
||||
const res = await axios.put(`${getBackendUrl()}/items/${itemId}`, itemData);
|
||||
return res.data;
|
||||
},
|
||||
|
||||
adjustStock: async (endpoint: string, data: any) => {
|
||||
const res = await axios.post(`${getBackendUrl()}/operations/${endpoint}`, data);
|
||||
return res.data;
|
||||
},
|
||||
|
||||
deleteItem: async (itemId: number) => {
|
||||
const res = await axios.delete(`${getBackendUrl()}/items/${itemId}`);
|
||||
return res.data;
|
||||
},
|
||||
|
||||
getAuditLogs: async (limit: number = 50) => {
|
||||
const res = await axios.get(`${getBackendUrl()}/operations/logs`, { params: { limit } });
|
||||
return res.data;
|
||||
},
|
||||
|
||||
// Users
|
||||
getUsers: async () => {
|
||||
const res = await axios.get(`${getBackendUrl()}/users/`);
|
||||
return res.data;
|
||||
},
|
||||
|
||||
createUser: async (userData: any) => {
|
||||
const res = await axios.post(`${getBackendUrl()}/users/`, userData);
|
||||
return res.data;
|
||||
},
|
||||
|
||||
login: async (credentials: any) => {
|
||||
const res = await axios.post(`${getBackendUrl()}/users/login`, credentials);
|
||||
return res.data;
|
||||
},
|
||||
|
||||
deleteUser: async (userId: number) => {
|
||||
const res = await axios.delete(`${getBackendUrl()}/users/${userId}`);
|
||||
return res.data;
|
||||
},
|
||||
|
||||
// Categories
|
||||
getCategories: async () => {
|
||||
const res = await axios.get(`${getBackendUrl()}/categories/`);
|
||||
return res.data;
|
||||
},
|
||||
createCategory: async (data: any) => {
|
||||
const res = await axios.post(`${getBackendUrl()}/categories/`, data);
|
||||
return res.data;
|
||||
},
|
||||
deleteCategory: async (id: number) => {
|
||||
const res = await axios.delete(`${getBackendUrl()}/categories/${id}`);
|
||||
return res.data;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user