Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

feat: embedder hooks — CLIENT_DIST/FREEAPI_ENV_PATH overrides + desktop-mode navbar

Tashfeenf42249b16e7b
diff --git a/client/src/App.tsx b/client/src/App.tsx
index ff85f89b..948c44f9 100644
--- a/client/src/App.tsx
+++ b/client/src/App.tsx
@@ -94,6 +94,11 @@ function Brand() {
)
}
+// True when the dashboard runs inside the desktop shell (Electron preload
+// sets this). The navbar then doubles as the window title bar: draggable,
+// padded for the macOS traffic lights, and without the web-only Sign out.
+const isDesktopApp = typeof window !== 'undefined' && (window as any).__FREEAPI_DESKTOP__ === true
+
function Navbar() {
const { dark, toggle } = useDarkMode()
const location = useLocation()
@@ -104,21 +109,35 @@ function Navbar() {
}
return (
- <header className="sticky top-0 z-40 border-b bg-background/80 backdrop-blur">
- <div className="mx-auto flex max-w-6xl items-center px-4 sm:px-6">
+ <header
+ className="sticky top-0 z-40 border-b bg-background/80 backdrop-blur"
+ style={isDesktopApp ? ({ WebkitAppRegion: 'drag' } as React.CSSProperties) : undefined}
+ >
+ <div
+ className={`mx-auto flex max-w-6xl items-center px-4 sm:px-6 ${isDesktopApp ? 'pl-20 sm:pl-20' : ''}`}
+ style={isDesktopApp ? { minHeight: 52 } : undefined}
+ >
<Brand />
- <nav className="ml-10 hidden items-center gap-6 md:flex">
+ <nav
+ className="ml-10 hidden items-center gap-6 md:flex"
+ style={isDesktopApp ? ({ WebkitAppRegion: 'no-drag' } as React.CSSProperties) : undefined}
+ >
{navItems.map((item) => (
<NavItem key={item.to} to={item.to}>
{item.label}
</NavItem>
))}
</nav>
- <div className="ml-auto hidden items-center gap-1 md:flex">
+ <div
+ className="ml-auto hidden items-center gap-1 md:flex"
+ style={isDesktopApp ? ({ WebkitAppRegion: 'no-drag' } as React.CSSProperties) : undefined}
+ >
<DarkModeToggle dark={dark} onToggle={toggle} />
- <Button variant="ghost" size="sm" onClick={() => logout()}>
- Sign out
- </Button>
+ {!isDesktopApp && (
+ <Button variant="ghost" size="sm" onClick={() => logout()}>
+ Sign out
+ </Button>
+ )}
</div>
<div className="ml-auto md:hidden">
<DropdownMenu>
@@ -146,7 +165,9 @@ function Navbar() {
<span>Theme</span>
{dark ? <Sun /> : <Moon />}
</DropdownMenuItem>
- <DropdownMenuItem onClick={() => logout()}>Sign out</DropdownMenuItem>
+ {!isDesktopApp && (
+ <DropdownMenuItem onClick={() => logout()}>Sign out</DropdownMenuItem>
+ )}
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
diff --git a/server/src/__tests__/routes/static-client.test.ts b/server/src/__tests__/routes/static-client.test.ts
new file mode 100644
index 00000000..4d28f548
--- /dev/null
+++ b/server/src/__tests__/routes/static-client.test.ts
@@ -0,0 +1,39 @@
+import { describe, it, expect, beforeAll, afterAll } from 'vitest';
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import { createApp } from '../../app.js';
+import { initDb } from '../../db/index.js';
+
+// CLIENT_DIST relocates the built dashboard for embedders (the desktop app
+// ships client/dist in extraResources, out of reach of the default
+// __dirname-relative path).
+describe('CLIENT_DIST override', () => {
+ let tmpDir: string;
+
+ beforeAll(() => {
+ process.env.ENCRYPTION_KEY = '0'.repeat(64);
+ initDb(':memory:');
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'freeapi-client-dist-'));
+ fs.writeFileSync(path.join(tmpDir, 'index.html'), '<!doctype html><title>custom-dist</title>');
+ });
+
+ afterAll(() => {
+ delete process.env.CLIENT_DIST;
+ fs.rmSync(tmpDir, { recursive: true, force: true });
+ });
+
+ it('serves the SPA from the overridden directory', async () => {
+ process.env.CLIENT_DIST = tmpDir;
+ const app = createApp();
+ const server = app.listen(0);
+ const port = (server.address() as any).port;
+
+ const res = await fetch(`http://127.0.0.1:${port}/`);
+ const html = await res.text();
+ server.close();
+
+ expect(res.status).toBe(200);
+ expect(html).toContain('custom-dist');
+ });
+});
diff --git a/server/src/app.ts b/server/src/app.ts
index 355f649c..7e67a06e 100644
--- a/server/src/app.ts
+++ b/server/src/app.ts
@@ -82,8 +82,12 @@ export function createApp() {
// Error handler (for API routes)
app.use(errorHandler);
- // Serve client static files (after API error handler)
- const clientDist = path.resolve(__dirname, '../../client/dist');
+ // Serve client static files (after API error handler). CLIENT_DIST lets
+ // embedders relocate the built dashboard (e.g. the desktop app ships it in
+ // extraResources, where the __dirname-relative path can't reach).
+ const clientDist = process.env.CLIENT_DIST
+ ? path.resolve(process.env.CLIENT_DIST)
+ : path.resolve(__dirname, '../../client/dist');
app.use(express.static(clientDist));
// SPA fallback — serve index.html for non-API routes
app.use((req, res, next) => {
diff --git a/server/src/env.ts b/server/src/env.ts
index 37748220..d6ea3e51 100644
--- a/server/src/env.ts
+++ b/server/src/env.ts
@@ -4,4 +4,7 @@ import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
-dotenv.config({ path: path.resolve(__dirname, '../../.env') });
+// FREEAPI_ENV_PATH lets embedders (e.g. the desktop app, where __dirname sits
+// inside a bundle) point at an explicit .env — or at nothing: dotenv silently
+// no-ops on a missing file either way.
+dotenv.config({ path: process.env.FREEAPI_ENV_PATH ?? path.resolve(__dirname, '../../.env') });