84 lines
2.9 KiB
TypeScript
84 lines
2.9 KiB
TypeScript
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<AuthSession> {
|
|
const res = await centerClient(centerApiBase).post<LoginResponse>('/auth/login', payload)
|
|
return { ...res.data, centerApiBase }
|
|
}
|
|
|
|
export async function refreshCenter(centerApiBase: string, refreshToken: string): Promise<RefreshResponse> {
|
|
const res = await centerClient(centerApiBase).post<RefreshResponse>('/auth/refresh', { refreshToken })
|
|
return res.data
|
|
}
|
|
|
|
export async function logoutCenter(centerApiBase: string, refreshToken: string): Promise<void> {
|
|
await centerClient(centerApiBase).post('/auth/logout', { refreshToken })
|
|
}
|
|
|
|
export async function meGuildsCenter(centerApiBase: string, accessToken: string): Promise<MeGuildsResponse> {
|
|
const res = await centerClient(centerApiBase).get<MeGuildsResponse>('/auth/me/guilds', {
|
|
headers: { Authorization: `Bearer ${accessToken}` },
|
|
})
|
|
return res.data
|
|
}
|
|
|
|
export async function joinGuildCenter(centerApiBase: string, accessToken: string, guildNodeId: string): Promise<void> {
|
|
await centerClient(centerApiBase).post(
|
|
'/auth/me/guilds/join',
|
|
{ guildNodeId },
|
|
{ headers: { Authorization: `Bearer ${accessToken}` } },
|
|
)
|
|
}
|
|
|
|
export async function guildMembersCenter(
|
|
centerApiBase: string,
|
|
accessToken: string,
|
|
guildNodeId: string,
|
|
): Promise<Array<{ userId: string; email: string; status: string }>> {
|
|
const res = await centerClient(centerApiBase).get<Array<{ userId: string; email: string; status: string }>>(
|
|
`/auth/guilds/${encodeURIComponent(guildNodeId)}/members`,
|
|
{ headers: { Authorization: `Bearer ${accessToken}` } },
|
|
)
|
|
return Array.isArray(res.data) ? res.data : []
|
|
}
|