/** * The bindings vocabulary on the binary: a binding is a trigger bound to an * event type, so the wire shape is the trigger's. The status ramp is * binding-draft.ts's. */ import type { EventType } from "../captures/event-types"; import type { Trigger, TriggerInput } from "../lib/api"; /** A trigger bound to an event type, named by the trigger's name. */ export interface Binding { id: string; name: string; source: string; event_type_id?: string; mapping_id?: string; workflow?: string; status?: string; /** The source takes deliveries with no verification. */ unsigned?: boolean; } export function bindingOfTrigger(trigger: Trigger): Binding { return { id: trigger.name, name: trigger.name, source: trigger.source ?? "", event_type_id: trigger.event_type_id, mapping_id: trigger.mapping_id, workflow: trigger.workflow, status: trigger.status, unsigned: trigger.unsigned, }; } /** The fields of GET /api/mappings this page needs. */ export interface MappingOption { id: string; name: string; event_type_id?: string; } /** The mappings that belong to one event type, which are the ones a binding * on that type can use. */ export function mappingsForEventType( mappings: MappingOption[], eventTypeId: string, ): MappingOption[] { if (!eventTypeId) return []; return mappings.filter((m) => m.event_type_id === eventTypeId); } /** A workflow a binding can start. */ export interface WorkflowOption { id: string; name: string; } /** The editor's field state. */ export interface BindingDraft { /** The trigger's name when editing; null for a new binding. */ id: string | null; name: string; /** The event type the binding applies to; its source is the binding's. */ eventTypeId: string; mappingId: string; workflow: string; } export function emptyDraft(): BindingDraft { return { id: null, name: "", eventTypeId: "", mappingId: "", workflow: "" }; } export function draftFromBinding(binding: Binding): BindingDraft { return { id: binding.id, name: binding.name, eventTypeId: binding.event_type_id || "", mappingId: binding.mapping_id || "", workflow: binding.workflow || "", }; } /** The body PUT /api/triggers/{name} takes. A saved binding waits for * approval again. */ export function bindingPayload( draft: BindingDraft, eventTypes: EventType[], ): TriggerInput { const eventType = eventTypes.find((t) => t.id === draft.eventTypeId); return { source: eventType?.source ?? "", event_type_id: draft.eventTypeId, mapping_id: draft.mappingId || undefined, workflow: draft.workflow, disabled: false, pending: true, }; }