From 168a870c025bfef6efdeb46e166e79a16093c157 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Thu, 10 Aug 2023 19:46:45 -0700 Subject: Move internal packages to an internal folder For reference: https://go.dev/doc/go1.4#internalpackages --- internal/ui/static/js/request_builder.js | 48 ++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 internal/ui/static/js/request_builder.js (limited to 'internal/ui/static/js/request_builder.js') diff --git a/internal/ui/static/js/request_builder.js b/internal/ui/static/js/request_builder.js new file mode 100644 index 00000000..e19168fc --- /dev/null +++ b/internal/ui/static/js/request_builder.js @@ -0,0 +1,48 @@ +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() + }) + }; + } + + withHttpMethod(method) { + this.options.method = method; + return this; + } + + withBody(body) { + this.options.body = JSON.stringify(body); + return this; + } + + withCallback(callback) { + this.callback = callback; + return this; + } + + getCsrfToken() { + let element = document.querySelector("body[data-csrf-token]"); + if (element !== null) { + return element.dataset.csrfToken; + } + + return ""; + } + + execute() { + fetch(new Request(this.url, this.options)).then((response) => { + if (this.callback) { + this.callback(response); + } + }); + } +} -- cgit v1.2.3