OpenAuth_server/FrontendLegacy/shared/request.js

27 lines
733 B
JavaScript
Raw Permalink Normal View History

2018-11-06 19:48:50 +00:00
export default function request(endpoint, method, data) {
var headers = new Headers();
2020-08-07 14:16:39 +00:00
headers.set("Content-Type", "application/json");
2018-11-06 19:48:50 +00:00
return fetch(endpoint, {
method: method,
body: JSON.stringify(data),
headers: headers,
2020-08-07 14:16:39 +00:00
credentials: "include",
2018-11-06 19:48:50 +00:00
})
2020-08-07 14:16:39 +00:00
.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;
});
}