import { ConnectorMetadataSchema, type RawObservation } from "@market-atlas/market-model"; import type { ConnectorDefinition } from "./types.js"; /** Validates metadata eagerly so a mis-declared connector fails at load time, not at 3 a.m. */ export function defineConnector(def: ConnectorDefinition): ConnectorDefinition { const parsed = ConnectorMetadataSchema.safeParse(def.metadata); if (!parsed.success) { throw new Error(`connector "${def.metadata?.id ?? "?"}" has invalid metadata: ${parsed.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ")}`); } if (!def.start && !def.poll) throw new Error(`connector "${def.metadata.id}" must implement start() or poll()`); if (def.poll && !def.schedule) throw new Error(`connector "${def.metadata.id}" polls but declares no schedule`); if (def.metadata.rightsStatus === "UNKNOWN") { // Allowed to exist, but the runtime will refuse to expose its values publicly. } return def; } /** Helper for connectors to build raw observations consistently. */ export function raw(connectorId: string, sourceId: string, kind: string, payload: unknown, meta?: Record): RawObservation { return { connectorId, sourceId, kind, payload, receivedAt: Date.now(), ...(meta ? { meta } : {}) }; }