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
package/src/factories.ts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Factory functions for creating events subscribers
|
|
3
|
+
*
|
|
4
|
+
* Function-based alternatives to `new SubscriberClass()` pattern.
|
|
5
|
+
* Provides a consistent API and better tree-shaking.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createPostHogSubscriber, createWebhookSubscriber } from 'autotel-subscribers/factories'
|
|
10
|
+
*
|
|
11
|
+
* const events = new Events('my-service', {
|
|
12
|
+
* subscribers: [
|
|
13
|
+
* createPostHogSubscriber({ apiKey: 'phc_...' }),
|
|
14
|
+
* createWebhookSubscriber({ url: 'https://...' })
|
|
15
|
+
* ]
|
|
16
|
+
* })
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { PostHogSubscriber } from './posthog';
|
|
21
|
+
import { MixpanelSubscriber } from './mixpanel';
|
|
22
|
+
import { AmplitudeSubscriber } from './amplitude';
|
|
23
|
+
import { SegmentSubscriber } from './segment';
|
|
24
|
+
import { WebhookSubscriber } from './webhook';
|
|
25
|
+
import { SlackSubscriber } from './slack';
|
|
26
|
+
import { MockEventSubscriber } from './mock-event-subscriber';
|
|
27
|
+
|
|
28
|
+
import type { EventSubscriber, EventAttributes, OutcomeStatus, FunnelStatus } from 'autotel/event-subscriber';
|
|
29
|
+
|
|
30
|
+
// Re-export config types
|
|
31
|
+
export type { PostHogConfig } from './posthog';
|
|
32
|
+
export type { MixpanelConfig } from './mixpanel';
|
|
33
|
+
export type { AmplitudeConfig } from './amplitude';
|
|
34
|
+
export type { SegmentConfig } from './segment';
|
|
35
|
+
export type { WebhookConfig } from './webhook';
|
|
36
|
+
export type { SlackSubscriberConfig } from './slack';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Create a PostHog events subscriber
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const posthog = createPostHogSubscriber({
|
|
44
|
+
* apiKey: 'phc_...',
|
|
45
|
+
* host: 'https://app.posthog.com' // optional
|
|
46
|
+
* })
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export function createPostHogSubscriber(config: {
|
|
50
|
+
apiKey: string;
|
|
51
|
+
host?: string;
|
|
52
|
+
enabled?: boolean;
|
|
53
|
+
}): EventSubscriber {
|
|
54
|
+
return new PostHogSubscriber(config);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Create a Mixpanel events subscriber
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const mixpanel = createMixpanelSubscriber({
|
|
63
|
+
* token: 'YOUR_TOKEN'
|
|
64
|
+
* })
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export function createMixpanelSubscriber(config: {
|
|
68
|
+
token: string;
|
|
69
|
+
enabled?: boolean;
|
|
70
|
+
}): EventSubscriber {
|
|
71
|
+
return new MixpanelSubscriber(config);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Create an Amplitude events subscriber
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const amplitude = createAmplitudeSubscriber({
|
|
80
|
+
* apiKey: 'YOUR_API_KEY'
|
|
81
|
+
* })
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export function createAmplitudeSubscriber(config: {
|
|
85
|
+
apiKey: string;
|
|
86
|
+
enabled?: boolean;
|
|
87
|
+
}): EventSubscriber {
|
|
88
|
+
return new AmplitudeSubscriber(config);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Create a Segment events subscriber
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* const segment = createSegmentSubscriber({
|
|
97
|
+
* writeKey: 'YOUR_WRITE_KEY'
|
|
98
|
+
* })
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export function createSegmentSubscriber(config: {
|
|
102
|
+
writeKey: string;
|
|
103
|
+
enabled?: boolean;
|
|
104
|
+
}): EventSubscriber {
|
|
105
|
+
return new SegmentSubscriber(config);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Create a Webhook events subscriber
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const webhook = createWebhookSubscriber({
|
|
114
|
+
* url: 'https://your-webhook-endpoint.com/events',
|
|
115
|
+
* headers: { 'Authorization': 'Bearer token' }
|
|
116
|
+
* })
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export function createWebhookSubscriber(config: {
|
|
120
|
+
url: string;
|
|
121
|
+
method?: 'POST' | 'PUT';
|
|
122
|
+
headers?: Record<string, string>;
|
|
123
|
+
enabled?: boolean;
|
|
124
|
+
}): EventSubscriber {
|
|
125
|
+
return new WebhookSubscriber(config);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Create a Slack events subscriber
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* const slack = createSlackSubscriber({
|
|
134
|
+
* webhookUrl: 'https://hooks.slack.com/services/...',
|
|
135
|
+
* channel: '#events'
|
|
136
|
+
* })
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export function createSlackSubscriber(config: {
|
|
140
|
+
webhookUrl: string;
|
|
141
|
+
channel?: string;
|
|
142
|
+
enabled?: boolean;
|
|
143
|
+
}): EventSubscriber {
|
|
144
|
+
return new SlackSubscriber(config);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Create a mock events subscriber (for testing)
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* const mock = createMockSubscriber()
|
|
154
|
+
*
|
|
155
|
+
* // Capture events
|
|
156
|
+
* events.trackEvent('test.event', { foo: 'bar' })
|
|
157
|
+
*
|
|
158
|
+
* // Assert
|
|
159
|
+
* expect(mock.events).toHaveLength(1)
|
|
160
|
+
* expect(mock.events[0].name).toBe('test.event')
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
export function createMockSubscriber(): MockEventSubscriber {
|
|
164
|
+
return new MockEventSubscriber();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Compose multiple subscribers into one
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* const multiSubscriber = composeSubscribers([
|
|
173
|
+
* createPostHogSubscriber({ apiKey: '...' }),
|
|
174
|
+
* createWebhookSubscriber({ url: '...' })
|
|
175
|
+
* ])
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
export function composeSubscribers(adapters: EventSubscriber[]): EventSubscriber {
|
|
179
|
+
return {
|
|
180
|
+
name: 'ComposedSubscriber',
|
|
181
|
+
async trackEvent(name: string, attributes: EventAttributes) {
|
|
182
|
+
await Promise.all(adapters.map(a => a.trackEvent(name, attributes)));
|
|
183
|
+
},
|
|
184
|
+
async trackFunnelStep(funnel: string, step: FunnelStatus, attributes: EventAttributes) {
|
|
185
|
+
await Promise.all(adapters.map(a => a.trackFunnelStep(funnel, step, attributes)));
|
|
186
|
+
},
|
|
187
|
+
async trackOutcome(operation: string, outcome: OutcomeStatus, attributes: EventAttributes) {
|
|
188
|
+
await Promise.all(adapters.map(a => a.trackOutcome(operation, outcome, attributes)));
|
|
189
|
+
},
|
|
190
|
+
async trackValue(name: string, value: number, attributes: EventAttributes) {
|
|
191
|
+
await Promise.all(adapters.map(a => a.trackValue(name, value, attributes)));
|
|
192
|
+
},
|
|
193
|
+
async shutdown() {
|
|
194
|
+
await Promise.all(adapters.map(a => a.shutdown?.()));
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* autotel-subscribers
|
|
3
|
+
*
|
|
4
|
+
* Send events to multiple platforms:
|
|
5
|
+
* - PostHog (product events)
|
|
6
|
+
* - Mixpanel (product events)
|
|
7
|
+
* - Amplitude (product events)
|
|
8
|
+
* - Segment (customer data platform)
|
|
9
|
+
* - Slack (team notifications)
|
|
10
|
+
* - Webhook (custom integrations, Zapier, Make.com)
|
|
11
|
+
*
|
|
12
|
+
* @example Multi-platform tracking
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { Events } from 'autotel/events';
|
|
15
|
+
* import { PostHogSubscriber, MixpanelSubscriber } from 'autotel-subscribers';
|
|
16
|
+
*
|
|
17
|
+
* const events = new Events('checkout', {
|
|
18
|
+
* subscribers: [
|
|
19
|
+
* new PostHogSubscriber({ apiKey: 'phc_...' }),
|
|
20
|
+
* new MixpanelSubscriber({ token: '...' })
|
|
21
|
+
* ]
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Sent to: OpenTelemetry + PostHog + Mixpanel
|
|
25
|
+
* events.trackEvent('order.completed', { userId: '123', amount: 99.99 });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
// ============================================================================
|
|
31
|
+
// Destination Subscribers (where events go)
|
|
32
|
+
// ============================================================================
|
|
33
|
+
|
|
34
|
+
export { PostHogSubscriber, type PostHogConfig } from './posthog';
|
|
35
|
+
export { MixpanelSubscriber, type MixpanelConfig } from './mixpanel';
|
|
36
|
+
export { SegmentSubscriber, type SegmentConfig } from './segment';
|
|
37
|
+
export { AmplitudeSubscriber, type AmplitudeConfig } from './amplitude';
|
|
38
|
+
export { SlackSubscriber, type SlackSubscriberConfig } from './slack';
|
|
39
|
+
export { WebhookSubscriber, type WebhookConfig } from './webhook';
|
|
40
|
+
|
|
41
|
+
// ============================================================================
|
|
42
|
+
// Base Classes for Building Custom Subscribers
|
|
43
|
+
// ============================================================================
|
|
44
|
+
|
|
45
|
+
// Standard base class - extend this for custom subscribers
|
|
46
|
+
export { EventSubscriber, type EventPayload } from './event-subscriber-base';
|
|
47
|
+
|
|
48
|
+
// Specialized base class for streaming platforms (Kafka, Kinesis, Pub/Sub)
|
|
49
|
+
export { StreamingEventSubscriber } from './streaming-event-subscriber';
|
|
50
|
+
|