mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 17:55:08 +01:00
cbf64e561a
- Remove redundant checks and returns - Add an additional check for local storage (some browsers still lack it) - Using some better practices - Use MutationObserver for events - Add initial events on DOM full render, send first trackView on complete readyState
67 lines
1.8 KiB
JavaScript
67 lines
1.8 KiB
JavaScript
import { makeUrl } from './url';
|
|
|
|
export const apiRequest = (method, url, body, headers) =>
|
|
fetch(url, {
|
|
method,
|
|
cache: 'no-cache',
|
|
credentials: 'same-origin',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json',
|
|
...headers,
|
|
},
|
|
body,
|
|
}).then(res => {
|
|
if (res.ok) {
|
|
return res.json().then(data => ({ ok: res.ok, status: res.status, data }));
|
|
}
|
|
|
|
return res.text().then(data => ({ ok: res.ok, status: res.status, res: res, data }));
|
|
});
|
|
|
|
export const get = (url, params, headers) =>
|
|
apiRequest('get', makeUrl(url, params), undefined, headers);
|
|
|
|
export const del = (url, params, headers) =>
|
|
apiRequest('delete', makeUrl(url, params), undefined, headers);
|
|
|
|
export const post = (url, params, headers) =>
|
|
apiRequest('post', url, JSON.stringify(params), headers);
|
|
|
|
export const put = (url, params, headers) =>
|
|
apiRequest('put', url, JSON.stringify(params), headers);
|
|
|
|
export const hook = (_this, method, callback) => {
|
|
const orig = _this[method];
|
|
|
|
return (...args) => {
|
|
callback.apply(null, args);
|
|
|
|
return orig.apply(_this, args);
|
|
};
|
|
};
|
|
|
|
export const doNotTrack = () => {
|
|
const { doNotTrack, navigator, external } = window;
|
|
|
|
const msTrackProtection = 'msTrackingProtectionEnabled';
|
|
const msTracking = () => {
|
|
return external && msTrackProtection in external && external[msTrackProtection]();
|
|
};
|
|
|
|
const dnt = doNotTrack || navigator.doNotTrack || navigator.msDoNotTrack || msTracking();
|
|
|
|
return dnt == '1' || dnt === 'yes';
|
|
};
|
|
|
|
export const setItem = (key, data, session) => {
|
|
if (typeof window !== 'undefined') {
|
|
(session ? sessionStorage : localStorage).setItem(key, JSON.stringify(data));
|
|
}
|
|
};
|
|
|
|
export const getItem = (key, session) =>
|
|
typeof window !== 'undefined'
|
|
? JSON.parse((session ? sessionStorage : localStorage).getItem(key))
|
|
: null;
|