@databuddy/sdk 2.3.0 → 2.3.2

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,128 @@
1
+ /** Flag evaluation result from API */
2
+ interface FlagResult {
3
+ enabled: boolean;
4
+ value: boolean | string | number;
5
+ payload: Record<string, unknown> | null;
6
+ reason: string;
7
+ variant?: string;
8
+ }
9
+ /** User context for flag evaluation */
10
+ interface UserContext {
11
+ userId?: string;
12
+ email?: string;
13
+ properties?: Record<string, unknown>;
14
+ }
15
+ /** SDK configuration */
16
+ interface FlagsConfig {
17
+ /** Client ID (website or organization ID) */
18
+ clientId: string;
19
+ /** API URL (default: https://api.databuddy.cc) */
20
+ apiUrl?: string;
21
+ /** User context for evaluation */
22
+ user?: UserContext;
23
+ /** Disable flag evaluation entirely */
24
+ disabled?: boolean;
25
+ /** Enable debug logging */
26
+ debug?: boolean;
27
+ /** Skip persistent storage (browser only) */
28
+ skipStorage?: boolean;
29
+ /** Session is loading - defer evaluation */
30
+ isPending?: boolean;
31
+ /** Auto-fetch all flags on init (default: true) */
32
+ autoFetch?: boolean;
33
+ /** Environment name */
34
+ environment?: string;
35
+ /** Cache TTL in ms - after this, cache is invalid (default: 60000) */
36
+ cacheTtl?: number;
37
+ /** Stale time in ms - after this, revalidate in background (default: cacheTtl/2) */
38
+ staleTime?: number;
39
+ }
40
+ /** Flag status for clear state management */
41
+ type FlagStatus = "loading" | "ready" | "error" | "pending";
42
+ /** Synchronous flag state for React hooks */
43
+ interface FlagState {
44
+ /** Whether the flag is enabled (true/false) */
45
+ on: boolean;
46
+ /** @deprecated Use `on` instead */
47
+ enabled: boolean;
48
+ /** Current status: loading, ready, error, or pending */
49
+ status: FlagStatus;
50
+ /** Whether the flag is still loading */
51
+ loading: boolean;
52
+ /** @deprecated Use `status === 'ready'` instead */
53
+ isLoading: boolean;
54
+ /** @deprecated Use `status === 'ready'` instead */
55
+ isReady: boolean;
56
+ /** The flag's value (boolean, string, or number) */
57
+ value?: boolean | string | number;
58
+ /** Variant name for multivariate flags */
59
+ variant?: string;
60
+ }
61
+ /** Feature state returned by useFeature hook */
62
+ interface FeatureState {
63
+ /** Whether the feature is enabled */
64
+ on: boolean;
65
+ /** Whether the flag is loading */
66
+ loading: boolean;
67
+ /** Current status */
68
+ status: FlagStatus;
69
+ /** The flag's value */
70
+ value?: boolean | string | number;
71
+ /** Variant for A/B tests */
72
+ variant?: string;
73
+ }
74
+ /** Context returned by useFlags hook */
75
+ interface FlagsContext {
76
+ /** @deprecated Use getFlag instead - confusing name */
77
+ isEnabled: (key: string) => FlagState;
78
+ /** Get a flag's full state */
79
+ getFlag: (key: string) => FlagState;
80
+ /** Get a flag's value with type safety */
81
+ getValue: <T extends boolean | string | number = boolean>(key: string, defaultValue?: T) => T;
82
+ /** Check if a flag is on (simple boolean) */
83
+ isOn: (key: string) => boolean;
84
+ /** Async fetch a specific flag */
85
+ fetchFlag: (key: string) => Promise<FlagResult>;
86
+ /** Fetch all flags */
87
+ fetchAllFlags: () => Promise<void>;
88
+ /** Update user context */
89
+ updateUser: (user: UserContext) => void;
90
+ /** Refresh all flags */
91
+ refresh: (forceClear?: boolean) => Promise<void>;
92
+ /** Whether the SDK is ready */
93
+ isReady: boolean;
94
+ }
95
+ /** Storage interface for persistence */
96
+ interface StorageInterface {
97
+ get(key: string): FlagResult | null;
98
+ set(key: string, value: FlagResult): void;
99
+ getAll(): Record<string, FlagResult>;
100
+ setAll(flags: Record<string, FlagResult>): void;
101
+ delete?(key: string): void;
102
+ deleteMultiple?(keys: string[]): void;
103
+ clear(): void;
104
+ cleanupExpired(): void;
105
+ }
106
+ /** Manager constructor options */
107
+ interface FlagsManagerOptions {
108
+ config: FlagsConfig;
109
+ storage?: StorageInterface;
110
+ onFlagsUpdate?: (flags: Record<string, FlagResult>) => void;
111
+ onConfigUpdate?: (config: FlagsConfig) => void;
112
+ onReady?: () => void;
113
+ }
114
+ /** Flags manager interface */
115
+ interface FlagsManager {
116
+ getFlag: (key: string, user?: UserContext) => Promise<FlagResult>;
117
+ isEnabled: (key: string) => FlagState;
118
+ getValue: <T = boolean | string | number>(key: string, defaultValue?: T) => T;
119
+ fetchAllFlags: (user?: UserContext) => Promise<void>;
120
+ updateUser: (user: UserContext) => void;
121
+ refresh: (forceClear?: boolean) => Promise<void>;
122
+ updateConfig: (config: FlagsConfig) => void;
123
+ getMemoryFlags: () => Record<string, FlagResult>;
124
+ isReady: () => boolean;
125
+ destroy?: () => void;
126
+ }
127
+
128
+ export type { FlagsConfig as F, UserContext as U, FeatureState as a, FlagState as b, FlagsContext as c, FlagResult as d, FlagsManager as e, FlagsManagerOptions as f };
@@ -0,0 +1,128 @@
1
+ /** Flag evaluation result from API */
2
+ interface FlagResult {
3
+ enabled: boolean;
4
+ value: boolean | string | number;
5
+ payload: Record<string, unknown> | null;
6
+ reason: string;
7
+ variant?: string;
8
+ }
9
+ /** User context for flag evaluation */
10
+ interface UserContext {
11
+ userId?: string;
12
+ email?: string;
13
+ properties?: Record<string, unknown>;
14
+ }
15
+ /** SDK configuration */
16
+ interface FlagsConfig {
17
+ /** Client ID (website or organization ID) */
18
+ clientId: string;
19
+ /** API URL (default: https://api.databuddy.cc) */
20
+ apiUrl?: string;
21
+ /** User context for evaluation */
22
+ user?: UserContext;
23
+ /** Disable flag evaluation entirely */
24
+ disabled?: boolean;
25
+ /** Enable debug logging */
26
+ debug?: boolean;
27
+ /** Skip persistent storage (browser only) */
28
+ skipStorage?: boolean;
29
+ /** Session is loading - defer evaluation */
30
+ isPending?: boolean;
31
+ /** Auto-fetch all flags on init (default: true) */
32
+ autoFetch?: boolean;
33
+ /** Environment name */
34
+ environment?: string;
35
+ /** Cache TTL in ms - after this, cache is invalid (default: 60000) */
36
+ cacheTtl?: number;
37
+ /** Stale time in ms - after this, revalidate in background (default: cacheTtl/2) */
38
+ staleTime?: number;
39
+ }
40
+ /** Flag status for clear state management */
41
+ type FlagStatus = "loading" | "ready" | "error" | "pending";
42
+ /** Synchronous flag state for React hooks */
43
+ interface FlagState {
44
+ /** Whether the flag is enabled (true/false) */
45
+ on: boolean;
46
+ /** @deprecated Use `on` instead */
47
+ enabled: boolean;
48
+ /** Current status: loading, ready, error, or pending */
49
+ status: FlagStatus;
50
+ /** Whether the flag is still loading */
51
+ loading: boolean;
52
+ /** @deprecated Use `status === 'ready'` instead */
53
+ isLoading: boolean;
54
+ /** @deprecated Use `status === 'ready'` instead */
55
+ isReady: boolean;
56
+ /** The flag's value (boolean, string, or number) */
57
+ value?: boolean | string | number;
58
+ /** Variant name for multivariate flags */
59
+ variant?: string;
60
+ }
61
+ /** Feature state returned by useFeature hook */
62
+ interface FeatureState {
63
+ /** Whether the feature is enabled */
64
+ on: boolean;
65
+ /** Whether the flag is loading */
66
+ loading: boolean;
67
+ /** Current status */
68
+ status: FlagStatus;
69
+ /** The flag's value */
70
+ value?: boolean | string | number;
71
+ /** Variant for A/B tests */
72
+ variant?: string;
73
+ }
74
+ /** Context returned by useFlags hook */
75
+ interface FlagsContext {
76
+ /** @deprecated Use getFlag instead - confusing name */
77
+ isEnabled: (key: string) => FlagState;
78
+ /** Get a flag's full state */
79
+ getFlag: (key: string) => FlagState;
80
+ /** Get a flag's value with type safety */
81
+ getValue: <T extends boolean | string | number = boolean>(key: string, defaultValue?: T) => T;
82
+ /** Check if a flag is on (simple boolean) */
83
+ isOn: (key: string) => boolean;
84
+ /** Async fetch a specific flag */
85
+ fetchFlag: (key: string) => Promise<FlagResult>;
86
+ /** Fetch all flags */
87
+ fetchAllFlags: () => Promise<void>;
88
+ /** Update user context */
89
+ updateUser: (user: UserContext) => void;
90
+ /** Refresh all flags */
91
+ refresh: (forceClear?: boolean) => Promise<void>;
92
+ /** Whether the SDK is ready */
93
+ isReady: boolean;
94
+ }
95
+ /** Storage interface for persistence */
96
+ interface StorageInterface {
97
+ get(key: string): FlagResult | null;
98
+ set(key: string, value: FlagResult): void;
99
+ getAll(): Record<string, FlagResult>;
100
+ setAll(flags: Record<string, FlagResult>): void;
101
+ delete?(key: string): void;
102
+ deleteMultiple?(keys: string[]): void;
103
+ clear(): void;
104
+ cleanupExpired(): void;
105
+ }
106
+ /** Manager constructor options */
107
+ interface FlagsManagerOptions {
108
+ config: FlagsConfig;
109
+ storage?: StorageInterface;
110
+ onFlagsUpdate?: (flags: Record<string, FlagResult>) => void;
111
+ onConfigUpdate?: (config: FlagsConfig) => void;
112
+ onReady?: () => void;
113
+ }
114
+ /** Flags manager interface */
115
+ interface FlagsManager {
116
+ getFlag: (key: string, user?: UserContext) => Promise<FlagResult>;
117
+ isEnabled: (key: string) => FlagState;
118
+ getValue: <T = boolean | string | number>(key: string, defaultValue?: T) => T;
119
+ fetchAllFlags: (user?: UserContext) => Promise<void>;
120
+ updateUser: (user: UserContext) => void;
121
+ refresh: (forceClear?: boolean) => Promise<void>;
122
+ updateConfig: (config: FlagsConfig) => void;
123
+ getMemoryFlags: () => Record<string, FlagResult>;
124
+ isReady: () => boolean;
125
+ destroy?: () => void;
126
+ }
127
+
128
+ export type { FlagsConfig as F, UserContext as U, FeatureState as a, FlagState as b, FlagsContext as c, FlagResult as d, FlagsManager as e, FlagsManagerOptions as f };
@@ -13,7 +13,7 @@
13
13
  * />
