初始化客户端
在服务端、worker 或内部工具里创建 Appaloft client。认证材料由你的可信环境注入。
import { createAppaloftClient } from "@appaloft/sdk";
export const appaloft = createAppaloftClient({
baseUrl: process.env.APPALOFT_BASE_URL ?? "https://app.appaloft.com",
auth: () => ({
kind: "deploy-token",
token: process.env.APPALOFT_TOKEN!,
}),
headers: { "x-app-name": "internal-ops" },
});
创建项目
facade 按 operation group 组织;项目相关操作在复数 projects 下。
const created = await appaloft.projects.create({
body: {
name: "acme-web",
description: "Marketing site and API workers",
},
});
if (!created.ok) {
throw new Error(created.error.message);
}
console.log("project", created.data);
读取列表和详情
查询类操作同样返回 ok/error 结果,方便把 Appaloft 状态嵌进自己的 dashboard。
const projects = await appaloft.projects.list();
if (projects.ok) {
for (const project of projects.data as unknown[]) {
console.log(project);
}
}
const resource = await appaloft.resources.show({
pathParams: { resourceId: "res_123" },
});
处理错误
不要解析错误字符串;使用 code、category 和 retryable 字段决定提示、重试或升级人工处理。
const result = await appaloft.deployments.create({
body: {
projectId: "prj_123",
resourceId: "res_123",
source: { kind: "git", ref: "main" },
},
});
if (!result.ok) {
if (result.error.retryable) {
console.warn("retry later", result.error.code);
} else {
console.error(result.error.category, result.error.message);
}
}