S124 AstrBot页面Bridge适配
这个提交存在于:
父节点
c5cfc285b1
当前提交
a940e8dd60
共有 7 个文件被更改,包括 77 次插入 和 21 次删除
1
frontend/env.d.ts
Vendored
1
frontend/env.d.ts
Vendored
|
|
@ -9,6 +9,7 @@ declare module "*.vue" {
|
||||||
|
|
||||||
interface Window {
|
interface Window {
|
||||||
AstrBotPluginPage?: {
|
AstrBotPluginPage?: {
|
||||||
|
ready?(): Promise<void>
|
||||||
apiGet<T>(path: string, params?: Record<string, unknown>): Promise<T>
|
apiGet<T>(path: string, params?: Record<string, unknown>): Promise<T>
|
||||||
apiPost<T>(path: string, body?: unknown): Promise<T>
|
apiPost<T>(path: string, body?: unknown): Promise<T>
|
||||||
apiPut<T>(path: string, body?: unknown): Promise<T>
|
apiPut<T>(path: string, body?: unknown): Promise<T>
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import { defineConfig } from "vite"
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [vue()],
|
plugins: [vue()],
|
||||||
root: __dirname,
|
root: __dirname,
|
||||||
|
base: "./",
|
||||||
build: {
|
build: {
|
||||||
outDir: resolve(__dirname, "../../pages/imagestudio"),
|
outDir: resolve(__dirname, "../../pages/imagestudio"),
|
||||||
emptyOutDir: true,
|
emptyOutDir: true,
|
||||||
|
|
|
||||||
|
|
@ -29,18 +29,72 @@ export async function request<T>(
|
||||||
body?: unknown
|
body?: unknown
|
||||||
} = {},
|
} = {},
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
|
const bridgePayload = await requestViaPluginBridge<T>(endpoint, options)
|
||||||
|
if (bridgePayload !== undefined) {
|
||||||
|
return unwrapApiResponse(bridgePayload)
|
||||||
|
}
|
||||||
|
|
||||||
const response = await fetch(buildUrl(endpoint, options.query), {
|
const response = await fetch(buildUrl(endpoint, options.query), {
|
||||||
method: options.method ?? "GET",
|
method: options.method ?? "GET",
|
||||||
headers: options.body === undefined ? undefined : { "Content-Type": "application/json" },
|
headers: options.body === undefined ? undefined : { "Content-Type": "application/json" },
|
||||||
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
||||||
})
|
})
|
||||||
const payload = (await response.json()) as ApiResponse<T>
|
const payload = (await response.json()) as ApiResponse<T>
|
||||||
|
return unwrapApiResponse(payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function requestViaPluginBridge<T>(
|
||||||
|
endpoint: string,
|
||||||
|
options: {
|
||||||
|
method?: "GET" | "POST" | "PUT" | "DELETE"
|
||||||
|
query?: Record<string, string | number | boolean | undefined>
|
||||||
|
body?: unknown
|
||||||
|
},
|
||||||
|
): Promise<T | ApiResponse<T> | undefined> {
|
||||||
|
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") {
|
||||||
|
return bridge.apiGet<T | ApiResponse<T>>(endpoint, compactQuery(options.query))
|
||||||
|
}
|
||||||
|
if (method === "POST" && typeof bridge.apiPost === "function") {
|
||||||
|
return bridge.apiPost<T | ApiResponse<T>>(endpoint, options.body)
|
||||||
|
}
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
function unwrapApiResponse<T>(payload: T | ApiResponse<T>): T {
|
||||||
|
if (!isApiResponse<T>(payload)) {
|
||||||
|
return payload
|
||||||
|
}
|
||||||
if (!payload.ok) {
|
if (!payload.ok) {
|
||||||
throw new ApiClientError(payload.error_code, payload.message, payload.details)
|
throw new ApiClientError(payload.error_code, payload.message, payload.details)
|
||||||
}
|
}
|
||||||
return payload.data
|
return payload.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isApiResponse<T>(payload: T | ApiResponse<T>): payload is ApiResponse<T> {
|
||||||
|
return Boolean(payload && typeof payload === "object" && "ok" in payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
export function buildUrl(endpoint: string, query?: Record<string, string | number | boolean | undefined>) {
|
export function buildUrl(endpoint: string, query?: Record<string, string | number | boolean | undefined>) {
|
||||||
const trimmed = endpoint.replace(/^\/+/, "")
|
const trimmed = endpoint.replace(/^\/+/, "")
|
||||||
const url = new URL(`${API_BASE}/${trimmed}`, window.location.origin)
|
const url = new URL(`${API_BASE}/${trimmed}`, window.location.origin)
|
||||||
|
|
|
||||||
文件差异因一行或多行过长而隐藏
文件差异因一行或多行过长而隐藏
|
|
@ -1,3 +1,3 @@
|
||||||
<script type="module" crossorigin src="assets/index-BmeDOVPT.js"></script>
|
<script type="module" crossorigin src="./assets/index-B-Kfj9Ny.js"></script>
|
||||||
<link rel="stylesheet" crossorigin href="assets/index-DJ52Rx93.css">
|
<link rel="stylesheet" crossorigin href="./assets/index-DJ52Rx93.css">
|
||||||
<div id="app"></div>
|
<div id="app"></div>
|
||||||
|
|
|
||||||
|
|
@ -86,8 +86,8 @@ class PluginEntrypointTests(unittest.TestCase):
|
||||||
page = Path(__file__).resolve().parents[1] / "pages" / "imagestudio" / "index.html"
|
page = Path(__file__).resolve().parents[1] / "pages" / "imagestudio" / "index.html"
|
||||||
html = page.read_text(encoding="utf-8")
|
html = page.read_text(encoding="utf-8")
|
||||||
|
|
||||||
self.assertIn('src="assets/', html)
|
self.assertRegex(html, r'src="\.?/assets/')
|
||||||
self.assertIn('href="assets/', html)
|
self.assertRegex(html, r'href="\.?/assets/')
|
||||||
self.assertNotIn('src="/assets/', html)
|
self.assertNotIn('src="/assets/', html)
|
||||||
self.assertNotIn('href="/assets/', html)
|
self.assertNotIn('href="/assets/', html)
|
||||||
|
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用