Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion docs/http-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ API 密钥可在 **设置 → HTTP 服务** 中查看和复制。
| ---- | ----------- | --------------------------------- |
| 0 | 200 | 操作成功 |
| 401 | 401 | API 密钥无效或未提供 |
| 404 | 200 | 未知接口路径 |
| 404 | 200 | 未知接口路径、插件或功能不存在 |
| 400 | 200 | 请求参数错误 |
| 405 | 405 | 请求方法不允许(仅支持 GET/POST) |
| 500 | 500 | 服务器内部错误 |

Expand Down Expand Up @@ -211,6 +212,77 @@ curl -X POST http://127.0.0.1:36578/api/window/toggle \

---

### POST /api/plugin/launch — 启动插件

按 `plugin.json` 中的 `name` 和功能 code 启动已安装插件,并按手动启动插件时的参数格式传入 `type` 与 `payload`。

**请求**

```
POST /api/plugin/launch
Authorization: Bearer <API_KEY>
Content-Type: application/json
```

**入参**

| 字段 | 类型 | 必填 | 说明 |
| ------------ | -------- | ---- | -------------------------------------------------------------------------------------- |
| `pluginName` | `string` | 是 | 插件 `plugin.json` 中的 `name` |
| `code` | `string` | 是 | 要启动的插件功能 code |
| `type` | `string` | 否 | 启动类型,支持 `text`、`over`、`regex`、`img`、`files`、`window`,默认 `text` |
| `payload` | `any` | 否 | 传给插件的内容,与手动启动插件时的 `payload` 含义一致;`type=files` 时可传文件路径数组 |

**请求体示例**

```json
{
"pluginName": "demo-plugin",
"code": "open",
"type": "text",
"payload": "来自 HTTP API 的内容"
}
```

文件类型插件也可以传文件路径数组,ZTools 会转换为手动启动插件时一致的文件对象数组。

```json
{
"pluginName": "demo-plugin",
"code": "open-files",
"type": "files",
"payload": ["/Users/me/Desktop/a.txt", "/Users/me/Desktop/images"]
}
```

**返回**

```json
{
"code": 0,
"message": "操作成功",
"data": {
"name": "demo-plugin",
"title": "Demo 插件",
"path": "/path/to/demo-plugin",
"result": {
"success": true
}
}
}
```

**curl 示例**

```bash
curl -X POST http://127.0.0.1:36578/api/plugin/launch \
-H "Authorization: Bearer <API_KEY>" \
-H "Content-Type: application/json" \
-d '{"pluginName": "demo-plugin", "code": "open", "type": "text", "payload": "hello"}'
```

---

## 在各语言/工具中调用

### JavaScript / Node.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface ApiEndpoint {
path: string
desc: string
auth: boolean
body?: Record<string, unknown>
body?: unknown
}

const apiEndpoints: ApiEndpoint[] = [
Expand All @@ -37,6 +37,18 @@ const apiEndpoints: ApiEndpoint[] = [
path: '/api/window/toggle',
desc: '切换 ZTools 主窗口显示/隐藏状态',
auth: true
},
{
method: 'POST',
path: '/api/plugin/launch',
desc: '启动插件,支持传参。pluginName 对应 plugin.json 中的 name,code 和 type(text、over 等)也保持和 plugin.json 一致。type=files 时 payload 可传文件路径数组',
auth: true,
body: {
pluginName: '插件名称',
code: '功能 code',
type: 'text',
payload: '传给插件的内容'
}
}
]

Expand Down Expand Up @@ -247,6 +259,7 @@ onMounted(() => {
<button class="btn btn-sm copy-curl-btn" @click="copyCurl(item)">复制 curl</button>
</div>
<p class="api-desc">{{ item.desc }}</p>
<pre v-if="item.body" class="api-body">{{ JSON.stringify(item.body, null, 2) }}</pre>
</div>
</div>

Expand Down Expand Up @@ -461,6 +474,19 @@ onMounted(() => {
margin: 0;
}

.api-body {
font-family: 'SF Mono', 'Menlo', 'Monaco', monospace;
font-size: 12px;
padding: 10px 12px;
background: var(--hover-bg);
border-radius: 6px;
color: var(--text-color);
margin: 10px 0 0 0;
line-height: 1.5;
white-space: pre-wrap;
word-break: break-word;
}

.docs-response {
display: flex;
flex-direction: column;
Expand Down
3 changes: 2 additions & 1 deletion src/main/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ class APIManager {
// 初始化软件更新API
updaterAPI.init(mainWindow)

// 初始化 HTTP 服务
// 初始化 HTTP 服务,并复用应用内插件启动入口处理外部插件启动请求。
httpServer.setPluginLauncher((options) => this.launchPlugin(options))
httpServer.init().catch((error) => {
console.error('[API] HTTP 服务初始化失败:', error)
})
Expand Down
Loading