diff options
author | Frédéric Guillot <fred@miniflux.net> | 2018-07-05 22:18:51 -0700 |
---|---|---|
committer | Frédéric Guillot <fred@miniflux.net> | 2018-07-05 22:18:51 -0700 |
commit | 53deb0b8cd1899ec325eca93631b3e137bdd3ec3 (patch) | |
tree | 23894ed57040ea689e9f60243656e1889d39a275 /ui/static/js/request_builder.js | |
parent | e1c56b2e53ba3c6f48d5e159d18ae59c180cc388 (diff) |
Refactor assets bundler and split Javascript files
Diffstat (limited to 'ui/static/js/request_builder.js')
-rw-r--r-- | ui/static/js/request_builder.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/ui/static/js/request_builder.js b/ui/static/js/request_builder.js new file mode 100644 index 0000000..52ed2a1 --- /dev/null +++ b/ui/static/js/request_builder.js @@ -0,0 +1,43 @@ +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() + }) + }; + } + + withBody(body) { + this.options.body = JSON.stringify(body); + return this; + } + + withCallback(callback) { + this.callback = callback; + return this; + } + + getCsrfToken() { + let element = document.querySelector("meta[name=X-CSRF-Token]"); + if (element !== null) { + return element.getAttribute("value"); + } + + return ""; + } + + execute() { + fetch(new Request(this.url, this.options)).then((response) => { + if (this.callback) { + this.callback(response); + } + }); + } +} |