add api utils
This commit is contained in:
parent
ee3ac34e4b
commit
bdd90b50d9
6 changed files with 99 additions and 93 deletions
42
frontend/src/api/utils/requests.ts
Normal file
42
frontend/src/api/utils/requests.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { setAuthHeader, setContentTypeHeaderJson } from "./headers";
|
||||
|
||||
export async function get(url: string) : Promise<Response>{
|
||||
const requestHeaders = new Headers();
|
||||
setAuthHeader(requestHeaders);
|
||||
console.log("GET to " + url);
|
||||
const response = await fetch(url, {
|
||||
method: "GET",
|
||||
headers: requestHeaders
|
||||
})
|
||||
if(!response.ok){
|
||||
throw new Error("GET to " + url + "failed!")
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function postJson(url: string, requestBody: string, logBody = true) : Promise<Response>{
|
||||
console.log("POST to " + url + (logBody) ? "with body " + requestBody : "");
|
||||
return persistJson(url, requestBody, "POST");
|
||||
}
|
||||
|
||||
export async function putJson(url: string, requestBody: string, logBody = true) : Promise<Response>{
|
||||
console.log("PUT to " + url + (logBody) ? "with body " + requestBody : "");
|
||||
return persistJson(url, requestBody, "PUT");
|
||||
}
|
||||
|
||||
async function persistJson(url: string, requestBody: string, requestMethod: string) : Promise<Response>{
|
||||
const requestHeaders = new Headers();
|
||||
setContentTypeHeaderJson(requestHeaders);
|
||||
setAuthHeader(requestHeaders);
|
||||
const response = await fetch(url, {
|
||||
method: requestMethod,
|
||||
headers: requestHeaders,
|
||||
body: requestBody,
|
||||
});
|
||||
if(!response.ok){
|
||||
throw new Error(requestMethod + " to " + url + "failed!")
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue