2018-07-06 07:18:51 +02:00
|
|
|
class RequestBuilder {
|
|
|
|
constructor(url) {
|
|
|
|
this.callback = null;
|
|
|
|
this.url = url;
|
|
|
|
this.options = {
|
|
|
|
method: "POST",
|
|
|
|
cache: "no-cache",
|
|
|
|
credentials: "include",
|
|
|
|
body: null,
|
|
|
|
headers: new Headers({
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"X-Csrf-Token": this.getCsrfToken()
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-27 06:35:44 +02:00
|
|
|
withHttpMethod(method) {
|
|
|
|
this.options.method = method;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-07-06 07:18:51 +02:00
|
|
|
withBody(body) {
|
|
|
|
this.options.body = JSON.stringify(body);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
withCallback(callback) {
|
|
|
|
this.callback = callback;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCsrfToken() {
|
2022-06-05 17:06:04 +02:00
|
|
|
let element = document.querySelector("body[data-csrf-token]");
|
2018-07-06 07:18:51 +02:00
|
|
|
if (element !== null) {
|
2021-05-31 22:44:34 +02:00
|
|
|
return element.dataset.csrfToken;
|
2018-07-06 07:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
execute() {
|
|
|
|
fetch(new Request(this.url, this.options)).then((response) => {
|
|
|
|
if (this.callback) {
|
|
|
|
this.callback(response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|