OpenAuth_server/views/shared/request.js
Fabian Stamm 51a8609880
All checks were successful
continuous-integration/drone/push Build is passing
Running prettier
2020-08-07 16:16:39 +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;
});
}