import axios from 'axios' import type { AuthSession } from './auth-storage' export type LoginPayload = { email: string; password: string } type LoginResponse = { accessToken: string refreshToken: string tokenType: string expiresIn?: number user: { id: string; email: string } guilds: Array<{ nodeId: string; name: string; endpoint: string; status: 'active' | 'offline' | 'revoked' }> guildAccessTokens: Array<{ guildNodeId: string; token: string; tokenType: string; expiresIn?: number }> } type RefreshResponse = { accessToken: string refreshToken: string tokenType: string expiresIn?: number } type MeGuildsResponse = { guilds: Array<{ nodeId: string; name: string; endpoint: string; status: 'active' | 'offline' | 'revoked' }> guildAccessTokens: Array<{ guildNodeId: string; token: string; tokenType: string; expiresIn?: number }> } function centerClient(centerApiBase: string) { const client = axios.create({ baseURL: centerApiBase, timeout: 10000, }) client.interceptors.request.use((request) => { const requestId = crypto.randomUUID() request.headers['x-request-id'] = requestId request.headers['x-client-name'] = 'fabric-frontend' return request }) return client } export async function loginCenter(centerApiBase: string, payload: LoginPayload): Promise { const res = await centerClient(centerApiBase).post('/auth/login', payload) return { ...res.data, centerApiBase } } export async function refreshCenter(centerApiBase: string, refreshToken: string): Promise { const res = await centerClient(centerApiBase).post('/auth/refresh', { refreshToken }) return res.data } export async function logoutCenter(centerApiBase: string, refreshToken: string): Promise { await centerClient(centerApiBase).post('/auth/logout', { refreshToken }) } export async function meGuildsCenter(centerApiBase: string, accessToken: string): Promise { const res = await centerClient(centerApiBase).get('/auth/me/guilds', { headers: { Authorization: `Bearer ${accessToken}` }, }) return res.data } export async function joinGuildCenter(centerApiBase: string, accessToken: string, guildNodeId: string): Promise { await centerClient(centerApiBase).post( '/auth/me/guilds/join', { guildNodeId }, { headers: { Authorization: `Bearer ${accessToken}` } }, ) } export async function guildMembersCenter( centerApiBase: string, accessToken: string, guildNodeId: string, ): Promise> { const res = await centerClient(centerApiBase).get>( `/auth/guilds/${encodeURIComponent(guildNodeId)}/members`, { headers: { Authorization: `Bearer ${accessToken}` } }, ) return Array.isArray(res.data) ? res.data : [] }