14
14
  * ```
15
15
  */
16
- type DatabuddyConfig = {
16
+ interface DatabuddyConfig {
17
17
  /**
18
18
  * Your Databuddy project client ID.
19
19
  * If not provided, will automatically detect from NEXT_PUBLIC_DATABUDDY_CLIENT_ID environment variable.
@@ -149,11 +149,11 @@ type DatabuddyConfig = {
149
149
  skipPatterns?: string[];
150
150
  /** Array of glob patterns to mask sensitive paths (e.g., ['/users/*']) */
151
151
  maskPatterns?: string[];
152
- };
152
+ }
153
153
  /**
154
154
  * Base event properties that can be attached to any event
155
155
  */
156
- type BaseEventProperties = {
156
+ interface BaseEventProperties {
157
157
  /** Page URL */
158
158
  __path?: string;
159
159
  /** Page title */
@@ -180,7 +180,7 @@ type BaseEventProperties = {
180
180
  utm_campaign?: string;
181
181
  utm_term?: string;
182
182
  utm_content?: string;
183
- };
183
+ }
184
184
  /**
185
185
  * Custom event properties that can be attached to any event
186
186
  */
@@ -191,7 +191,7 @@ interface EventProperties extends BaseEventProperties {
191
191
  /**
192
192
  * Pre-defined event types with their specific properties
193
193
  */
194
- type EventTypeMap = {
194
+ interface EventTypeMap {
195
195
  screen_view: {
196
196
  time_on_page?: number;
197
197
  scroll_depth?: number;
@@ -244,7 +244,7 @@ type EventTypeMap = {
244
244
  error_type?: string;
245
245
  };
246
246
  [eventName: string]: EventProperties;
247
- };
247
+ }
248
248
  /**
249
249
  * Available event names
250
250
  */
@@ -266,7 +266,7 @@ type PropertiesForEvent<T extends EventName> = T extends keyof EventTypeMap ? Ev
266
266
  * const options = window.databuddy.options;
267
267
  * ```
