Download frontend/src/lib/api.ts from abhyudit/Stash_Go: direct link, hf CLI and curl.
- Browser
- Download file 13.2 kB
-
https://huggingface.co/spaces/abhyudit/Stash_Go/resolve/main/frontend/src/lib/api.ts
- Command line
-
hf download hf://spaces/abhyudit/Stash_Go/frontend/src/lib/api.ts
-
curl -L -o api.ts https://huggingface.co/spaces/abhyudit/Stash_Go/resolve/main/frontend/src/lib/api.ts
13.2 kB
| export const Base_URL = typeof window !== "undefined" && (window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1") | |
| ? "http://127.0.0.1:8000" | |
| : "https://abhyudit-stash-go.hf.space"; | |
| import type { Product } from "./types"; | |
| import { toast } from "./toastStore"; | |
| export async function pingServer() { | |
| try { | |
| const response = await fetch(`${Base_URL}/`); | |
| if (!response.ok) { | |
| throw new Error("The server rejects the request"); | |
| } | |
| const data = await response.json(); | |
| return data; | |
| } catch (error) { | |
| console.error("Could not reach the StashGO:", error); | |
| return { status: "offline" }; | |
| } | |
| } | |
| export async function getProduct() { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/products/getall`, { | |
| method: "GET", | |
| headers: { | |
| "Authorization": `Bearer ${token}`, | |
| "Content-Type": "application/json" | |
| } | |
| }); | |
| if (!response.ok) { | |
| throw new Error("The server rejects the request"); | |
| } | |
| const data = await response.json(); | |
| return data; | |
| } catch (error) { | |
| console.error("Could not reach the StashGO:", error); | |
| return []; | |
| } | |
| } | |
| export async function createProduct(productData: { sku: string; name: string; cost_price: number; selling_price: number; quantity_left: number }) { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/products/create`, { | |
| method: "POST", | |
| headers: { | |
| "Authorization": `Bearer ${token}`, | |
| "Content-Type": "application/json" | |
| }, | |
| body: JSON.stringify(productData) | |
| }); | |
| return response.ok; | |
| } catch (error) { | |
| console.error("Error creating product:", error); | |
| return false; | |
| } | |
| } | |
| export async function adjustStock(productId: string, action: "increase-stock" | "decrease-stock") { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/products/${productId}/${action}`, { | |
| method: "PATCH", | |
| headers: { | |
| "Authorization": `Bearer ${token}` | |
| } | |
| }); | |
| return response.ok; | |
| } catch (error) { | |
| console.error(`Error executing ${action}:`, error); | |
| return false; | |
| } | |
| } | |
| export async function deleteProduct(productId: string) { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/products/${productId}`, { | |
| method: "DELETE", | |
| headers: { | |
| "Authorization": `Bearer ${token}` | |
| } | |
| }); | |
| return response.ok; | |
| } catch (error) { | |
| console.error("Error deleting product:", error); | |
| return false; | |
| } | |
| } | |
| export async function getAllSales() { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/sales/`, { | |
| method: "GET", | |
| headers: { | |
| "Authorization": `Bearer ${token}`, | |
| "Content-Type": "application/json" | |
| } | |
| }); | |
| if (!response.ok) return []; | |
| return await response.json(); | |
| } catch (error) { | |
| console.error("Error fetching historical sales ledger:", error); | |
| return []; | |
| } | |
| } | |
| export async function createDraftBill(customerData: { customer_name: string; customer_phone: string; customer_email: string }) { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/bills/`, { | |
| method: "POST", | |
| headers: { | |
| "Authorization": `Bearer ${token}`, | |
| "Content-Type": "application/json" | |
| }, | |
| body: JSON.stringify(customerData) | |
| }); | |
| if (!response.ok) return null; | |
| return await response.json(); | |
| } catch (error) { | |
| console.error("Error creating draft bill:", error); | |
| return null; | |
| } | |
| } | |
| export async function getBillById(billId: number) { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/bills/${billId}`, { | |
| method: "GET", | |
| headers: { | |
| "Authorization": `Bearer ${token}`, | |
| "Content-Type": "application/json" | |
| } | |
| }); | |
| if (!response.ok) return null; | |
| return await response.json(); | |
| } catch (error) { | |
| console.error("Error fetching bill by ID:", error); | |
| return null; | |
| } | |
| } | |
| export async function addItemToBill(billId: number, itemData: { product_id: string; quantity: number }) { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/bills/${billId}/items`, { | |
| method: "POST", | |
| headers: { | |
| "Authorization": `Bearer ${token}`, | |
| "Content-Type": "application/json" | |
| }, | |
| body: JSON.stringify(itemData) | |
| }); | |
| if (!response.ok) { | |
| const err = await response.json(); | |
| const detailMsg = typeof err.detail === 'string' ? err.detail : "Failed to append item."; | |
| toast.error("Append Item Failed", detailMsg); | |
| return null; | |
| } | |
| return await response.json(); | |
| } catch (error) { | |
| console.error("Error appending item:", error); | |
| toast.error("Network Error", "Failed to connect to backend server."); | |
| return null; | |
| } | |
| } | |
| export async function updateBillDetails(billId: number, updateData: { discount: number; tax: number; payment_method: string }) { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/bills/${billId}`, { | |
| method: "PUT", | |
| headers: { | |
| "Authorization": `Bearer ${token}`, | |
| "Content-Type": "application/json" | |
| }, | |
| body: JSON.stringify(updateData) | |
| }); | |
| if (!response.ok) return null; | |
| return await response.json(); | |
| } catch (error) { | |
| console.error("Error patching invoice configurations:", error); | |
| return null; | |
| } | |
| } | |
| export async function finalizeBill(billId: number) { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/bills/${billId}/finalize`, { | |
| method: "POST", | |
| headers: { | |
| "Authorization": `Bearer ${token}` | |
| } | |
| }); | |
| if (!response.ok) { | |
| const err = await response.json(); | |
| const detailMsg = typeof err.detail === 'string' ? err.detail : "Failed to finalize invoice."; | |
| toast.error("Finalize Error", detailMsg); | |
| return null; | |
| } | |
| return await response.json(); | |
| } catch (error) { | |
| console.error("Error finalizing transaction:", error); | |
| toast.error("Network Error", "Failed to connect to backend server."); | |
| return null; | |
| } | |
| } | |
| export async function getDashboardAnalytics() { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/analytics/dashboard`, { | |
| method: "GET", | |
| headers: { | |
| "Authorization": `Bearer ${token}`, | |
| "Content-Type": "application/json" | |
| } | |
| }); | |
| if (!response.ok) return null; | |
| return await response.json(); | |
| } catch (error) { | |
| console.error("Error fetching dashboard analytics:", error); | |
| return null; | |
| } | |
| } | |
| export async function initiateProductReturn(returnData: { sale_id: number; product_id: number, quantity: number; reason: string }) { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/returns/create`, { // ✅ trailing slash removed | |
| method: "POST", | |
| headers: { | |
| "Authorization": `Bearer ${token}`, | |
| "Content-Type": "application/json" | |
| }, | |
| body: JSON.stringify(returnData) | |
| }); | |
| if (!response.ok) { | |
| const err = await response.json(); | |
| alert(`Return Refused: ${err.detail}`); | |
| return null; | |
| } | |
| return await response.json(); | |
| } catch (error) { | |
| console.error("Error processing return execution:", error); | |
| return null; | |
| } | |
| } | |
| export async function getAllReturns() { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/returns/all_returns`, { // ✅ trailing slash removed | |
| method: "GET", | |
| headers: { "Authorization": `Bearer ${token}` } | |
| }); | |
| if (!response.ok) return []; | |
| return await response.json(); | |
| } catch (error) { | |
| console.error("Error gathering historical returns log:", error); | |
| return []; | |
| } | |
| } | |
| export async function removeItemFromBill(billId: number, itemId: number) { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const res = await fetch(`${Base_URL}/bills/${billId}/items/${itemId}`, { | |
| method: "DELETE", | |
| headers: { | |
| "Authorization": `Bearer ${token}` | |
| } | |
| }); | |
| if (!res.ok) { | |
| console.error("Failed to remove item. Status:", res.status); | |
| return null; | |
| } | |
| return await res.json(); | |
| } catch (error) { | |
| console.error("Error calling remove item API:", error); | |
| return null; | |
| } | |
| } | |
| export async function deleteSaleBill(billId: number) { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const res = await fetch(`${Base_URL}/bills/${billId}`, { | |
| method: "DELETE", | |
| headers: { "Authorization": `Bearer ${token}` } | |
| }); | |
| if (!res.ok) throw new Error("Failed to delete bill"); | |
| return true; | |
| } catch (error) { | |
| console.error("Error deleting bill:", error); | |
| return false; | |
| } | |
| } | |
| // --- AI Feature API Functions --- | |
| export async function aiChat(question: string) { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/ai/chat`, { | |
| method: "POST", | |
| headers: { | |
| "Authorization": `Bearer ${token}`, | |
| "Content-Type": "application/json" | |
| }, | |
| body: JSON.stringify({ question }) | |
| }); | |
| if (!response.ok) return null; | |
| return await response.json(); | |
| } catch (error) { | |
| console.error("AI Chat error:", error); | |
| return null; | |
| } | |
| } | |
| export async function aiSemanticSearch(query: string, limit: number = 10) { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/ai/search?q=${encodeURIComponent(query)}&limit=${limit}`, { | |
| method: "GET", | |
| headers: { "Authorization": `Bearer ${token}` } | |
| }); | |
| if (!response.ok) return []; | |
| return await response.json(); | |
| } catch (error) { | |
| console.error("AI Search error:", error); | |
| return []; | |
| } | |
| } | |
| export async function aiForecast(days: number = 7) { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/ai/forecast?days=${days}`, { | |
| method: "GET", | |
| headers: { "Authorization": `Bearer ${token}` } | |
| }); | |
| if (!response.ok) return null; | |
| return await response.json(); | |
| } catch (error) { | |
| console.error("AI Forecast error:", error); | |
| return null; | |
| } | |
| } | |
| export async function aiRecommendations(productId: number, limit: number = 5) { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/ai/recommendations/${productId}?limit=${limit}`, { | |
| method: "GET", | |
| headers: { "Authorization": `Bearer ${token}` } | |
| }); | |
| if (!response.ok) return []; | |
| return await response.json(); | |
| } catch (error) { | |
| console.error("AI Recommendations error:", error); | |
| return []; | |
| } | |
| } | |
| export async function aiInsights() { | |
| try { | |
| const token = localStorage.getItem("token"); | |
| const response = await fetch(`${Base_URL}/ai/insights`, { | |
| method: "GET", | |
| headers: { "Authorization": `Bearer ${token}` } | |
| }); | |
| if (!response.ok) return null; | |
| return await response.json(); | |
| } catch (error) { | |
| console.error("AI Insights error:", error); | |
| return null; | |
| } | |
| } |