Commit 037d854d authored by Hendrik Garske's avatar Hendrik Garske

fix: TypeScript-Fehler in wg-api.ts behoben

parent 85cc2200
...@@ -56,9 +56,9 @@ export class WgApiClient { ...@@ -56,9 +56,9 @@ export class WgApiClient {
private async request<T>(endpoint: string, options: RequestInit = {}): Promise<T> { private async request<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
const url = `${this.baseUrl}${endpoint}` const url = `${this.baseUrl}${endpoint}`
const headers = { const headers: Record<string, string> = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
...options.headers, ...(options.headers as Record<string, string> || {}),
} }
// Add basic auth if password is set // Add basic auth if password is set
...@@ -80,7 +80,24 @@ export class WgApiClient { ...@@ -80,7 +80,24 @@ export class WgApiClient {
} }
async getStats(): Promise<WgStats> { async getStats(): Promise<WgStats> {
return this.request<WgStats>('/api/sessions') try {
return await this.request<WgStats>('/api/sessions')
} catch (error) {
// If API is not available, return empty stats
console.warn('WG API not available, returning empty stats:', error)
return {
clients: [],
server: {
address: '',
listenPort: 0,
publicKey: '',
endpoint: '',
dns: '',
allowedIPs: '',
createdAt: new Date().toISOString(),
},
}
}
} }
async getClients(): Promise<WgClient[]> { async getClients(): Promise<WgClient[]> {
...@@ -119,10 +136,13 @@ export class WgApiClient { ...@@ -119,10 +136,13 @@ export class WgApiClient {
} }
async getClientConfig(id: string): Promise<string> { async getClientConfig(id: string): Promise<string> {
const headers: Record<string, string> = {}
if (this.password) {
headers['Authorization'] = `Basic ${Buffer.from(`:${this.password}`).toString('base64')}`
}
const response = await fetch(`${this.baseUrl}/api/users/${id}/configuration`, { const response = await fetch(`${this.baseUrl}/api/users/${id}/configuration`, {
headers: this.password ? { headers,
'Authorization': `Basic ${Buffer.from(`:${this.password}`).toString('base64')}`
} : {},
}) })
if (!response.ok) { if (!response.ok) {
...@@ -133,10 +153,13 @@ export class WgApiClient { ...@@ -133,10 +153,13 @@ export class WgApiClient {
} }
async getClientQR(id: string): Promise<string> { async getClientQR(id: string): Promise<string> {
const headers: Record<string, string> = {}
if (this.password) {
headers['Authorization'] = `Basic ${Buffer.from(`:${this.password}`).toString('base64')}`
}
const response = await fetch(`${this.baseUrl}/api/users/${id}/qr-code`, { const response = await fetch(`${this.baseUrl}/api/users/${id}/qr-code`, {
headers: this.password ? { headers,
'Authorization': `Basic ${Buffer.from(`:${this.password}`).toString('base64')}`
} : {},
}) })
if (!response.ok) { if (!response.ok) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment