OpenAuth_server/FrontendLegacy/shared/request.js
Fabian Stamm 0453e461c9 Restructuring the Project
Updating dependencies
2023-04-07 19:54:47 +02:00

27 lines
733 B
JavaScript

export default function request(endpoint, method, data) {
var headers = new Headers();
headers.set("Content-Type", "application/json");
return fetch(endpoint, {
method: method,
body: JSON.stringify(data),
headers: headers,
credentials: "include",
})
.then(async (e) => {
if (e.status !== 200)
throw new Error((await e.text()) || e.statusText);
return e.json();
})
.then((e) => {
if (e.error)
return Promise.reject(
new Error(
typeof e.error === "string"
? e.error
: JSON.stringify(e.error)
)
);
return e;
});
}