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.
- package/LICENSE +21 -0
- package/README.md +669 -0
- package/dist/amplitude.cjs +2486 -0
- package/dist/amplitude.cjs.map +1 -0
- package/dist/amplitude.d.cts +49 -0
- package/dist/amplitude.d.ts +49 -0
- package/dist/amplitude.js +2463 -0
- package/dist/amplitude.js.map +1 -0
- package/dist/event-subscriber-base-CnF3V56W.d.cts +182 -0
- package/dist/event-subscriber-base-CnF3V56W.d.ts +182 -0
- package/dist/factories.cjs +16660 -0
- package/dist/factories.cjs.map +1 -0
- package/dist/factories.d.cts +304 -0
- package/dist/factories.d.ts +304 -0
- package/dist/factories.js +16624 -0
- package/dist/factories.js.map +1 -0
- package/dist/index.cjs +16575 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +179 -0
- package/dist/index.d.ts +179 -0
- package/dist/index.js +16539 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware.cjs +220 -0
- package/dist/middleware.cjs.map +1 -0
- package/dist/middleware.d.cts +227 -0
- package/dist/middleware.d.ts +227 -0
- package/dist/middleware.js +208 -0
- package/dist/middleware.js.map +1 -0
- package/dist/mixpanel.cjs +2940 -0
- package/dist/mixpanel.cjs.map +1 -0
- package/dist/mixpanel.d.cts +47 -0
- package/dist/mixpanel.d.ts +47 -0
- package/dist/mixpanel.js +2932 -0
- package/dist/mixpanel.js.map +1 -0
- package/dist/posthog.cjs +4115 -0
- package/dist/posthog.cjs.map +1 -0
- package/dist/posthog.d.cts +299 -0
- package/dist/posthog.d.ts +299 -0
- package/dist/posthog.js +4113 -0
- package/dist/posthog.js.map +1 -0
- package/dist/segment.cjs +6822 -0
- package/dist/segment.cjs.map +1 -0
- package/dist/segment.d.cts +49 -0
- package/dist/segment.d.ts +49 -0
- package/dist/segment.js +6794 -0
- package/dist/segment.js.map +1 -0
- package/dist/slack.cjs +368 -0
- package/dist/slack.cjs.map +1 -0
- package/dist/slack.d.cts +126 -0
- package/dist/slack.d.ts +126 -0
- package/dist/slack.js +366 -0
- package/dist/slack.js.map +1 -0
- package/dist/webhook.cjs +100 -0
- package/dist/webhook.cjs.map +1 -0
- package/dist/webhook.d.cts +53 -0
- package/dist/webhook.d.ts +53 -0
- package/dist/webhook.js +98 -0
- package/dist/webhook.js.map +1 -0
- package/examples/quickstart-custom-subscriber.ts +144 -0
- package/examples/subscriber-bigquery.ts +219 -0
- package/examples/subscriber-databricks.ts +280 -0
- package/examples/subscriber-kafka.ts +326 -0
- package/examples/subscriber-kinesis.ts +307 -0
- package/examples/subscriber-posthog.ts +421 -0
- package/examples/subscriber-pubsub.ts +336 -0
- package/examples/subscriber-snowflake.ts +232 -0
- package/package.json +141 -0
- package/src/amplitude.test.ts +231 -0
- package/src/amplitude.ts +148 -0
- package/src/event-subscriber-base.ts +325 -0
- package/src/factories.ts +197 -0
- package/src/index.ts +50 -0
- package/src/middleware.ts +489 -0
- package/src/mixpanel.test.ts +194 -0
- package/src/mixpanel.ts +134 -0
- package/src/mock-event-subscriber.ts +333 -0
- package/src/posthog.test.ts +629 -0
- package/src/posthog.ts +530 -0
- package/src/segment.test.ts +228 -0
- package/src/segment.ts +148 -0
- package/src/slack.ts +383 -0
- package/src/streaming-event-subscriber.ts +323 -0
- package/src/testing/index.ts +37 -0
- package/src/testing/mock-webhook-server.ts +242 -0
- package/src/testing/subscriber-test-harness.ts +365 -0
- package/src/webhook.test.ts +264 -0
- 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 };
|