2026-07-05 09:24:00 +08:00
|
|
|
|
import { API_BASE } from "./endpoints"
|
2026-07-11 21:50:34 +08:00
|
|
|
|
import type { ApiResponse, AstrBotApiResponse } from "../types/api"
|
2026-07-05 09:24:00 +08:00
|
|
|
|
|
2026-07-08 02:25:50 +08:00
|
|
|
|
export type ApiTransport = "plugin-bridge" | "direct-fetch"
|
2026-07-11 21:50:34 +08:00
|
|
|
|
export type ApiMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE"
|
|
|
|
|
|
|
|
|
|
|
|
const METHOD_OVERRIDE_KEY = "__imagestudio_method"
|
|
|
|
|
|
const METHOD_OVERRIDE_BODY_KEY = "__imagestudio_body"
|
2026-07-08 02:25:50 +08:00
|
|
|
|
|
2026-07-05 09:24:00 +08:00
|
|
|
|
export class ApiClientError extends Error {
|
|
|
|
|
|
readonly code: string
|
|
|
|
|
|
readonly details: Record<string, unknown>
|
|
|
|
|
|
|
|
|
|
|
|
constructor(code: string, message: string, details: Record<string, unknown> = {}) {
|
|
|
|
|
|
super(message)
|
|
|
|
|
|
this.name = "ApiClientError"
|
|
|
|
|
|
this.code = code
|
|
|
|
|
|
this.details = details
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function getJson<T>(endpoint: string, query?: Record<string, string | number | boolean | undefined>) {
|
|
|
|
|
|
return request<T>(endpoint, { method: "GET", query })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function postJson<T>(endpoint: string, body?: unknown) {
|
|
|
|
|
|
return request<T>(endpoint, { method: "POST", body })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-10 01:25:15 +08:00
|
|
|
|
export async function putJson<T>(endpoint: string, body?: unknown) {
|
|
|
|
|
|
return request<T>(endpoint, { method: "PUT", body })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-11 21:50:34 +08:00
|
|
|
|
export async function patchJson<T>(endpoint: string, body?: unknown) {
|
|
|
|
|
|
return request<T>(endpoint, { method: "PATCH", body })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function deleteJson<T>(endpoint: string, body?: unknown) {
|
|
|
|
|
|
return request<T>(endpoint, { method: "DELETE", body })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-08 02:25:50 +08:00
|
|
|
|
export function getApiTransport(): ApiTransport {
|
|
|
|
|
|
const bridge = typeof window === "undefined" ? undefined : window.AstrBotPluginPage
|
|
|
|
|
|
return bridge && (typeof bridge.apiGet === "function" || typeof bridge.apiPost === "function")
|
|
|
|
|
|
? "plugin-bridge"
|
|
|
|
|
|
: "direct-fetch"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-05 09:24:00 +08:00
|
|
|
|
export async function request<T>(
|
|
|
|
|
|
endpoint: string,
|
|
|
|
|
|
options: {
|
2026-07-11 21:50:34 +08:00
|
|
|
|
method?: ApiMethod
|
2026-07-05 09:24:00 +08:00
|
|
|
|
query?: Record<string, string | number | boolean | undefined>
|
|
|
|
|
|
body?: unknown
|
|
|
|
|
|
} = {},
|
|
|
|
|
|
): Promise<T> {
|
2026-07-11 21:50:34 +08:00
|
|
|
|
const hasPluginBridge = typeof window !== "undefined" && Boolean(window.AstrBotPluginPage)
|
2026-07-06 09:45:13 +08:00
|
|
|
|
const bridgePayload = await requestViaPluginBridge<T>(endpoint, options)
|
2026-07-11 21:50:34 +08:00
|
|
|
|
if (hasPluginBridge) {
|
|
|
|
|
|
if (bridgePayload === undefined) {
|
|
|
|
|
|
throw new ApiClientError("invalid_api_response", "AstrBot Plugin Page bridge returned no response", {
|
|
|
|
|
|
endpoint,
|
|
|
|
|
|
method: options.method ?? "GET",
|
|
|
|
|
|
transport: "plugin-bridge",
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2026-07-06 09:45:13 +08:00
|
|
|
|
return unwrapApiResponse(bridgePayload)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-08 02:25:50 +08:00
|
|
|
|
const url = buildUrl(endpoint, options.query)
|
|
|
|
|
|
let response: Response
|
|
|
|
|
|
try {
|
|
|
|
|
|
response = await fetch(url, {
|
|
|
|
|
|
method: options.method ?? "GET",
|
|
|
|
|
|
headers: options.body === undefined ? undefined : { "Content-Type": "application/json" },
|
|
|
|
|
|
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
|
|
|
|
|
})
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
throw new ApiClientError("network_unreachable", "无法连接 ImageStudio 后端接口", {
|
|
|
|
|
|
endpoint,
|
|
|
|
|
|
transport: "direct-fetch",
|
|
|
|
|
|
url,
|
|
|
|
|
|
reason: err instanceof Error ? err.message : String(err),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const payload = await parseResponsePayload<T>(response, endpoint, url)
|
2026-07-11 21:50:34 +08:00
|
|
|
|
if (!response.ok && !isApiResponse(payload) && !isAstrBotApiResponse(payload)) {
|
2026-07-08 02:25:50 +08:00
|
|
|
|
throw new ApiClientError("http_error", `接口请求失败(HTTP ${response.status})`, {
|
|
|
|
|
|
endpoint,
|
|
|
|
|
|
transport: "direct-fetch",
|
|
|
|
|
|
url,
|
|
|
|
|
|
status: response.status,
|
|
|
|
|
|
status_text: response.statusText,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2026-07-06 09:45:13 +08:00
|
|
|
|
return unwrapApiResponse(payload)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function requestViaPluginBridge<T>(
|
|
|
|
|
|
endpoint: string,
|
|
|
|
|
|
options: {
|
2026-07-11 21:50:34 +08:00
|
|
|
|
method?: ApiMethod
|
2026-07-06 09:45:13 +08:00
|
|
|
|
query?: Record<string, string | number | boolean | undefined>
|
|
|
|
|
|
body?: unknown
|
|
|
|
|
|
},
|
2026-07-11 21:50:34 +08:00
|
|
|
|
): Promise<T | ApiResponse<T> | AstrBotApiResponse<T> | undefined> {
|
2026-07-06 09:45:13 +08:00
|
|
|
|
const bridge = window.AstrBotPluginPage
|
|
|
|
|
|
if (!bridge) {
|
|
|
|
|
|
return undefined
|
|
|
|
|
|
}
|
|
|
|
|
|
const method = options.method ?? "GET"
|
|
|
|
|
|
if (typeof bridge.ready === "function") {
|
|
|
|
|
|
await bridge.ready()
|
|
|
|
|
|
}
|
|
|
|
|
|
if (method === "GET" && typeof bridge.apiGet === "function") {
|
2026-07-11 21:50:34 +08:00
|
|
|
|
return bridge.apiGet<T | ApiResponse<T> | AstrBotApiResponse<T>>(endpoint, compactQuery(options.query))
|
2026-07-06 09:45:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (method === "POST" && typeof bridge.apiPost === "function") {
|
2026-07-11 21:50:34 +08:00
|
|
|
|
return bridge.apiPost<T | ApiResponse<T> | AstrBotApiResponse<T>>(endpoint, options.body)
|
2026-07-06 09:45:13 +08:00
|
|
|
|
}
|
2026-07-10 01:25:15 +08:00
|
|
|
|
if (method === "PUT" && typeof bridge.apiPut === "function") {
|
2026-07-11 21:50:34 +08:00
|
|
|
|
return bridge.apiPut<T | ApiResponse<T> | AstrBotApiResponse<T>>(endpoint, options.body)
|
|
|
|
|
|
}
|
|
|
|
|
|
if (method === "PATCH" && typeof bridge.apiPatch === "function") {
|
|
|
|
|
|
return bridge.apiPatch<T | ApiResponse<T> | AstrBotApiResponse<T>>(endpoint, options.body)
|
2026-07-10 01:25:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (method === "DELETE" && typeof bridge.apiDelete === "function") {
|
2026-07-11 21:50:34 +08:00
|
|
|
|
return bridge.apiDelete<T | ApiResponse<T> | AstrBotApiResponse<T>>(endpoint)
|
2026-07-10 01:25:15 +08:00
|
|
|
|
}
|
2026-07-11 21:50:34 +08:00
|
|
|
|
if (["PUT", "PATCH", "DELETE"].includes(method) && typeof bridge.apiPost === "function") {
|
|
|
|
|
|
return bridge.apiPost<T | ApiResponse<T> | AstrBotApiResponse<T>>(endpoint, {
|
|
|
|
|
|
[METHOD_OVERRIDE_KEY]: method,
|
|
|
|
|
|
[METHOD_OVERRIDE_BODY_KEY]: method === "DELETE" ? {} : options.body === undefined ? {} : options.body,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
throw new ApiClientError("bridge_method_unavailable", `AstrBot Plugin Page bridge does not support ${method}`, {
|
|
|
|
|
|
endpoint,
|
|
|
|
|
|
method,
|
|
|
|
|
|
transport: "plugin-bridge",
|
|
|
|
|
|
})
|
2026-07-06 09:45:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-11 21:50:34 +08:00
|
|
|
|
function unwrapApiResponse<T>(payload: T | ApiResponse<T> | AstrBotApiResponse<T>): T {
|
|
|
|
|
|
let current: unknown = payload
|
|
|
|
|
|
for (let depth = 0; depth < 4; depth += 1) {
|
|
|
|
|
|
if (isApiResponse(current)) {
|
|
|
|
|
|
if (!current.ok) {
|
|
|
|
|
|
throw new ApiClientError(current.error_code, current.message, current.details ?? {})
|
|
|
|
|
|
}
|
|
|
|
|
|
current = current.data
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if (isAstrBotApiResponse(current)) {
|
|
|
|
|
|
if (current.status === "error") {
|
|
|
|
|
|
if (isApiResponse(current.data)) {
|
|
|
|
|
|
current = current.data
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
throw new ApiClientError(
|
|
|
|
|
|
current.error_code || "astrbot_bridge_error",
|
|
|
|
|
|
current.message || current.error || "AstrBot Plugin Page bridge request failed",
|
|
|
|
|
|
current.details ?? bridgeErrorDetails(current.data),
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
current = current.data
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
return current as T
|
2026-07-05 09:24:00 +08:00
|
|
|
|
}
|
2026-07-11 21:50:34 +08:00
|
|
|
|
throw new ApiClientError("invalid_api_response", "API response wrapper nesting is too deep")
|
2026-07-05 09:24:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-11 21:50:34 +08:00
|
|
|
|
async function parseResponsePayload<T>(
|
|
|
|
|
|
response: Response,
|
|
|
|
|
|
endpoint: string,
|
|
|
|
|
|
url: string,
|
|
|
|
|
|
): Promise<T | ApiResponse<T> | AstrBotApiResponse<T>> {
|
2026-07-08 02:25:50 +08:00
|
|
|
|
const contentType = response.headers.get("content-type") ?? ""
|
|
|
|
|
|
const isJson = contentType.includes("application/json") || contentType.includes("+json")
|
|
|
|
|
|
if (!isJson) {
|
|
|
|
|
|
const preview = (await response.text()).slice(0, 240)
|
|
|
|
|
|
throw new ApiClientError("invalid_api_response", "接口返回了非 JSON 响应", {
|
|
|
|
|
|
endpoint,
|
|
|
|
|
|
transport: "direct-fetch",
|
|
|
|
|
|
url,
|
|
|
|
|
|
status: response.status,
|
|
|
|
|
|
status_text: response.statusText,
|
|
|
|
|
|
content_type: contentType,
|
|
|
|
|
|
preview,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
2026-07-11 21:50:34 +08:00
|
|
|
|
return (await response.json()) as T | ApiResponse<T> | AstrBotApiResponse<T>
|
2026-07-08 02:25:50 +08:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
throw new ApiClientError("invalid_api_response", "接口返回格式不正确", {
|
|
|
|
|
|
endpoint,
|
|
|
|
|
|
transport: "direct-fetch",
|
|
|
|
|
|
url,
|
|
|
|
|
|
status: response.status,
|
|
|
|
|
|
status_text: response.statusText,
|
|
|
|
|
|
content_type: contentType,
|
|
|
|
|
|
reason: err instanceof Error ? err.message : String(err),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-11 21:50:34 +08:00
|
|
|
|
function isApiResponse(payload: unknown): payload is ApiResponse<unknown> {
|
|
|
|
|
|
if (!payload || typeof payload !== "object" || !("ok" in payload) || typeof payload.ok !== "boolean") {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
return payload.ok ? "data" in payload : "error_code" in payload && "message" in payload
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function isAstrBotApiResponse(payload: unknown): payload is AstrBotApiResponse<unknown> {
|
|
|
|
|
|
if (!payload || typeof payload !== "object" || !("status" in payload)) {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
return payload.status === "error" || (payload.status === "ok" && "data" in payload)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function bridgeErrorDetails(data: unknown): Record<string, unknown> {
|
|
|
|
|
|
return data && typeof data === "object" ? { bridge_data: data } : {}
|
2026-07-06 09:45:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function compactQuery(query?: Record<string, string | number | boolean | undefined>): Record<string, unknown> | undefined {
|
|
|
|
|
|
if (!query) {
|
|
|
|
|
|
return undefined
|
|
|
|
|
|
}
|
|
|
|
|
|
const compacted: Record<string, unknown> = {}
|
|
|
|
|
|
for (const [key, value] of Object.entries(query)) {
|
|
|
|
|
|
if (value !== undefined) {
|
|
|
|
|
|
compacted[key] = value
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return compacted
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-05 09:24:00 +08:00
|
|
|
|
export function buildUrl(endpoint: string, query?: Record<string, string | number | boolean | undefined>) {
|
|
|
|
|
|
const trimmed = endpoint.replace(/^\/+/, "")
|
|
|
|
|
|
const url = new URL(`${API_BASE}/${trimmed}`, window.location.origin)
|
|
|
|
|
|
for (const [key, value] of Object.entries(query ?? {})) {
|
|
|
|
|
|
if (value !== undefined) {
|
|
|
|
|
|
url.searchParams.set(key, String(value))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return `${url.pathname}${url.search}`
|
|
|
|
|
|
}
|