@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.
- package/dist/ai/vercel/index.d.mts +146 -7
- package/dist/ai/vercel/index.d.ts +146 -7
- package/dist/ai/vercel/index.mjs +221 -34
- package/dist/core/index.d.mts +3 -43
- package/dist/core/index.d.ts +3 -43
- package/dist/core/index.mjs +1 -1
- package/dist/node/index.d.mts +121 -10
- package/dist/node/index.d.ts +121 -10
- package/dist/node/index.mjs +257 -11
- package/dist/react/index.d.mts +57 -17
- package/dist/react/index.d.ts +57 -17
- package/dist/react/index.mjs +177 -85
- package/dist/shared/@databuddy/sdk.3kaCzyfu.mjs +199 -0
- package/dist/shared/@databuddy/sdk.B6nwxnPC.d.mts +128 -0
- package/dist/shared/@databuddy/sdk.B6nwxnPC.d.ts +128 -0
- package/dist/shared/@databuddy/{sdk.CeYE_kaj.d.mts → sdk.C9b3SVYK.d.mts} +10 -10
- package/dist/shared/@databuddy/{sdk.CeYE_kaj.d.ts → sdk.C9b3SVYK.d.ts} +10 -10
- package/dist/shared/@databuddy/sdk.Du7SE7-M.mjs +479 -0
- package/dist/shared/@databuddy/sdk.F8Xt1uF7.mjs +35 -0
- package/dist/vue/index.d.mts +11 -2
- package/dist/vue/index.d.ts +11 -2
- package/dist/vue/index.mjs +19 -9
- package/package.json +3 -4
- package/dist/shared/@databuddy/sdk.Cn2tDA8H.mjs +0 -471
- package/dist/shared/@databuddy/sdk.OK9Nbqlf.d.mts +0 -64
- package/dist/shared/@databuddy/sdk.OK9Nbqlf.d.ts +0 -64
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import { l as logger, c as createCacheEntry, i as isCacheValid, b as buildQueryParams, R as RequestBatcher, a as isCacheStale, D as DEFAULT_RESULT, g as getCacheKey, f as fetchAllFlags } from './sdk.3kaCzyfu.mjs';
|
|
2
|
+
|
|
3
|
+
class BrowserFlagStorage {
|
|
4
|
+
ttl = 24 * 60 * 60 * 1e3;
|
|
5
|
+
// 24 hours in milliseconds
|
|
6
|
+
get(key) {
|
|
7
|
+
return this.getFromLocalStorage(key);
|
|
8
|
+
}
|
|
9
|
+
set(key, value) {
|
|
10
|
+
this.setToLocalStorage(key, value);
|
|
11
|
+
}
|
|
12
|
+
getAll() {
|
|
13
|
+
const result = {};
|
|
14
|
+
const now = Date.now();
|
|
15
|
+
const keys = Object.keys(localStorage).filter(
|
|
16
|
+
(key) => key.startsWith("db-flag-")
|
|
17
|
+
);
|
|
18
|
+
for (const key of keys) {
|
|
19
|
+
const flagKey = key.replace("db-flag-", "");
|
|
20
|
+
try {
|
|
21
|
+
const item = localStorage.getItem(key);
|
|
22
|
+
if (item) {
|
|
23
|
+
const parsed = JSON.parse(item);
|
|
24
|
+
if (parsed.expiresAt && now > parsed.expiresAt) {
|
|
25
|
+
localStorage.removeItem(key);
|
|
26
|
+
} else {
|
|
27
|
+
result[flagKey] = parsed.value || parsed;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
} catch {
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
clear() {
|
|
36
|
+
const keys = Object.keys(localStorage).filter(
|
|
37
|
+
(key) => key.startsWith("db-flag-")
|
|
38
|
+
);
|
|
39
|
+
for (const key of keys) {
|
|
40
|
+
localStorage.removeItem(key);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
getFromLocalStorage(key) {
|
|
44
|
+
try {
|
|
45
|
+
const item = localStorage.getItem(`db-flag-${key}`);
|
|
46
|
+
if (!item) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const parsed = JSON.parse(item);
|
|
50
|
+
if (parsed.expiresAt) {
|
|
51
|
+
if (this.isExpired(parsed.expiresAt)) {
|
|
52
|
+
localStorage.removeItem(`db-flag-${key}`);
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return parsed.value;
|
|
56
|
+
}
|
|
57
|
+
return parsed;
|
|
58
|
+
} catch {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
setToLocalStorage(key, value) {
|
|
63
|
+
try {
|
|
64
|
+
const item = {
|
|
65
|
+
value,
|
|
66
|
+
timestamp: Date.now(),
|
|
67
|
+
expiresAt: Date.now() + this.ttl
|
|
68
|
+
};
|
|
69
|
+
localStorage.setItem(`db-flag-${key}`, JSON.stringify(item));
|
|
70
|
+
} catch {
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
isExpired(expiresAt) {
|
|
74
|
+
if (!expiresAt) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
return Date.now() > expiresAt;
|
|
78
|
+
}
|
|
79
|
+
delete(key) {
|
|
80
|
+
localStorage.removeItem(`db-flag-${key}`);
|
|
81
|
+
}
|
|
82
|
+
deleteMultiple(keys) {
|
|
83
|
+
for (const key of keys) {
|
|
84
|
+
localStorage.removeItem(`db-flag-${key}`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
setAll(flags) {
|
|
88
|
+
const currentFlags = this.getAll();
|
|
89
|
+
const currentKeys = Object.keys(currentFlags);
|
|
90
|
+
const newKeys = Object.keys(flags);
|
|
91
|
+
const removedKeys = currentKeys.filter((key) => !newKeys.includes(key));
|
|
92
|
+
if (removedKeys.length > 0) {
|
|
93
|
+
this.deleteMultiple(removedKeys);
|
|
94
|
+
}
|
|
95
|
+
for (const [key, value] of Object.entries(flags)) {
|
|
96
|
+
this.set(key, value);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
cleanupExpired() {
|
|
100
|
+
const now = Date.now();
|
|
101
|
+
const keys = Object.keys(localStorage).filter(
|
|
102
|
+
(key) => key.startsWith("db-flag-")
|
|
103
|
+
);
|
|
104
|
+
for (const key of keys) {
|
|
105
|
+
try {
|
|
106
|
+
const item = localStorage.getItem(key);
|
|
107
|
+
if (item) {
|
|
108
|
+
const parsed = JSON.parse(item);
|
|
109
|
+
if (parsed.expiresAt && now > parsed.expiresAt) {
|
|
110
|
+
localStorage.removeItem(key);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
} catch {
|
|
114
|
+
localStorage.removeItem(key);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
class CoreFlagsManager {
|
|
121
|
+
config;
|
|
122
|
+
storage;
|
|
123
|
+
onFlagsUpdate;
|
|
124
|
+
onConfigUpdate;
|
|
125
|
+
onReady;
|
|
126
|
+
/** In-memory cache with stale tracking */
|
|
127
|
+
cache = /* @__PURE__ */ new Map();
|
|
128
|
+
/** In-flight requests for deduplication */
|
|
129
|
+
inFlight = /* @__PURE__ */ new Map();
|
|
130
|
+
/** Request batcher for batching multiple flag requests */
|
|
131
|
+
batcher = null;
|
|
132
|
+
/** Ready state */
|
|
133
|
+
ready = false;
|
|
134
|
+
/** Visibility state */
|
|
135
|
+
isVisible = true;
|
|
136
|
+
/** Visibility listener cleanup */
|
|
137
|
+
visibilityCleanup;
|
|
138
|
+
constructor(options) {
|
|
139
|
+
this.config = this.withDefaults(options.config);
|
|
140
|
+
this.storage = options.storage;
|
|
141
|
+
this.onFlagsUpdate = options.onFlagsUpdate;
|
|
142
|
+
this.onConfigUpdate = options.onConfigUpdate;
|
|
143
|
+
this.onReady = options.onReady;
|
|
144
|
+
logger.setDebug(this.config.debug ?? false);
|
|
145
|
+
logger.debug("FlagsManager initialized", {
|
|
146
|
+
clientId: this.config.clientId,
|
|
147
|
+
hasUser: Boolean(this.config.user)
|
|
148
|
+
});
|
|
149
|
+
this.setupVisibilityListener();
|
|
150
|
+
this.initialize();
|
|
151
|
+
}
|
|
152
|
+
withDefaults(config) {
|
|
153
|
+
return {
|
|
154
|
+
clientId: config.clientId,
|
|
155
|
+
apiUrl: config.apiUrl ?? "https://api.databuddy.cc",
|
|
156
|
+
user: config.user,
|
|
157
|
+
disabled: config.disabled ?? false,
|
|
158
|
+
debug: config.debug ?? false,
|
|
159
|
+
skipStorage: config.skipStorage ?? false,
|
|
160
|
+
isPending: config.isPending,
|
|
161
|
+
autoFetch: config.autoFetch !== false,
|
|
162
|
+
environment: config.environment,
|
|
163
|
+
cacheTtl: config.cacheTtl ?? 6e4,
|
|
164
|
+
// 1 minute
|
|
165
|
+
staleTime: config.staleTime ?? 3e4
|
|
166
|
+
// 30 seconds - revalidate after this
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
setupVisibilityListener() {
|
|
170
|
+
if (typeof document === "undefined") {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const handleVisibility = () => {
|
|
174
|
+
this.isVisible = document.visibilityState === "visible";
|
|
175
|
+
if (this.isVisible) {
|
|
176
|
+
this.revalidateStale();
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
document.addEventListener("visibilitychange", handleVisibility);
|
|
180
|
+
this.visibilityCleanup = () => {
|
|
181
|
+
document.removeEventListener("visibilitychange", handleVisibility);
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
async initialize() {
|
|
185
|
+
if (!this.config.skipStorage && this.storage) {
|
|
186
|
+
this.loadFromStorage();
|
|
187
|
+
}
|
|
188
|
+
if (this.config.autoFetch && !this.config.isPending) {
|
|
189
|
+
await this.fetchAllFlags();
|
|
190
|
+
}
|
|
191
|
+
this.ready = true;
|
|
192
|
+
this.onReady?.();
|
|
193
|
+
}
|
|
194
|
+
loadFromStorage() {
|
|
195
|
+
if (!this.storage) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
const stored = this.storage.getAll();
|
|
200
|
+
const ttl = this.config.cacheTtl ?? 6e4;
|
|
201
|
+
const staleTime = this.config.staleTime ?? ttl / 2;
|
|
202
|
+
for (const [key, value] of Object.entries(stored)) {
|
|
203
|
+
if (value && typeof value === "object") {
|
|
204
|
+
this.cache.set(key, createCacheEntry(value, ttl, staleTime));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (this.cache.size > 0) {
|
|
208
|
+
logger.debug(`Loaded ${this.cache.size} flags from storage`);
|
|
209
|
+
this.notifyUpdate();
|
|
210
|
+
}
|
|
211
|
+
} catch (err) {
|
|
212
|
+
logger.warn("Failed to load from storage:", err);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
saveToStorage() {
|
|
216
|
+
if (!this.storage || this.config.skipStorage) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
try {
|
|
220
|
+
const flags = {};
|
|
221
|
+
for (const [key, entry] of this.cache) {
|
|
222
|
+
flags[key] = entry.result;
|
|
223
|
+
}
|
|
224
|
+
this.storage.setAll(flags);
|
|
225
|
+
} catch (err) {
|
|
226
|
+
logger.warn("Failed to save to storage:", err);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
getFromCache(key) {
|
|
230
|
+
const cached = this.cache.get(key);
|
|
231
|
+
if (isCacheValid(cached)) {
|
|
232
|
+
return cached;
|
|
233
|
+
}
|
|
234
|
+
if (cached) {
|
|
235
|
+
this.cache.delete(key);
|
|
236
|
+
}
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
getBatcher() {
|
|
240
|
+
if (!this.batcher) {
|
|
241
|
+
const apiUrl = this.config.apiUrl ?? "https://api.databuddy.cc";
|
|
242
|
+
const params = buildQueryParams(this.config);
|
|
243
|
+
this.batcher = new RequestBatcher(apiUrl, params);
|
|
244
|
+
}
|
|
245
|
+
return this.batcher;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Revalidate stale entries in background
|
|
249
|
+
*/
|
|
250
|
+
revalidateStale() {
|
|
251
|
+
const staleKeys = [];
|
|
252
|
+
for (const [key, entry] of this.cache) {
|
|
253
|
+
if (isCacheStale(entry)) {
|
|
254
|
+
staleKeys.push(key.split(":")[0]);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (staleKeys.length > 0) {
|
|
258
|
+
logger.debug(`Revalidating ${staleKeys.length} stale flags`);
|
|
259
|
+
this.fetchAllFlags().catch(
|
|
260
|
+
(err) => logger.error("Revalidation error:", err)
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Fetch a single flag from API with deduplication and batching
|
|
266
|
+
*/
|
|
267
|
+
async getFlag(key, user) {
|
|
268
|
+
if (this.config.disabled) {
|
|
269
|
+
return DEFAULT_RESULT;
|
|
270
|
+
}
|
|
271
|
+
if (this.config.isPending) {
|
|
272
|
+
return { ...DEFAULT_RESULT, reason: "SESSION_PENDING" };
|
|
273
|
+
}
|
|
274
|
+
const cacheKey = getCacheKey(key, user ?? this.config.user);
|
|
275
|
+
const cached = this.getFromCache(cacheKey);
|
|
276
|
+
if (cached) {
|
|
277
|
+
if (isCacheStale(cached) && this.isVisible) {
|
|
278
|
+
this.revalidateFlag(key, cacheKey);
|
|
279
|
+
}
|
|
280
|
+
return cached.result;
|
|
281
|
+
}
|
|
282
|
+
const existing = this.inFlight.get(cacheKey);
|
|
283
|
+
if (existing) {
|
|
284
|
+
logger.debug(`Deduplicating request: ${key}`);
|
|
285
|
+
return existing;
|
|
286
|
+
}
|
|
287
|
+
const promise = this.getBatcher().request(key);
|
|
288
|
+
this.inFlight.set(cacheKey, promise);
|
|
289
|
+
try {
|
|
290
|
+
const result = await promise;
|
|
291
|
+
const ttl = this.config.cacheTtl ?? 6e4;
|
|
292
|
+
const staleTime = this.config.staleTime ?? ttl / 2;
|
|
293
|
+
this.cache.set(cacheKey, createCacheEntry(result, ttl, staleTime));
|
|
294
|
+
this.notifyUpdate();
|
|
295
|
+
this.saveToStorage();
|
|
296
|
+
return result;
|
|
297
|
+
} finally {
|
|
298
|
+
this.inFlight.delete(cacheKey);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
async revalidateFlag(key, cacheKey) {
|
|
302
|
+
if (this.inFlight.has(cacheKey)) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
const promise = this.getBatcher().request(key);
|
|
306
|
+
this.inFlight.set(cacheKey, promise);
|
|
307
|
+
try {
|
|
308
|
+
const result = await promise;
|
|
309
|
+
const ttl = this.config.cacheTtl ?? 6e4;
|
|
310
|
+
const staleTime = this.config.staleTime ?? ttl / 2;
|
|
311
|
+
this.cache.set(cacheKey, createCacheEntry(result, ttl, staleTime));
|
|
312
|
+
this.notifyUpdate();
|
|
313
|
+
this.saveToStorage();
|
|
314
|
+
logger.debug(`Revalidated flag: ${key}`);
|
|
315
|
+
} catch (err) {
|
|
316
|
+
logger.error(`Revalidation error: ${key}`, err);
|
|
317
|
+
} finally {
|
|
318
|
+
this.inFlight.delete(cacheKey);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Fetch all flags for current user
|
|
323
|
+
*/
|
|
324
|
+
async fetchAllFlags(user) {
|
|
325
|
+
if (this.config.disabled || this.config.isPending) {
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
if (!this.isVisible && this.cache.size > 0) {
|
|
329
|
+
logger.debug("Skipping fetch - tab hidden");
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const apiUrl = this.config.apiUrl ?? "https://api.databuddy.cc";
|
|
333
|
+
const params = buildQueryParams(this.config, user);
|
|
334
|
+
try {
|
|
335
|
+
const flags = await fetchAllFlags(apiUrl, params);
|
|
336
|
+
const ttl = this.config.cacheTtl ?? 6e4;
|
|
337
|
+
const staleTime = this.config.staleTime ?? ttl / 2;
|
|
338
|
+
for (const [key, result] of Object.entries(flags)) {
|
|
339
|
+
const cacheKey = getCacheKey(key, user ?? this.config.user);
|
|
340
|
+
this.cache.set(cacheKey, createCacheEntry(result, ttl, staleTime));
|
|
341
|
+
}
|
|
342
|
+
this.ready = true;
|
|
343
|
+
this.notifyUpdate();
|
|
344
|
+
this.saveToStorage();
|
|
345
|
+
logger.debug(`Fetched ${Object.keys(flags).length} flags`);
|
|
346
|
+
} catch (err) {
|
|
347
|
+
logger.error("Bulk fetch error:", err);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Check if flag is enabled (synchronous, returns cached value)
|
|
352
|
+
* Uses stale-while-revalidate pattern
|
|
353
|
+
*/
|
|
354
|
+
isEnabled(key) {
|
|
355
|
+
const cacheKey = getCacheKey(key, this.config.user);
|
|
356
|
+
const cached = this.getFromCache(cacheKey);
|
|
357
|
+
if (cached) {
|
|
358
|
+
if (isCacheStale(cached) && this.isVisible) {
|
|
359
|
+
this.revalidateFlag(key, cacheKey);
|
|
360
|
+
}
|
|
361
|
+
return {
|
|
362
|
+
on: cached.result.enabled,
|
|
363
|
+
enabled: cached.result.enabled,
|
|
364
|
+
status: cached.result.reason === "ERROR" ? "error" : "ready",
|
|
365
|
+
loading: false,
|
|
366
|
+
isLoading: false,
|
|
367
|
+
isReady: true,
|
|
368
|
+
value: cached.result.value,
|
|
369
|
+
variant: cached.result.variant
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
if (this.inFlight.has(cacheKey)) {
|
|
373
|
+
return {
|
|
374
|
+
on: false,
|
|
375
|
+
enabled: false,
|
|
376
|
+
status: "loading",
|
|
377
|
+
loading: true,
|
|
378
|
+
isLoading: true,
|
|
379
|
+
isReady: false
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
this.getFlag(key).catch(
|
|
383
|
+
(err) => logger.error(`Background fetch error: ${key}`, err)
|
|
384
|
+
);
|
|
385
|
+
return {
|
|
386
|
+
on: false,
|
|
387
|
+
enabled: false,
|
|
388
|
+
status: "loading",
|
|
389
|
+
loading: true,
|
|
390
|
+
isLoading: true,
|
|
391
|
+
isReady: false
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Get flag value with type (synchronous, returns cached or default)
|
|
396
|
+
*/
|
|
397
|
+
getValue(key, defaultValue) {
|
|
398
|
+
const cacheKey = getCacheKey(key, this.config.user);
|
|
399
|
+
const cached = this.getFromCache(cacheKey);
|
|
400
|
+
if (cached) {
|
|
401
|
+
if (isCacheStale(cached) && this.isVisible) {
|
|
402
|
+
this.revalidateFlag(key, cacheKey);
|
|
403
|
+
}
|
|
404
|
+
return cached.result.value;
|
|
405
|
+
}
|
|
406
|
+
if (!this.inFlight.has(cacheKey)) {
|
|
407
|
+
this.getFlag(key).catch(
|
|
408
|
+
(err) => logger.error(`Background fetch error: ${key}`, err)
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
return defaultValue ?? false;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Update user context and refresh flags
|
|
415
|
+
*/
|
|
416
|
+
updateUser(user) {
|
|
417
|
+
this.config = { ...this.config, user };
|
|
418
|
+
this.batcher?.destroy();
|
|
419
|
+
this.batcher = null;
|
|
420
|
+
this.onConfigUpdate?.(this.config);
|
|
421
|
+
this.refresh().catch((err) => logger.error("Refresh error:", err));
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Refresh all flags
|
|
425
|
+
*/
|
|
426
|
+
async refresh(forceClear = false) {
|
|
427
|
+
if (forceClear) {
|
|
428
|
+
this.cache.clear();
|
|
429
|
+
this.storage?.clear();
|
|
430
|
+
this.notifyUpdate();
|
|
431
|
+
}
|
|
432
|
+
await this.fetchAllFlags();
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Update configuration
|
|
436
|
+
*/
|
|
437
|
+
updateConfig(config) {
|
|
438
|
+
const wasDisabled = this.config.disabled;
|
|
439
|
+
const wasPending = this.config.isPending;
|
|
440
|
+
this.config = this.withDefaults(config);
|
|
441
|
+
this.batcher?.destroy();
|
|
442
|
+
this.batcher = null;
|
|
443
|
+
this.onConfigUpdate?.(this.config);
|
|
444
|
+
if ((wasDisabled || wasPending) && !this.config.disabled && !this.config.isPending) {
|
|
445
|
+
this.fetchAllFlags().catch((err) => logger.error("Fetch error:", err));
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Get all cached flags
|
|
450
|
+
*/
|
|
451
|
+
getMemoryFlags() {
|
|
452
|
+
const flags = {};
|
|
453
|
+
for (const [key, entry] of this.cache) {
|
|
454
|
+
const flagKey = key.split(":")[0];
|
|
455
|
+
flags[flagKey] = entry.result;
|
|
456
|
+
}
|
|
457
|
+
return flags;
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Check if manager is ready
|
|
461
|
+
*/
|
|
462
|
+
isReady() {
|
|
463
|
+
return this.ready;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Cleanup resources
|
|
467
|
+
*/
|
|
468
|
+
destroy() {
|
|
469
|
+
this.batcher?.destroy();
|
|
470
|
+
this.visibilityCleanup?.();
|
|
471
|
+
this.cache.clear();
|
|
472
|
+
this.inFlight.clear();
|
|
473
|
+
}
|
|
474
|
+
notifyUpdate() {
|
|
475
|
+
this.onFlagsUpdate?.(this.getMemoryFlags());
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
export { BrowserFlagStorage as B, CoreFlagsManager as C };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const version = "2.3.2";
|
|
2
|
+
|
|
3
|
+
const INJECTED_SCRIPT_ATTRIBUTE = "data-databuddy-injected";
|
|
4
|
+
function isScriptInjected() {
|
|
5
|
+
return !!document.querySelector(`script[${INJECTED_SCRIPT_ATTRIBUTE}]`);
|
|
6
|
+
}
|
|
7
|
+
function createScript({
|
|
8
|
+
scriptUrl,
|
|
9
|
+
sdkVersion,
|
|
10
|
+
clientSecret,
|
|
11
|
+
filter,
|
|
12
|
+
debug,
|
|
13
|
+
...props
|
|
14
|
+
}) {
|
|
15
|
+
const script = document.createElement("script");
|
|
16
|
+
script.src = scriptUrl || "https://cdn.databuddy.cc/databuddy.js";
|
|
17
|
+
script.async = true;
|
|
18
|
+
script.crossOrigin = "anonymous";
|
|
19
|
+
script.setAttribute(INJECTED_SCRIPT_ATTRIBUTE, "true");
|
|
20
|
+
script.setAttribute("data-sdk-version", sdkVersion || version);
|
|
21
|
+
for (const [key, value] of Object.entries(props)) {
|
|
22
|
+
if (value === void 0 || value === null) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const dataKey = `data-${key.replace(/([A-Z])/g, "-$1").toLowerCase()}`;
|
|
26
|
+
if (Array.isArray(value) || value && typeof value === "object") {
|
|
27
|
+
script.setAttribute(dataKey, JSON.stringify(value));
|
|
28
|
+
} else {
|
|
29
|
+
script.setAttribute(dataKey, String(value));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return script;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { createScript as c, isScriptInjected as i };
|
package/dist/vue/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as vue from 'vue';
|
|
2
2
|
import { App, ComputedRef } from 'vue';
|
|
3
|
-
import {
|
|
4
|
-
export {
|
|
3
|
+
import { F as FlagsConfig, b as FlagState, d as FlagResult } from '../shared/@databuddy/sdk.B6nwxnPC.mjs';
|
|
4
|
+
export { c as FlagsContext } from '../shared/@databuddy/sdk.B6nwxnPC.mjs';
|
|
5
5
|
|
|
6
6
|
declare const Databuddy: vue.DefineComponent<{
|
|
7
7
|
clientId?: string | undefined;
|
|
@@ -78,9 +78,17 @@ declare function useFlags(): {
|
|
|
78
78
|
};
|
|
79
79
|
|
|
80
80
|
interface UseFlagReturn {
|
|
81
|
+
/** Whether the flag is on */
|
|
82
|
+
on: ComputedRef<boolean>;
|
|
83
|
+
/** @deprecated Use `on` instead */
|
|
81
84
|
enabled: ComputedRef<boolean>;
|
|
85
|
+
/** Whether the flag is loading */
|
|
86
|
+
loading: ComputedRef<boolean>;
|
|
87
|
+
/** @deprecated Use `loading` instead */
|
|
82
88
|
isLoading: ComputedRef<boolean>;
|
|
89
|
+
/** @deprecated Use `status === 'ready'` instead */
|
|
83
90
|
isReady: ComputedRef<boolean>;
|
|
91
|
+
/** Full flag state */
|
|
84
92
|
state: ComputedRef<FlagState>;
|
|
85
93
|
}
|
|
86
94
|
/**
|
|
@@ -89,6 +97,7 @@ interface UseFlagReturn {
|
|
|
89
97
|
declare function useFlag(key: string): UseFlagReturn;
|
|
90
98
|
/**
|
|
91
99
|
* Vue composable for boolean flag checking
|
|
100
|
+
* @deprecated Use `useFlag(key).on` instead
|
|
92
101
|
*/
|
|
93
102
|
declare function useBooleanFlag(key: string): ComputedRef<boolean>;
|
|
94
103
|
|
package/dist/vue/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as vue from 'vue';
|
|
2
2
|
import { App, ComputedRef } from 'vue';
|
|
3
|
-
import {
|
|
4
|
-
export {
|
|
3
|
+
import { F as FlagsConfig, b as FlagState, d as FlagResult } from '../shared/@databuddy/sdk.B6nwxnPC.js';
|
|
4
|
+
export { c as FlagsContext } from '../shared/@databuddy/sdk.B6nwxnPC.js';
|
|
5
5
|
|
|
6
6
|
declare const Databuddy: vue.DefineComponent<{
|
|
7
7
|
clientId?: string | undefined;
|
|
@@ -78,9 +78,17 @@ declare function useFlags(): {
|
|
|
78
78
|
};
|
|
79
79
|
|
|
80
80
|
interface UseFlagReturn {
|
|
81
|
+
/** Whether the flag is on */
|
|
82
|
+
on: ComputedRef<boolean>;
|
|
83
|
+
/** @deprecated Use `on` instead */
|
|
81
84
|
enabled: ComputedRef<boolean>;
|
|
85
|
+
/** Whether the flag is loading */
|
|
86
|
+
loading: ComputedRef<boolean>;
|
|
87
|
+
/** @deprecated Use `loading` instead */
|
|
82
88
|
isLoading: ComputedRef<boolean>;
|
|
89
|
+
/** @deprecated Use `status === 'ready'` instead */
|
|
83
90
|
isReady: ComputedRef<boolean>;
|
|
91
|
+
/** Full flag state */
|
|
84
92
|
state: ComputedRef<FlagState>;
|
|
85
93
|
}
|
|
86
94
|
/**
|
|
@@ -89,6 +97,7 @@ interface UseFlagReturn {
|
|
|
89
97
|
declare function useFlag(key: string): UseFlagReturn;
|
|
90
98
|
/**
|
|
91
99
|
* Vue composable for boolean flag checking
|
|
100
|
+
* @deprecated Use `useFlag(key).on` instead
|
|
92
101
|
*/
|
|
93
102
|
declare function useBooleanFlag(key: string): ComputedRef<boolean>;
|
|
94
103
|
|
package/dist/vue/index.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { defineComponent, ref, onMounted, onUnmounted, watch, reactive, watchEffect, computed } from 'vue';
|
|
2
|
-
import { i as isScriptInjected, c as createScript
|
|
2
|
+
import { i as isScriptInjected, c as createScript } from '../shared/@databuddy/sdk.F8Xt1uF7.mjs';
|
|
3
|
+
import { B as BrowserFlagStorage, C as CoreFlagsManager } from '../shared/@databuddy/sdk.Du7SE7-M.mjs';
|
|
4
|
+
import '../shared/@databuddy/sdk.3kaCzyfu.mjs';
|
|
3
5
|
|
|
4
6
|
const Databuddy = defineComponent({
|
|
5
7
|
props: {},
|
|
@@ -77,7 +79,9 @@ function useFlags() {
|
|
|
77
79
|
const isEnabled = (key) => manager.isEnabled(key);
|
|
78
80
|
const fetchAllFlags = () => manager.fetchAllFlags();
|
|
79
81
|
const updateUser = (user) => {
|
|
80
|
-
|
|
82
|
+
if (user) {
|
|
83
|
+
manager.updateUser(user);
|
|
84
|
+
}
|
|
81
85
|
};
|
|
82
86
|
const refresh = (forceClear = false) => {
|
|
83
87
|
manager.refresh(forceClear);
|
|
@@ -95,26 +99,32 @@ function useFlags() {
|
|
|
95
99
|
};
|
|
96
100
|
}
|
|
97
101
|
|
|
102
|
+
const defaultState = {
|
|
103
|
+
on: false,
|
|
104
|
+
enabled: false,
|
|
105
|
+
status: "loading",
|
|
106
|
+
loading: true,
|
|
107
|
+
isLoading: true,
|
|
108
|
+
isReady: false
|
|
109
|
+
};
|
|
98
110
|
function useFlag(key) {
|
|
99
111
|
const { isEnabled } = useFlags();
|
|
100
|
-
const flagState = ref(
|
|
101
|
-
enabled: false,
|
|
102
|
-
isLoading: true,
|
|
103
|
-
isReady: false
|
|
104
|
-
});
|
|
112
|
+
const flagState = ref(defaultState);
|
|
105
113
|
watchEffect(() => {
|
|
106
114
|
flagState.value = isEnabled(key);
|
|
107
115
|
});
|
|
108
116
|
return {
|
|
117
|
+
on: computed(() => flagState.value.on),
|
|
109
118
|
enabled: computed(() => flagState.value.enabled),
|
|
119
|
+
loading: computed(() => flagState.value.loading),
|
|
110
120
|
isLoading: computed(() => flagState.value.isLoading),
|
|
111
121
|
isReady: computed(() => flagState.value.isReady),
|
|
112
122
|
state: computed(() => flagState.value)
|
|
113
123
|
};
|
|
114
124
|
}
|
|
115
125
|
function useBooleanFlag(key) {
|
|
116
|
-
const {
|
|
117
|
-
return
|
|
126
|
+
const { on } = useFlag(key);
|
|
127
|
+
return on;
|
|
118
128
|
}
|
|
119
129
|
|
|
120
130
|
export { Databuddy, createFlagsPlugin, useBooleanFlag, useFlag, useFlags };
|
package/package.json
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@databuddy/sdk",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.2",
|
|
4
4
|
"description": "Official Databuddy Analytics SDK",
|
|
5
5
|
"main": "./dist/core/index.mjs",
|
|
6
6
|
"types": "./dist/core/index.d.ts",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"private": false,
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"type": "module",
|
|
9
11
|
"scripts": {
|
|
10
12
|
"build": "unbuild",
|
|
11
13
|
"test": "bun test",
|
|
12
14
|
"vscode:prepublish": "bun run build"
|
|
13
15
|
},
|
|
14
|
-
"dependencies": {
|
|
15
|
-
"jotai": "^2.15.1"
|
|
16
|
-
},
|
|
17
16
|
"devDependencies": {
|
|
18
17
|
"@ai-sdk/provider": "^2.0.0",
|
|
19
18
|
"@types/node": "^20.0.0",
|