OpenAuth_server/views/shared/request.js

16 lines
561 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;
})
}