Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

1.5 KB
Raw Blame
import type {
  WebhookFilter,
  WebhookScope,
} from '../../../generated/storage/types.gen';
import { unwrap } from '../../utils';
import type { MacroClient } from '../../utils/client';
import { Webhook } from './webhook';

/**
 * Webhook registrations for receiving signed entity-event deliveries.
 *
 * @routing Use when managing API webhook subscriptions; not database webhooks.
 */
export class WebhooksNamespace {
  constructor(private readonly client: MacroClient) {}

  /**
   * Register a webhook. `namespace` must be unique among the owning
   * workspace's webhooks (the endpoint returns 409 on a clash) and cannot be
   * changed later. `filters` must be non-empty (each entry matches event
   * names, optionally narrowed to entity ids); `scope` defaults to `'user'`.
   */
  create(opts: {
    url: string;
    namespace: string;
    name: string;
    filters: WebhookFilter[];
    scope?: WebhookScope;
    headers?: Record<string, string>;
  }): Promise<Webhook> {
    return Webhook.create(this.client, opts);
  }

  /** A handle to an existing webhook by id. Details load on first access. */
  byId(id: string): Webhook {
    return Webhook.byId(this.client, id);
  }

  /**
   * The caller's webhooks across their personal and team workspaces, newest
   * first. Each returned handle already holds its record.
   */
  async list(): Promise<Webhook[]> {
    const { webhooks } = unwrap(await this.client.storage.listWebhooks());
    return webhooks.map((record) => Webhook.fromRecord(this.client, record));
  }
}