Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Changed fetch response.

Mike Cao26463973cb25
diff --git a/src/components/hooks/useApi.ts b/src/components/hooks/useApi.ts
index be386a296..d8a05d92c 100644
--- a/src/components/hooks/useApi.ts
+++ b/src/components/hooks/useApi.ts
@@ -2,16 +2,16 @@ import { useCallback } from 'react';
import * as reactQuery from '@tanstack/react-query';
import { getClientAuthToken } from '@/lib/client';
import { SHARE_TOKEN_HEADER } from '@/lib/constants';
-import { httpGet, httpPost, httpPut, httpDelete } from '@/lib/fetch';
+import { httpGet, httpPost, httpPut, httpDelete, FetchResponse } from '@/lib/fetch';
import useStore from '@/store/app';
const selector = (state: { shareToken: { token?: string } }) => state.shareToken;
-async function handleResponse(data: any): Promise<any> {
- if (data.error) {
- return Promise.reject(new Error(data.error));
+async function handleResponse(res: FetchResponse): Promise<any> {
+ if (!res.ok) {
+ return Promise.reject(new Error(res.error));
}
- return Promise.resolve(data);
+ return Promise.resolve(res.data);
}
function handleError(err: Error | string) {
diff --git a/src/components/hooks/useCountryNames.ts b/src/components/hooks/useCountryNames.ts
index 691167e12..12f2f0dde 100644
--- a/src/components/hooks/useCountryNames.ts
+++ b/src/components/hooks/useCountryNames.ts
@@ -10,7 +10,7 @@ export function useCountryNames(locale: string) {
const [list, setList] = useState(countryNames[locale] || enUS);
async function loadData(locale: string) {
- const data = await httpGet(`${process.env.basePath || ''}/intl/country/${locale}.json`);
+ const { data } = await httpGet(`${process.env.basePath || ''}/intl/country/${locale}.json`);
if (data) {
countryNames[locale] = data;
diff --git a/src/components/hooks/useLanguageNames.ts b/src/components/hooks/useLanguageNames.ts
index d00b0968c..8c28d5606 100644
--- a/src/components/hooks/useLanguageNames.ts
+++ b/src/components/hooks/useLanguageNames.ts
@@ -10,7 +10,7 @@ export function useLanguageNames(locale) {
const [list, setList] = useState(languageNames[locale] || enUS);
async function loadData(locale) {
- const data = await httpGet(`${process.env.basePath || ''}/intl/language/${locale}.json`);
+ const { data } = await httpGet(`${process.env.basePath || ''}/intl/language/${locale}.json`);
if (data) {
languageNames[locale] = data;
diff --git a/src/components/hooks/useLocale.ts b/src/components/hooks/useLocale.ts
index 30f780376..863b20a54 100644
--- a/src/components/hooks/useLocale.ts
+++ b/src/components/hooks/useLocale.ts
@@ -20,7 +20,9 @@ export function useLocale() {
const dateLocale = getDateLocale(locale);
async function loadMessages(locale: string) {
- messages[locale] = await httpGet(`${process.env.basePath || ''}/intl/messages/${locale}.json`);
+ const { data } = await httpGet(`${process.env.basePath || ''}/intl/messages/${locale}.json`);
+
+ messages[locale] = data;
}
async function saveLocale(value: string) {
diff --git a/src/components/metrics/MetricsTable.tsx b/src/components/metrics/MetricsTable.tsx
index 33b457b57..616262cb0 100644
--- a/src/components/metrics/MetricsTable.tsx
+++ b/src/components/metrics/MetricsTable.tsx
@@ -72,7 +72,7 @@ export function MetricsTable({
return filter(arr);
}, items);
} else {
- items = dataFilter(data);
+ items = dataFilter(items);
}
}
diff --git a/src/lib/clickhouse.ts b/src/lib/clickhouse.ts
index 3c8b7bbfb..7b6291c0c 100644
--- a/src/lib/clickhouse.ts
+++ b/src/lib/clickhouse.ts
@@ -2,7 +2,7 @@ import { ClickHouseClient, createClient } from '@clickhouse/client';
import { formatInTimeZone } from 'date-fns-tz';
import debug from 'debug';
import { CLICKHOUSE } from '@/lib/db';
-import { getWebsite } from '@/queries/index';
+import { getWebsite } from '@/queries';
import { DEFAULT_PAGE_SIZE, OPERATORS } from './constants';
import { maxDate } from './date';
import { filtersToArray } from './params';
diff --git a/src/lib/fetch.ts b/src/lib/fetch.ts
index 754da56f1..ee9e160f8 100644
--- a/src/lib/fetch.ts
+++ b/src/lib/fetch.ts
@@ -1,6 +1,18 @@
import { buildUrl } from '@/lib/url';
-export async function request(method: string, url: string, body?: string, headers: object = {}) {
+export interface FetchResponse {
+ ok: boolean;
+ status: number;
+ data?: any;
+ error?: any;
+}
+
+export async function request(
+ method: string,
+ url: string,
+ body?: string,
+ headers: object = {},
+): Promise<FetchResponse> {
return fetch(url, {
method,
cache: 'no-cache',
@@ -10,7 +22,16 @@ export async function request(method: string, url: string, body?: string, header
...headers,
},
body,
- }).then(res => res.json());
+ }).then(async res => {
+ const data = await res.json();
+
+ return {
+ ok: res.ok,
+ status: res.status,
+ data: res.ok ? data : undefined,
+ error: res.ok ? undefined : data,
+ };
+ });
}
export async function httpGet(url: string, params: object = {}, headers: object = {}) {
diff --git a/src/queries/sql/getRealtimeData.ts b/src/queries/sql/getRealtimeData.ts
index e07dfc311..a9b06814c 100644
--- a/src/queries/sql/getRealtimeData.ts
+++ b/src/queries/sql/getRealtimeData.ts
@@ -1,4 +1,4 @@
-import { getPageviewStats, getRealtimeActivity, getSessionStats } from '@/queries/index';
+import { getPageviewStats, getRealtimeActivity, getSessionStats } from '@/queries';
function increment(data: object, key: string) {
if (key) {