TypeScript SDK
零运行时依赖 —— 仅依赖 Node 18+ 或浏览器全局
fetch+AbortController。
安装
npm install @gateway-new/sdk
初始化
import { Client } from "@gateway-new/sdk";
const c = new Client({
baseUrl: "https://api.gateway.wechas.com",
apiKey: "ak_xxx.yyy",
});
方法
| 方法 | HTTP | controls |
|---|---|---|
listHosts() | GET /v1/api/hosts | — |
makeDir(hostId, path) | POST .../files/dir | 1 |
removeDir(hostId, path) | POST .../files/dir | 2 |
execShell(hostId, ShellRequest) | POST .../process/shell | 1<<13 |
execProgram(hostId, path, args) | POST .../process/exec | 1<<10 |
killProcess(hostId, name, timeoutS) | POST .../process/kill | 1<<12 |
systemMetrics(hostId) | POST .../system/metrics | 1<<25 |
rawControl(hostId, controls, path, data) | POST .../controls/raw | 任意 |
错误处理
import { isInsufficientBalance, isHostOffline } from "@gateway-new/sdk";
try {
await c.execShell(hostId, { path: "powershell.exe" });
} catch (err) {
if (isInsufficientBalance(err)) { /* 402 */ }
if (isHostOffline(err)) { /* 409 */ }
throw err;
}
浏览器使用
依赖全局 fetch + AbortController,所有现代浏览器原生支持,可在前端直接 import:
// React / Vue / 任何前端框架
import { Client } from "@gateway-new/sdk";
const client = new Client({
baseUrl: import.meta.env.VITE_API_BASE,
apiKey: getApiKeyFromUserSession(), // 从用户会话拿 API Key
});
安全提醒:浏览器代码里不要硬编码 API Key;让用户登录后从你自己的后端短期颁发。
完整示例
import { Client, isInsufficientBalance } from "@gateway-new/sdk";
const c = new Client({
baseUrl: "https://api.gateway.wechas.com",
apiKey: "ak_xxx.yyy",
});
const hosts = await c.listHosts();
for (const h of hosts) {
console.log(`${h.name} online=${h.online}`);
}
const m = await c.systemMetrics("<host-uuid>");
console.log(`CPU=${m.cpu_pct}% Mem=${m.mem_pct}%`);
try {
const r = await c.execShell("<host-uuid>", {
path: "powershell.exe",
args: "Get-Process | Select-Object -First 5",
timeout_s: 10,
});
console.log(r.stdout);
} catch (err) {
if (isInsufficientBalance(err)) {
console.error("余额不足");
} else {
throw err;
}
}
源码
完整源码与单测见 sdk-clients/typescript/。