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",
});

方法

方法HTTPcontrols
listHosts()GET /v1/api/hosts
makeDir(hostId, path)POST .../files/dir1
removeDir(hostId, path)POST .../files/dir2
execShell(hostId, ShellRequest)POST .../process/shell1<<13
execProgram(hostId, path, args)POST .../process/exec1<<10
killProcess(hostId, name, timeoutS)POST .../process/kill1<<12
systemMetrics(hostId)POST .../system/metrics1<<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/