Acquire.js 901 B

1234567891011121314151617181920212223242526272829303132333435
  1. export class Acquire {
  2. static fetchCache = {};
  3. constructor(basePath) {
  4. this.path = basePath;
  5. this.cacheBusting = "?t=" + new Date().getTime();
  6. }
  7. async loadText(targetFile) {
  8. if (!Acquire.fetchCache[targetFile]) {
  9. let response = await fetch(this.path + "/" + targetFile + this.cacheBusting);
  10. Acquire.fetchCache[targetFile] = response.text();
  11. }
  12. return Acquire.fetchCache[targetFile];
  13. }
  14. async loadDOM(targetFile) {
  15. let text = await this.loadText(targetFile);
  16. return new DOMParser().parseFromString(text, "text/html").body.firstChild;
  17. }
  18. async apiGet(path) {
  19. let response = await fetch(path + this.cacheBusting);
  20. return response.json();
  21. }
  22. async apiPut(path, data) {
  23. let response = await fetch(path + this.cacheBusting, {
  24. method: 'PUT',
  25. headers: {
  26. 'Content-Type': 'application/json'
  27. },
  28. body: JSON.stringify(data)
  29. });
  30. return response.json();
  31. }
  32. }