52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { del, get, post } from '@/lib/http/client'
|
|
import type {
|
|
SocialAccountInfo,
|
|
SocialAccountUnbindRequest,
|
|
SocialBindingStartRequest,
|
|
SocialBindingStartResponse,
|
|
} from '@/types'
|
|
|
|
interface SocialAccountsResponse {
|
|
items?: SocialAccountInfo[]
|
|
accounts?: SocialAccountInfo[]
|
|
social_accounts?: SocialAccountInfo[]
|
|
}
|
|
|
|
function normalizeSocialAccounts(payload: SocialAccountInfo[] | SocialAccountsResponse): SocialAccountInfo[] {
|
|
if (Array.isArray(payload)) {
|
|
return payload
|
|
}
|
|
|
|
if (Array.isArray(payload.items)) {
|
|
return payload.items
|
|
}
|
|
|
|
if (Array.isArray(payload.accounts)) {
|
|
return payload.accounts
|
|
}
|
|
|
|
if (Array.isArray(payload.social_accounts)) {
|
|
return payload.social_accounts
|
|
}
|
|
|
|
return []
|
|
}
|
|
|
|
export async function listSocialAccounts(): Promise<SocialAccountInfo[]> {
|
|
const payload = await get<SocialAccountInfo[] | SocialAccountsResponse>('/users/me/social-accounts')
|
|
return normalizeSocialAccounts(payload)
|
|
}
|
|
|
|
export function startSocialBinding(
|
|
payload: SocialBindingStartRequest,
|
|
): Promise<SocialBindingStartResponse> {
|
|
return post<SocialBindingStartResponse>('/users/me/bind-social', payload)
|
|
}
|
|
|
|
export function unbindSocialAccount(
|
|
provider: string,
|
|
payload?: SocialAccountUnbindRequest,
|
|
): Promise<void> {
|
|
return del<void>(`/users/me/bind-social/${provider}`, { body: payload })
|
|
}
|