Allow override in get(). Refactor.

This commit is contained in:
trading_peter 2023-08-25 15:04:17 +02:00
parent 284d0af3a7
commit 2661e25cec
2 changed files with 26 additions and 17 deletions

View File

@ -5,40 +5,44 @@ export const fetchMixin = function(superClass) {
this.__abortControllers = new Map(); this.__abortControllers = new Map();
} }
get(url) { get(url, overwrite = true) {
return fetch(url).then(response => response.json()); return this.do('GET', url, null, overwrite);
} }
head(url) { head(url) {
return fetch(url, { method: 'HEAD' }); return fetch(url, { method: 'HEAD' });
} }
async post(url, data, overwrite = true) { post(url, data, overwrite = true) {
this.__cancelRunningRequest(url); return this.do('POST', url, data, overwrite);
}
async do(method, url, data, overwrite = true) {
this.__cancelRunningRequest(method, url);
if (overwrite === true) { if (overwrite === true) {
const ac = new AbortController(); const ac = new AbortController();
this.__abortControllers.set(url, ac); this.__abortControllers.set(`${method}:${url}`, ac);
} }
try { try {
const reqOptions = { const reqOptions = {
method: 'POST', method,
signal: overwrite ? this.__abortControllers.get(url).signal : null, signal: overwrite ? this.__abortControllers.get(`${method}:${url}`).signal : null,
mode: 'cors', mode: 'cors',
cache: 'no-cache', cache: 'no-cache',
headers: { headers: {
'Content-Type': 'application/json; charset=utf-8' 'Content-Type': 'application/json; charset=utf-8'
}, },
referrer: 'no-referrer', referrer: 'no-referrer',
body: JSON.stringify(data) body: data ? JSON.stringify(data) : undefined
}; };
document.dispatchEvent(new CustomEvent('before-request', { detail: reqOptions, bubbles: true, composed: true })); document.dispatchEvent(new CustomEvent('before-request', { detail: reqOptions, bubbles: true, composed: true }));
const result = await fetch(url, reqOptions).then(response => response.json()); const result = await fetch(url, reqOptions).then(response => response.json());
this.__abortControllers.delete(url); this.__abortControllers.delete(`${method}:${url}`);
if (result.statusCode === 500) { if (result.statusCode === 500) {
console.error(result); console.error(result);
@ -49,22 +53,27 @@ export const fetchMixin = function(superClass) {
if (err.name === 'AbortError') { if (err.name === 'AbortError') {
return { statusCode: -1, error: err }; return { statusCode: -1, error: err };
} else { } else {
this.__abortControllers.delete(url); this.__abortControllers.delete(`${method}:${url}`);
return { statusCode: null, error: err }; return { statusCode: null, error: err };
} }
} }
} }
isInFlight(url) { isInFlight(method, url) {
return Boolean(this.__abortControllers.get(url)); if (url === undefined) {
console.error('Missing url parameter');
return false;
} }
__cancelRunningRequest(url) { return Boolean(this.__abortControllers.get(`${method}:${url}`));
if (this.__abortControllers.has(url)) { }
__cancelRunningRequest(method, url) {
if (this.__abortControllers.has(`${method}:${url}`)) {
try { try {
this.__abortControllers.get(url).abort(); this.__abortControllers.get(`${method}:${url}`).abort();
} catch (err) { } } catch (err) { }
this.__abortControllers.delete(url); this.__abortControllers.delete(`${method}:${url}`);
} }
} }
}; };

View File

@ -1,6 +1,6 @@
{ {
"name": "@tp/helpers", "name": "@tp/helpers",
"version": "1.3.0", "version": "2.1.0",
"description": "", "description": "",
"main": "closest.js", "main": "closest.js",
"scripts": { "scripts": {