Skip to content

Commit 3f69bfd

Browse files
committed
refactor(web): replace instances of BACKEND_URL with buildBackendUrl for improved URL handling
1 parent 371ff86 commit 3f69bfd

21 files changed

Lines changed: 98 additions & 95 deletions

File tree

surfsense_web/app/(home)/login/GoogleLoginButton.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useTranslations } from "next-intl";
33
import { useState } from "react";
44
import { Logo } from "@/components/Logo";
55
import { Button } from "@/components/ui/button";
6-
import { BACKEND_URL } from "@/lib/env-config";
6+
import { buildBackendUrl } from "@/lib/env-config";
77
import { trackLoginAttempt } from "@/lib/posthog/events";
88
import { AmbientBackground } from "./AmbientBackground";
99

@@ -51,7 +51,7 @@ export function GoogleLoginButton() {
5151
// cross-origin fetch requests may not be sent on subsequent redirects.
5252
// The authorize-redirect endpoint does a server-side redirect to Google
5353
// and sets the CSRF cookie properly for same-site context.
54-
window.location.href = `${BACKEND_URL}/auth/google/authorize-redirect`;
54+
window.location.href = buildBackendUrl("/auth/google/authorize-redirect");
5555
};
5656
return (
5757
<div className="relative w-full overflow-hidden">

surfsense_web/app/dashboard/[search_space_id]/user-settings/components/MessagingChannelsContent.tsx

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { Skeleton } from "@/components/ui/skeleton";
1919
import type { SearchSpace } from "@/contracts/types/search-space.types";
2020
import { searchSpacesApiService } from "@/lib/apis/search-spaces-api.service";
2121
import { authenticatedFetch } from "@/lib/auth-utils";
22-
import { BACKEND_URL } from "@/lib/env-config";
22+
import { buildBackendUrl } from "@/lib/env-config";
2323
import { cn } from "@/lib/utils";
2424

2525
type GatewayConnection = {
@@ -82,13 +82,14 @@ export function MessagingChannelsContent() {
8282
const discordGatewayEnabled = gatewayConfig?.discord_enabled ?? false;
8383

8484
const fetchConnections = useCallback(async (platform?: GatewayPlatform) => {
85-
const query = platform ? `?platform=${encodeURIComponent(platform)}` : "";
86-
const res = await authenticatedFetch(`${BACKEND_URL}/api/v1/gateway/connections${query}`);
85+
const res = await authenticatedFetch(
86+
buildBackendUrl("/api/v1/gateway/connections", platform ? { platform } : undefined)
87+
);
8788
return (await res.json()) as GatewayConnection[];
8889
}, []);
8990

9091
const fetchGatewayConfig = useCallback(async () => {
91-
const res = await authenticatedFetch(`${BACKEND_URL}/api/v1/gateway/config`);
92+
const res = await authenticatedFetch(buildBackendUrl("/api/v1/gateway/config"));
9293
return (await res.json()) as GatewayConfig;
9394
}, []);
9495

@@ -125,7 +126,9 @@ export function MessagingChannelsContent() {
125126

126127
const refreshBaileysHealth = useCallback(async () => {
127128
if (whatsappMode !== "baileys") return;
128-
const res = await authenticatedFetch(`${BACKEND_URL}/api/v1/gateway/whatsapp/baileys/health`);
129+
const res = await authenticatedFetch(
130+
buildBackendUrl("/api/v1/gateway/whatsapp/baileys/health")
131+
);
129132
if (!res.ok) return;
130133
const data = (await res.json()) as BaileysHealth;
131134
setBaileysHealth(data);
@@ -136,7 +139,7 @@ export function MessagingChannelsContent() {
136139
}, [refreshBaileysHealth]);
137140

138141
async function startPairing(platform: PairingPlatform) {
139-
const res = await authenticatedFetch(`${BACKEND_URL}/api/v1/gateway/bindings/start`, {
142+
const res = await authenticatedFetch(buildBackendUrl("/api/v1/gateway/bindings/start"), {
140143
method: "POST",
141144
headers: { "Content-Type": "application/json" },
142145
body: JSON.stringify({ platform, search_space_id: searchSpaceId }),
@@ -148,7 +151,7 @@ export function MessagingChannelsContent() {
148151

149152
async function installSlackGateway() {
150153
const res = await authenticatedFetch(
151-
`${BACKEND_URL}/api/v1/gateway/slack/install?search_space_id=${searchSpaceId}`
154+
buildBackendUrl("/api/v1/gateway/slack/install", { search_space_id: searchSpaceId })
152155
);
153156
if (!res.ok) return;
154157
const data = (await res.json()) as { auth_url?: string };
@@ -159,7 +162,7 @@ export function MessagingChannelsContent() {
159162

160163
async function installDiscordGateway() {
161164
const res = await authenticatedFetch(
162-
`${BACKEND_URL}/api/v1/gateway/discord/install?search_space_id=${searchSpaceId}`
165+
buildBackendUrl("/api/v1/gateway/discord/install", { search_space_id: searchSpaceId })
163166
);
164167
if (!res.ok) return;
165168
const data = (await res.json()) as { auth_url?: string };
@@ -181,8 +184,8 @@ export function MessagingChannelsContent() {
181184
async function revoke(connection: GatewayConnection) {
182185
const url =
183186
connection.route_type === "account" && connection.account_id
184-
? `${BACKEND_URL}/api/v1/gateway/accounts/${connection.account_id}`
185-
: `${BACKEND_URL}/api/v1/gateway/bindings/${connection.id}`;
187+
? buildBackendUrl(`/api/v1/gateway/accounts/${connection.account_id}`)
188+
: buildBackendUrl(`/api/v1/gateway/bindings/${connection.id}`);
186189
await authenticatedFetch(url, {
187190
method: "DELETE",
188191
});
@@ -205,8 +208,8 @@ export function MessagingChannelsContent() {
205208
);
206209
const url =
207210
connection.route_type === "account" && connection.account_id
208-
? `${BACKEND_URL}/api/v1/gateway/accounts/${connection.account_id}/search-space`
209-
: `${BACKEND_URL}/api/v1/gateway/bindings/${connection.id}/search-space`;
211+
? buildBackendUrl(`/api/v1/gateway/accounts/${connection.account_id}/search-space`)
212+
: buildBackendUrl(`/api/v1/gateway/bindings/${connection.id}/search-space`);
210213
const res = await authenticatedFetch(url, {
211214
method: "PATCH",
212215
headers: { "Content-Type": "application/json" },
@@ -222,9 +225,12 @@ export function MessagingChannelsContent() {
222225
}
223226

224227
async function resume(connection: GatewayConnection) {
225-
await authenticatedFetch(`${BACKEND_URL}/api/v1/gateway/bindings/${connection.id}/resume`, {
226-
method: "POST",
227-
});
228+
await authenticatedFetch(
229+
buildBackendUrl(`/api/v1/gateway/bindings/${connection.id}/resume`),
230+
{
231+
method: "POST",
232+
}
233+
);
228234
await refreshPlatform(connection.platform as GatewayPlatform);
229235
}
230236

surfsense_web/app/desktop/login/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { Spinner } from "@/components/ui/spinner";
1818
import { useElectronAPI } from "@/hooks/use-platform";
1919
import { searchSpacesApiService } from "@/lib/apis/search-spaces-api.service";
2020
import { setBearerToken } from "@/lib/auth-utils";
21-
import { BACKEND_URL } from "@/lib/env-config";
21+
import { buildBackendUrl } from "@/lib/env-config";
2222

2323
type ShortcutKey = "generalAssist" | "quickAsk" | "screenshotAssist";
2424
type ShortcutMap = typeof DEFAULT_SHORTCUTS;
@@ -240,7 +240,7 @@ export default function DesktopLoginPage() {
240240
const handleGoogleLogin = () => {
241241
if (isGoogleRedirecting) return;
242242
setIsGoogleRedirecting(true);
243-
window.location.href = `${BACKEND_URL}/auth/google/authorize-redirect`;
243+
window.location.href = buildBackendUrl("/auth/google/authorize-redirect");
244244
};
245245

246246
const autoSetSearchSpace = async () => {

surfsense_web/components/assistant-ui/connector-popup/connect-forms/components/obsidian-connect-form.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
66
import { Button } from "@/components/ui/button";
77
import { EnumConnectorName } from "@/contracts/enums/connector";
88
import { useApiKey } from "@/hooks/use-api-key";
9-
import { BACKEND_URL } from "@/lib/env-config";
109
import { getConnectorBenefits } from "../connector-benefits";
1110
import type { ConnectFormProps } from "../index";
1211

surfsense_web/components/assistant-ui/connector-popup/connector-configs/components/circleback-config.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Button } from "@/components/ui/button";
99
import { Input } from "@/components/ui/input";
1010
import { Label } from "@/components/ui/label";
1111
import { authenticatedFetch } from "@/lib/auth-utils";
12-
import { BACKEND_URL } from "@/lib/env-config";
12+
import { buildBackendUrl } from "@/lib/env-config";
1313
import type { ConnectorConfigProps } from "../index";
1414
export interface CirclebackConfigProps extends ConnectorConfigProps {
1515
onNameChange?: (name: string) => void;
@@ -42,12 +42,10 @@ export const CirclebackConfig: FC<CirclebackConfigProps> = ({ connector, onNameC
4242
const doFetch = async () => {
4343
if (!connector.search_space_id) return;
4444

45-
const baseUrl = BACKEND_URL;
46-
4745
setIsLoading(true);
4846
try {
4947
const response = await authenticatedFetch(
50-
`${baseUrl}/api/v1/webhooks/circleback/${connector.search_space_id}/info`,
48+
buildBackendUrl(`/api/v1/webhooks/circleback/${connector.search_space_id}/info`),
5149
{ signal: controller.signal }
5250
);
5351
if (controller.signal.aborted) return;

surfsense_web/components/assistant-ui/connector-popup/hooks/use-connector-dialog.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type { SearchSourceConnector } from "@/contracts/types/connector.types";
1616
import { searchSourceConnector } from "@/contracts/types/connector.types";
1717
import { OAUTH_RESULT_COOKIE, parseOAuthCallbackResult } from "@/contracts/types/oauth.types";
1818
import { authenticatedFetch } from "@/lib/auth-utils";
19-
import { BACKEND_URL } from "@/lib/env-config";
19+
import { buildBackendUrl } from "@/lib/env-config";
2020
import {
2121
trackConnectorConnected,
2222
trackConnectorDeleted,
@@ -351,9 +351,7 @@ export const useConnectorDialog = () => {
351351
trackConnectorSetupStarted(Number(searchSpaceId), connector.connectorType, "oauth_click");
352352

353353
try {
354-
// Check if authEndpoint already has query parameters
355-
const separator = connector.authEndpoint.includes("?") ? "&" : "?";
356-
const url = `${BACKEND_URL}${connector.authEndpoint}${separator}space_id=${searchSpaceId}`;
354+
const url = buildBackendUrl(connector.authEndpoint, { space_id: searchSpaceId });
357355

358356
const response = await authenticatedFetch(url, { method: "GET" });
359357

surfsense_web/components/documents/download-original-button.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Button } from "@/components/ui/button";
77
import { Spinner } from "@/components/ui/spinner";
88
import { documentsApiService } from "@/lib/apis/documents-api.service";
99
import { authenticatedFetch } from "@/lib/auth-utils";
10-
import { BACKEND_URL } from "@/lib/env-config";
10+
import { buildBackendUrl } from "@/lib/env-config";
1111

1212
interface DownloadOriginalButtonProps {
1313
documentId: number;
@@ -41,7 +41,7 @@ export function DownloadOriginalButton({ documentId }: DownloadOriginalButtonPro
4141
setDownloading(true);
4242
try {
4343
const response = await authenticatedFetch(
44-
`${BACKEND_URL}/api/v1/documents/${documentId}/download-original`,
44+
buildBackendUrl(`/api/v1/documents/${documentId}/download-original`),
4545
{ method: "GET" }
4646
);
4747
if (!response.ok) throw new Error("Download failed");

surfsense_web/components/editor-panel/memory.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import { authenticatedFetch } from "@/lib/auth-utils";
4-
import { BACKEND_URL } from "@/lib/env-config";
4+
import { buildBackendUrl } from "@/lib/env-config";
55

66
export type MemoryScope = "user" | "team";
77

@@ -30,10 +30,6 @@ function getMemoryPath(scope: MemoryScope, searchSpaceId?: number | null) {
3030
return `/api/v1/searchspaces/${searchSpaceId}/memory`;
3131
}
3232

33-
function getBackendUrl(path: string) {
34-
return `${BACKEND_URL}${path}`;
35-
}
36-
3733
export function getMemoryLimitState(length: number, limits?: MemoryLimits | null) {
3834
if (!limits) {
3935
return {
@@ -66,7 +62,7 @@ export async function fetchMemoryEditorDocument({
6662
title?: string | null;
6763
signal?: AbortSignal;
6864
}) {
69-
const response = await authenticatedFetch(getBackendUrl(getMemoryPath(scope, searchSpaceId)), {
65+
const response = await authenticatedFetch(buildBackendUrl(getMemoryPath(scope, searchSpaceId)), {
7066
method: "GET",
7167
signal,
7268
});
@@ -98,7 +94,7 @@ export async function saveMemoryMarkdown({
9894
searchSpaceId?: number | null;
9995
markdown: string;
10096
}) {
101-
const response = await authenticatedFetch(getBackendUrl(getMemoryPath(scope, searchSpaceId)), {
97+
const response = await authenticatedFetch(buildBackendUrl(getMemoryPath(scope, searchSpaceId)), {
10298
method: "PUT",
10399
headers: { "Content-Type": "application/json" },
104100
body: JSON.stringify({ memory_md: markdown }),

surfsense_web/components/free-chat/anonymous-chat.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Button } from "@/components/ui/button";
66
import type { AnonModel, AnonQuotaResponse } from "@/contracts/types/anonymous-chat.types";
77
import { anonymousChatApiService } from "@/lib/apis/anonymous-chat-api.service";
88
import { readSSEStream } from "@/lib/chat/streaming-state";
9-
import { BACKEND_URL } from "@/lib/env-config";
9+
import { buildBackendUrl } from "@/lib/env-config";
1010
import { trackAnonymousChatMessageSent } from "@/lib/posthog/events";
1111
import { cn } from "@/lib/utils";
1212
import { QuotaBar } from "./quota-bar";
@@ -81,7 +81,7 @@ export function AnonymousChat({ model }: AnonymousChatProps) {
8181
content: m.content,
8282
}));
8383

84-
const response = await fetch(`${BACKEND_URL}/api/v1/public/anon-chat/stream`, {
84+
const response = await fetch(buildBackendUrl("/api/v1/public/anon-chat/stream"), {
8585
method: "POST",
8686
headers: { "Content-Type": "application/json" },
8787
credentials: "include",

surfsense_web/components/free-chat/free-chat-page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
updateThinkingSteps,
3434
updateToolCall,
3535
} from "@/lib/chat/streaming-state";
36-
import { BACKEND_URL } from "@/lib/env-config";
36+
import { buildBackendUrl } from "@/lib/env-config";
3737
import { trackAnonymousChatMessageSent } from "@/lib/posthog/events";
3838
import { FreeThread } from "./free-thread";
3939
import { RemoveAdsBanner } from "./remove-ads-banner";
@@ -176,7 +176,7 @@ export function FreeChatPage() {
176176
if (!webSearchEnabled) reqBody.disabled_tools = ["web_search"];
177177
if (turnstileToken) reqBody.turnstile_token = turnstileToken;
178178

179-
const response = await fetch(`${BACKEND_URL}/api/v1/public/anon-chat/stream`, {
179+
const response = await fetch(buildBackendUrl("/api/v1/public/anon-chat/stream"), {
180180
method: "POST",
181181
headers: { "Content-Type": "application/json" },
182182
credentials: "include",

0 commit comments

Comments
 (0)