autotel-subscribers 4.0.0

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.
Files changed (87) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +669 -0
  3. package/dist/amplitude.cjs +2486 -0
  4. package/dist/amplitude.cjs.map +1 -0
  5. package/dist/amplitude.d.cts +49 -0
  6. package/dist/amplitude.d.ts +49 -0
  7. package/dist/amplitude.js +2463 -0
  8. package/dist/amplitude.js.map +1 -0
  9. package/dist/event-subscriber-base-CnF3V56W.d.cts +182 -0
  10. package/dist/event-subscriber-base-CnF3V56W.d.ts +182 -0
  11. package/dist/factories.cjs +16660 -0
  12. package/dist/factories.cjs.map +1 -0
  13. package/dist/factories.d.cts +304 -0
  14. package/dist/factories.d.ts +304 -0
  15. package/dist/factories.js +16624 -0
  16. package/dist/factories.js.map +1 -0
  17. package/dist/index.cjs +16575 -0
  18. package/dist/index.cjs.map +1 -0
  19. package/dist/index.d.cts +179 -0
  20. package/dist/index.d.ts +179 -0
  21. package/dist/index.js +16539 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/middleware.cjs +220 -0
  24. package/dist/middleware.cjs.map +1 -0
  25. package/dist/middleware.d.cts +227 -0
  26. package/dist/middleware.d.ts +227 -0
  27. package/dist/middleware.js +208 -0
  28. package/dist/middleware.js.map +1 -0
  29. package/dist/mixpanel.cjs +2940 -0
  30. package/dist/mixpanel.cjs.map +1 -0
  31. package/dist/mixpanel.d.cts +47 -0
  32. package/dist/mixpanel.d.ts +47 -0
  33. package/dist/mixpanel.js +2932 -0
  34. package/dist/mixpanel.js.map +1 -0
  35. package/dist/posthog.cjs +4115 -0
  36. package/dist/posthog.cjs.map +1 -0
  37. package/dist/posthog.d.cts +299 -0
  38. package/dist/posthog.d.ts +299 -0
  39. package/dist/posthog.js +4113 -0
  40. package/dist/posthog.js.map +1 -0
  41. package/dist/segment.cjs +6822 -0
  42. package/dist/segment.cjs.map +1 -0
  43. package/dist/segment.d.cts +49 -0
  44. package/dist/segment.d.ts +49 -0
  45. package/dist/segment.js +6794 -0
  46. package/dist/segment.js.map +1 -0
  47. package/dist/slack.cjs +368 -0
  48. package/dist/slack.cjs.map +1 -0
  49. package/dist/slack.d.cts +126 -0
  50. package/dist/slack.d.ts +126 -0
  51. package/dist/slack.js +366 -0
  52. package/dist/slack.js.map +1 -0
  53. package/dist/webhook.cjs +100 -0
  54. package/dist/webhook.cjs.map +1 -0
  55. package/dist/webhook.d.cts +53 -0
  56. package/dist/webhook.d.ts +53 -0
  57. package/dist/webhook.js +98 -0
  58. package/dist/webhook.js.map +1 -0
  59. package/examples/quickstart-custom-subscriber.ts +144 -0
  60. package/examples/subscriber-bigquery.ts +219 -0
  61. package/examples/subscriber-databricks.ts +280 -0
  62. package/examples/subscriber-kafka.ts +326 -0
  63. package/examples/subscriber-kinesis.ts +307 -0
  64. package/examples/subscriber-posthog.ts +421 -0
  65. package/examples/subscriber-pubsub.ts +336 -0
  66. package/examples/subscriber-snowflake.ts +232 -0
  67. package/package.json +141 -0
  68. package/src/amplitude.test.ts +231 -0
  69. package/src/amplitude.ts +148 -0
  70. package/src/event-subscriber-base.ts +325 -0
  71. package/src/factories.ts +197 -0
  72. package/src/index.ts +50 -0
  73. package/src/middleware.ts +489 -0
  74. package/src/mixpanel.test.ts +194 -0
  75. package/src/mixpanel.ts +134 -0
  76. package/src/mock-event-subscriber.ts +333 -0
  77. package/src/posthog.test.ts +629 -0
  78. package/src/posthog.ts +530 -0
  79. package/src/segment.test.ts +228 -0
  80. package/src/segment.ts +148 -0
  81. package/src/slack.ts +383 -0
  82. package/src/streaming-event-subscriber.ts +323 -0
  83. package/src/testing/index.ts +37 -0
  84. package/src/testing/mock-webhook-server.ts +242 -0
  85. package/src/testing/subscriber-test-harness.ts +365 -0
  86. package/src/webhook.test.ts +264 -0
  87. package/src/webhook.ts +158 -0
