2019-11-19 07:30:42 +03:00
|
|
|
import {formatISO} from './date'
|
|
|
|
|
|
|
|
function serialize(obj) {
|
|
|
|
var str = [];
|
|
|
|
for (var p in obj)
|
|
|
|
if (obj.hasOwnProperty(p)) {
|
|
|
|
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
|
|
|
|
}
|
|
|
|
return str.join("&");
|
|
|
|
}
|
|
|
|
|
2020-01-13 16:46:28 +03:00
|
|
|
export function serializeQuery(query, extraQuery=[]) {
|
2019-11-19 07:30:42 +03:00
|
|
|
query = Object.assign({}, query, {
|
2019-11-20 08:48:27 +03:00
|
|
|
date: query.date ? formatISO(query.date) : undefined,
|
2020-03-04 18:24:18 +03:00
|
|
|
from: query.from ? formatISO(query.from) : undefined,
|
|
|
|
to: query.to ? formatISO(query.to) : undefined,
|
2019-11-20 08:48:27 +03:00
|
|
|
filters: query.filters ? JSON.stringify(query.filters) : undefined
|
2019-11-19 07:30:42 +03:00
|
|
|
}, ...extraQuery)
|
|
|
|
|
2020-01-13 16:16:35 +03:00
|
|
|
return '?' + serialize(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function get(url, query, ...extraQuery) {
|
|
|
|
url = url + serializeQuery(query, extraQuery)
|
2019-11-19 07:30:42 +03:00
|
|
|
return fetch(url).then(res => res.json())
|
|
|
|
}
|