mirror of
https://github.com/plausible/analytics.git
synced 2024-12-21 00:21:43 +03:00
40900c7653
* Ability to add event metadata * Close Dropdown on outside click * Show (none) value in metadata breakdown * Allow filtering for metadata key/val pairs * Use correct clickhouse_ecto * Better naming for meta filter * Add tests * Add changelog entry * Remove change made for testing
45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
import {formatISO} from './date'
|
|
|
|
let abortController = new AbortController()
|
|
|
|
function serialize(obj) {
|
|
var str = [];
|
|
for (var p in obj)
|
|
if (obj.hasOwnProperty(p)) {
|
|
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
|
|
}
|
|
return str.join("&");
|
|
}
|
|
|
|
export function cancelAll() {
|
|
abortController.abort()
|
|
abortController = new AbortController()
|
|
}
|
|
|
|
function serializeFilters(filters) {
|
|
const cleaned = {}
|
|
Object.entries(filters).forEach(([key, val]) => val ? cleaned[key] = val : null);
|
|
return JSON.stringify(cleaned)
|
|
}
|
|
|
|
export function serializeQuery(query, extraQuery=[]) {
|
|
const queryObj = {}
|
|
if (query.period) { queryObj.period = query.period }
|
|
if (query.date) { queryObj.date = formatISO(query.date) }
|
|
if (query.from) { queryObj.from = formatISO(query.from) }
|
|
if (query.to) { queryObj.to = formatISO(query.to) }
|
|
if (query.filters) { queryObj.filters = serializeFilters(query.filters) }
|
|
Object.assign(queryObj, ...extraQuery)
|
|
|
|
return '?' + serialize(queryObj)
|
|
}
|
|
|
|
export function get(url, query, ...extraQuery) {
|
|
url = url + serializeQuery(query, extraQuery)
|
|
return fetch(url, {signal: abortController.signal})
|
|
.then( response => {
|
|
if (!response.ok) { throw response }
|
|
return response.json()
|
|
})
|
|
}
|