import { z } from "zod"; import { KyInstance } from "ky"; import { PlatformSchema, WorkflowPageSchema, WorkflowStatsSchema, WorkflowChargeSchema, WorkflowDetailSchema, WorkflowJobSchema, WorkflowDslSchema, StaffSchema, WebhookInfoSchema, WorkflowSampleDataSchema, } from "./types.js"; import type { WorkflowState } from "./types.js"; // Helper to unwrap { data: T } response async function unwrap(response: Response, schema: z.ZodType): Promise { const json = (await response.json()) as { data: unknown }; return schema.parse(json.data); } // Helper for simple success responses async function ok(response: Response): Promise<{ success: boolean }> { return { success: response.status === 200 }; } // ── 自建应用 ────────────────────────────────────────────────────────────────── export async function listPlatforms( client: KyInstance, corpId: string ): Promise { const response = await client.get("api/v2/datalink/platform/search", { searchParams: { scope: "enable", corpId }, }); const json = (await response.json()) as { items: unknown[] }; return z.array(PlatformSchema).parse(json.items); } // ── 工作流(EASO)───────────────────────────────────────────────────────────── // 注意:EASO 客户端已自动注入 corpId,无需再传 export async function listWorkflows( client: KyInstance, platformId: string, page: number = 1, pageSize: number = 20 ): Promise { const response = await client.get("api/easo/pipelines/logs", { searchParams: { platformId, number: String(page), pageSize: String(pageSize), }, }); return unwrap(response, WorkflowPageSchema); } export async function getWorkflowStats( client: KyInstance, platformId: string ): Promise { const response = await client.get("api/easo/stats", { searchParams: { platformId }, }); return unwrap(response, WorkflowStatsSchema); } export async function getWorkflowCharge( client: KyInstance ): Promise { const response = await client.get("api/easo/charge"); return unwrap(response, WorkflowChargeSchema); } // ── 工作流详情(Piper)──────────────────────────────────────────────────────── export async function getWorkflowDetail( client: KyInstance, pipelineId: string ): Promise { const response = await client.get(`api/easo/pipelines/${pipelineId}`); return unwrap(response, WorkflowDetailSchema); } // ── 工作流启停(Piper)──────────────────────────────────────────────────────── export async function toggleWorkflow( client: KyInstance, pipelineId: string, state: WorkflowState ): Promise<{ success: boolean }> { const response = await client.put(`api/easo/pipelines/state/${pipelineId}`, { searchParams: { state: String(state) }, }); return ok(response); } // ── 运行中的任务(Piper)────────────────────────────────────────────────────── export async function listRunningJobs( client: KyInstance, pipelineId: string, runType: "DEBUG" | "PROD" = "DEBUG" ): Promise { const response = await client.get("api/easo/jobs/running", { searchParams: { pipelineId, runType }, }); return unwrap(response, z.array(WorkflowJobSchema)); } // ── DSL 配置(Piper)────────────────────────────────────────────────────────── export async function listDsl( client: KyInstance ): Promise { const response = await client.get("api/easo/dsl/all"); return unwrap(response, z.array(WorkflowDslSchema)); } // ── 员工列表(Piper)────────────────────────────────────────────────────────── export async function listStaffs( client: KyInstance, external: boolean = false ): Promise { const response = await client.get("api/easo/common/staffs", { searchParams: { external: String(external) }, }); return unwrap(response, z.array(StaffSchema)); } // ── Webhook 信息(Piper)────────────────────────────────────────────────────── export async function getWebhookInfo( client: KyInstance, pipelineId: string ): Promise { const response = await client.get(`api/webhook/${pipelineId}`); return unwrap(response, WebhookInfoSchema); } // ── 测试/采样数据(Piper)───────────────────────────────────────────────────── export async function sampleWorkflowData( client: KyInstance, pipelineId: string ): Promise { const response = await client.post(`api/easo/pipelines/${pipelineId}/sampleData`); return WorkflowSampleDataSchema.parse(await response.json()); }