268
268
  */
269
- type DatabuddyTracker = {
269
+ interface DatabuddyTracker {
270
270
  /**
271
271
  * Track a custom event.
272
272
  * @param eventName - Name of the event (e.g., "purchase", "signup")
@@ -305,7 +305,7 @@ type DatabuddyTracker = {
305
305
  * Current tracker configuration options.
306
306
  */
307
307
  options: DatabuddyConfig;
308
- };
308
+ }
309
309
  /**
310
310
  * Global window interface extensions
311
311
  */
@@ -348,12 +348,12 @@ declare global {
348
348
  * </a>
349
349
  * ```
350
350
  */
351
- type DataAttributes = {
351
+ interface DataAttributes {
352
352
  /** Event name to track when element is clicked */
353
353
  "data-track": string;
354
354
  /** Additional data attributes (auto-converted from kebab-case to camelCase) */
355
355
  [key: `data-${string}`]: string;
356
- };
356
+ }
357
357
  /**
358
358
  * Utility types for creating typed event tracking functions
359
359
  */
@@ -13,7 +13,7 @@
13
13
  * />
14
14
  * ```
15
15
  */
16
- type DatabuddyConfig = {
16
+ interface DatabuddyConfig {
17
17
  /**
18
18
  * Your Databuddy project client ID.
19
19
  * If not provided, will automatically detect from NEXT_PUBLIC_DATABUDDY_CLIENT_ID environment variable.
@@ -149,11 +149,11 @@ type DatabuddyConfig = {
149
149
  skipPatterns?: string[];
150
150
  /** Array of glob patterns to mask sensitive paths (e.g., ['/users/*']) */
151
151
  maskPatterns?: string[];
152
- };
152
+ }
153
153
  /**
154
154
  * Base event properties that can be attached to any event
155
155
  */
156
- type BaseEventProperties = {
156
+ interface BaseEventProperties {
157
157
  /** Page URL */
158
158
  __path?: string;
159
159
  /** Page title */
@@ -180,7 +180,7 @@ type BaseEventProperties = {
180
180
  utm_campaign?: string;
181
181
  utm_term?: string;
182
182
  utm_content?: string;
183
- };
183
+ }
184
184
  /**
185
185
  * Custom event properties that can be attached to any event
186
186
  */
@@ -191,7 +191,7 @@ interface EventProperties extends BaseEventProperties {
191
191
  /**
192
192
  * Pre-defined event types with their specific properties
193
193
  */
194
- type EventTypeMap = {
194
+ interface EventTypeMap {
195
195
  screen_view: {
196
196
  time_on_page?: number;
197
197
  scroll_depth?: number;
@@ -244,7 +244,7 @@ type EventTypeMap = {
244
244
  error_type?: string;
245
245
  };
246
246
  [eventName: string]: EventProperties;
247
- };
247
+ }
248
248
  /**
249
249
  * Available event names
250
250
  */
@@ -266,7 +266,7 @@ type PropertiesForEvent<T extends EventName> = T extends keyof EventTypeMap ? Ev
266
266
  * const options = window.databuddy.options;
267
267
  * ```
268
268
  */
269
- type DatabuddyTracker = {
269
+ interface DatabuddyTracker {
270
270
  /**
271
271
  * Track a custom event.
272
272
  * @param eventName - Name of the event (e.g., "purchase", "signup")
@@ -305,7 +305,7 @@ type DatabuddyTracker = {
305
305
  * Current tracker configuration options.
306
306
  */
307
307
  options: DatabuddyConfig;
308
- };
308
+ }
309
309
  /**
310
310
  * Global window interface extensions
311
311
  */
@@ -348,12 +348,12 @@ declare global {
348
348
  * </a>
349
349
  * ```
350
350
  */
351
- type DataAttributes = {
351
+ interface DataAttributes {
352
352
  /** Event name to track when element is clicked */
353
353
  "data-track": string;
354
354
  /** Additional data attributes (auto-converted from kebab-case to camelCase) */
355
355
  [key: `data-${string}`]: string;
356
- };
356
+ }
357
357
  /**
358
358
  * Utility types for creating typed event tracking functions
359
359
  */