1234567891011121314151617181920212223242526272829303132333435 |
- export class Acquire {
- static fetchCache = {};
- constructor(basePath) {
- this.path = basePath;
- this.cacheBusting = "?t=" + new Date().getTime();
- }
- async loadText(targetFile) {
- if (!Acquire.fetchCache[targetFile]) {
- let response = await fetch(this.path + "/" + targetFile + this.cacheBusting);
- Acquire.fetchCache[targetFile] = response.text();
- }
- return Acquire.fetchCache[targetFile];
- }
- async loadDOM(targetFile) {
- let text = await this.loadText(targetFile);
- return new DOMParser().parseFromString(text, "text/html").body.firstChild;
- }
- async apiGet(path) {
- let response = await fetch(path + this.cacheBusting);
- return response.json();
- }
- async apiPut(path, data) {
- let response = await fetch(path + this.cacheBusting, {
- method: 'PUT',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify(data)
- });
- return response.json();
- }
- }
|