Files
juwan-frontend/lib/api/favorites.ts
T
2026-04-26 01:53:05 +08:00

56 lines
1.3 KiB
TypeScript

import type { Favorite } from "@/lib/types"
import { httpJson } from "./http"
type Paginated<T> = {
items: T[] | null
meta: {
total: number
offset: number
limit: number
}
}
export async function listFavorites(): Promise<Favorite[]> {
const res = await httpJson<Paginated<Favorite> | Favorite[]>("/api/v1/favorites", {
cache: "no-store",
})
return Array.isArray(res) ? res : (res.items ?? [])
}
export async function isFavorited(
userId: string,
targetType: "player" | "shop",
targetId: string,
): Promise<boolean> {
const searchParams = new URLSearchParams({
targetType,
targetId,
})
const res = await httpJson<{ favorited: boolean }>(
`/api/v1/users/${encodeURIComponent(userId)}/favorites/check?${searchParams.toString()}`,
{
cache: "no-store",
},
)
return res.favorited
}
export async function addFavorite(input: {
targetType: "player" | "shop"
targetId: string
}): Promise<void> {
await httpJson<unknown>("/api/v1/favorites", {
method: "POST",
json: input,
cache: "no-store",
})
}
export async function removeFavorite(favoriteId: string): Promise<void> {
await httpJson<unknown>(`/api/v1/favorites/${encodeURIComponent(favoriteId)}`, {
method: "DELETE",
cache: "no-store",
})
}