matiru
Blockchain-Based Supply Chain Transparency for Agricultural Producegit clone https://github.com/maydayv7/matiru
frontend/services/api.js (2004B)
1import { API_BASE } from "../config";
2
3const request = async (endpoint, options = {}) => {
4 const { body, token, ...customConfig } = options;
5
6 const headers = { "Content-Type": "application/json" };
7 if (token) headers.Authorization = `Bearer ${token}`;
8
9 const config = {
10 method: body ? "POST" : "GET",
11 ...customConfig,
12 headers: { ...headers, ...customConfig.headers },
13 };
14 if (body) config.body = JSON.stringify(body);
15
16 const response = await fetch(`${API_BASE}${endpoint}`, config);
17 const data = await response.json();
18 if (!response.ok) throw new Error(data.error || "An API error occurred");
19 return data;
20};
21
22const uploadImage = (formData, token) => {
23 return fetch(`${API_BASE}/uploadImage`, {
24 method: "POST",
25 headers: {
26 Authorization: `Bearer ${token}`,
27 },
28 body: formData,
29 }).then(async (res) => {
30 const data = await res.json();
31 if (!res.ok) throw new Error(data.error || "Image upload failed");
32 return data;
33 });
34};
35
36export const api = {
37 login: (credentials) => request("/auth/login", { body: credentials }),
38 uploadImage,
39 getNotifications: (token) => request("/notifications", { token }),
40 registerProduce: (data, token) =>
41 request("/registerProduce", { body: data, token }),
42 updateLocation: (data, token) =>
43 request("/updateLocation", { body: data, token }),
44 transferOwnership: (data, token) =>
45 request("/transferOwnership", { body: data, token }),
46 inspectProduce: (data, token) =>
47 request("/inspectProduce", { body: data, token }),
48 updateDetails: (data, token) =>
49 request("/updateDetails", { body: data, token }),
50 markAsUnavailable: (data, token) =>
51 request("/markAsUnavailable", { body: data, token }),
52 splitProduce: (data, token) =>
53 request("/splitProduce", { body: data, token }),
54 getProduceById: (id) => request(`/getProduce/${id}`),
55 getProduceByOwner: (ownerId) => request(`/getProduceByOwner/${ownerId}`),
56 getUserDetails: (userKey) => request(`/getUser/${userKey}`),
57};