From 7133409c04b191effb62c33065c4ba4bbb67de83 Mon Sep 17 00:00:00 2001 From: davidels1 <87242587+davidels1@users.noreply.github.com> Date: Thu, 25 Sep 2025 01:37:21 +0200 Subject: [PATCH] Update PocketBase URL handling and environment type definitions --- src/lib/pocketbase.ts | 23 +++++++++++++++++++++-- src/vite-env.d.ts | 1 + 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/lib/pocketbase.ts b/src/lib/pocketbase.ts index 8b8e3c8..1a4ac9b 100644 --- a/src/lib/pocketbase.ts +++ b/src/lib/pocketbase.ts @@ -1,10 +1,29 @@ import PocketBase from 'pocketbase'; -// Use env for PocketBase URL (falls back to local dev) -const PB_URL = import.meta.env.VITE_PB_URL || 'pb.jeistk.apphub.co.za'; +// Derive PocketBase URL; add protocol if missing, default to https in production +function buildPbUrl(raw?: string): string { + let url = (raw || '').trim(); + if (!url) { + return (import.meta.env.DEV ? 'http://127.0.0.1:8090' : 'https://pb.jeistk.apphub.co.za'); + } + if (!/^https?:\/\//i.test(url)) { + // If looks like localhost or 127.* assume http, else https + if (/^(localhost|127\.|10\.|192\.168\.)/i.test(url)) { + url = 'http://' + url; + } else { + url = 'https://' + url; + } + } + return url.replace(/\/$/, ''); +} +const PB_URL = buildPbUrl(import.meta.env.VITE_PB_URL); const pb = new PocketBase(PB_URL); +// Optional: expose resolved URL for diagnostics +// @ts-ignore +pb.__resolvedUrl = PB_URL; + // Persist auth in localStorage (default behavior of SDK) // Do NOT auto-auth as admin here; let the app use user auth via AuthContext diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 9675507..239f89c 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -7,6 +7,7 @@ interface ImportMetaEnv { readonly PROD: boolean readonly DEV: boolean readonly VITE_APP_TITLE?: string + readonly VITE_PB_URL?: string // more env variables... }