product.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. async function sendOrder(order, token) {
  2. console.log("Enviando orden:", order);
  3. try {
  4. const response = await fetch("/api/orders/send", {
  5. method: "POST",
  6. headers: {
  7. "Content-Type": "application/json",
  8. "Authorization": `Bearer ${token}`
  9. },
  10. body: JSON.stringify(order)
  11. });
  12. if (!response.ok) {
  13. const errorData = await response.json().catch(() => ({ message: "Respuesta no válida del servidor." }));
  14. throw new Error(errorData.message || `Error del servidor: ${response.status}`);
  15. }
  16. const data = await response.json();
  17. return data;
  18. } catch (error) {
  19. console.error("Error al enviar la orden:", error);
  20. throw error;
  21. }
  22. }
  23. async function getProducts(token){
  24. const response = await fetch("/api/products?status=1", {
  25. headers: {
  26. "Content-Type": "application/json",
  27. "Authorization": `Bearer ${token}`
  28. }
  29. });
  30. if (!response.ok) {
  31. const errorData = await response.json().catch(() => ({ message: "Respuesta no válida del servidor." }));
  32. throw new Error(errorData.message || `Error del servidor: ${response.status}`);
  33. }
  34. const data = await response.json();
  35. return data.products;
  36. }
  37. export {sendOrder, getProducts}