import type { DataSourceHandle, PropertyHandle } from "./notion-as-code/database.js"; import type { NotionAsCodeSchema } from "./notion-as-code/intents.js"; import type { RecurrenceSchedule } from "./notion-as-code/recurrence.js"; export type RecurrenceTrigger = { type: "recurrence" } & ( | RecurrenceSchedule | { frequency?: never } ); export type NotionPageCreatedTrigger = { type: "notion.page.created"; dataSourceResourceId?: string; }; export type NotionPageUpdatedTrigger = { type: "notion.page.updated"; dataSourceResourceId?: string; propertyResourceIds?: string[]; includePageContent?: boolean; }; type WatchablePropertyType = | "title" | "text" | "number" | "select" | "multi_select" | "status" | "date" | "person" | "checkbox" | "url" | "email" | "phone_number" | "relation"; type WatchableProperty = Property extends { type: WatchablePropertyType; } ? PropertyHandle : never; type SourceProperty = WatchableProperty; export type PageUpdatedOptions = { dataSource: DataSourceHandle; } & ( | { properties: readonly [ SourceProperty>, ...SourceProperty>[], ]; includePageContent?: boolean; } | { properties?: readonly SourceProperty>[]; includePageContent: true } ); /** Configure a recurrence, or omit options to set it up in Notion. */ export function scheduled(options?: RecurrenceSchedule): RecurrenceTrigger { return { type: "recurrence", ...options }; } /** Watch new rows in a declared data source, or configure the source in Notion. */ export function notionPageCreated(options?: { dataSource: DataSourceHandle; }): NotionPageCreatedTrigger { return { type: "notion.page.created", ...(options ? { dataSourceResourceId: options.dataSource.resourceId } : {}), }; } /** Watch typed properties and/or page content in a declared data source. */ export function notionPageUpdated( options?: PageUpdatedOptions, ): NotionPageUpdatedTrigger { if (!options) return { type: "notion.page.updated" }; const { dataSource, properties = [], includePageContent = false } = options; if (properties.length === 0 && !includePageContent) { throw new Error("Page-updated events must watch at least one property or page content"); } for (const property of properties) { if ( property.dataSourceResourceId !== dataSource.resourceId || dataSource.properties[property.name]?.resourceId !== property.resourceId ) { throw new Error( `Property "${property.name}" does not belong to data source "${dataSource.resourceId}"`, ); } } return { type: "notion.page.updated", dataSourceResourceId: dataSource.resourceId, ...(properties.length ? { propertyResourceIds: [ ...new Set(properties.map((property) => property.resourceId)), ], } : {}), includePageContent, }; }