fix(api): skip unavailable backend routes

This commit is contained in:
zetaloop
2026-04-25 14:04:53 +08:00
parent 54cd6d29fe
commit 70230483f8
2 changed files with 19 additions and 95 deletions
+17 -90
View File
@@ -1,18 +1,7 @@
import { allow, deny } from "@/lib/decision" import { deny } from "@/lib/decision"
import { isApiError, toApiError, type ApiDecision } from "@/lib/errors" import type { ApiDecision } from "@/lib/errors"
import type { ChatMessage, ChatSession } from "@/lib/types" import type { ChatMessage, ChatSession } from "@/lib/types"
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
meta: {
total: number
offset: number
limit: number
}
}
export type ListChatSessionsOptions = { export type ListChatSessionsOptions = {
offset?: number offset?: number
limit?: number limit?: number
@@ -23,92 +12,30 @@ export type ListChatMessagesOptions = {
limit?: number limit?: number
} }
function withOffsetLimit(path: string, options?: { offset?: number; limit?: number }): string { const unavailable = "聊天接口暂未开放"
const offset = options?.offset ?? 0
const limit = options?.limit ?? 100
const searchParams = new URLSearchParams({ export async function listChatSessions(_options?: ListChatSessionsOptions): Promise<ChatSession[]> {
offset: String(offset), return []
limit: String(limit),
})
return `${path}?${searchParams.toString()}`
} }
function unwrapItems<T>(value: Paginated<T> | T[]): T[] { export async function getChatSessionById(_sessionId: string): Promise<ChatSession | undefined> {
return Array.isArray(value) ? value : value.items return undefined
}
function denyFromError(error: unknown): ApiDecision {
if (error instanceof Error && error.message === "UNAUTHORIZED") {
return deny(401, "请先登录")
}
const apiError = toApiError(error)
return deny(apiError.code, apiError.msg)
}
export async function listChatSessions(options?: ListChatSessionsOptions): Promise<ChatSession[]> {
const res = await httpJson<Paginated<ChatSession> | ChatSession[]>(
withOffsetLimit("/api/v1/chat/sessions", options),
{
cache: "no-store",
},
)
return unwrapItems(res)
}
export async function getChatSessionById(sessionId: string): Promise<ChatSession | undefined> {
try {
return await httpJson<ChatSession>(`/api/v1/chat/sessions/${encodeURIComponent(sessionId)}`, {
cache: "no-store",
})
} catch (error) {
if (error instanceof Error && error.message === "UNAUTHORIZED") {
throw error
}
if (isApiError(error) && error.code === 404) {
return undefined
}
throw error
}
} }
export async function listChatMessages( export async function listChatMessages(
sessionId: string, _sessionId: string,
options?: ListChatMessagesOptions, _options?: ListChatMessagesOptions,
): Promise<ChatMessage[]> { ): Promise<ChatMessage[]> {
const res = await httpJson<Paginated<ChatMessage> | ChatMessage[]>( return []
withOffsetLimit(`/api/v1/chat/sessions/${encodeURIComponent(sessionId)}/messages`, options),
{
cache: "no-store",
},
)
return unwrapItems(res)
} }
export async function sendTextMessage(sessionId: string, content: string): Promise<ApiDecision> { export async function sendTextMessage(_sessionId: string, _content: string): Promise<ApiDecision> {
try { return deny(404, unavailable)
await httpJson<unknown>(`/api/v1/chat/sessions/${encodeURIComponent(sessionId)}/messages`, {
method: "POST",
cache: "no-store",
json: { type: "text", content },
})
return allow()
} catch (error) {
return denyFromError(error)
}
} }
export async function sendImageMessage(sessionId: string, imageUrl: string): Promise<ApiDecision> { export async function sendImageMessage(
try { _sessionId: string,
await httpJson<unknown>(`/api/v1/chat/sessions/${encodeURIComponent(sessionId)}/messages`, { _imageUrl: string,
method: "POST", ): Promise<ApiDecision> {
cache: "no-store", return deny(404, unavailable)
json: { type: "image", content: imageUrl },
})
return allow()
} catch (error) {
return denyFromError(error)
}
} }
+2 -5
View File
@@ -36,9 +36,6 @@ export async function getPlayerById(playerId: string): Promise<Player | undefine
} }
export async function listPlayersByShop(shopId: string): Promise<Player[]> { export async function listPlayersByShop(shopId: string): Promise<Player[]> {
const res = await httpJson<Paginated<Player>>( const players = await listPlayers()
`/api/v1/shops/${encodeURIComponent(shopId)}/players?offset=0&limit=100`, return players.filter((player) => player.shopId === shopId)
{ cache: "no-store" },
)
return res.items
} }