workspace 由 api.workspaces 模块暴露,这是 v4 新增的功能。它们在 Standard、Pro 和 Enterprise 套餐上可用。
创建 workspace
const { id: workspaceId } = await api.workspaces.create({ name: "Acme" });
name 长度必须为 1–32 个字符。
列出 workspace
api.workspaces.list() 返回当前已认证用户作为所有者或成员的所有 workspace。
const workspaces = await api.workspaces.list();
for (const ws of workspaces) {
console.log(
`${ws.name} (${ws.memberList.length} members, ${ws.applicationList.length} apps)`,
);
}
在 v3 中,ws.members 和 ws.applications 持有原始数据数组。在 v4 中它们现在是模块(ws.members.add(...)、ws.applications.add(...))。原始数组被移到了 ws.memberList 和 ws.applicationList。
获取单个 workspace
const workspace = await api.workspaces.fetch(workspaceId);
console.log(workspace.id);
console.log(workspace.name);
console.log(workspace.owner);
console.log(workspace.createdAt);
console.log(workspace.memberList);
console.log(workspace.applicationList);
邀请成员
邀请成员是一个两步握手过程:受邀者通过在其自己的客户端生成一个短时(5 分钟)代码来证明同意,然后由所有者消费该代码。
// 1. From the invitee's own SDK client
const otherApi = new SquareCloudAPI(process.env.OTHER_USER_API_KEY!);
const code = await otherApi.workspaces.generateInviteCode();
// 2. From the workspace owner's client
await workspace.members.add(code, "maintain");
group 参数的有效角色:
| Role | Description |
|---|
view | 只读访问 |
manager | 管理应用 |
maintain | 管理应用 + 管理其层级以下的成员 |
admin | 完全管理员 |
owner 角色无法通过 API 分配,所有权转移未对外开放。
更改成员角色
await workspace.members.update("1234567890abcdef1234567890abcdef", "admin");
移除成员
await workspace.members.remove("1234567890abcdef1234567890abcdef");
共享应用
await workspace.applications.add("abc123def456abc123def456");
await workspace.applications.remove("abc123def456abc123def456");
离开或删除 workspace
// As a member
await workspace.leave();
// As the owner (irreversible)
await workspace.delete();
你也可以直接通过模块按 ID 删除或离开:
await api.workspaces.delete(workspaceId);
await api.workspaces.leave(workspaceId);