@leanbase.com/js 0.1.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.
@@ -0,0 +1,129 @@
1
+ import { PostHogCore, getFetch } from '@posthog/core'
2
+ import type {
3
+ PostHogCoreOptions,
4
+ PostHogEventProperties,
5
+ PostHogFetchOptions,
6
+ PostHogFetchResponse,
7
+ PostHogPersistedProperty,
8
+ PostHogCaptureOptions,
9
+ } from '@posthog/core'
10
+ import { version } from './version'
11
+ import { logger } from './leanbase-logger'
12
+ import { isNull } from '@posthog/core'
13
+
14
+ export interface LeanbaseOptions extends Partial<PostHogCoreOptions> {
15
+ /**
16
+ * Enable autocapture of clicks and form interactions
17
+ * @default true
18
+ */
19
+ autocapture?: boolean
20
+
21
+ /**
22
+ * API host for Leanbase
23
+ * @default 'https://i.leanbase.co'
24
+ */
25
+ host?: string
26
+ }
27
+
28
+ export class Leanbase extends PostHogCore {
29
+ private _storage: Map<string, any> = new Map()
30
+ private _storageKey: string
31
+
32
+ constructor(apiKey: string, options?: LeanbaseOptions) {
33
+ // Leanbase defaults
34
+ const leanbaseOptions: PostHogCoreOptions = {
35
+ host: 'https://i.leanbase.co',
36
+ ...options,
37
+ }
38
+
39
+ super(apiKey, leanbaseOptions)
40
+
41
+ this._storageKey = `leanbase_${apiKey}`
42
+
43
+ // Load from localStorage if available
44
+ if (typeof window !== 'undefined' && window.localStorage) {
45
+ try {
46
+ const stored = window.localStorage.getItem(this._storageKey)
47
+ if (stored) {
48
+ const parsed = JSON.parse(stored)
49
+ Object.entries(parsed).forEach(([key, value]) => {
50
+ this._storage.set(key, value)
51
+ })
52
+ }
53
+ } catch (err) {
54
+ logger.warn('Failed to load persisted data', err)
55
+ }
56
+ }
57
+
58
+ logger.info('Leanbase initialized', { apiKey, host: leanbaseOptions.host })
59
+
60
+ // Preload feature flags if not explicitly disabled
61
+ if (options?.preloadFeatureFlags !== false) {
62
+ this.reloadFeatureFlags()
63
+ }
64
+ }
65
+
66
+ // PostHogCore abstract methods
67
+ fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> {
68
+ const fetchFn = getFetch()
69
+
70
+ if (!fetchFn) {
71
+ return Promise.reject(new Error('Fetch API is not available in this environment.'))
72
+ }
73
+
74
+ return fetchFn(url, options)
75
+ }
76
+
77
+ getLibraryId(): string {
78
+ return 'leanbase'
79
+ }
80
+
81
+ getLibraryVersion(): string {
82
+ return version
83
+ }
84
+
85
+ getCustomUserAgent(): void {
86
+ return
87
+ }
88
+
89
+ getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined {
90
+ return this._storage.get(key)
91
+ }
92
+
93
+ setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void {
94
+ if (isNull(value)) {
95
+ this._storage.delete(key)
96
+ } else {
97
+ this._storage.set(key, value)
98
+ }
99
+
100
+ // Persist to localStorage if available
101
+ if (typeof window !== 'undefined' && window.localStorage) {
102
+ try {
103
+ const obj: Record<string, any> = {}
104
+ this._storage.forEach((v, k) => {
105
+ obj[k] = v
106
+ })
107
+ window.localStorage.setItem(this._storageKey, JSON.stringify(obj))
108
+ } catch (err) {
109
+ logger.warn('Failed to persist data', err)
110
+ }
111
+ }
112
+ }
113
+
114
+ // Public API: leanbase.capture()
115
+ capture(event: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void {
116
+ super.capture(event, properties, options)
117
+ }
118
+
119
+ // Public API: leanbase.identify()
120
+ identify(distinctId?: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void {
121
+ super.identify(distinctId, properties, options)
122
+ }
123
+
124
+ // Cleanup
125
+ destroy(): void {
126
+ // Future: cleanup autocapture and session recording
127
+ this._storage.clear()
128
+ }
129
+ }
package/src/version.ts ADDED
@@ -0,0 +1 @@
1
+ export const version = '0.1.0'