技能 编程开发 Hex网络钩子和事件处理

Hex网络钩子和事件处理

v20260423
hex-webhooks-events
本技能指导开发者如何实现Hex CI/CD工作流的事件驱动逻辑。由于Hex不提供原生推送网络钩子,本方法展示了如何使用回调函数通过轮询(polling)来可靠地检测任务的完成或失败状态。这对于将Hex流水线与Slack或自定义通知系统等外部服务集成至关重要。
获取技能
479 次下载
概览

Hex Webhooks & Events

Overview

Hex doesn't provide push webhooks. For event-driven integrations, poll run status or build your own notification system around run completions.

Instructions

Run Status Polling with Callback

async function runWithCallback(
  client: HexClient,
  projectId: string,
  params: Record<string, any>,
  onComplete: (result: any) => void,
  onError: (error: Error) => void
) {
  try {
    const { runId } = await client.runProject(projectId, params);
    const poll = async () => {
      const status = await client.getRunStatus(projectId, runId);
      if (status.status === 'COMPLETED') { onComplete(status); return; }
      if (status.status === 'ERRORED' || status.status === 'KILLED') { onError(new Error(status.status)); return; }
      setTimeout(poll, 5000);
    };
    poll();
  } catch (err) { onError(err as Error); }
}

Notify on Completion

runWithCallback(client, 'project-id', { date: '2025-01-01' },
  (result) => {
    // Send Slack notification, email, etc.
    fetch(process.env.SLACK_WEBHOOK_URL!, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ text: `Hex project completed: ${result.runId}` }),
    });
  },
  (error) => console.error('Run failed:', error)
);

Resources

信息
Category 编程开发
Name hex-webhooks-events
版本 v20260423
大小 1.91KB
更新时间 2026-04-26
语言