@avsbhq/vue 1.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/dist/index.cjs ADDED
@@ -0,0 +1,522 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ AvsbInjectionKey: () => AvsbInjectionKey,
24
+ AvsbPlugin: () => AvsbPlugin,
25
+ AvsbProvider: () => AvsbProvider,
26
+ useAlias: () => useAlias,
27
+ useAllFlags: () => useAllFlags,
28
+ useAvsbClient: () => useAvsbClient,
29
+ useAvsbStore: () => useAvsbStore,
30
+ useBoolFlag: () => useBoolFlag,
31
+ useFlag: () => useFlag,
32
+ useFlagReady: () => useFlagReady,
33
+ useFlagValue: () => useFlagValue,
34
+ useIdentify: () => useIdentify,
35
+ useJsonFlag: () => useJsonFlag,
36
+ useNumberFlag: () => useNumberFlag,
37
+ useStringFlag: () => useStringFlag,
38
+ useTrack: () => useTrack
39
+ });
40
+ module.exports = __toCommonJS(index_exports);
41
+
42
+ // src/AvsbProvider.ts
43
+ var import_vue2 = require("vue");
44
+ var import_browser = require("@avsbhq/browser");
45
+
46
+ // src/context.ts
47
+ var import_vue = require("vue");
48
+ var AvsbInjectionKey = /* @__PURE__ */ Symbol("avsb-injection-key");
49
+ function useAvsbContext() {
50
+ const v = (0, import_vue.inject)(AvsbInjectionKey);
51
+ if (v === void 0) {
52
+ throw new Error(
53
+ '[avsb] composables must be used inside <AvsbProvider> or app.use(AvsbPlugin). Wrap your component tree with <AvsbProvider sdkKey="..." context={...}> or install the plugin.'
54
+ );
55
+ }
56
+ return v;
57
+ }
58
+
59
+ // src/AvsbProvider.ts
60
+ var AvsbProvider = (0, import_vue2.defineComponent)({
61
+ name: "AvsbProvider",
62
+ props: {
63
+ // Mode B — caller passes a pre-built client
64
+ client: {
65
+ type: Object,
66
+ default: void 0
67
+ },
68
+ // Mode A — sdkKey is the minimum required field from AvsbClientOptions
69
+ sdkKey: {
70
+ type: String,
71
+ default: void 0
72
+ }
73
+ },
74
+ // Accept any additional option props (pollingInterval, bootstrap, etc.)
75
+ // via inheritAttrs + $attrs so callers can pass the full AvsbClientOptions
76
+ // surface without enumerating every field as a prop.
77
+ inheritAttrs: false,
78
+ setup(props, { slots, attrs }) {
79
+ const injectedClient = props.client ?? null;
80
+ const isInjectionMode = injectedClient !== null;
81
+ let ownedClient = null;
82
+ if (!isInjectionMode) {
83
+ if (!props.sdkKey) {
84
+ throw new Error(
85
+ "[avsb] <AvsbProvider> requires either a `client` prop (Mode B) or a `sdkKey` prop (Mode A)."
86
+ );
87
+ }
88
+ const opts = {
89
+ sdkKey: props.sdkKey,
90
+ ...attrs
91
+ };
92
+ ownedClient = new import_browser.AvsbClient(opts);
93
+ }
94
+ const activeClient = isInjectionMode ? injectedClient : ownedClient;
95
+ const status = (0, import_vue2.ref)("loading");
96
+ const error = (0, import_vue2.ref)(void 0);
97
+ (0, import_vue2.provide)(AvsbInjectionKey, {
98
+ get client() {
99
+ return activeClient;
100
+ },
101
+ get status() {
102
+ return status.value;
103
+ },
104
+ get error() {
105
+ return error.value;
106
+ }
107
+ });
108
+ let cancelled = false;
109
+ let unsubscribeError;
110
+ (0, import_vue2.onMounted)(() => {
111
+ if (!activeClient) return;
112
+ activeClient.onReady().then(() => {
113
+ if (!cancelled) status.value = "ready";
114
+ }).catch((err) => {
115
+ if (!cancelled) {
116
+ status.value = "error";
117
+ error.value = err instanceof Error ? err : new Error(String(err));
118
+ }
119
+ });
120
+ if (typeof activeClient.on === "function") {
121
+ unsubscribeError = activeClient.on("error", (e) => {
122
+ if (!cancelled) {
123
+ status.value = "error";
124
+ error.value = e;
125
+ }
126
+ });
127
+ }
128
+ });
129
+ (0, import_vue2.onUnmounted)(() => {
130
+ cancelled = true;
131
+ unsubscribeError?.();
132
+ if (!isInjectionMode && ownedClient) {
133
+ ownedClient.close().catch(() => {
134
+ });
135
+ ownedClient = null;
136
+ }
137
+ });
138
+ return () => slots.default?.();
139
+ },
140
+ render() {
141
+ return (0, import_vue2.h)("div");
142
+ }
143
+ });
144
+
145
+ // src/plugin.ts
146
+ var import_vue3 = require("vue");
147
+ var import_browser2 = require("@avsbhq/browser");
148
+ var AvsbPlugin = {
149
+ install(app, options) {
150
+ const injectedClient = "client" in options && options.client ? options.client : null;
151
+ const isInjectionMode = injectedClient !== null;
152
+ let activeClient;
153
+ if (isInjectionMode) {
154
+ activeClient = injectedClient;
155
+ } else {
156
+ const opts = options;
157
+ activeClient = new import_browser2.AvsbClient(opts);
158
+ }
159
+ const status = (0, import_vue3.ref)("loading");
160
+ const error = (0, import_vue3.ref)(void 0);
161
+ app.provide(AvsbInjectionKey, {
162
+ get client() {
163
+ return activeClient;
164
+ },
165
+ get status() {
166
+ return status.value;
167
+ },
168
+ get error() {
169
+ return error.value;
170
+ }
171
+ });
172
+ activeClient.onReady().then(() => {
173
+ status.value = "ready";
174
+ }).catch((err) => {
175
+ status.value = "error";
176
+ error.value = err instanceof Error ? err : new Error(String(err));
177
+ });
178
+ if (typeof activeClient.on === "function") {
179
+ activeClient.on("error", (e) => {
180
+ status.value = "error";
181
+ error.value = e;
182
+ });
183
+ }
184
+ if (!isInjectionMode) {
185
+ app.unmount = new Proxy(app.unmount, {
186
+ apply(target, thisArg, args) {
187
+ activeClient.close().catch(() => {
188
+ });
189
+ return Reflect.apply(target, thisArg, args);
190
+ }
191
+ });
192
+ }
193
+ }
194
+ };
195
+
196
+ // src/composables/useFlag.ts
197
+ var import_vue4 = require("vue");
198
+ var import_core = require("@avsbhq/core");
199
+ function useFlag(flagKey, defaultValue) {
200
+ const { client } = useAvsbContext();
201
+ const noClientSentinel = (0, import_core.notFoundFlag)(defaultValue, "no client");
202
+ const flagRef = (0, import_vue4.ref)(
203
+ client !== null && typeof client.getSnapshot === "function" ? client.getSnapshot(
204
+ (0, import_vue4.isRef)(flagKey) ? flagKey.value : flagKey,
205
+ defaultValue
206
+ ) : noClientSentinel
207
+ );
208
+ let unsub;
209
+ function resubscribe() {
210
+ unsub?.();
211
+ unsub = void 0;
212
+ if (!client || typeof client.subscribe !== "function") return;
213
+ const key = (0, import_vue4.isRef)(flagKey) ? flagKey.value : flagKey;
214
+ if (typeof client.getSnapshot === "function") {
215
+ flagRef.value = client.getSnapshot(key, defaultValue);
216
+ }
217
+ unsub = client.subscribe(key, () => {
218
+ if (typeof client.getSnapshot === "function") {
219
+ flagRef.value = client.getSnapshot(
220
+ (0, import_vue4.isRef)(flagKey) ? flagKey.value : flagKey,
221
+ defaultValue
222
+ );
223
+ }
224
+ });
225
+ }
226
+ let stopWatcher;
227
+ (0, import_vue4.onMounted)(() => {
228
+ if ((0, import_vue4.isRef)(flagKey)) {
229
+ stopWatcher = (0, import_vue4.watchEffect)(() => {
230
+ void flagKey.value;
231
+ resubscribe();
232
+ });
233
+ } else {
234
+ resubscribe();
235
+ }
236
+ });
237
+ (0, import_vue4.onUnmounted)(() => {
238
+ stopWatcher?.();
239
+ unsub?.();
240
+ });
241
+ return (0, import_vue4.computed)(() => flagRef.value);
242
+ }
243
+
244
+ // src/composables/useFlagValue.ts
245
+ var import_vue5 = require("vue");
246
+ function useFlagValue(flagKey, defaultValue) {
247
+ const flag = useFlag(flagKey, defaultValue);
248
+ return (0, import_vue5.computed)(() => flag.value.value);
249
+ }
250
+
251
+ // src/composables/useBoolFlag.ts
252
+ var import_vue6 = require("vue");
253
+ var import_core2 = require("@avsbhq/core");
254
+ function useBoolFlag(flagKey, defaultValue) {
255
+ const { client } = useAvsbContext();
256
+ const noClientSentinel = (0, import_core2.notFoundFlag)(defaultValue, "no client");
257
+ const flagRef = (0, import_vue6.ref)(
258
+ client !== null && typeof client.getBoolFlag === "function" ? client.getBoolFlag(flagKey, defaultValue) : noClientSentinel
259
+ );
260
+ let unsub;
261
+ (0, import_vue6.onMounted)(() => {
262
+ if (!client || typeof client.subscribe !== "function") return;
263
+ if (typeof client.getBoolFlag === "function") {
264
+ flagRef.value = client.getBoolFlag(flagKey, defaultValue);
265
+ }
266
+ unsub = client.subscribe(flagKey, () => {
267
+ if (typeof client.getBoolFlag === "function") {
268
+ flagRef.value = client.getBoolFlag(flagKey, defaultValue);
269
+ }
270
+ });
271
+ });
272
+ (0, import_vue6.onUnmounted)(() => {
273
+ unsub?.();
274
+ });
275
+ return (0, import_vue6.computed)(() => flagRef.value);
276
+ }
277
+
278
+ // src/composables/useStringFlag.ts
279
+ var import_vue7 = require("vue");
280
+ var import_core3 = require("@avsbhq/core");
281
+ function useStringFlag(flagKey, defaultValue) {
282
+ const { client } = useAvsbContext();
283
+ const noClientSentinel = (0, import_core3.notFoundFlag)(defaultValue, "no client");
284
+ const flagRef = (0, import_vue7.ref)(
285
+ client !== null && typeof client.getStringFlag === "function" ? client.getStringFlag(flagKey, defaultValue) : noClientSentinel
286
+ );
287
+ let unsub;
288
+ (0, import_vue7.onMounted)(() => {
289
+ if (!client || typeof client.subscribe !== "function") return;
290
+ if (typeof client.getStringFlag === "function") {
291
+ flagRef.value = client.getStringFlag(flagKey, defaultValue);
292
+ }
293
+ unsub = client.subscribe(flagKey, () => {
294
+ if (typeof client.getStringFlag === "function") {
295
+ flagRef.value = client.getStringFlag(flagKey, defaultValue);
296
+ }
297
+ });
298
+ });
299
+ (0, import_vue7.onUnmounted)(() => {
300
+ unsub?.();
301
+ });
302
+ return (0, import_vue7.computed)(() => flagRef.value);
303
+ }
304
+
305
+ // src/composables/useNumberFlag.ts
306
+ var import_vue8 = require("vue");
307
+ var import_core4 = require("@avsbhq/core");
308
+ function useNumberFlag(flagKey, defaultValue) {
309
+ const { client } = useAvsbContext();
310
+ const noClientSentinel = (0, import_core4.notFoundFlag)(defaultValue, "no client");
311
+ const flagRef = (0, import_vue8.ref)(
312
+ client !== null && typeof client.getNumberFlag === "function" ? client.getNumberFlag(flagKey, defaultValue) : noClientSentinel
313
+ );
314
+ let unsub;
315
+ (0, import_vue8.onMounted)(() => {
316
+ if (!client || typeof client.subscribe !== "function") return;
317
+ if (typeof client.getNumberFlag === "function") {
318
+ flagRef.value = client.getNumberFlag(flagKey, defaultValue);
319
+ }
320
+ unsub = client.subscribe(flagKey, () => {
321
+ if (typeof client.getNumberFlag === "function") {
322
+ flagRef.value = client.getNumberFlag(flagKey, defaultValue);
323
+ }
324
+ });
325
+ });
326
+ (0, import_vue8.onUnmounted)(() => {
327
+ unsub?.();
328
+ });
329
+ return (0, import_vue8.computed)(() => flagRef.value);
330
+ }
331
+
332
+ // src/composables/useJsonFlag.ts
333
+ var import_vue9 = require("vue");
334
+ var import_core5 = require("@avsbhq/core");
335
+ function useJsonFlag(flagKey, defaultValue) {
336
+ const { client } = useAvsbContext();
337
+ const noClientSentinel = (0, import_core5.notFoundFlag)(defaultValue, "no client");
338
+ const flagRef = (0, import_vue9.ref)(
339
+ client !== null && typeof client.getJsonFlag === "function" ? client.getJsonFlag(flagKey, defaultValue) : noClientSentinel
340
+ );
341
+ let unsub;
342
+ (0, import_vue9.onMounted)(() => {
343
+ if (!client || typeof client.subscribe !== "function") return;
344
+ if (typeof client.getJsonFlag === "function") {
345
+ flagRef.value = client.getJsonFlag(flagKey, defaultValue);
346
+ }
347
+ unsub = client.subscribe(flagKey, () => {
348
+ if (typeof client.getJsonFlag === "function") {
349
+ flagRef.value = client.getJsonFlag(flagKey, defaultValue);
350
+ }
351
+ });
352
+ });
353
+ (0, import_vue9.onUnmounted)(() => {
354
+ unsub?.();
355
+ });
356
+ return (0, import_vue9.computed)(() => flagRef.value);
357
+ }
358
+
359
+ // src/composables/useAllFlags.ts
360
+ var import_vue10 = require("vue");
361
+ function useAllFlags() {
362
+ const { client } = useAvsbContext();
363
+ const snapshotRef = (0, import_vue10.ref)(
364
+ client !== null && typeof client.getAllFlags === "function" ? client.getAllFlags() : {}
365
+ );
366
+ let unsub;
367
+ (0, import_vue10.onMounted)(() => {
368
+ if (!client || typeof client.on !== "function") return;
369
+ if (typeof client.getAllFlags === "function") {
370
+ snapshotRef.value = client.getAllFlags();
371
+ }
372
+ unsub = client.on("flagChange", () => {
373
+ if (typeof client.getAllFlags === "function") {
374
+ snapshotRef.value = client.getAllFlags();
375
+ }
376
+ });
377
+ });
378
+ (0, import_vue10.onUnmounted)(() => {
379
+ unsub?.();
380
+ });
381
+ return (0, import_vue10.computed)(() => snapshotRef.value);
382
+ }
383
+
384
+ // src/composables/useFlagReady.ts
385
+ var import_vue11 = require("vue");
386
+ function useFlagReady() {
387
+ const context = useAvsbContext();
388
+ return (0, import_vue11.computed)(() => context.status === "ready");
389
+ }
390
+
391
+ // src/composables/useAvsbClient.ts
392
+ function useAvsbClient() {
393
+ const { client } = useAvsbContext();
394
+ return client;
395
+ }
396
+
397
+ // src/composables/useIdentify.ts
398
+ var import_vue12 = require("vue");
399
+ function useIdentify() {
400
+ const { client } = useAvsbContext();
401
+ const clientRef = (0, import_vue12.ref)(client);
402
+ clientRef.value = client;
403
+ function identify(ctx) {
404
+ const c = clientRef.value;
405
+ if (!c || typeof c.identify !== "function") return;
406
+ c.identify(ctx);
407
+ }
408
+ return identify;
409
+ }
410
+
411
+ // src/composables/useAlias.ts
412
+ var import_vue13 = require("vue");
413
+ function useAlias() {
414
+ const { client } = useAvsbContext();
415
+ const clientRef = (0, import_vue13.ref)(client);
416
+ clientRef.value = client;
417
+ async function alias(prev, next) {
418
+ const c = clientRef.value;
419
+ if (!c || typeof c.alias !== "function") return;
420
+ await c.alias(prev, next);
421
+ }
422
+ return alias;
423
+ }
424
+
425
+ // src/composables/useTrack.ts
426
+ var import_vue14 = require("vue");
427
+ function useTrack() {
428
+ const { client } = useAvsbContext();
429
+ const clientRef = (0, import_vue14.ref)(client);
430
+ clientRef.value = client;
431
+ function track(eventKey, payload) {
432
+ const c = clientRef.value;
433
+ if (!c || typeof c.track !== "function") return;
434
+ c.track(eventKey, payload);
435
+ }
436
+ return track;
437
+ }
438
+
439
+ // src/pinia.ts
440
+ var import_pinia = require("pinia");
441
+ var import_vue15 = require("vue");
442
+ var import_vue16 = require("vue");
443
+ var useAvsbStore = (0, import_pinia.defineStore)("avsb", () => {
444
+ const injectedContext = (0, import_vue16.inject)(AvsbInjectionKey);
445
+ const _client = (0, import_vue15.ref)(injectedContext?.client ?? null);
446
+ const _status = (0, import_vue15.ref)(
447
+ injectedContext?.status ?? "loading"
448
+ );
449
+ const _error = (0, import_vue15.ref)(injectedContext?.error);
450
+ if (injectedContext) {
451
+ }
452
+ const client = (0, import_vue15.computed)(
453
+ () => injectedContext ? injectedContext.client : _client.value
454
+ );
455
+ const status = (0, import_vue15.computed)(
456
+ () => injectedContext ? injectedContext.status : _status.value
457
+ );
458
+ const error = (0, import_vue15.computed)(
459
+ () => injectedContext ? injectedContext.error : _error.value
460
+ );
461
+ function setClient(c) {
462
+ _client.value = c;
463
+ _status.value = "loading";
464
+ _error.value = void 0;
465
+ if (!c) return;
466
+ c.onReady().then(() => {
467
+ _status.value = "ready";
468
+ }).catch((err) => {
469
+ _status.value = "error";
470
+ _error.value = err instanceof Error ? err : new Error(String(err));
471
+ });
472
+ }
473
+ function identify(ctx) {
474
+ const c = client.value;
475
+ if (!c || typeof c.identify !== "function") return;
476
+ c.identify(ctx);
477
+ }
478
+ function track(eventKey, payload) {
479
+ const c = client.value;
480
+ if (!c || typeof c.track !== "function") return;
481
+ c.track(eventKey, payload);
482
+ }
483
+ async function alias(prev, next) {
484
+ const c = client.value;
485
+ if (!c || typeof c.alias !== "function") return;
486
+ await c.alias(prev, next);
487
+ }
488
+ function reset() {
489
+ const c = client.value;
490
+ if (!c || typeof c.reset !== "function") return;
491
+ c.reset();
492
+ }
493
+ return {
494
+ client,
495
+ status,
496
+ error,
497
+ setClient,
498
+ identify,
499
+ track,
500
+ alias,
501
+ reset
502
+ };
503
+ });
504
+ // Annotate the CommonJS export names for ESM import in node:
505
+ 0 && (module.exports = {
506
+ AvsbInjectionKey,
507
+ AvsbPlugin,
508
+ AvsbProvider,
509
+ useAlias,
510
+ useAllFlags,
511
+ useAvsbClient,
512
+ useAvsbStore,
513
+ useBoolFlag,
514
+ useFlag,
515
+ useFlagReady,
516
+ useFlagValue,
517
+ useIdentify,
518
+ useJsonFlag,
519
+ useNumberFlag,
520
+ useStringFlag,
521
+ useTrack
522
+ });