diff --git a/package.json b/package.json index 066095c..3e3e6cc 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "lint": "biome check .", "check": "biome check --write .", "format": "biome format --write .", - "build": "bun build ./src/index.html --outdir=dist --sourcemap --target=browser --minify --define:process.env.NODE_ENV='\"production\"' --env='BUN_PUBLIC_*'", + "build": "bun build ./src/index.html --outdir=dist --sourcemap --target=browser --minify --define:process.env.NODE_ENV='\"production\"' --env='VITE_*'", "start": "NODE_ENV=production bun src/index.ts" }, "dependencies": { diff --git a/src/frontend.tsx b/src/frontend.tsx index 3660ea5..ed6a42d 100644 --- a/src/frontend.tsx +++ b/src/frontend.tsx @@ -13,6 +13,7 @@ import { Inspector } from "react-dev-inspector"; import { createRoot } from "react-dom/client"; import { routeTree } from "./routeTree.gen"; import { ModalsProvider } from "@mantine/modals"; +import { VITE_PUBLIC_URL } from "./utils/env"; // Create a new router instance export const router = createRouter({ @@ -39,11 +40,12 @@ const elem = document.getElementById("root")!; const app = ( { - if (!e.codeInfo) return; - - const url = import.meta.env.VITE_PUBLIC_URL; - fetch(`${url}/__open-in-editor`, { + onClickElement={(e) => { + if (!e.codeInfo) return; + + const url = VITE_PUBLIC_URL; + fetch(`${url}/__open-in-editor`, { + method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ diff --git a/src/index.ts b/src/index.ts index f8dd950..3c90f91 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,10 +15,8 @@ const api = new Elysia({ prefix: "/api", }) .all("/auth/*", ({ request }) => auth.handler(request)) - .use(cors()); - -if (!isProduction) { - api.use( + .use(cors()) + .use( swagger({ path: "/docs", documentation: { @@ -28,10 +26,7 @@ if (!isProduction) { }, }, }), - ); -} - -api + ) .get("/session", async ({ request }) => { const data = await auth.api.getSession({ headers: request.headers }); return { data }; @@ -191,4 +186,4 @@ console.log( `🚀 Server running at http://localhost:3000 in ${isProduction ? "production" : "development"} mode`, ); -export type ApiApp = typeof app; +export type ApiApp = typeof app; \ No newline at end of file diff --git a/src/middleware/authMiddleware.tsx b/src/middleware/authMiddleware.tsx index 3e71e30..021e26e 100644 --- a/src/middleware/authMiddleware.tsx +++ b/src/middleware/authMiddleware.tsx @@ -1,4 +1,5 @@ import { redirect } from "@tanstack/react-router"; +import { VITE_PUBLIC_URL } from "../utils/env"; /* ================================ * Types @@ -21,7 +22,7 @@ type SessionResponse = { async function fetchSession(): Promise { try { - const baseURL = import.meta.env.VITE_PUBLIC_URL || window.location.origin; + const baseURL = VITE_PUBLIC_URL || window.location.origin; const res = await fetch(`${baseURL}/api/session`, { method: "GET", credentials: "include", diff --git a/src/utils/auth-client.ts b/src/utils/auth-client.ts index 0505bb8..a352651 100644 --- a/src/utils/auth-client.ts +++ b/src/utils/auth-client.ts @@ -1,7 +1,8 @@ import { createAuthClient } from "better-auth/react"; +import { VITE_PUBLIC_URL } from "./env"; export const authClient = createAuthClient({ - baseURL: import.meta.env.VITE_PUBLIC_URL || "http://localhost:3000", + baseURL: VITE_PUBLIC_URL, }); export const { useSession, signIn, signOut, signUp, getSession } = authClient; diff --git a/src/utils/env.ts b/src/utils/env.ts new file mode 100644 index 0000000..466bd4d --- /dev/null +++ b/src/utils/env.ts @@ -0,0 +1,23 @@ +/** + * Safely access environment variables across different runtimes and builders. + * Supports Vite's import.meta.env and Bun's process.env (used in bun build). + */ +export const getEnv = (key: string, defaultValue = ""): string => { + // 1. Try Vite's import.meta.env + try { + if (typeof import.meta.env !== "undefined" && import.meta.env[key]) { + return import.meta.env[key]; + } + } catch {} + + // 2. Try process.env (injected by bun build --env) + try { + if (typeof process !== "undefined" && process.env[key]) { + return process.env[key]; + } + } catch {} + + return defaultValue; +}; + +export const VITE_PUBLIC_URL = getEnv("VITE_PUBLIC_URL", "http://localhost:3000");