fix(api): normalize empty list responses

This commit is contained in:
zetaloop
2026-04-26 01:53:05 +08:00
parent 904148bd55
commit 30c336345e
11 changed files with 28 additions and 23 deletions
+2 -2
View File
@@ -5,7 +5,7 @@ import type { Comment } from "@/lib/types"
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
items: T[] | null
meta: {
total: number
offset: number
@@ -39,7 +39,7 @@ export async function listCommentsByPost(
cache: "no-store",
},
)
return res.items
return res.items ?? []
}
export async function addComment(postId: string, content: string) {
+2 -1
View File
@@ -24,7 +24,7 @@ export type ListDisputesOptions = {
}
type Paginated<T> = {
items: T[]
items: T[] | null
meta?: {
total: number
offset: number
@@ -50,6 +50,7 @@ function unwrapItems<T>(value: unknown): T[] {
if ("items" in value) {
const envelope = value as { items?: unknown }
if (Array.isArray(envelope.items)) return envelope.items as T[]
if (envelope.items === null) return []
}
throw new Error("Invalid response")
}
+2 -2
View File
@@ -3,7 +3,7 @@ import type { Favorite } from "@/lib/types"
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
items: T[] | null
meta: {
total: number
offset: number
@@ -15,7 +15,7 @@ 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
return Array.isArray(res) ? res : (res.items ?? [])
}
export async function isFavorited(
+2 -2
View File
@@ -4,7 +4,7 @@ import type { Game } from "@/lib/types"
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
items: T[] | null
meta: {
total: number
offset: number
@@ -16,7 +16,7 @@ export async function listGames(): Promise<Game[]> {
const res = await httpJson<Paginated<Game>>("/api/v1/games?offset=0&limit=100", {
cache: "no-store",
})
return res.items
return res.items ?? []
}
export async function getGameById(gameId: string): Promise<Game | undefined> {
+2 -2
View File
@@ -3,7 +3,7 @@ import type { Notification } from "@/lib/types"
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
items: T[] | null
meta: {
total: number
offset: number
@@ -26,7 +26,7 @@ export async function listNotifications(input?: {
const res = await httpJson<Paginated<Notification>>(`/api/v1/notifications?${searchParams}`, {
cache: "no-store",
})
return res.items
return res.items ?? []
}
export async function markNotificationAsRead(notificationId: string): Promise<void> {
+2 -2
View File
@@ -5,7 +5,7 @@ import type { Order, OrderStatus } from "@/lib/types"
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
items: T[] | null
meta: {
total: number
offset: number
@@ -59,7 +59,7 @@ export async function listOrders(options?: ListOrdersOptions): Promise<Order[]>
const res = await httpJson<Paginated<Order>>(withOffsetLimit("/api/v1/orders", options), {
cache: "no-store",
})
return res.items
return res.items ?? []
}
export async function getOrderById(orderId: string): Promise<Order | undefined> {
+2 -2
View File
@@ -4,7 +4,7 @@ import type { Player } from "@/lib/types"
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
items: T[] | null
meta: {
total: number
offset: number
@@ -16,7 +16,7 @@ export async function listPlayers(): Promise<Player[]> {
const res = await httpJson<Paginated<Player>>("/api/v1/players?offset=0&limit=100", {
cache: "no-store",
})
return res.items
return res.items ?? []
}
export async function getPlayerById(playerId: string): Promise<Player | undefined> {
+3 -3
View File
@@ -4,7 +4,7 @@ import type { Post } from "@/lib/types"
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
items: T[] | null
meta: {
total: number
offset: number
@@ -40,7 +40,7 @@ export async function listPosts(options?: ListOptions): Promise<Post[]> {
const res = await httpJson<Paginated<Post>>(withOffsetLimit("/api/v1/posts", options), {
cache: "no-store",
})
return res.items
return res.items ?? []
}
export async function getPostById(postId: string): Promise<Post | undefined> {
@@ -80,7 +80,7 @@ export async function listPostsByAuthor(userId: string, options?: ListOptions):
cache: "no-store",
},
)
return res.items
return res.items ?? []
}
export async function togglePostLike(postId: string, currentlyLiked: boolean): Promise<void> {
+2 -1
View File
@@ -5,7 +5,7 @@ import type { Review } from "@/lib/types"
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
items: T[] | null
meta: {
total: number
offset: number
@@ -37,6 +37,7 @@ function unwrapItems<T>(value: unknown): T[] {
if ("items" in value) {
const envelope = value as { items?: unknown }
if (Array.isArray(envelope.items)) return envelope.items as T[]
if (envelope.items === null) return []
}
throw new Error("Invalid response")
}
+2 -2
View File
@@ -3,7 +3,7 @@ import type { WalletTransaction } from "@/lib/types"
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
items: T[] | null
meta: {
total: number
offset: number
@@ -57,7 +57,7 @@ export async function listWalletTransactions(
cache: "no-store",
},
)
return res.items
return res.items ?? []
}
export async function topUpWallet(input: { amount: number; method?: string }): Promise<void> {
+7 -4
View File
@@ -65,8 +65,11 @@ export async function applyCurrentUserVerification(input: {
}
export async function listCurrentUserVerifications(): Promise<VerificationRecord[]> {
const res = await httpJson<{ list: VerificationRecord[] }>("/api/v1/users/me/verification", {
cache: "no-store",
})
return res.list
const res = await httpJson<{ list: VerificationRecord[] | null }>(
"/api/v1/users/me/verification",
{
cache: "no-store",
},
)
return res.list ?? []
}