refactor: 将应用数据与工作流统一归入应用平台子模块
建立 applications 的应用模型和查询出口,保留基础及详情投影差异。 下沉 DataLink,将工作流模型、API、编排及执行上下文归回 workflow。 同步 CLI、SDK、生成脚本、测试和活跃文档引用,公开命令与 SDK 名称保持兼容。 验证:943 项测试、类型和版本检查通过;三份生成规范字节一致,README 审计通过。
This commit is contained in:
1 parent
edf300b808
commit
4e03c9bcd7
107 files changed
+423
-496
No files matched your search
@@ -0,0 +1,16 @@
|
||||
# 自建应用元数据(applications)
|
||||
|
||||
本模块拥有应用基础模型、详情模型、应用级权限元数据及目标解析。业务对象与工作流分别由 [DataLink](../datalink/README.datalink.md) 和 [Workflow](../workflow/README.workflow.md) 管理。
|
||||
|
||||
| 文件 | 职责 |
|
||||
|---|---|
|
||||
| [api.ts](./api.ts) | 应用 HTTP 请求和响应投影,共用启用应用列表请求 |
|
||||
| [types.ts](./types.ts) | PlatformSchema、应用详情与公开兼容类型 DatalinkPlatformDetail |
|
||||
| [services.ts](./services.ts) | 基础/详情列表、全量列表、详情与唯一目标解析出口 |
|
||||
| [application-contract.test.ts](./application-contract.test.ts) | 迁移前后两种投影、缺省与包装兼容回归 |
|
||||
|
||||
`listBasicPlatforms` 保留原工作流基础投影:剥离未建模字段,缺少 items 时拒绝。`listPlatforms` 保留详情投影:保留扩展字段、清洗 admin 信息、兼容空列表。两者共用请求实现,不强行混同既有错误及空值行为。
|
||||
|
||||
`listAllPlatforms` 与 `getPlatform` 保留原有全量及详情契约;当前全量列表原始数组仍被归为空列表,这属于已锁定的既有兼容行为,本轮不借迁移改变。`resolvePlatform` 按 ID、名称或编码做不区分大小写的精确匹配,再读取详情。
|
||||
|
||||
CLI 的 platform/app 详情及公共 SDK 的原函数名继续可用。不得从本模块反向依赖 DataLink 或工作流,不在应用模型中复制节点或记录结构。
|
||||
@@ -0,0 +1,74 @@
|
||||
import { z } from "zod";
|
||||
import type { KyInstance } from "ky";
|
||||
import { PlatformSchema, DatalinkPlatformDetailSchema, type DatalinkPlatformDetail } from "./types.js";
|
||||
|
||||
function asRecord(value: unknown): Record<string, any> {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value) ? value as Record<string, any> : {};
|
||||
}
|
||||
|
||||
async function fetchEnabledPlatforms(client: KyInstance, corpId: string): Promise<unknown> {
|
||||
const response = await client.get("api/v2/datalink/platform/search", { searchParams: { scope: "enable", corpId } });
|
||||
return response.json();
|
||||
}
|
||||
|
||||
/** 工作流使用的基础投影:保留缺少 items 时拒绝的既有契约。 */
|
||||
export async function listBasicPlatforms(client: KyInstance, corpId: string) {
|
||||
const json = await fetchEnabledPlatforms(client, corpId) as { items: unknown[] };
|
||||
return z.array(PlatformSchema).parse(json.items);
|
||||
}
|
||||
|
||||
function sanitizePlatformDetail(value: unknown): Record<string, unknown> {
|
||||
const platform = asRecord(value);
|
||||
const admin = Array.isArray(platform.admin)
|
||||
? platform.admin.map((item) => {
|
||||
const staff = asRecord(item);
|
||||
return {
|
||||
id: staff.id,
|
||||
name: staff.name,
|
||||
code: staff.code,
|
||||
active: staff.active,
|
||||
};
|
||||
})
|
||||
: platform.admin;
|
||||
return { ...platform, admin };
|
||||
}
|
||||
|
||||
/** 列出当前企业可见的启用自建应用。 */
|
||||
export async function listPlatforms(client: KyInstance, corpId: string) {
|
||||
const json = asRecord(await fetchEnabledPlatforms(client, corpId));
|
||||
const items = Array.isArray(json.items) ? json.items.map(sanitizePlatformDetail) : [];
|
||||
return z.array(DatalinkPlatformDetailSchema).parse(items);
|
||||
}
|
||||
|
||||
/** 列出当前企业全量自建应用与模板平台(包含 HAB 模板与全部自建应用)。 */
|
||||
export async function listAllPlatforms(client: KyInstance, corpId: string) {
|
||||
const response = await client.get("api/v2/datalink/platform", {
|
||||
searchParams: { corpId },
|
||||
});
|
||||
const json = asRecord(await response.json());
|
||||
const rawList = Array.isArray(json) ? json : Array.isArray(json.items) ? json.items : [];
|
||||
const items = rawList.map(sanitizePlatformDetail);
|
||||
return z.array(DatalinkPlatformDetailSchema).parse(items);
|
||||
}
|
||||
|
||||
/** 获取单个应用详情以及应用级人员/角色/部门权限。 */
|
||||
export async function getPlatform(
|
||||
client: KyInstance,
|
||||
corpId: string,
|
||||
platformId: string
|
||||
): Promise<DatalinkPlatformDetail> {
|
||||
const normalizedId = platformId.startsWith("$") ? platformId : `$${platformId}`;
|
||||
const bareId = platformId.startsWith("$") ? platformId.slice(1) : platformId;
|
||||
const response = await client.get(`api/v2/datalink/platform/${normalizedId}`, {
|
||||
// 前端实际使用两个 join,并且详情接口要求同时传 id。
|
||||
searchParams: new URLSearchParams([
|
||||
["id", bareId],
|
||||
["join", "icon.fileId,fileId,/v1/attachment/attachments"],
|
||||
["corpId", corpId],
|
||||
["join", "adminIds,admin,/v1/organization/staffs"],
|
||||
]),
|
||||
});
|
||||
const json = asRecord(await response.json());
|
||||
return DatalinkPlatformDetailSchema.parse(sanitizePlatformDetail(json.value ?? json.data ?? json));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import type { KyInstance } from "ky";
|
||||
import { listBasicPlatforms as listBasic } from "./api.js";
|
||||
import { listPlatforms as listDetailed, listAllPlatforms, getPlatform } from "./api.js";
|
||||
|
||||
function client(body: unknown) {
|
||||
const calls: Array<{ path: string; options: any }> = [];
|
||||
return { calls, http: { get: async (path: string, options: unknown) => {
|
||||
calls.push({ path, options });
|
||||
return Response.json(body);
|
||||
} } as unknown as KyInstance };
|
||||
}
|
||||
const platform = { id: "app", name: "Fixture", corporationId: "corp", adminIds: null, importMethod: null,
|
||||
extra: "preserve", staffIds: null, admin: [{ id: "s", name: "Staff", code: "S", active: true, secret: "omit" }] };
|
||||
|
||||
test("应用基础与详情查询共享请求形状,但保持不同投影和脱敏", async () => {
|
||||
const a = client({ items: [platform] }), b = client({ items: [platform] });
|
||||
const basic = (await listBasic(a.http, "corp"))[0]!;
|
||||
const detail = (await listDetailed(b.http, "corp"))[0]!;
|
||||
expect(a.calls).toEqual(b.calls);
|
||||
expect(a.calls[0]).toEqual({ path: "api/v2/datalink/platform/search", options: { searchParams: { scope: "enable", corpId: "corp" } } });
|
||||
expect(basic.adminIds).toEqual([]);
|
||||
expect(basic.importMethod).toEqual([]);
|
||||
expect(basic).not.toHaveProperty("extra");
|
||||
expect(basic).not.toHaveProperty("admin");
|
||||
expect(detail.extra).toBe("preserve");
|
||||
expect(detail.staffIds).toEqual([]);
|
||||
expect(detail.admin).toEqual([{ id: "s", name: "Staff", code: "S", active: true }]);
|
||||
});
|
||||
|
||||
test("应用查询保留缺失 items 和无效响应的既有差异", async () => {
|
||||
for (const body of [{}, { items: {} }, { code: 403, msg: "fixture" }]) {
|
||||
await expect(listBasic(client(body).http, "corp")).rejects.toThrow();
|
||||
expect(await listDetailed(client(body).http, "corp")).toEqual([]);
|
||||
}
|
||||
for (const query of [listBasic, listDetailed]) {
|
||||
await expect(query(client({ items: [{ name: "missing id" }] }).http, "corp")).rejects.toThrow();
|
||||
}
|
||||
});
|
||||
|
||||
test("全量应用及详情保留响应包装、ID 与 join 参数兼容", async () => {
|
||||
expect(await listAllPlatforms(client([platform]).http, "corp")).toEqual([]);
|
||||
expect((await listAllPlatforms(client({ items: [platform] }).http, "corp"))[0]?.id).toBe("app");
|
||||
for (const body of [platform, { data: platform }, { value: platform }]) {
|
||||
for (const id of ["app", "$app"]) {
|
||||
const f = client(body);
|
||||
expect((await getPlatform(f.http, "corp", id)).id).toBe("app");
|
||||
expect(f.calls[0]?.path).toBe("api/v2/datalink/platform/$app");
|
||||
const params = f.calls[0]?.options.searchParams as URLSearchParams;
|
||||
expect(params.get("id")).toBe("app");
|
||||
expect(params.getAll("join")).toEqual(["icon.fileId,fileId,/v1/attachment/attachments", "adminIds,admin,/v1/organization/staffs"]);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { KyInstance } from "ky";
|
||||
import * as api from "./api.js";
|
||||
import type { DatalinkPlatformDetail } from "./types.js";
|
||||
|
||||
export function listBasicPlatforms(...args: Parameters<typeof api.listBasicPlatforms>) { return api.listBasicPlatforms(...args); }
|
||||
export function listPlatforms(...args: Parameters<typeof api.listPlatforms>) { return api.listPlatforms(...args); }
|
||||
export function listAllPlatforms(...args: Parameters<typeof api.listAllPlatforms>) { return api.listAllPlatforms(...args); }
|
||||
export function getPlatform(...args: Parameters<typeof api.getPlatform>) { return api.getPlatform(...args); }
|
||||
|
||||
function matches(value: unknown, target: string): boolean {
|
||||
return typeof value === "string" && value.toLowerCase() === target.toLowerCase();
|
||||
}
|
||||
|
||||
export async function resolvePlatform(
|
||||
client: KyInstance,
|
||||
corpId: string,
|
||||
target: string
|
||||
): Promise<DatalinkPlatformDetail | undefined> {
|
||||
const platforms = await api.listPlatforms(client, corpId);
|
||||
const found = platforms.find((platform) =>
|
||||
[platform.id, platform.name, platform.code].some((value) => matches(value, target))
|
||||
);
|
||||
return found ? api.getPlatform(client, corpId, found.id) : undefined;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { z } from "zod";
|
||||
|
||||
// ── 自建应用(DataLink Platform)─────────────────────────────
|
||||
|
||||
export const PlatformSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
nameSpell: z.string().default(""),
|
||||
enName: z.string().nullable().optional(),
|
||||
code: z.string().default(""),
|
||||
corporationId: z.string(),
|
||||
active: z.boolean().default(true),
|
||||
type: z.string().default(""),
|
||||
groupType: z.string().default(""),
|
||||
platformType: z.string().default(""),
|
||||
source: z.string().default(""),
|
||||
adminIds: z.array(z.string()).nullish().transform((value) => value ?? []),
|
||||
desc: z.string().nullable().optional(),
|
||||
importMethod: z.array(z.string()).nullish().transform((value) => value ?? []),
|
||||
createTime: z.number().default(0),
|
||||
updateTime: z.number().default(0),
|
||||
});
|
||||
|
||||
export const DatalinkPlatformDetailSchema = PlatformSchema.passthrough().extend({
|
||||
staffIds: z.array(z.string()).nullish().transform((value) => value ?? []),
|
||||
roleIds: z.array(z.string()).nullish().transform((value) => value ?? []),
|
||||
departmentIds: z.array(z.string()).nullish().transform((value) => value ?? []),
|
||||
departmentsIncludeChildren: z.boolean().nullish().transform((value) => value ?? false),
|
||||
});
|
||||
|
||||
export type Platform = z.infer<typeof PlatformSchema>;
|
||||
export type DatalinkPlatformDetail = z.infer<typeof DatalinkPlatformDetailSchema>;
|
||||
Reference in new issue
Block a user