@nurdd/web-sdk 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.
@@ -0,0 +1,67 @@
1
+ // Vue 3 adapter: a plugin + a composable. Vue is a peer dependency.
2
+ import { inject, onBeforeUnmount, onMounted, watch, ref } from "vue";
3
+ import { createSdk, getSdk, hasDefaultSdk, initSdk, setDefaultSdk } from "../src/sdk.js";
4
+
5
+ const INJECTION_KEY = Symbol("nurdd-sdk");
6
+
7
+ // app.use(NurddPlugin, { sdkKey, baseUrl })
8
+ export const NurddPlugin = {
9
+ install(app, options) {
10
+ let sdk;
11
+ if (options?.sdk) sdk = options.sdk;
12
+ else if (hasDefaultSdk()) sdk = getSdk();
13
+ else if (options?.config || (options && options.sdkKey)) sdk = initSdk(options.config || options);
14
+ else throw new Error("[nurdd] NurddPlugin requires either { sdk } or a config with { sdkKey, baseUrl }");
15
+
16
+ sdk.init().catch(() => {});
17
+ app.provide(INJECTION_KEY, sdk);
18
+ app.config.globalProperties.$nurdd = sdk;
19
+ },
20
+ };
21
+
22
+ export function useNurdd() {
23
+ const sdk = inject(INJECTION_KEY, null);
24
+ if (sdk) return sdk;
25
+ if (hasDefaultSdk()) return getSdk();
26
+ throw new Error("[nurdd] useNurdd() called outside of an app where NurddPlugin was installed");
27
+ }
28
+
29
+ // Vue Router integration: pass `router.currentRoute` (a ref) to
30
+ // automatically fire a `page_view` whenever the route changes.
31
+ //
32
+ // import { useRoute } from 'vue-router'
33
+ // trackPageViews(useRoute())
34
+ export function trackPageViews(routeRef) {
35
+ const sdk = useNurdd();
36
+ watch(routeRef, (to) => {
37
+ if (!to) return;
38
+ sdk.page(to.fullPath || to.path, {
39
+ name: to.name?.toString?.() || null,
40
+ params: to.params,
41
+ query: to.query,
42
+ });
43
+ }, { immediate: true });
44
+ }
45
+
46
+ export function useDeepLink(callback) {
47
+ const sdk = useNurdd();
48
+ let off = null;
49
+ onMounted(() => { off = sdk.onDeepLink((payload) => callback?.(payload)); });
50
+ onBeforeUnmount(() => { if (off) off(); off = null; });
51
+ }
52
+
53
+ export function useIdentity() {
54
+ const sdk = useNurdd();
55
+ const identity = ref(sdk.identity.current());
56
+ return {
57
+ identity,
58
+ async identify(externalUserId, traits) {
59
+ const r = await sdk.identify(externalUserId, traits);
60
+ identity.value = sdk.identity.current();
61
+ return r;
62
+ },
63
+ reset() { sdk.reset(); identity.value = null; },
64
+ };
65
+ }
66
+
67
+ export { createSdk, initSdk, getSdk, setDefaultSdk };