@@ -0,0 +1,182 @@
1
+ import { EventSubscriber as EventSubscriber$1, EventAttributes, FunnelStatus, OutcomeStatus } from 'autotel/event-subscriber';
2
+
3
+ /**
4
+ * EventSubscriber - Standard base class for building custom subscribers
5
+ *
6
+ * This is the recommended base class for creating custom events subscribers.
7
+ * It provides production-ready features out of the box:
8
+ *
9
+ * **Built-in Features:**
10
+ * - **Error Handling**: Automatic error catching with customizable handlers
11
+ * - **Pending Request Tracking**: Ensures all requests complete during shutdown
12
+ * - **Graceful Shutdown**: Drains pending requests before closing
13
+ * - **Enable/Disable**: Runtime control to turn subscriber on/off
14
+ * - **Normalized Payload**: Consistent event structure across all event types
15
+ *
16
+ * **When to use:**
17
+ * - Building custom subscribers for any platform
18
+ * - Production deployments requiring reliability
19
+ * - Need graceful shutdown and error handling
20
+ *
21
+ * @example Basic usage
22
+ * ```typescript
23
+ * import { EventSubscriber, EventPayload } from 'autotel-subscribers';
24
+ *
25
+ * class SnowflakeSubscriber extends EventSubscriber {
26
+ * name = 'SnowflakeSubscriber';
27
+ * version = '1.0.0';
28
+ *
29
+ * protected async sendToDestination(payload: EventPayload): Promise<void> {
30
+ * await snowflakeClient.execute(
31
+ * `INSERT INTO events VALUES (?, ?, ?)`,
32
+ * [payload.type, payload.name, JSON.stringify(payload.attributes)]
33
+ * );
34
+ * }
35
+ * }
36
+ * ```
37
+ *
38
+ * @example With buffering
39
+ * ```typescript
40
+ * class BufferedSubscriber extends EventSubscriber {
41
+ * name = 'BufferedSubscriber';
42
+ * private buffer: EventPayload[] = [];
43
+ *
44
+ * protected async sendToDestination(payload: EventPayload): Promise<void> {
45
+ * this.buffer.push(payload);
46
+ *
47
+ * if (this.buffer.length >= 100) {
48
+ * await this.flush();
49
+ * }
50
+ * }
51
+ *
52
+ * async shutdown(): Promise<void> {
53
+ * await super.shutdown(); // Drain pending requests first
54
+ * await this.flush(); // Then flush buffer
55
+ * }
56
+ *
57
+ * private async flush(): Promise<void> {
58
+ * if (this.buffer.length === 0) return;
59
+ *
60
+ * const batch = [...this.buffer];
61
+ * this.buffer = [];
62
+ *
63
+ * await apiClient.sendBatch(batch);
64
+ * }
65
+ * }
66
+ * ```
67
+ */
68
+
69
+ /**
70
+ * Payload sent to destination
71
+ */
72
+ interface EventPayload {
73
+ /** Event type: 'event', 'funnel', 'outcome', or 'value' */
74
+ type: 'event' | 'funnel' | 'outcome' | 'value';
75
+ /** Event name or metric name */
76
+ name: string;
77
+ /** Optional attributes */
78
+ attributes?: EventAttributes;
79
+ /** For funnel events: funnel name */
80
+ funnel?: string;
81
+ /** For funnel events: step status */
82
+ step?: FunnelStatus;
83
+ /** For outcome events: operation name */
84
+ operation?: string;
85
+ /** For outcome events: outcome status */
86
+ outcome?: OutcomeStatus;
87
+ /** For value events: numeric value */
88
+ value?: number;
89
+ /** Timestamp (ISO 8601) */
90
+ timestamp: string;
91
+ }
92
+ /**
93
+ * Standard base class for building custom events subscribers
94
+ *
95
+ * **What it provides:**
96
+ * - Consistent payload structure (normalized across all event types)
97
+ * - Enable/disable flag (runtime control)
98
+ * - Automatic error handling (with customizable error handlers)
99
+ * - Pending requests tracking (ensures no lost events during shutdown)
100
+ * - Graceful shutdown (drains pending requests before closing)
101
+ *
102
+ * **Usage:**
103
+ * Extend this class and implement `sendToDestination()`. All other methods
104
+ * (trackEvent, trackFunnelStep, trackOutcome, trackValue, shutdown) are handled automatically.
105
+ *
106
+ * For high-throughput streaming platforms (Kafka, Kinesis, Pub/Sub), use `StreamingEventSubscriber` instead.
107
+ */
108
+ declare abstract class EventSubscriber implements EventSubscriber$1 {
109
+ /**
110
+ * Subscriber name (required for debugging)
111
+ */
112
+ abstract readonly name: string;
113
+ /**
114
+ * Subscriber version (optional)
115
+ */
116
+ readonly version?: string;
117
+ /**
118
+ * Enable/disable the subscriber (default: true)
119
+ */
120
+ protected enabled: boolean;
121
+ /**
122
+ * Track pending requests for graceful shutdown
123
+ */
124
+ private pendingRequests;
125
+ /**
126
+ * Send payload to destination
127
+ *
128
+ * Override this method to implement your destination-specific logic.
129
+ * This is called for all event types (event, funnel, outcome, value).
130
+ *
131
+ * @param payload - Normalized event payload
132
+ */
133
+ protected abstract sendToDestination(payload: EventPayload): Promise<void>;
134
+ /**
135
+ * Optional: Handle errors
136
+ *
137
+ * Override this to customize error handling (logging, retries, etc.).
138
+ * Default behavior: log to console.error
139
+ *
140
+ * @param error - Error that occurred
141
+ * @param payload - Event payload that failed
142
+ */
143
+ protected handleError(error: Error, payload: EventPayload): void;
144
+ /**
145
+ * Track an event
146
+ */
147
+ trackEvent(name: string, attributes?: EventAttributes): Promise<void>;
148
+ /**
149
+ * Track a funnel step
150
+ */
151
+ trackFunnelStep(funnelName: string, step: FunnelStatus, attributes?: EventAttributes): Promise<void>;
152
+ /**
153
+ * Track an outcome
154
+ */
155
+ trackOutcome(operationName: string, outcome: OutcomeStatus, attributes?: EventAttributes): Promise<void>;
156
+ /**
157
+ * Track a value/metric
158
+ */
159
+ trackValue(name: string, value: number, attributes?: EventAttributes): Promise<void>;
160
+ /**
161
+ * Flush pending requests and clean up
162
+ *
163
+ * CRITICAL: Prevents race condition during shutdown
164
+ * 1. Disables subscriber to stop new events
165
+ * 2. Drains all pending requests (with retry logic)
166
+ * 3. Ensures flush guarantee
167
+ *
168
+ * Override this if you need custom cleanup logic (close connections, flush buffers, etc.),
169
+ * but ALWAYS call super.shutdown() first to drain pending requests.
170
+ */
171
+ shutdown(): Promise<void>;
172
+ /**
173
+ * Internal: Send payload and track request
174
+ */
175
+ private send;
176
+ /**
177
+ * Internal: Send with error handling
178
+ */
179
+ private sendWithErrorHandling;
180
+ }
181
+
182
+ export { EventSubscriber as E, type EventPayload as a };
@@ -0,0 +1,182 @@
1
+ import { EventSubscriber as EventSubscriber$1, EventAttributes, FunnelStatus, OutcomeStatus } from 'autotel/event-subscriber';
2
+
3
+ /**
4
+ * EventSubscriber - Standard base class for building custom subscribers
5
+ *
6
+ * This is the recommended base class for creating custom events subscribers.
7
+ * It provides production-ready features out of the box:
8
+ *
9
+ * **Built-in Features:**
10
+ * - **Error Handling**: Automatic error catching with customizable handlers
11
+ * - **Pending Request Tracking**: Ensures all requests complete during shutdown
12
+ * - **Graceful Shutdown**: Drains pending requests before closing
13
+ * - **Enable/Disable**: Runtime control to turn subscriber on/off
14
+ * - **Normalized Payload**: Consistent event structure across all event types
15
+ *
16
+ * **When to use:**
17
+ * - Building custom subscribers for any platform
18
+ * - Production deployments requiring reliability
19
+ * - Need graceful shutdown and error handling
20
+ *
21
+ * @example Basic usage
22
+ * ```typescript
23
+ * import { EventSubscriber, EventPayload } from 'autotel-subscribers';
24
+ *
25
+ * class SnowflakeSubscriber extends EventSubscriber {
26
+ * name = 'SnowflakeSubscriber';
27
+ * version = '1.0.0';
28
+ *
29
+ * protected async sendToDestination(payload: EventPayload): Promise<void> {
30
+ * await snowflakeClient.execute(
31
+ * `INSERT INTO events VALUES (?, ?, ?)`,
32
+ * [payload.type, payload.name, JSON.stringify(payload.attributes)]
33
+ * );
34
+ * }
35
+ * }
36
+ * ```
37
+ *
38
+ * @example With buffering
39
+ * ```typescript
40
+ * class BufferedSubscriber extends EventSubscriber {
41
+ * name = 'BufferedSubscriber';
42
+ * private buffer: EventPayload[] = [];
43
+ *
44
+ * protected async sendToDestination(payload: EventPayload): Promise<void> {
45
+ * this.buffer.push(payload);
46
+ *
47
+ * if (this.buffer.length >= 100) {
48
+ * await this.flush();
49
+ * }
50
+ * }
51
+ *
52
+ * async shutdown(): Promise<void> {
53
+ * await super.shutdown(); // Drain pending requests first
54
+ * await this.flush(); // Then flush buffer
55
+ * }
56
+ *
57
+ * private async flush(): Promise<void> {
58
+ * if (this.buffer.length === 0) return;
59
+ *
60
+ * const batch = [...this.buffer];
61
+ * this.buffer = [];
62
+ *
63
+ * await apiClient.sendBatch(batch);
64
+ * }
65
+ * }
66
+ * ```
67
+ */
68
+
69
+ /**
70
+ * Payload sent to destination
71
+ */
72
+ interface EventPayload {
73
+ /** Event type: 'event', 'funnel', 'outcome', or 'value' */
74
+ type: 'event' | 'funnel' | 'outcome' | 'value';
75
+ /** Event name or metric name */
76
+ name: string;
77
+ /** Optional attributes */
78
+ attributes?: EventAttributes;
79
+ /** For funnel events: funnel name */
80
+ funnel?: string;
81
+ /** For funnel events: step status */
82
+ step?: FunnelStatus;
83
+ /** For outcome events: operation name */
84
+ operation?: string;
85
+ /** For outcome events: outcome status */
86
+ outcome?: OutcomeStatus;
87
+ /** For value events: numeric value */
88
+ value?: number;
89
+ /** Timestamp (ISO 8601) */
90
+ timestamp: string;
91
+ }
92
+ /**
93
+ * Standard base class for building custom events subscribers
94
+ *
95
+ * **What it provides:**
96
+ * - Consistent payload structure (normalized across all event types)
97
+ * - Enable/disable flag (runtime control)
98
+ * - Automatic error handling (with customizable error handlers)
99
+ * - Pending requests tracking (ensures no lost events during shutdown)
100
+ * - Graceful shutdown (drains pending requests before closing)
101
+ *
102
+ * **Usage:**
103
+ * Extend this class and implement `sendToDestination()`. All other methods
104
+ * (trackEvent, trackFunnelStep, trackOutcome, trackValue, shutdown) are handled automatically.
105
+ *
106
+ * For high-throughput streaming platforms (Kafka, Kinesis, Pub/Sub), use `StreamingEventSubscriber` instead.
107
+ */
108
+ declare abstract class EventSubscriber implements EventSubscriber$1 {
109
+ /**
110
+ * Subscriber name (required for debugging)
111
+ */
112
+ abstract readonly name: string;
113
+ /**
114
+ * Subscriber version (optional)
115
+ */
116
+ readonly version?: string;
117
+ /**
118
+ * Enable/disable the subscriber (default: true)
119
+ */
120
+ protected enabled: boolean;
121
+ /**
122
+ * Track pending requests for graceful shutdown
123
+ */
124
+ private pendingRequests;
125
+ /**
126
+ * Send payload to destination
127
+ *
128
+ * Override this method to implement your destination-specific logic.
129
+ * This is called for all event types (event, funnel, outcome, value).
130
+ *
131
+ * @param payload - Normalized event payload
132
+ */
133
+ protected abstract sendToDestination(payload: EventPayload): Promise<void>;
134
+ /**
135
+ * Optional: Handle errors
136
+ *
137
+ * Override this to customize error handling (logging, retries, etc.).
138
+ * Default behavior: log to console.error
139
+ *
140
+ * @param error - Error that occurred
141
+ * @param payload - Event payload that failed
142
+ */
143
+ protected handleError(error: Error, payload: EventPayload): void;
144
+ /**
145
+ * Track an event
146
+ */
147
+ trackEvent(name: string, attributes?: EventAttributes): Promise<void>;
148
+ /**
149
+ * Track a funnel step
150
+ */
151
+ trackFunnelStep(funnelName: string, step: FunnelStatus, attributes?: EventAttributes): Promise<void>;
152
+ /**
153
+ * Track an outcome
154
+ */
155
+ trackOutcome(operationName: string, outcome: OutcomeStatus, attributes?: EventAttributes): Promise<void>;
156
+ /**
157
+ * Track a value/metric
158
+ */
159
+ trackValue(name: string, value: number, attributes?: EventAttributes): Promise<void>;
160
+ /**
161
+ * Flush pending requests and clean up
162
+ *
163
+ * CRITICAL: Prevents race condition during shutdown
164
+ * 1. Disables subscriber to stop new events
165
+ * 2. Drains all pending requests (with retry logic)
166
+ * 3. Ensures flush guarantee
167
+ *
168
+ * Override this if you need custom cleanup logic (close connections, flush buffers, etc.),
169
+ * but ALWAYS call super.shutdown() first to drain pending requests.
170
+ */
171
+ shutdown(): Promise<void>;
172
+ /**
173
+ * Internal: Send payload and track request
174
+ */
175
+ private send;
176
+ /**
177
+ * Internal: Send with error handling
178
+ */
179
+ private sendWithErrorHandling;
180
+ }
181
+
182
+ export { EventSubscriber as E, type EventPayload as a };