Files
rk-llama.cpp/tools/ui/src/lib/utils/api-key-validation.ts
T
Aleksander Grygier 1ff0fc1384 ui: Refactor models store, MCP service, and gate logs behind VITE_DEBUG (#23236)
* refactor: Scope console logs to `DEV` + `VITE_DEBUG` env vars

* refactor: skip MCP proxy probe when no server requires it

* refactor: suppress expected disconnect errors during MCP client shutdown

* refactor: Deduplicate requests

* refactor: deduplicate model fetching across ROUTER and MODEL modes

* refactor: Clean up models logic

* chore: Add `.env.example` file

* refactor: replace client-side CORS proxy probe with server status flag

* refactor: Post-review fixes

* test: add vitest client setup with API fetch mocks
2026-05-18 16:09:40 +02:00

50 lines
1.4 KiB
TypeScript

import { base } from '$app/paths';
import { error } from '@sveltejs/kit';
import { browser } from '$app/environment';
import { config } from '$lib/stores/settings.svelte';
/**
* Validates API key by making a request to the server props endpoint
* Throws SvelteKit errors for authentication failures or server issues
*/
export async function validateApiKey(fetch: typeof globalThis.fetch): Promise<void> {
if (!browser) {
return;
}
const apiKey = config().apiKey;
// No API key configured — server doesn't require auth, skip the request entirely.
// The /props endpoint is only protected when the server has API keys configured,
// and in that case the client always has one set (from settings).
if (!apiKey) {
return;
}
try {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`
};
const response = await fetch(`${base}/props`, { headers });
if (!response.ok) {
if (response.status === 401 || response.status === 403) {
throw error(401, 'Access denied');
}
console.warn(`Server responded with status ${response.status} during API key validation`);
return;
}
} catch (err) {
// If it's already a SvelteKit error, re-throw it
if (err && typeof err === 'object' && 'status' in err) {
throw err;
}
// Network or other errors
console.warn('Cannot connect to server for API key validation:', err);
}
}