@databuddy/sdk 2.1.78 → 2.2.1

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.
@@ -0,0 +1,237 @@
1
+ interface Logger {
2
+ info(msg: string, data?: Record<string, unknown>): void;
3
+ error(msg: string, data?: Record<string, unknown>): void;
4
+ warn(msg: string, data?: Record<string, unknown>): void;
5
+ debug(msg: string, data?: Record<string, unknown>): void;
6
+ }
7
+
8
+ /**
9
+ * Middleware function that can transform or filter events
10
+ * Return null to drop the event, or return a modified event
11
+ */
12
+ type Middleware = (event: BatchEventInput) => BatchEventInput | null | Promise<BatchEventInput | null>;
13
+ interface DatabuddyConfig {
14
+ /** Client ID from Databuddy dashboard */
15
+ clientId: string;
16
+ /** Custom API endpoint (default: https://basket.databuddy.cc) */
17
+ apiUrl?: string;
18
+ /** Enable debug logging */
19
+ debug?: boolean;
20
+ /** Custom logger instance */
21
+ logger?: Logger;
22
+ /** Enable automatic batching (default: true) */
23
+ enableBatching?: boolean;
24
+ /** Number of events to batch before flushing (default: 10, max: 100) */
25
+ batchSize?: number;
26
+ /** Time in ms before auto-flushing batched events (default: 2000) */
27
+ batchTimeout?: number;
28
+ /** Maximum number of events to queue (default: 1000) */
29
+ maxQueueSize?: number;
30
+ /** Middleware functions to transform events */
31
+ middleware?: Middleware[];
32
+ /** Enable event deduplication based on eventId (default: true) */
33
+ enableDeduplication?: boolean;
34
+ /** Maximum deduplication cache size (default: 10000) */
35
+ maxDeduplicationCacheSize?: number;
36
+ }
37
+ interface CustomEventInput {
38
+ /** Event name (required) */
39
+ name: string;
40
+ /** Unique event ID for deduplication */
41
+ eventId?: string;
42
+ /** Anonymous user ID */
43
+ anonymousId?: string | null;
44
+ /** Session ID */
45
+ sessionId?: string | null;
46
+ /** Event timestamp in milliseconds */
47
+ timestamp?: number | null;
48
+ /** Event properties/metadata */
49
+ properties?: Record<string, unknown> | null;
50
+ }
51
+ interface EventResponse {
52
+ /** Whether the event was successfully sent */
53
+ success: boolean;
54
+ /** Server-assigned event ID */
55
+ eventId?: string;
56
+ /** Error message if failed */
57
+ error?: string;
58
+ }
59
+ interface BatchEventInput {
60
+ /** Event type */
61
+ type: 'custom';
62
+ /** Event name */
63
+ name: string;
64
+ /** Unique event ID for deduplication */
65
+ eventId?: string;
66
+ /** Anonymous user ID */
67
+ anonymousId?: string | null;
68
+ /** Session ID */
69
+ sessionId?: string | null;
70
+ /** Event timestamp in milliseconds */
71
+ timestamp?: number | null;
72
+ /** Event properties/metadata */
73
+ properties?: Record<string, unknown> | null;
74
+ }
75
+ /**
76
+ * Global properties that will be attached to all events
77
+ */
78
+ interface GlobalProperties {
79
+ [key: string]: unknown;
80
+ }
81
+ interface BatchEventResponse {
82
+ /** Whether the batch was successfully sent */
83
+ success: boolean;
84
+ /** Number of events processed */
85
+ processed?: number;
86
+ /** Results for each event in the batch */
87
+ results?: Array<{
88
+ status: string;
89
+ type?: string;
90
+ eventId?: string;
91
+ message?: string;
92
+ error?: string;
93
+ }>;
94
+ /** Error message if batch failed */
95
+ error?: string;
96
+ }
97
+
98
+ declare class Databuddy {
99
+ private clientId;
100
+ private apiUrl;
101
+ private logger;
102
+ private enableBatching;
103
+ private batchSize;
104
+ private batchTimeout;
105
+ private queue;
106
+ private flushTimer;
107
+ private globalProperties;
108
+ private middleware;
109
+ private enableDeduplication;
110
+ private deduplicationCache;
111
+ private maxDeduplicationCacheSize;
112
+ constructor(config: DatabuddyConfig);
113
+ /**
114
+ * Track a custom event
115
+ * @param event - Event data to track
116
+ * @returns Promise with success/error response
117
+ * @example
118
+ * ```typescript
119
+ * await client.track({
120
+ * name: 'user_signup',
121
+ * properties: { plan: 'pro', source: 'web' }
122
+ * });
123
+ * ```
124
+ */
125
+ track(event: CustomEventInput): Promise<EventResponse>;
126
+ private send;
127
+ private scheduleFlush;
128
+ /**
129
+ * Manually flush all queued events
130
+ * Useful in serverless environments to ensure events are sent before process exits
131
+ * @returns Promise with batch results
132
+ * @example
133
+ * ```typescript
134
+ * await client.track({ name: 'api_call' });
135
+ * await client.flush(); // Ensure events are sent
136
+ * ```
137
+ */
138
+ flush(): Promise<BatchEventResponse>;
139
+ /**
140
+ * Send multiple events in a single batch request
141
+ * @param events - Array of events to batch (max 100)
142
+ * @returns Promise with batch results
143
+ * @example
144
+ * ```typescript
145
+ * await client.batch([
146
+ * { type: 'custom', name: 'event1', properties: { foo: 'bar' } },
147
+ * { type: 'custom', name: 'event2', properties: { baz: 'qux' } }
148
+ * ]);
149
+ * ```
150
+ */
151
+ batch(events: BatchEventInput[]): Promise<BatchEventResponse>;
152
+ /**
153
+ * Set global properties attached to all events
154
+ * Properties are merged with existing globals; event properties override globals
155
+ * @param properties - Properties to merge with existing globals
156
+ * @example
157
+ * ```typescript
158
+ * client.setGlobalProperties({ environment: 'production', version: '1.0.0' });
159
+ * // All subsequent events will include these properties
160
+ * ```
161
+ */
162
+ setGlobalProperties(properties: GlobalProperties): void;
163
+ /**
164
+ * Get current global properties
165
+ * @returns Copy of current global properties
166
+ * @example
167
+ * ```typescript
168
+ * const globals = client.getGlobalProperties();
169
+ * console.log(globals); // { environment: 'production', version: '1.0.0' }
170
+ * ```
171
+ */
172
+ getGlobalProperties(): GlobalProperties;
173
+ /**
174
+ * Clear all global properties
175
+ * @example
176
+ * ```typescript
177
+ * client.clearGlobalProperties();
178
+ * // No more global properties will be attached to events
179
+ * ```
180
+ */
181
+ clearGlobalProperties(): void;
182
+ /**
183
+ * Add middleware to transform events
184
+ * Middleware runs before deduplication and is executed in order
185
+ * Return null to drop the event, or return a modified event
186
+ * @param middleware - Middleware function
187
+ * @example
188
+ * ```typescript
189
+ * client.addMiddleware((event) => {
190
+ * // Add custom field
191
+ * event.properties = { ...event.properties, processed: true };
192
+ * return event;
193
+ * });
194
+ *
195
+ * // Drop events matching a condition
196
+ * client.addMiddleware((event) => {
197
+ * if (event.name === 'unwanted_event') return null;
198
+ * return event;
199
+ * });
200
+ * ```
201
+ */
202
+ addMiddleware(middleware: Middleware): void;
203
+ /**
204
+ * Remove all middleware functions
205
+ * @example
206
+ * ```typescript
207
+ * client.clearMiddleware();
208
+ * // All middleware transformations removed
209
+ * ```
210
+ */
211
+ clearMiddleware(): void;
212
+ /**
213
+ * Get current deduplication cache size
214
+ * @returns Number of event IDs in cache
215
+ * @example
216
+ * ```typescript
217
+ * const size = client.getDeduplicationCacheSize();
218
+ * console.log(`Cache size: ${size}`);
219
+ * ```
220
+ */
221
+ getDeduplicationCacheSize(): number;
222
+ /**
223
+ * Clear the deduplication cache
224
+ * Useful for testing or resetting duplicate detection
225
+ * @example
226
+ * ```typescript
227
+ * client.clearDeduplicationCache();
228
+ * // All cached event IDs cleared
229
+ * ```
230
+ */
231
+ clearDeduplicationCache(): void;
232
+ private applyMiddleware;
233
+ private addToDeduplicationCache;
234
+ }
235
+
236
+ export { Databuddy, Databuddy as db };
237
+ export type { BatchEventInput, BatchEventResponse, CustomEventInput, DatabuddyConfig, EventResponse, GlobalProperties, Logger, Middleware };