Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor authedFetch to support query parameters and enhance API calls

Bill Yangc14c2e379214
diff --git a/client/src/api/analytics/useGetOverview.ts b/client/src/api/analytics/useGetOverview.ts
index c5a3c78a7..f6c4c58b6 100644
--- a/client/src/api/analytics/useGetOverview.ts
+++ b/client/src/api/analytics/useGetOverview.ts
@@ -31,13 +31,14 @@ export function useGetOverview({
queryKey: ["overview", timeToUse, site, filters, past24Hours],
queryFn: () => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
- return authedFetch(
- `${BACKEND_URL}/overview?${startDate ? `startDate=${startDate}&` : ""}${
- endDate ? `endDate=${endDate}&` : ""
- }timezone=${timezone}&site=${site}&filters=${JSON.stringify(filters)}${
- past24Hours ? `&past24Hours=${past24Hours}` : ""
- }`
- ).then((res) => res.json());
+ return authedFetch(`${BACKEND_URL}/overview`, {
+ startDate,
+ endDate,
+ timezone,
+ site,
+ filters,
+ past24Hours,
+ }).then((res) => res.json());
},
staleTime: Infinity,
placeholderData: (_, query: any) => {
diff --git a/client/src/api/analytics/useGetOverviewBucketed.ts b/client/src/api/analytics/useGetOverviewBucketed.ts
index f86aa4b31..5c11eb0af 100644
--- a/client/src/api/analytics/useGetOverviewBucketed.ts
+++ b/client/src/api/analytics/useGetOverviewBucketed.ts
@@ -42,15 +42,15 @@ export function useGetOverviewBucketed({
],
queryFn: () => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
- return authedFetch(
- `${BACKEND_URL}/overview-bucketed?${
- startDate ? `startDate=${startDate}&` : ""
- }${
- endDate ? `endDate=${endDate}&` : ""
- }timezone=${timezone}&bucket=${bucket}&site=${site}&filters=${JSON.stringify(
- filters
- )}${past24Hours ? `&past24Hours=${past24Hours}` : ""}`
- ).then((res) => res.json());
+ return authedFetch(`${BACKEND_URL}/overview-bucketed`, {
+ startDate,
+ endDate,
+ timezone,
+ bucket,
+ site,
+ filters,
+ past24Hours,
+ }).then((res) => res.json());
},
placeholderData: (_, query: any) => {
if (!query?.queryKey) return undefined;
diff --git a/client/src/api/analytics/useSingleCol.ts b/client/src/api/analytics/useSingleCol.ts
index 84cee0174..1cbf48124 100644
--- a/client/src/api/analytics/useSingleCol.ts
+++ b/client/src/api/analytics/useSingleCol.ts
@@ -33,17 +33,16 @@ export function useSingleCol({
queryKey: [parameter, timeToUse, site, filters, limit, type],
queryFn: () => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
- return authedFetch(
- `${BACKEND_URL}/single-col?${
- startDate ? `startDate=${startDate}&` : ""
- }${
- endDate ? `endDate=${endDate}&` : ""
- }timezone=${timezone}&site=${site}&parameter=${parameter}${
- limit ? `&limit=${limit}` : ""
- }${
- useFilters ? `&filters=${JSON.stringify(filters)}` : ""
- }&type=${type}`
- ).then((res) => res.json());
+ return authedFetch(`${BACKEND_URL}/single-col`, {
+ startDate,
+ endDate,
+ timezone,
+ site,
+ parameter,
+ limit,
+ filters: useFilters ? filters : undefined,
+ type,
+ }).then((res) => res.json());
},
staleTime: Infinity,
placeholderData: (_, query: any) => {
diff --git a/client/src/api/analytics/userSessions.ts b/client/src/api/analytics/userSessions.ts
index fcd2db981..b3812fa23 100644
--- a/client/src/api/analytics/userSessions.ts
+++ b/client/src/api/analytics/userSessions.ts
@@ -30,13 +30,13 @@ export function useGetUserSessions(userId: string) {
queryKey: ["user-sessions", userId, time, site, filters],
queryFn: () => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
- return authedFetch(
- `${BACKEND_URL}/user/${userId}/sessions?${
- startDate ? `startDate=${startDate}&` : ""
- }${
- endDate ? `endDate=${endDate}&` : ""
- }timezone=${timezone}&site=${site}&filters=${JSON.stringify(filters)}`
- ).then((res) => res.json());
+ return authedFetch(`${BACKEND_URL}/user/${userId}/sessions`, {
+ startDate,
+ endDate,
+ timezone,
+ site,
+ filters,
+ }).then((res) => res.json());
},
staleTime: Infinity,
});
@@ -64,15 +64,14 @@ export function useGetSessionsInfinite() {
queryKey: ["sessions-infinite", time, site, filters],
queryFn: ({ pageParam = 1 }) => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
- let url = `${BACKEND_URL}/sessions?${
- startDate ? `startDate=${startDate}&` : ""
- }${
- endDate ? `endDate=${endDate}&` : ""
- }timezone=${timezone}&site=${site}&filters=${JSON.stringify(
- filters
- )}&page=${pageParam}`;
-
- return authedFetch(url).then((res) => res.json());
+ return authedFetch(`${BACKEND_URL}/sessions`, {
+ startDate,
+ endDate,
+ timezone,
+ site,
+ filters,
+ page: pageParam,
+ }).then((res) => res.json());
},
initialPageParam: 1,
getNextPageParam: (
diff --git a/client/src/api/utils.ts b/client/src/api/utils.ts
index 115ca2af9..582745451 100644
--- a/client/src/api/utils.ts
+++ b/client/src/api/utils.ts
@@ -32,8 +32,54 @@ export function getStartAndEndDate(time: Time) {
return { startDate: time.day, endDate: time.day };
}
-export async function authedFetch(url: string, opts: RequestInit = {}) {
- return fetch(url, {
+/**
+ * Builds a URL with query parameters from an object
+ * @param url Base URL
+ * @param params Object containing query parameters
+ * @returns URL with query parameters
+ */
+export function buildUrl(url: string, params?: Record<string, any>): string {
+ if (!params) return url;
+
+ const searchParams = new URLSearchParams();
+
+ Object.entries(params).forEach(([key, value]) => {
+ if (value === undefined || value === null) return;
+
+ if (typeof value === "object") {
+ searchParams.append(key, JSON.stringify(value));
+ } else {
+ searchParams.append(key, String(value));
+ }
+ });
+
+ const queryString = searchParams.toString();
+ return queryString ? `${url}?${queryString}` : url;
+}
+
+export async function authedFetch(
+ url: string,
+ paramsOrOpts?: Record<string, any> | RequestInit,
+ opts: RequestInit = {}
+) {
+ // Handle overloaded function signature
+ if (
+ paramsOrOpts &&
+ ("method" in paramsOrOpts ||
+ "headers" in paramsOrOpts ||
+ "body" in paramsOrOpts)
+ ) {
+ // If paramsOrOpts looks like RequestInit, treat it as options
+ return fetch(url, {
+ credentials: "include",
+ ...(paramsOrOpts as RequestInit),
+ });
+ }
+
+ // Otherwise treat it as params
+ const fullUrl = buildUrl(url, paramsOrOpts as Record<string, any>);
+
+ return fetch(fullUrl, {
credentials: "include",
...opts,
});