refactor(web): align resource APIs and forms with the starter conventions

Centralize typed resource functions under common/apis and return complete JSON envelopes while preserving raw responses, bearer session isolation, and single-owner errors.

Keep settings state and queues inside the setup store. Use Element Plus forms and dialogs for login, settings, imports, moves, and images; preserve editor interactions and themes. Update callers, tests, generated declarations, and request import constraints together.
This commit is contained in:
eezd committed 2026-10-26 11:53:37 +08:00
1 parent cef0298c34
commit 6b537c03e0
114 files changed
+2876 -2217

No files matched your search

+9 -4
View File
@@ -1,11 +1,16 @@
import type { PluginAPI } from '@@/plugin-api'
import { pluginsApi } from '@@/apis/plugins'
import * as pluginsApi from '@@/apis/plugins'
// @vitest-environment happy-dom
import { afterEach, expect, it, vi } from 'vitest'
import { settings } from '../../pinia/stores/settings'
import { pinia } from '@/pinia'
import { useSettingsStore } from '@/pinia/stores/settings'
import { apiSuccess } from '../../../tests/api-response'
import { allCommands, commandKeys } from '../utils/commands'
import { pluginState, startPlugins, stopPlugins } from './host'
const { settings } = useSettingsStore(pinia)
const { cause } = vi.hoisted(() => ({ cause: new Error('插件命令的原始原因') }))
vi.mock('/api/v1/resources/test-plugin/plugin/plugins/fault/assets/rev/main.js', () => ({
default: (sdk: PluginAPI) => sdk.addCommand({ id: 'fail', title: '失败命令', keys: ['Mod+B'], run() {
@@ -17,7 +22,7 @@ afterEach(() => {
})
it('命令失败保留原始异常并清理注册,正常插件列表读取不受影响', async () => {
vi.spyOn(pluginsApi, 'plugins').mockResolvedValue([{ id: 'fault', name: '故障插件', version: '1', apiVersion: 1, revision: 'rev', enabled: true }])
vi.spyOn(pluginsApi, 'getPluginsApi').mockResolvedValue(apiSuccess({ plugins: [{ id: 'fault', name: '故障插件', version: '1', apiVersion: 1, revision: 'rev', enabled: true }] }))
const notices: string[] = []
startPlugins({ notice: message => notices.push(message), getActiveNote: () => null, openNote: async () => {}, writeNote: async () => ({ rev: 'r1' }) })
await vi.waitFor(() => expect(pluginState.status.fault?.phase).toBe('active'))
@@ -33,7 +38,7 @@ it('命令失败保留原始异常并清理注册,正常插件列表读取不
expect(notices.some(message => message.includes('命令失败'))).toBe(true)
})
vi.mock('@@/apis/auth/resources', () => ({
vi.mock('@@/utils/resource-tickets', () => ({
prepareResources: async () => {},
imageResourceRoot: () => '/api/v1/resources/test-images/images/',
fontResourceRoot: () => '/api/v1/resources/test-fonts/fonts/',
+10 -8
View File
@@ -1,8 +1,10 @@
import type { InstalledPlugin } from '@@/apis/plugins/type'
import type { AsyncCleanup, JSONValue, PluginAPI, PluginView, PluginWorkspace, VaultEvents } from '@@/plugin-api'
import { pluginResourceURL } from '@@/apis/auth/resources'
import { pluginsApi } from '@@/apis/plugins'
import { vaultApi } from '@@/apis/vault'
import { getPluginDataApi, getPluginsApi, savePluginDataApi } from '@@/apis/plugins'
import { getTreeApi } from '@@/apis/tree'
import { readNoteFile } from '@@/utils/note-files'
import { pluginResourceURL } from '@@/utils/resource-tickets'
import { reactive } from 'vue'
import { allCommands, registerCommand } from '../utils/commands'
import { editorExtensions, markdownSyntax, pluginEvents, pluginsPaused, registerCodeRenderer, registerEntry, settingTabs, sidebarViews } from './registry'
@@ -144,10 +146,10 @@ async function createAPI(instance: Instance): Promise<PluginAPI> {
},
vault: {
list: () => {
check(); return vaultApi.tree()
check(); return getTreeApi().then(({ data }) => data.entries)
},
read: (path) => {
check(); return vaultApi.readFile(path)
check(); return readNoteFile(path)
},
write: (path, content, options) => {
check(); return adapter.writeNote(path, content, options)
@@ -177,10 +179,10 @@ async function createAPI(instance: Instance): Promise<PluginAPI> {
},
data: {
load: async () => {
check(); return (await pluginsApi.pluginData(id)).data as JSONValue
check(); return ((await getPluginDataApi(id)).data).data as JSONValue
},
save: async (data) => {
check(); await pluginsApi.savePluginData(id, data)
check(); await savePluginDataApi({ id, data })
},
},
notice: (message) => {
@@ -257,7 +259,7 @@ export async function refreshPlugins() {
const currentSession = session
const sequence = ++request
try {
const installed = await pluginsApi.plugins()
const installed = (await getPluginsApi()).data.plugins
if (!running || currentSession !== session || sequence !== request) return
pluginState.installed = installed
pluginState.error = ''