request_builder.js 892 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. class RequestBuilder {
  2. constructor(url) {
  3. this.callback = null;
  4. this.url = url;
  5. this.options = {
  6. method: "POST",
  7. cache: "no-cache",
  8. credentials: "include",
  9. body: null,
  10. headers: new Headers({
  11. "Content-Type": "application/json",
  12. "X-Csrf-Token": getCsrfToken()
  13. })
  14. };
  15. }
  16. withHttpMethod(method) {
  17. this.options.method = method;
  18. return this;
  19. }
  20. withBody(body) {
  21. this.options.body = JSON.stringify(body);
  22. return this;
  23. }
  24. withCallback(callback) {
  25. this.callback = callback;
  26. return this;
  27. }
  28. execute() {
  29. fetch(new Request(this.url, this.options)).then((response) => {
  30. if (this.callback) {
  31. this.callback(response);
  32. }
  33. });
  34. }
  35. }