/** * API Service - Handles all API communication */ const ApiService = { /** * Base URL for API calls */ baseUrl: 'api.php', /** * Check if user session is valid * @returns {Promise<{authenticated: boolean, user?: string}>} */ async checkSession() { const res = await fetch(`${this.baseUrl}?action=check_session`); return res.json(); }, /** * Login user * @param {string} username * @param {string} password * @returns {Promise<{success?: boolean, user?: string, error?: string}>} */ async login(username, password) { const res = await fetch(`${this.baseUrl}?action=login`, { method: 'POST', body: JSON.stringify({ username, password }) }); return { ok: res.ok, data: await res.json() }; }, /** * Logout user * @returns {Promise<{success: boolean}>} */ async logout() { const res = await fetch(`${this.baseUrl}?action=logout`); return res.json(); }, /** * Get all products * @returns {Promise} */ async getProducts() { const res = await fetch(`${this.baseUrl}?action=get_products`); if (res.ok) { return res.json(); } return []; }, /** * Lookup address by postcode and house number * @param {string} postcode * @param {string} number * @returns {Promise<{street?: string, city?: string, error?: string}>} */ async lookupPostcode(postcode, number) { const res = await fetch( `${this.baseUrl}?action=postcode_check&postcode=${encodeURIComponent(postcode)}&number=${encodeURIComponent(number)}` ); return res.json(); }, /** * Create a new order * @param {Object} orderData * @returns {Promise<{success?: boolean, order_id?: number, total?: string, error?: string}>} */ async createOrder(orderData) { const res = await fetch(`${this.baseUrl}?action=create_order`, { method: 'POST', body: JSON.stringify(orderData) }); return res.json(); } };