fixes the cmd prompt on start + api key defaults
This commit is contained in:
@@ -1,21 +1,41 @@
|
||||
import type { AppSettings } from '@/types/api';
|
||||
|
||||
const defaults: AppSettings = {
|
||||
backendUrl: 'http://localhost:8080',
|
||||
apiKey: 'dev-api-key',
|
||||
backendUrl: 'http://146.59.230.190:8080',
|
||||
apiKey:
|
||||
'b8184608fcab419da2ce9a220ee6017daaff637b627e43ebac31b6b7c3344349',
|
||||
};
|
||||
|
||||
const SETTINGS_KEY =
|
||||
'lr-openwrt-tool.settings';
|
||||
|
||||
function joinUrl(
|
||||
baseUrl: string,
|
||||
path: string,
|
||||
) {
|
||||
return `${baseUrl.replace(/\/+$/, '')}/${path.replace(
|
||||
/^\/+/,
|
||||
'',
|
||||
)}`;
|
||||
}
|
||||
|
||||
export function getSettings(): AppSettings {
|
||||
try {
|
||||
const storedSettings =
|
||||
localStorage.getItem(SETTINGS_KEY);
|
||||
|
||||
const parsed =
|
||||
JSON.parse(storedSettings || '{}');
|
||||
|
||||
return {
|
||||
...defaults,
|
||||
...JSON.parse(storedSettings || '{}'),
|
||||
...parsed,
|
||||
backendUrl:
|
||||
parsed.backendUrl?.trim() ||
|
||||
defaults.backendUrl,
|
||||
apiKey:
|
||||
parsed.apiKey?.trim() ||
|
||||
defaults.apiKey,
|
||||
};
|
||||
} catch {
|
||||
return defaults;
|
||||
@@ -27,18 +47,32 @@ export function saveSettings(
|
||||
) {
|
||||
localStorage.setItem(
|
||||
SETTINGS_KEY,
|
||||
JSON.stringify(settings),
|
||||
JSON.stringify({
|
||||
...settings,
|
||||
backendUrl:
|
||||
settings.backendUrl.trim(),
|
||||
apiKey: settings.apiKey.trim(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function resetSettings() {
|
||||
localStorage.removeItem(SETTINGS_KEY);
|
||||
}
|
||||
|
||||
export async function apiRequest<T>(
|
||||
path: string,
|
||||
init: RequestInit = {},
|
||||
): Promise<T> {
|
||||
const settings = getSettings();
|
||||
|
||||
console.log(
|
||||
'[API]',
|
||||
joinUrl(settings.backendUrl, path),
|
||||
);
|
||||
|
||||
const response = await fetch(
|
||||
`${settings.backendUrl}${path}`,
|
||||
joinUrl(settings.backendUrl, path),
|
||||
{
|
||||
...init,
|
||||
headers: {
|
||||
|
||||
Reference in New Issue
Block a user