PHP Classes

File: frontend/js/componentes/models/productModel.js

Recommend this page to a friend!
  Classes of Rodrigo Faustino   Livraria   frontend/js/componentes/models/productModel.js   Download  
File: frontend/js/componentes/models/productModel.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Livraria
Manage a bookstore using micro-services
Author: By
Last change:
Date: 2 months ago
Size: 3,805 bytes
 

Contents

Class file image Download
import config from '../utils/config.js'; export async function fetchProducts(searchTerm = '') { try{ const response = await fetch(`${config.baseURL}/products?search=${searchTerm}`, { method: 'GET', headers: { 'Content-Type': 'application/json', } }); if (!response.ok) { const errorData = await response.json(); if (response.status === 429) { console.error('Limite de requisições excedido:', errorData.message); } else { throw new Error(errorData.message || 'Erro desconhecido'); } } return response.json(); }catch (e){ if(e == 'TypeError: Failed to fetch'){ alert('Limite de requisições excedido:'); } } } export async function fetchPurchasedProducts() { const response = await fetch(`${config.baseURL}/purchased-products`, { method: 'GET', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem('token'), } }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.message || 'Erro desconhecido'); } return response.json(); } export async function fetchProductsFromAPI() { const response = await fetch(`${config.baseURL}/products`, { method: 'GET', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem('token') } }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.message || 'Erro desconhecido'); } return response.json(); } export async function addProductToAPI(product) { const response = await fetch(`${config.baseURL}/products`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem('token') }, body: JSON.stringify(product) }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.message || 'Erro desconhecido'); } return response.json(); } export async function updateProductInAPI(product) { const response = await fetch(`${config.baseURL}/products`, { method: 'PUT', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem('token') }, body: JSON.stringify(product) }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.message || 'Erro desconhecido'); } return response.json(); } export async function deleteProductFromAPI(product) { const response = await fetch(`${config.baseURL}/products`, { method: 'DELETE', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem('token') }, body: JSON.stringify(product) }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.message || 'Erro desconhecido'); } return response.json(); } export function saveProductOffline(product, action) { let offlineProducts = JSON.parse(localStorage.getItem('offlineProducts')) || []; offlineProducts.push({ product, action }); localStorage.setItem('offlineProducts', JSON.stringify(offlineProducts)); } export function getOfflineProducts() { return JSON.parse(localStorage.getItem('offlineProducts')) || []; } export function clearOfflineProducts() { localStorage.removeItem('offlineProducts'); }