167 lines
4.9 KiB
TypeScript
167 lines
4.9 KiB
TypeScript
import axios from 'axios';
|
|
|
|
export class BackendService {
|
|
private url: string;
|
|
private token?: string;
|
|
|
|
constructor(url: string, token?: string) {
|
|
this.url = url;
|
|
this.token = token;
|
|
}
|
|
|
|
private get headers() {
|
|
return {
|
|
Authorization: `Bearer ${this.token}`,
|
|
'Content-Type': 'application/json'
|
|
};
|
|
}
|
|
|
|
public async getSharedAccounts() {
|
|
if (!this.token) return [];
|
|
try {
|
|
const response = await axios.get(`${this.url}/api/sync`, { headers: this.headers });
|
|
return response.data;
|
|
} catch (e) {
|
|
console.error('[Backend] Failed to fetch shared accounts');
|
|
return [];
|
|
}
|
|
}
|
|
|
|
public async getCommunityAccounts() {
|
|
if (!this.token) return [];
|
|
try {
|
|
const response = await axios.get(`${this.url}/api/sync/community`, { headers: this.headers });
|
|
return response.data;
|
|
} catch (e) {
|
|
console.error('[Backend] Failed to fetch community accounts');
|
|
return [];
|
|
}
|
|
}
|
|
|
|
public async getServerUsers() {
|
|
if (!this.token) return [];
|
|
try {
|
|
const response = await axios.get(`${this.url}/api/sync/users`, { headers: this.headers });
|
|
return response.data;
|
|
} catch (e) {
|
|
console.error('[Backend] Failed to fetch server users');
|
|
return [];
|
|
}
|
|
}
|
|
|
|
public async shareAccount(account: any) {
|
|
if (!this.token) return;
|
|
try {
|
|
await axios.post(`${this.url}/api/sync`, {
|
|
steamId: account.steamId,
|
|
personaName: account.personaName,
|
|
avatar: account.avatar,
|
|
profileUrl: account.profileUrl,
|
|
vacBanned: account.vacBanned,
|
|
gameBans: account.gameBans,
|
|
loginName: account.loginName,
|
|
steamLoginSecure: account.steamLoginSecure,
|
|
loginConfig: account.loginConfig,
|
|
sessionUpdatedAt: account.sessionUpdatedAt
|
|
}, { headers: this.headers });
|
|
} catch (e) {
|
|
console.error('[Backend] Failed to share account');
|
|
}
|
|
}
|
|
|
|
public async pushCooldown(steamId: string, cooldownExpiresAt?: string) {
|
|
if (!this.token) return;
|
|
try {
|
|
await axios.patch(`${this.url}/api/sync/${steamId}/cooldown`, {
|
|
cooldownExpiresAt
|
|
}, { headers: this.headers });
|
|
} catch (e) {
|
|
console.error(`[Backend] Failed to push cooldown for ${steamId}`);
|
|
}
|
|
}
|
|
|
|
public async shareWithUser(steamId: string, targetSteamId: string) {
|
|
if (!this.token) return;
|
|
try {
|
|
const response = await axios.post(`${this.url}/api/sync/${steamId}/share`, {
|
|
targetSteamId
|
|
}, { headers: this.headers });
|
|
return response.data;
|
|
} catch (e: any) {
|
|
console.error(`[Backend] Failed to share account ${steamId} with ${targetSteamId}`);
|
|
throw new Error(e.response?.data?.message || 'Failed to share account');
|
|
}
|
|
}
|
|
|
|
public async revokeAccess(steamId: string, targetSteamId: string) {
|
|
if (!this.token) return;
|
|
try {
|
|
const response = await axios.delete(`${this.url}/api/sync/${steamId}/share`, {
|
|
headers: this.headers,
|
|
data: { targetSteamId }
|
|
});
|
|
return response.data;
|
|
} catch (e: any) {
|
|
console.error(`[Backend] Failed to revoke access for ${steamId} from ${targetSteamId}`);
|
|
throw new Error(e.response?.data?.message || 'Failed to revoke access');
|
|
}
|
|
}
|
|
|
|
public async revokeAllAccess(steamId: string) {
|
|
if (!this.token) return;
|
|
try {
|
|
const response = await axios.delete(`${this.url}/api/sync/${steamId}/share/all`, {
|
|
headers: this.headers
|
|
});
|
|
return response.data;
|
|
} catch (e: any) {
|
|
console.error(`[Backend] Failed to revoke all access for ${steamId}`);
|
|
throw new Error(e.response?.data?.message || 'Failed to revoke all access');
|
|
}
|
|
}
|
|
|
|
// --- Admin API ---
|
|
|
|
public async getAdminStats() {
|
|
if (!this.token) return null;
|
|
try {
|
|
const response = await axios.get(`${this.url}/api/admin/stats`, { headers: this.headers });
|
|
return response.data;
|
|
} catch (e) { return null; }
|
|
}
|
|
|
|
public async getAdminUsers() {
|
|
if (!this.token) return [];
|
|
try {
|
|
const response = await axios.get(`${this.url}/api/admin/users`, { headers: this.headers });
|
|
return response.data;
|
|
} catch (e) { return []; }
|
|
}
|
|
|
|
public async deleteUser(userId: string) {
|
|
if (!this.token) return;
|
|
try {
|
|
await axios.delete(`${this.url}/api/admin/users/${userId}`, { headers: this.headers });
|
|
} catch (e: any) {
|
|
throw new Error(e.response?.data?.message || 'Failed to delete user');
|
|
}
|
|
}
|
|
|
|
public async getAdminAccounts() {
|
|
if (!this.token) return [];
|
|
try {
|
|
const response = await axios.get(`${this.url}/api/admin/accounts`, { headers: this.headers });
|
|
return response.data;
|
|
} catch (e) { return []; }
|
|
}
|
|
|
|
public async forceRemoveAccount(steamId: string) {
|
|
if (!this.token) return;
|
|
try {
|
|
await axios.delete(`${this.url}/api/admin/accounts/${steamId}`, { headers: this.headers });
|
|
} catch (e: any) {
|
|
throw new Error(e.response?.data?.message || 'Failed to remove account');
|
|
}
|
|
}
|
|
}
|