From 9d8dadc6344b6efd24b0b61da92aa71dd7bfcc18 Mon Sep 17 00:00:00 2001 From: glax Date: Fri, 18 Oct 2024 19:42:17 +0200 Subject: [PATCH] Add postData and deleteData functions. Return promise.reject() on failure --- Website/App.tsx | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/Website/App.tsx b/Website/App.tsx index b3bb95b..818893e 100644 --- a/Website/App.tsx +++ b/Website/App.tsx @@ -28,6 +28,7 @@ export default function App(){ export function getData (uri: string) : Promise { return fetch(uri, { + method: 'GET', headers : { 'Content-Type': 'application/json', 'Accept': 'application/json' @@ -38,8 +39,43 @@ export function getData (uri: string) : Promise { return response.json(); }) .catch(function(err){ - console.error(err); - return null; + console.error(`Error GETting Data ${uri}\n${err}`); + return Promise.reject(); + }); +} + +export function postData(uri: string, content: object) : Promise { + return fetch(uri, + { + method: 'POST', + headers : { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + }, + body: JSON.stringify(content) + }) + .then(function(response){ + if(!response.ok) throw new Error("Could not fetch data"); + return response.json(); + }) + .catch(function(err){ + console.error(`Error POSTing Data ${uri}\n${err}`); + return Promise.reject(); + }); +} + +export function deleteData(uri: string) { + fetch(uri, + { + method: 'DELETE', + headers : { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + } + }) + .catch(function(err){ + console.error(`Error DELETEing Data ${uri}\n${err}`); + return Promise.reject(); }); }