@newhomestar/sdk 0.6.7 → 0.6.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/events.js ADDED
@@ -0,0 +1,503 @@
1
+ // @newhomestar/sdk/events — Transactional outbox + HTTP event client
2
+ // =====================================================================
3
+ // Producer-side helpers for emitting platform events safely.
4
+ //
5
+ // The central problem: a service can write to its DB successfully and
6
+ // then fail to call POST /events/queue, leaving state changed but the
7
+ // event missing. The transactional outbox solves this by writing data
8
+ // + outbox row in ONE atomic transaction — they succeed or fail together.
9
+ //
10
+ // ─── Usage ───────────────────────────────────────────────────────────
11
+ //
12
+ // Producer service (e.g. HRIS):
13
+ //
14
+ // import { withEventOutbox, startOutboxRelay } from '@newhomestar/sdk/events';
15
+ //
16
+ // // In a route handler — atomically writes data + emits event:
17
+ // const employee = await withEventOutbox(db, async (tx) => {
18
+ // const row = await tx.hrisEmployee.update({ where: { id }, data });
19
+ // return {
20
+ // events: [{ entity_type: 'employee', action: 'updated', entity_id: id, ... }],
21
+ // result: row,
22
+ // };
23
+ // });
24
+ //
25
+ // // In layout.tsx — starts the retry loop for any failed relays:
26
+ // startOutboxRelay(db);
27
+ //
28
+ // ─── Required env vars ───────────────────────────────────────────────
29
+ // NOVA_EVENTS_SERVICE_URL — URL of the nova-events-service
30
+ // NOVA_SERVICE_TOKEN — JWT with scope to POST /events/queue
31
+ // NOVA_SERVICE_SLUG — Stamped as source_service on every event
32
+ // =====================================================================
33
+ import dotenv from 'dotenv';
34
+ // Load .env.local in dev if NOVA_EVENTS_SERVICE_URL not already set
35
+ if (!process.env.NOVA_EVENTS_SERVICE_URL) {
36
+ dotenv.config({ path: '.env.local', override: false });
37
+ }
38
+ // ── Internal helpers ───────────────────────────────────────────────────────────
39
+ function _getEnvOrThrow(key, context) {
40
+ const val = process.env[key];
41
+ if (!val)
42
+ throw new Error(`[${context}] ${key} env var is required but not set`);
43
+ return val;
44
+ }
45
+ /**
46
+ * Relay a single outbox row to the events service and update the outbox.
47
+ * Used by both withEventOutbox (immediate relay) and startOutboxRelay (retry loop).
48
+ */
49
+ async function _relayRow(db, row, eventsUrl, serviceToken) {
50
+ let lastError = null;
51
+ try {
52
+ const body = {
53
+ ...row.payload,
54
+ idempotency_key: row.idempotencyKey,
55
+ };
56
+ const res = await fetch(`${eventsUrl}/events/queue`, {
57
+ method: 'POST',
58
+ headers: {
59
+ 'Authorization': `Bearer ${serviceToken}`,
60
+ 'Content-Type': 'application/json',
61
+ 'Idempotency-Key': row.idempotencyKey,
62
+ },
63
+ body: JSON.stringify(body),
64
+ });
65
+ if (res.ok) {
66
+ await db.eventOutbox.update({
67
+ where: { id: row.id },
68
+ data: { delivered: true, deliveredAt: new Date() },
69
+ });
70
+ return;
71
+ }
72
+ // Non-2xx: treat as transient failure, update error fields
73
+ const text = await res.text().catch(() => `HTTP ${res.status}`);
74
+ lastError = `HTTP ${res.status}: ${text}`.slice(0, 500);
75
+ }
76
+ catch (err) {
77
+ lastError = String(err).slice(0, 500);
78
+ }
79
+ // Relay failed — record the attempt
80
+ await db.eventOutbox.update({
81
+ where: { id: row.id },
82
+ data: {
83
+ attempts: { increment: 1 },
84
+ lastAttemptAt: new Date(),
85
+ lastError,
86
+ },
87
+ }).catch((updateErr) => {
88
+ console.error('[nova/events] Failed to update outbox row after relay failure:', updateErr);
89
+ });
90
+ }
91
+ // ── isIntegrationSync ─────────────────────────────────────────────────────────
92
+ /**
93
+ * Returns `true` when a request originated from an integration sync or worker,
94
+ * indicated by the `x-source: integration_sync` header.
95
+ *
96
+ * **Producer side** — pass `req` to `withServiceEventOutbox()` and this check is
97
+ * done for you automatically. Use this helper directly only when you need
98
+ * conditional logic beyond event emission (e.g. skipping a side-effect).
99
+ *
100
+ * **Consumer side** — integration workers MUST check this before writing back to
101
+ * the source system to avoid infinite sync loops:
102
+ *
103
+ * @example
104
+ * // In a worker handler — don't write back to BambooHR if BambooHR caused the event
105
+ * if (isIntegrationSync(event)) return { status: 'skipped' };
106
+ * await ctx.fetch(`https://api.bamboohr.com/...`, { method: 'PUT', ... });
107
+ *
108
+ * @example
109
+ * // In a Next.js route handler (producer side) — pass req instead of calling this
110
+ * const employee = await withServiceEventOutbox(db, req, async (tx, emit) => { ... });
111
+ */
112
+ export function isIntegrationSync(reqOrEvent) {
113
+ // Next.js / fetch Request object
114
+ if (typeof reqOrEvent.headers?.get === 'function') {
115
+ return reqOrEvent.headers.get('x-source') === 'integration_sync';
116
+ }
117
+ // Plain event/message object (consumer side: check metadata stamped at emit time)
118
+ const meta = reqOrEvent?.metadata;
119
+ return meta?.source === 'integration_sync';
120
+ }
121
+ /**
122
+ * Run a Prisma transaction that atomically writes business data + outbox rows,
123
+ * then immediately relays events to the Nova Events Service.
124
+ *
125
+ * This is the **preferred** high-level API for emitting events from service
126
+ * route handlers. It is source-aware: if the incoming request carries
127
+ * `x-source: integration_sync` or `x-integration-id: <slug>`, those values are
128
+ * automatically stamped on every event's `metadata` field. This lets downstream
129
+ * consumers self-filter to prevent sync loops without suppressing events for
130
+ * other subscribers.
131
+ *
132
+ * Events **always fan out** to all subscriber queues — no events are silently
133
+ * dropped. An integration worker that receives an event it caused should check
134
+ * `isIntegrationSync(event)` and skip processing if true.
135
+ *
136
+ * @param db - Prisma client instance (must have `eventOutbox` model)
137
+ * @param reqOrCallback - Either the inbound `Request` (for source tagging) OR
138
+ * the callback directly (if source context is not needed)
139
+ * @param maybeCallback - The callback when `req` is provided as the second arg
140
+ *
141
+ * @example
142
+ * // Route handler — source metadata auto-stamped from request headers
143
+ * export async function POST(req: Request) {
144
+ * const employee = await withServiceEventOutbox(db, req, async (tx, emit) => {
145
+ * const row = await tx.hrisEmployee.create({ data });
146
+ * emit('employee.created', { id: row.id, firstName: row.firstName });
147
+ * return row;
148
+ * });
149
+ * return NextResponse.json(employee);
150
+ * }
151
+ *
152
+ * @example
153
+ * // Without a request (background job, scheduled task)
154
+ * const result = await withServiceEventOutbox(db, async (tx, emit) => {
155
+ * const row = await tx.hrisEmployee.update({ where: { id }, data });
156
+ * emit('employee.updated', { id, ...changes });
157
+ * return row;
158
+ * });
159
+ */
160
+ export async function withServiceEventOutbox(db, reqOrCallback, maybeCallback) {
161
+ // ── Resolve overloads ────────────────────────────────────────────────────
162
+ const req = typeof reqOrCallback !== 'function' ? reqOrCallback : undefined;
163
+ const callback = (typeof reqOrCallback === 'function' ? reqOrCallback : maybeCallback);
164
+ const eventsUrl = process.env.NOVA_EVENTS_SERVICE_URL;
165
+ const serviceToken = process.env.NOVA_SERVICE_TOKEN;
166
+ const serviceSlug = process.env.NOVA_SERVICE_SLUG;
167
+ if (!eventsUrl || !serviceToken) {
168
+ throw new Error('[withServiceEventOutbox] NOVA_EVENTS_SERVICE_URL and NOVA_SERVICE_TOKEN must be set. ' +
169
+ 'Add them to your .env.local or deployment environment.');
170
+ }
171
+ // ── Extract source metadata from request headers ─────────────────────────
172
+ // x-source: set by integration sync handlers (value: "integration_sync")
173
+ // x-integration-id: set by integration workers to identify the source system
174
+ const xSource = req?.headers?.get('x-source') ?? 'user';
175
+ const integrationId = req?.headers?.get('x-integration-id') ?? undefined;
176
+ const stagedEmits = [];
177
+ const outboxRows = [];
178
+ // ── Atomic: callback + outbox INSERTs in one transaction ─────────────────
179
+ const result = await db.$transaction(async (tx) => {
180
+ // The emit fn is synchronous — events are staged and written after callback returns
181
+ const emit = (topic, payload, idempotencyKey) => {
182
+ stagedEmits.push({ topic, payload, idempotencyKey });
183
+ };
184
+ const callbackResult = await callback(tx, emit);
185
+ // Write one outbox row per staged emit
186
+ for (const staged of stagedEmits) {
187
+ // ── Derive entity_type + action from topic string ──────────────────
188
+ // Supports dot notation ("employee.created" → entity_type=employee, action=created)
189
+ // dotted path ("hris.employee.created" → entity_type=hris.employee, action=created)
190
+ // UPPER_SNAKE ("EMPLOYEE_CREATED" → entity_type=employee, action=created)
191
+ // bare string ("employee" → entity_type=employee, action=event)
192
+ let entity_type;
193
+ let action;
194
+ if (staged.topic.includes('.')) {
195
+ const lastDot = staged.topic.lastIndexOf('.');
196
+ entity_type = staged.topic.slice(0, lastDot);
197
+ action = staged.topic.slice(lastDot + 1);
198
+ }
199
+ else if (staged.topic.includes('_')) {
200
+ const firstUnderscore = staged.topic.indexOf('_');
201
+ entity_type = staged.topic.slice(0, firstUnderscore).toLowerCase();
202
+ action = staged.topic.slice(firstUnderscore + 1).toLowerCase().replace(/_/g, '.');
203
+ }
204
+ else {
205
+ entity_type = staged.topic.toLowerCase();
206
+ action = 'event';
207
+ }
208
+ const fullPayload = {
209
+ entity_type,
210
+ action,
211
+ source_service: serviceSlug ?? undefined,
212
+ attributes: staged.payload,
213
+ metadata: {
214
+ source: xSource,
215
+ ...(integrationId ? { integration_id: integrationId } : {}),
216
+ },
217
+ };
218
+ const row = await tx.eventOutbox.create({
219
+ data: {
220
+ eventType: staged.topic,
221
+ payload: fullPayload,
222
+ ...(staged.idempotencyKey
223
+ ? { idempotencyKey: staged.idempotencyKey }
224
+ : {}),
225
+ },
226
+ select: { id: true, idempotencyKey: true },
227
+ });
228
+ outboxRows.push({
229
+ id: row.id,
230
+ idempotencyKey: row.idempotencyKey,
231
+ payload: fullPayload,
232
+ });
233
+ }
234
+ return callbackResult;
235
+ });
236
+ // ── Immediate relay: best-effort POST after commit ────────────────────────
237
+ await Promise.allSettled(outboxRows.map(row => _relayRow(db, row, eventsUrl, serviceToken)));
238
+ return result;
239
+ }
240
+ // ── withEventOutbox ────────────────────────────────────────────────────────────
241
+ /**
242
+ * Run a Prisma transaction that atomically writes business data + an outbox row,
243
+ * then immediately relays the event to the Nova Events Service.
244
+ *
245
+ * If the relay fails (network error or non-2xx), the outbox row is preserved and
246
+ * startOutboxRelay() will retry it with exponential backoff.
247
+ *
248
+ * @param db - Prisma client instance (must have `eventOutbox` model)
249
+ * @param callback - Receives the transaction `tx`; must return `{ events, result }`
250
+ *
251
+ * @example
252
+ * const employee = await withEventOutbox(db, async (tx) => {
253
+ * const row = await tx.hrisEmployee.update({ where: { id }, data: updates });
254
+ * return {
255
+ * events: [{
256
+ * entity_type: 'employee',
257
+ * action: 'updated',
258
+ * entity_id: id,
259
+ * diff: { before: old, after: mapRow(row), changed_fields },
260
+ * }],
261
+ * result: row,
262
+ * };
263
+ * });
264
+ */
265
+ export async function withEventOutbox(db, callback) {
266
+ const eventsUrl = process.env.NOVA_EVENTS_SERVICE_URL;
267
+ const serviceToken = process.env.NOVA_SERVICE_TOKEN;
268
+ const serviceSlug = process.env.NOVA_SERVICE_SLUG;
269
+ if (!eventsUrl || !serviceToken) {
270
+ throw new Error('[withEventOutbox] NOVA_EVENTS_SERVICE_URL and NOVA_SERVICE_TOKEN must be set. ' +
271
+ 'Add them to your .env.local or deployment environment.');
272
+ }
273
+ // Rows inserted in the transaction — relayed after commit
274
+ const outboxRows = [];
275
+ // ── Atomic: business write + outbox INSERT in one transaction ──────────────
276
+ const result = await db.$transaction(async (tx) => {
277
+ const { events, result: callbackResult } = await callback(tx);
278
+ for (const event of events) {
279
+ const fullPayload = {
280
+ ...event,
281
+ source_service: event.source_service ?? serviceSlug ?? undefined,
282
+ };
283
+ const row = await tx.eventOutbox.create({
284
+ data: {
285
+ eventType: `${event.entity_type}.${event.action}`,
286
+ payload: fullPayload,
287
+ },
288
+ select: { id: true, idempotencyKey: true },
289
+ });
290
+ outboxRows.push({
291
+ id: row.id,
292
+ idempotencyKey: row.idempotencyKey,
293
+ payload: fullPayload,
294
+ });
295
+ }
296
+ return callbackResult;
297
+ });
298
+ // ── Immediate relay: best-effort POST after commit ─────────────────────────
299
+ // Failures are recorded in the outbox and retried by startOutboxRelay().
300
+ // We fire all relays concurrently (most calls have a single event anyway).
301
+ await Promise.allSettled(outboxRows.map(row => _relayRow(db, row, eventsUrl, serviceToken)));
302
+ return result;
303
+ }
304
+ // ── logEvent ───────────────────────────────────────────────────────────────────
305
+ /**
306
+ * POST /events — Record an event in the audit log WITHOUT queue fan-out.
307
+ *
308
+ * Use for:
309
+ * • Inbound sync audits (metadata.source = "integration_sync")
310
+ * • Read events ("employee.viewed")
311
+ * • System events that don't need integration write-back
312
+ *
313
+ * For events that need downstream processing, use queueEvent() or withEventOutbox().
314
+ */
315
+ export async function logEvent(payload) {
316
+ const eventsUrl = _getEnvOrThrow('NOVA_EVENTS_SERVICE_URL', 'logEvent');
317
+ const serviceToken = _getEnvOrThrow('NOVA_SERVICE_TOKEN', 'logEvent');
318
+ const serviceSlug = process.env.NOVA_SERVICE_SLUG;
319
+ const res = await fetch(`${eventsUrl}/events`, {
320
+ method: 'POST',
321
+ headers: {
322
+ 'Authorization': `Bearer ${serviceToken}`,
323
+ 'Content-Type': 'application/json',
324
+ },
325
+ body: JSON.stringify({
326
+ ...payload,
327
+ source_service: payload.source_service ?? serviceSlug,
328
+ }),
329
+ });
330
+ if (!res.ok) {
331
+ const text = await res.text().catch(() => '');
332
+ throw new Error(`[logEvent] HTTP ${res.status}: ${text}`);
333
+ }
334
+ return res.json();
335
+ }
336
+ // ── queueEvent ─────────────────────────────────────────────────────────────────
337
+ /**
338
+ * POST /events/queue — Log an event AND fan-out to all active subscriber queues.
339
+ *
340
+ * Use when there is NO database transaction context (e.g. a scheduled job,
341
+ * a webhook handler that doesn't update its own DB first).
342
+ *
343
+ * When you have a DB transaction, prefer withEventOutbox() for atomicity.
344
+ *
345
+ * @throws if the events service returns non-2xx (no retry logic)
346
+ */
347
+ export async function queueEvent(payload) {
348
+ const eventsUrl = _getEnvOrThrow('NOVA_EVENTS_SERVICE_URL', 'queueEvent');
349
+ const serviceToken = _getEnvOrThrow('NOVA_SERVICE_TOKEN', 'queueEvent');
350
+ const serviceSlug = process.env.NOVA_SERVICE_SLUG;
351
+ const res = await fetch(`${eventsUrl}/events/queue`, {
352
+ method: 'POST',
353
+ headers: {
354
+ 'Authorization': `Bearer ${serviceToken}`,
355
+ 'Content-Type': 'application/json',
356
+ },
357
+ body: JSON.stringify({
358
+ ...payload,
359
+ source_service: payload.source_service ?? serviceSlug,
360
+ }),
361
+ });
362
+ if (!res.ok) {
363
+ const text = await res.text().catch(() => '');
364
+ throw new Error(`[queueEvent] HTTP ${res.status}: ${text}`);
365
+ }
366
+ return res.json();
367
+ }
368
+ // ── startOutboxRelay ───────────────────────────────────────────────────────────
369
+ /**
370
+ * Start a background interval that retries undelivered outbox rows.
371
+ *
372
+ * Call ONCE at service startup (e.g. in layout.tsx or the service entry point).
373
+ * Returns the interval handle so you can clear it in tests or graceful shutdown.
374
+ *
375
+ * Retry schedule (exponential backoff): 2^attempts * 5s
376
+ * attempt 0 → 5s, attempt 1 → 10s, attempt 2 → 20s,
377
+ * attempt 3 → 40s, attempt 4 → 80s
378
+ *
379
+ * Rows with attempts >= maxAttempts (default 5) are skipped and left for
380
+ * a manual reconciler — they won't be retried automatically.
381
+ *
382
+ * @param db - Prisma client instance (must have `eventOutbox` model)
383
+ * @param options - Relay configuration
384
+ *
385
+ * @example
386
+ * // src/app/layout.tsx
387
+ * import { startOutboxRelay } from '@newhomestar/sdk/events';
388
+ * import { db } from '@/lib/db';
389
+ *
390
+ * startOutboxRelay(db);
391
+ */
392
+ export function startOutboxRelay(db, options = {}) {
393
+ const { intervalMs = 60_000, maxAttempts = 5, eventsUrl = process.env.NOVA_EVENTS_SERVICE_URL, serviceToken = process.env.NOVA_SERVICE_TOKEN, } = options;
394
+ if (!eventsUrl || !serviceToken) {
395
+ console.warn('[nova/events] startOutboxRelay: NOVA_EVENTS_SERVICE_URL or NOVA_SERVICE_TOKEN not set — ' +
396
+ 'outbox relay is DISABLED. Set these env vars to enable automatic retry.');
397
+ // Return a no-op interval that never fires
398
+ return setInterval(() => { }, 2_147_483_647);
399
+ }
400
+ console.log(`[nova/events] Outbox relay started (interval: ${intervalMs}ms, maxAttempts: ${maxAttempts})`);
401
+ const handle = setInterval(async () => {
402
+ try {
403
+ const now = new Date();
404
+ // Find undelivered rows where the backoff window has elapsed.
405
+ // The query fetches rows where last_attempt_at is old enough for the
406
+ // minimum backoff (5s for attempt 0). The per-row check below applies
407
+ // the precise 2^attempts * 5 calculation to skip rows still in cooldown.
408
+ const undelivered = await db.eventOutbox.findMany({
409
+ where: {
410
+ delivered: false,
411
+ attempts: { lt: maxAttempts },
412
+ OR: [
413
+ { lastAttemptAt: null },
414
+ { lastAttemptAt: { lt: new Date(now.getTime() - 5_000) } },
415
+ ],
416
+ },
417
+ orderBy: [{ attempts: 'asc' }, { createdAt: 'asc' }],
418
+ take: 50,
419
+ });
420
+ for (const row of undelivered) {
421
+ // Precise per-row backoff gate: skip if cooldown hasn't elapsed
422
+ if (row.lastAttemptAt != null) {
423
+ const backoffMs = Math.pow(2, row.attempts) * 5_000;
424
+ if (now.getTime() - row.lastAttemptAt.getTime() < backoffMs) {
425
+ continue;
426
+ }
427
+ }
428
+ await _relayRow(db, row, eventsUrl, serviceToken);
429
+ }
430
+ // Cleanup: delete delivered rows older than 7 days (keep audit trail brief)
431
+ await db.eventOutbox.deleteMany({
432
+ where: {
433
+ delivered: true,
434
+ deliveredAt: { lt: new Date(now.getTime() - 7 * 24 * 60 * 60 * 1_000) },
435
+ },
436
+ });
437
+ }
438
+ catch (err) {
439
+ console.error('[nova/events] Outbox relay cycle error:', err);
440
+ }
441
+ }, intervalMs);
442
+ return handle;
443
+ }
444
+ // ── NovaEventsClient ───────────────────────────────────────────────────────────
445
+ /**
446
+ * Class-based client when you need more control than the top-level functions.
447
+ * Useful when you want to pass configuration explicitly rather than relying on env vars.
448
+ *
449
+ * @example
450
+ * const client = new NovaEventsClient({ serviceSlug: 'my-service' });
451
+ * await client.queueEvent({ entity_type: 'employee', action: 'updated', ... });
452
+ * client.startOutboxRelay(db);
453
+ */
454
+ export class NovaEventsClient {
455
+ eventsUrl;
456
+ serviceToken;
457
+ serviceSlug;
458
+ constructor(options) {
459
+ this.eventsUrl = options?.eventsUrl ?? process.env.NOVA_EVENTS_SERVICE_URL ?? '';
460
+ this.serviceToken = options?.serviceToken ?? process.env.NOVA_SERVICE_TOKEN ?? '';
461
+ this.serviceSlug = options?.serviceSlug ?? process.env.NOVA_SERVICE_SLUG;
462
+ }
463
+ /** POST /events — audit log only */
464
+ async logEvent(payload) {
465
+ return logEvent({ ...payload, source_service: payload.source_service ?? this.serviceSlug });
466
+ }
467
+ /** POST /events/queue — log + fan-out to all subscriber queues */
468
+ async queueEvent(payload) {
469
+ return queueEvent({ ...payload, source_service: payload.source_service ?? this.serviceSlug });
470
+ }
471
+ /** Transactional outbox helper (use in services with a Prisma DB) */
472
+ async withOutbox(db, callback) {
473
+ // Temporarily override env for the helpers
474
+ const original = {
475
+ eventsUrl: process.env.NOVA_EVENTS_SERVICE_URL,
476
+ serviceToken: process.env.NOVA_SERVICE_TOKEN,
477
+ serviceSlug: process.env.NOVA_SERVICE_SLUG,
478
+ };
479
+ process.env.NOVA_EVENTS_SERVICE_URL = this.eventsUrl;
480
+ process.env.NOVA_SERVICE_TOKEN = this.serviceToken;
481
+ if (this.serviceSlug)
482
+ process.env.NOVA_SERVICE_SLUG = this.serviceSlug;
483
+ try {
484
+ return await withEventOutbox(db, callback);
485
+ }
486
+ finally {
487
+ if (original.eventsUrl !== undefined)
488
+ process.env.NOVA_EVENTS_SERVICE_URL = original.eventsUrl;
489
+ if (original.serviceToken !== undefined)
490
+ process.env.NOVA_SERVICE_TOKEN = original.serviceToken;
491
+ if (original.serviceSlug !== undefined)
492
+ process.env.NOVA_SERVICE_SLUG = original.serviceSlug;
493
+ }
494
+ }
495
+ /** Start the background outbox retry relay */
496
+ startOutboxRelay(db, options) {
497
+ return startOutboxRelay(db, {
498
+ ...options,
499
+ eventsUrl: this.eventsUrl,
500
+ serviceToken: this.serviceToken,
501
+ });
502
+ }
503
+ }
package/dist/index.d.ts CHANGED
@@ -75,6 +75,20 @@ export interface JWTPayload {
75
75
  export interface ActionCtx {
76
76
  jobId: string;
77
77
  progress: (percent: number, meta?: unknown) => void;
78
+ /**
79
+ * Number of times this message has been delivered/retried.
80
+ * Populated in SSE worker mode; undefined in HTTP server mode.
81
+ * Use for alerting when read_ct is high (approaching DLQ threshold).
82
+ */
83
+ read_ct?: number;
84
+ /**
85
+ * Extend the message visibility lease for long-running handlers.
86
+ * Call periodically to prevent the message from re-appearing in the queue
87
+ * before processing is complete. SSE worker mode only.
88
+ *
89
+ * @param extend_by - Seconds to extend the lease (default: 30)
90
+ */
91
+ heartbeat?: (extend_by?: number) => Promise<void>;
78
92
  /** Raw HTTP headers from the inbound request (HTTP mode only) */
79
93
  headers?: Record<string, string | string[] | undefined>;
80
94
  /** Bearer token extracted from the Authorization header (HTTP mode only) */
@@ -231,11 +245,13 @@ export declare function runHttpServer<T extends WorkerDef>(def: T, opts?: HttpSe
231
245
  export declare function runDualMode<T extends WorkerDef>(def: T, opts?: {
232
246
  port?: number;
233
247
  }): void;
248
+ export { withServiceEventOutbox, withEventOutbox, isIntegrationSync, queueEvent, logEvent, startOutboxRelay, NovaEventsClient, } from './events.js';
249
+ export type { QueueEventPayload, LogEventPayload, OutboxRelayOptions, ServiceEmitFn, } from './events.js';
234
250
  export type { ZodTypeAny as SchemaAny, ZodTypeAny };
235
251
  export { parseNovaSpec } from "./parseSpec.js";
236
252
  export type { NovaSpec } from "./parseSpec.js";
237
253
  export { defineIntegration, validateIntegration, integrationSchema, integrationEvent, integrationFunction, schema, event, IntegrationDefSchema, } from "./integration.js";
238
- export type { IntegrationDef, IntegrationSchemaDef, IntegrationEventDef, IntegrationFunctionDef, ValidationResult, SchemaType, ParamMeta, ParamIn, ParamUiType, } from "./integration.js";
254
+ export type { IntegrationDef, IntegrationSchemaDef, IntegrationEventDef, IntegrationFunctionDef, ValidationResult, SchemaType, ParamMeta, ParamIn, ParamUiType, SyncMappingDef, SyncMappingFieldDef, } from "./integration.js";
239
255
  export { parseIntegrationSpec, IntegrationSpecSchema } from "./integrationSpec.js";
240
256
  export type { IntegrationSpec } from "./integrationSpec.js";
241
257
  export type WebhookCapability = {
@@ -268,5 +284,25 @@ export type StreamCapability = {
268
284
  consumerGroup?: string;
269
285
  };
270
286
  export type Capability = WebhookCapability | ScheduledCapability | QueueCapability | StreamCapability;
287
+ /**
288
+ * Event trigger — subscribes the worker to one or more Nova event topics.
289
+ * Topics are matched against `message.topic` / `message.event_type` in SSE frames.
290
+ */
291
+ export type EventTrigger = {
292
+ type: 'event';
293
+ /** Nova event topic strings (e.g. "hris.employee.created") */
294
+ events: string[];
295
+ };
296
+ /**
297
+ * Schedule trigger — runs the worker on a cron schedule.
298
+ */
299
+ export type ScheduleTrigger = {
300
+ type: 'schedule';
301
+ cron: string;
302
+ timezone?: string;
303
+ description?: string;
304
+ };
305
+ /** Union of all trigger variants */
306
+ export type Trigger = EventTrigger | ScheduleTrigger;
271
307
  export { createPlatformClient, resolveCredentials, resolveCredentialsViaHttp, detectCredentialStrategy, integrationFetch, emitPlatformEvent, IntegrationNotFoundError, IntegrationDisabledError, CredentialsNotConfiguredError, ConnectionNotFoundError, TokenExchangeError, } from "./credentials.js";
272
308
  export type { ResolvedCredentials, IntegrationConfig, AuthMode, } from "./credentials.js";