mirror of
https://github.com/plausible/analytics.git
synced 2024-12-25 02:24:55 +03:00
0fa6b688af
* Make TestUtils module available in all tests * Add macros patching the application env in tests Unfortunately a lot of existing functionality relies on certain application env setup. This isn't ideal because the app config is a shared state that prevents us from running the tests in parallel. Those macros encapsulate setting up new env for test purposes and make sure the changes are reverted when the test finishes. * Allow passing request opts to HTTPClient.post/4 We need this to swap custom request building in Google Analytics import. * Unify errors when listing sites * React: propagate backend error messages if available * React: catch API errors in Search Terms component * Propagate google API errors on referrer drilldown * Handle verified properties errors in SC settings * Add missing tests for SC settings controller * Unify errors for fetching search analytics queries (list stats) * Unify errors refreshing Google Auth Token * Test fetch_stats/3 errors and replace Double with Mox * Fixup makrup * s/class/className * Simplify Search Terms display in case of errors * Fix warnings
66 lines
1.9 KiB
JavaScript
66 lines
1.9 KiB
JavaScript
import {formatISO} from './util/date'
|
|
|
|
let abortController = new AbortController()
|
|
let SHARED_LINK_AUTH = null
|
|
|
|
class ApiError extends Error {
|
|
constructor(message, payload) {
|
|
super(message)
|
|
this.name = "ApiError"
|
|
this.payload = payload
|
|
}
|
|
}
|
|
|
|
function serialize(obj) {
|
|
var str = [];
|
|
for (var p in obj)
|
|
/* eslint-disable-next-line no-prototype-builtins */
|
|
if (obj.hasOwnProperty(p)) {
|
|
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
|
|
}
|
|
return str.join("&");
|
|
}
|
|
|
|
export function setSharedLinkAuth(auth) {
|
|
SHARED_LINK_AUTH = auth
|
|
}
|
|
|
|
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) }
|
|
if (query.with_imported) { queryObj.with_imported = query.with_imported }
|
|
if (SHARED_LINK_AUTH) { queryObj.auth = SHARED_LINK_AUTH }
|
|
Object.assign(queryObj, ...extraQuery)
|
|
|
|
return '?' + serialize(queryObj)
|
|
}
|
|
|
|
export function get(url, query={}, ...extraQuery) {
|
|
const headers = SHARED_LINK_AUTH ? {'X-Shared-Link-Auth': SHARED_LINK_AUTH} : {}
|
|
url = url + serializeQuery(query, extraQuery)
|
|
return fetch(url, {signal: abortController.signal, headers: headers})
|
|
.then( response => {
|
|
if (!response.ok) {
|
|
return response.json().then((msg) => {
|
|
throw new ApiError(msg.error, msg)
|
|
})
|
|
}
|
|
return response.json()
|
|
})
|
|
}
|