OpenAuth_server/views/shared/request.js
2018-11-06 20:48:50 +01:00

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;
})
}