From 40139eabacba3418c02463a5909a5325effd8f71 Mon Sep 17 00:00:00 2001 From: T'JAMPENS QUENTIN p2406187 Date: Wed, 28 Jan 2026 14:11:35 +0100 Subject: [PATCH] Create fetchWrapper function --- src/utils/fetchWrapper.js | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/utils/fetchWrapper.js diff --git a/src/utils/fetchWrapper.js b/src/utils/fetchWrapper.js new file mode 100644 index 0000000..aa9f0a0 --- /dev/null +++ b/src/utils/fetchWrapper.js @@ -0,0 +1,45 @@ +export default async function fetchWrapper( + path, + data = null, + method = "GET", + headers = {} +) { + try { + const res = await fetch( + `${import.meta.env.VITE_API_URL}${path}`, + { + method, + credentials: "include", + headers: { + "Accept": "application/json", + "Content-Type": "application/json", + ...headers, + }, + body: data ? JSON.stringify(data) : null, + } + ); + + let responseData = null; + + try { + responseData = await res.json(); + } catch (_) { + responseData = null; + } + + return { + status: res.status, + data: responseData, + }; + + } catch (err) { + console.error("❌ Erreur réseau :", err); + + return { + status: 0, + data: { + message: "Network error", + }, + }; + } +}