@timeback/sdk 0.1.7 → 0.1.8

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.
Files changed (109) hide show
  1. package/README.md +91 -2
  2. package/dist/chunk-07j8zre9.js +2 -0
  3. package/dist/chunk-3886xy48.js +8 -0
  4. package/dist/chunk-9se82640.js +1 -0
  5. package/dist/chunk-ahy54f2r.js +2 -0
  6. package/dist/chunk-ewsp6v3b.js +16 -0
  7. package/dist/chunk-j1xdrfqj.js +2 -0
  8. package/dist/chunk-qaa129bd.js +2 -0
  9. package/dist/chunk-qr0bbnsr.js +1 -0
  10. package/dist/chunk-rgbpvxbv.js +1 -0
  11. package/dist/chunk-whc53e0y.js +11 -0
  12. package/dist/client/adapters/react/hooks/types.d.ts +46 -0
  13. package/dist/client/adapters/react/hooks/types.d.ts.map +1 -1
  14. package/dist/client/adapters/react/hooks/useTimebackProfile.d.ts +42 -0
  15. package/dist/client/adapters/react/hooks/useTimebackProfile.d.ts.map +1 -0
  16. package/dist/client/adapters/react/index.d.ts +2 -1
  17. package/dist/client/adapters/react/index.d.ts.map +1 -1
  18. package/dist/client/adapters/react/index.js +2 -494
  19. package/dist/client/adapters/solid/index.d.ts +3 -0
  20. package/dist/client/adapters/solid/index.d.ts.map +1 -1
  21. package/dist/client/adapters/solid/index.ts +12 -0
  22. package/dist/client/adapters/solid/primitives/createTimebackProfile.d.ts +58 -0
  23. package/dist/client/adapters/solid/primitives/createTimebackProfile.d.ts.map +1 -0
  24. package/dist/client/adapters/solid/primitives/createTimebackProfile.ts +209 -0
  25. package/dist/client/adapters/solid/primitives/createTimebackVerification.d.ts +36 -0
  26. package/dist/client/adapters/solid/primitives/createTimebackVerification.d.ts.map +1 -0
  27. package/dist/client/adapters/solid/primitives/createTimebackVerification.ts +133 -0
  28. package/dist/client/adapters/solid/types.d.ts +86 -0
  29. package/dist/client/adapters/solid/types.d.ts.map +1 -0
  30. package/dist/client/adapters/solid/types.ts +85 -0
  31. package/dist/client/adapters/svelte/index.d.ts +2 -1
  32. package/dist/client/adapters/svelte/index.d.ts.map +1 -1
  33. package/dist/client/adapters/svelte/index.ts +11 -2
  34. package/dist/client/adapters/svelte/{stores.d.ts → stores/client.d.ts} +11 -9
  35. package/dist/client/adapters/svelte/stores/client.d.ts.map +1 -0
  36. package/dist/client/adapters/svelte/{stores.ts → stores/client.ts} +24 -52
  37. package/dist/client/adapters/svelte/stores/index.d.ts +10 -0
  38. package/dist/client/adapters/svelte/stores/index.d.ts.map +1 -0
  39. package/dist/client/adapters/svelte/stores/index.ts +22 -0
  40. package/dist/client/adapters/svelte/stores/profile.d.ts +66 -0
  41. package/dist/client/adapters/svelte/stores/profile.d.ts.map +1 -0
  42. package/dist/client/adapters/svelte/stores/profile.ts +168 -0
  43. package/dist/client/adapters/svelte/stores/verification.d.ts +43 -0
  44. package/dist/client/adapters/svelte/stores/verification.d.ts.map +1 -0
  45. package/dist/client/adapters/svelte/stores/verification.ts +126 -0
  46. package/dist/client/adapters/svelte/types.d.ts +35 -0
  47. package/dist/client/adapters/svelte/types.d.ts.map +1 -0
  48. package/dist/client/adapters/vue/composables/useTimebackProfile.d.ts +51 -0
  49. package/dist/client/adapters/vue/composables/useTimebackProfile.d.ts.map +1 -0
  50. package/dist/client/adapters/vue/composables/useTimebackProfile.ts +186 -0
  51. package/dist/client/adapters/vue/composables/useTimebackVerification.d.ts +44 -0
  52. package/dist/client/adapters/vue/composables/useTimebackVerification.d.ts.map +1 -0
  53. package/dist/client/adapters/vue/composables/useTimebackVerification.ts +128 -0
  54. package/dist/client/adapters/vue/index.d.ts +3 -0
  55. package/dist/client/adapters/vue/index.d.ts.map +1 -1
  56. package/dist/client/adapters/vue/index.ts +12 -1
  57. package/dist/client/adapters/vue/types.d.ts +86 -0
  58. package/dist/client/adapters/vue/types.d.ts.map +1 -0
  59. package/dist/client/adapters/vue/types.ts +85 -0
  60. package/dist/client/lib/activity/activity.class.d.ts +5 -5
  61. package/dist/client/lib/activity/activity.class.d.ts.map +1 -1
  62. package/dist/client/lib/user-cache.d.ts +39 -0
  63. package/dist/client/lib/user-cache.d.ts.map +1 -0
  64. package/dist/client/lib/user-cache.ts +168 -0
  65. package/dist/client/namespaces/activity.d.ts +2 -3
  66. package/dist/client/namespaces/activity.d.ts.map +1 -1
  67. package/dist/client.d.ts +1 -1
  68. package/dist/client.js +1 -257
  69. package/dist/edge.js +1 -86271
  70. package/dist/identity.js +1 -86131
  71. package/dist/index.js +22 -104883
  72. package/dist/server/adapters/express.js +1 -85973
  73. package/dist/server/adapters/native.js +2 -221
  74. package/dist/server/adapters/nextjs.js +1 -233
  75. package/dist/server/adapters/nuxt.js +1 -86046
  76. package/dist/server/adapters/solid-start.js +1 -85945
  77. package/dist/server/adapters/svelte-kit.js +1 -279
  78. package/dist/server/adapters/tanstack-start.js +1 -85918
  79. package/dist/server/handlers/activity/attempts.d.ts +51 -0
  80. package/dist/server/handlers/activity/attempts.d.ts.map +1 -0
  81. package/dist/server/handlers/activity/caliper.d.ts +46 -5
  82. package/dist/server/handlers/activity/caliper.d.ts.map +1 -1
  83. package/dist/server/handlers/activity/completion.d.ts +43 -0
  84. package/dist/server/handlers/activity/completion.d.ts.map +1 -0
  85. package/dist/server/handlers/activity/handler.d.ts +18 -1
  86. package/dist/server/handlers/activity/handler.d.ts.map +1 -1
  87. package/dist/server/handlers/activity/progress.d.ts +47 -0
  88. package/dist/server/handlers/activity/progress.d.ts.map +1 -0
  89. package/dist/server/handlers/activity/schema.d.ts +1 -2
  90. package/dist/server/handlers/activity/schema.d.ts.map +1 -1
  91. package/dist/server/handlers/activity/types.d.ts +1 -2
  92. package/dist/server/handlers/activity/types.d.ts.map +1 -1
  93. package/dist/server/lib/index.d.ts +1 -1
  94. package/dist/server/lib/index.d.ts.map +1 -1
  95. package/dist/server/lib/utils.d.ts +61 -0
  96. package/dist/server/lib/utils.d.ts.map +1 -1
  97. package/dist/server/timeback.d.ts.map +1 -1
  98. package/dist/server/types.d.ts +7 -1
  99. package/dist/server/types.d.ts.map +1 -1
  100. package/dist/shared/constants.d.ts +6 -0
  101. package/dist/shared/constants.d.ts.map +1 -1
  102. package/dist/shared/types.d.ts +62 -8
  103. package/dist/shared/types.d.ts.map +1 -1
  104. package/dist/shared/xp-calculator.d.ts +25 -0
  105. package/dist/shared/xp-calculator.d.ts.map +1 -0
  106. package/package.json +4 -2
  107. package/dist/client/adapters/svelte/stores.d.ts.map +0 -1
  108. package/dist/server/handlers/activity/gradebook.d.ts +0 -56
  109. package/dist/server/handlers/activity/gradebook.d.ts.map +0 -1
@@ -1,51 +1,14 @@
1
1
  /**
2
- * Svelte Stores
2
+ * Timeback Client Store
3
3
  *
4
- * Svelte stores for Timeback SDK reactivity.
4
+ * Core Svelte store for the Timeback client singleton.
5
5
  */
6
6
 
7
- import { TimebackClient } from '@timeback/sdk/client'
8
-
9
- /**
10
- * Minimal Svelte-compatible store interface.
11
- *
12
- * This allows the SDK to work without a direct Svelte dependency.
13
- */
14
- interface Readable<T> {
15
- subscribe: (run: (value: T) => void) => () => void
16
- }
17
-
18
- interface Writable<T> extends Readable<T> {
19
- set: (value: T) => void
20
- update: (updater: (value: T) => T) => void
21
- }
7
+ import { writable } from 'svelte/store'
22
8
 
23
- /**
24
- * Create a minimal writable store implementation.
25
- *
26
- * @param initialValue - Initial store value
27
- * @returns Writable store
28
- */
29
- function writable<T>(initialValue: T): Writable<T> {
30
- let value = initialValue
31
- const subscribers = new Set<(value: T) => void>()
9
+ import { TimebackClient } from '@timeback/sdk/client'
32
10
 
33
- return {
34
- subscribe(run) {
35
- subscribers.add(run)
36
- run(value)
37
- return () => subscribers.delete(run)
38
- },
39
- set(newValue) {
40
- value = newValue
41
- subscribers.forEach(fn => fn(value))
42
- },
43
- update(updater) {
44
- value = updater(value)
45
- subscribers.forEach(fn => fn(value))
46
- },
47
- }
48
- }
11
+ import type { Readable } from 'svelte/store'
49
12
 
50
13
  let clientInstance: TimebackClient | undefined
51
14
 
@@ -61,10 +24,28 @@ function getOrCreateClient(): TimebackClient {
61
24
  return clientInstance
62
25
  }
63
26
 
27
+ /**
28
+ * Check if we're running in the browser.
29
+ *
30
+ * @returns True if in browser
31
+ */
32
+ function isBrowser(): boolean {
33
+ return typeof window !== 'undefined'
34
+ }
35
+
64
36
  /**
65
37
  * Internal client store.
66
38
  */
67
- const clientStore = writable<TimebackClient | undefined>(undefined)
39
+ export const clientStore = writable<TimebackClient | undefined>(undefined)
40
+
41
+ /**
42
+ * Get the current client instance (for internal use by other stores).
43
+ *
44
+ * @returns Current client instance or undefined
45
+ */
46
+ export function getClientInstance(): TimebackClient | undefined {
47
+ return clientInstance
48
+ }
68
49
 
69
50
  /**
70
51
  * Initialize the Timeback client.
@@ -95,15 +76,6 @@ export function initTimeback(client?: TimebackClient): void {
95
76
  clientStore.set(getOrCreateClient())
96
77
  }
97
78
 
98
- /**
99
- * Check if we're running in the browser.
100
- *
101
- * @returns True if in browser
102
- */
103
- function isBrowser(): boolean {
104
- return typeof window !== 'undefined'
105
- }
106
-
107
79
  /**
108
80
  * Auto-initialize on first access in browser.
109
81
  */
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Svelte Stores
3
+ *
4
+ * Svelte stores for Timeback SDK reactivity.
5
+ */
6
+ export { initTimeback, timeback } from './client';
7
+ export { refreshTimebackVerification, timebackVerification } from './verification';
8
+ export { fetchTimebackProfile, refreshTimebackProfile, timebackProfile, timebackProfileCanFetch, } from './profile';
9
+ export type { TimebackProfileState, TimebackVerificationState } from '../types';
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/client/adapters/svelte/stores/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAGjD,OAAO,EAAE,2BAA2B,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAGlF,OAAO,EACN,oBAAoB,EACpB,sBAAsB,EACtB,eAAe,EACf,uBAAuB,GACvB,MAAM,WAAW,CAAA;AAGlB,YAAY,EAAE,oBAAoB,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAA"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Svelte Stores
3
+ *
4
+ * Svelte stores for Timeback SDK reactivity.
5
+ */
6
+
7
+ // Client
8
+ export { initTimeback, timeback } from './client'
9
+
10
+ // Verification
11
+ export { refreshTimebackVerification, timebackVerification } from './verification'
12
+
13
+ // Profile
14
+ export {
15
+ fetchTimebackProfile,
16
+ refreshTimebackProfile,
17
+ timebackProfile,
18
+ timebackProfileCanFetch,
19
+ } from './profile'
20
+
21
+ // Types re-exported for convenience
22
+ export type { TimebackProfileState, TimebackVerificationState } from '../types'
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Timeback Profile Store
3
+ *
4
+ * Svelte store for user profile state.
5
+ */
6
+ import type { TimebackProfileState } from '../types';
7
+ import type { Readable } from 'svelte/store';
8
+ /**
9
+ * Store containing the Timeback profile state.
10
+ *
11
+ * Use this to access the current user's Timeback profile.
12
+ * Call `fetchTimebackProfile()` to manually fetch, or `refreshTimebackProfile()` to force re-fetch.
13
+ *
14
+ * @example
15
+ * ```svelte
16
+ * <script>
17
+ * import { timebackProfile, timebackProfileCanFetch, fetchTimebackProfile } from '@timeback/sdk/svelte'
18
+ * </script>
19
+ *
20
+ * <button on:click={fetchTimebackProfile} disabled={!$timebackProfileCanFetch}>
21
+ * {$timebackProfile.status === 'loading' ? 'Loading...' : 'Fetch Profile'}
22
+ * </button>
23
+ *
24
+ * {#if $timebackProfile.status === 'loaded'}
25
+ * <pre>{JSON.stringify($timebackProfile.profile, null, 2)}</pre>
26
+ * {/if}
27
+ * ```
28
+ */
29
+ export declare const timebackProfile: Readable<TimebackProfileState>;
30
+ /**
31
+ * Store indicating whether the profile can be fetched.
32
+ *
33
+ * True when the user is verified and the client is ready.
34
+ */
35
+ export declare const timebackProfileCanFetch: Readable<boolean>;
36
+ /**
37
+ * Manually fetch the Timeback profile.
38
+ *
39
+ * No-op if the user is not verified or client is not ready.
40
+ *
41
+ * @example
42
+ * ```svelte
43
+ * <script>
44
+ * import { fetchTimebackProfile } from '@timeback/sdk/svelte'
45
+ * </script>
46
+ *
47
+ * <button on:click={fetchTimebackProfile}>Fetch Profile</button>
48
+ * ```
49
+ */
50
+ export declare function fetchTimebackProfile(): void;
51
+ /**
52
+ * Force a re-fetch of the Timeback profile.
53
+ *
54
+ * No-op if the user is not verified or client is not ready.
55
+ *
56
+ * @example
57
+ * ```svelte
58
+ * <script>
59
+ * import { refreshTimebackProfile } from '@timeback/sdk/svelte'
60
+ * </script>
61
+ *
62
+ * <button on:click={refreshTimebackProfile}>Refresh Profile</button>
63
+ * ```
64
+ */
65
+ export declare function refreshTimebackProfile(): void;
66
+ //# sourceMappingURL=profile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"profile.d.ts","sourceRoot":"","sources":["../../../../../src/client/adapters/svelte/stores/profile.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAOH,OAAO,KAAK,EAAE,oBAAoB,EAA6B,MAAM,UAAU,CAAA;AAC/E,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AA6E5C;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,eAAe,EAAE,QAAQ,CAAC,oBAAoB,CAAgB,CAAA;AAE3E;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,OAAO,CAAiB,CAAA;AAEvE;;;;;;;;;;;;;GAaG;AACH,wBAAgB,oBAAoB,IAAI,IAAI,CAS3C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,sBAAsB,IAAI,IAAI,CAS7C"}
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Timeback Profile Store
3
+ *
4
+ * Svelte store for user profile state.
5
+ */
6
+
7
+ import { fetchProfileOnce, getProfileCache } from '../../../lib/user-cache'
8
+ import { getClientInstance } from './client'
9
+ import { writable } from 'svelte/store'
10
+ import { verificationStore } from './verification'
11
+
12
+ import type { TimebackProfileState, TimebackVerificationState } from '../types'
13
+ import type { Readable } from 'svelte/store'
14
+
15
+ const profileStore = writable<TimebackProfileState>({ status: 'idle' })
16
+ const canFetchStore = writable<boolean>(false)
17
+ let profileInitialized = false
18
+
19
+ /**
20
+ * Check if we're running in the browser.
21
+ *
22
+ * @returns True if in browser
23
+ */
24
+ function isBrowser(): boolean {
25
+ return typeof window !== 'undefined'
26
+ }
27
+
28
+ /**
29
+ * Run the profile fetch.
30
+ *
31
+ * @param force - If true, bypass cache and force a fresh fetch
32
+ */
33
+ async function runProfileFetch(force: boolean): Promise<void> {
34
+ const client = getClientInstance()
35
+ if (!client) return
36
+
37
+ if (!force) {
38
+ const cached = getProfileCache(client)
39
+ if (cached) {
40
+ profileStore.set({ status: 'loaded', profile: cached })
41
+ return
42
+ }
43
+ }
44
+
45
+ profileStore.set({ status: 'loading' })
46
+
47
+ try {
48
+ const profile = await fetchProfileOnce(client, force)
49
+ profileStore.set({ status: 'loaded', profile })
50
+ } catch (err) {
51
+ profileStore.set({
52
+ status: 'error',
53
+ message: err instanceof Error ? err.message : 'Failed to fetch profile',
54
+ })
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Update canFetch based on verification state.
60
+ *
61
+ * @param verification - Current verification state
62
+ */
63
+ function updateCanFetch(verification: TimebackVerificationState): void {
64
+ const can = verification.status === 'verified' && !!getClientInstance()
65
+ canFetchStore.set(can)
66
+ }
67
+
68
+ /**
69
+ * Initialize profile store subscriptions.
70
+ */
71
+ function initProfile(): void {
72
+ if (profileInitialized) return
73
+ profileInitialized = true
74
+
75
+ verificationStore.subscribe(verification => {
76
+ updateCanFetch(verification)
77
+
78
+ // Reset to idle if not verified
79
+ if (verification.status !== 'verified') {
80
+ profileStore.set({ status: 'idle' })
81
+ }
82
+ })
83
+ }
84
+
85
+ // Auto-init in browser
86
+ if (isBrowser()) {
87
+ initProfile()
88
+ }
89
+
90
+ /**
91
+ * Store containing the Timeback profile state.
92
+ *
93
+ * Use this to access the current user's Timeback profile.
94
+ * Call `fetchTimebackProfile()` to manually fetch, or `refreshTimebackProfile()` to force re-fetch.
95
+ *
96
+ * @example
97
+ * ```svelte
98
+ * <script>
99
+ * import { timebackProfile, timebackProfileCanFetch, fetchTimebackProfile } from '@timeback/sdk/svelte'
100
+ * </script>
101
+ *
102
+ * <button on:click={fetchTimebackProfile} disabled={!$timebackProfileCanFetch}>
103
+ * {$timebackProfile.status === 'loading' ? 'Loading...' : 'Fetch Profile'}
104
+ * </button>
105
+ *
106
+ * {#if $timebackProfile.status === 'loaded'}
107
+ * <pre>{JSON.stringify($timebackProfile.profile, null, 2)}</pre>
108
+ * {/if}
109
+ * ```
110
+ */
111
+ export const timebackProfile: Readable<TimebackProfileState> = profileStore
112
+
113
+ /**
114
+ * Store indicating whether the profile can be fetched.
115
+ *
116
+ * True when the user is verified and the client is ready.
117
+ */
118
+ export const timebackProfileCanFetch: Readable<boolean> = canFetchStore
119
+
120
+ /**
121
+ * Manually fetch the Timeback profile.
122
+ *
123
+ * No-op if the user is not verified or client is not ready.
124
+ *
125
+ * @example
126
+ * ```svelte
127
+ * <script>
128
+ * import { fetchTimebackProfile } from '@timeback/sdk/svelte'
129
+ * </script>
130
+ *
131
+ * <button on:click={fetchTimebackProfile}>Fetch Profile</button>
132
+ * ```
133
+ */
134
+ export function fetchTimebackProfile(): void {
135
+ if (!isBrowser()) return
136
+
137
+ let canFetch = false
138
+ canFetchStore.subscribe(v => (canFetch = v))()
139
+
140
+ if (!canFetch) return
141
+
142
+ void runProfileFetch(false)
143
+ }
144
+
145
+ /**
146
+ * Force a re-fetch of the Timeback profile.
147
+ *
148
+ * No-op if the user is not verified or client is not ready.
149
+ *
150
+ * @example
151
+ * ```svelte
152
+ * <script>
153
+ * import { refreshTimebackProfile } from '@timeback/sdk/svelte'
154
+ * </script>
155
+ *
156
+ * <button on:click={refreshTimebackProfile}>Refresh Profile</button>
157
+ * ```
158
+ */
159
+ export function refreshTimebackProfile(): void {
160
+ if (!isBrowser()) return
161
+
162
+ let canFetch = false
163
+ canFetchStore.subscribe(v => (canFetch = v))()
164
+
165
+ if (!canFetch) return
166
+
167
+ void runProfileFetch(true)
168
+ }
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Timeback Verification Store
3
+ *
4
+ * Svelte store for user verification state.
5
+ */
6
+ import type { TimebackVerificationState } from '../types';
7
+ import type { Readable } from 'svelte/store';
8
+ export declare const verificationStore: import("svelte/store").Writable<TimebackVerificationState>;
9
+ /**
10
+ * Store containing the Timeback verification state.
11
+ *
12
+ * Use this to check if the current user is verified in Timeback.
13
+ *
14
+ * @example
15
+ * ```svelte
16
+ * <script>
17
+ * import { timebackVerification, refreshTimebackVerification } from '@timeback/sdk/svelte'
18
+ * </script>
19
+ *
20
+ * {#if $timebackVerification.status === 'verified'}
21
+ * <p>Verified! Timeback ID: {$timebackVerification.timebackId}</p>
22
+ * {:else if $timebackVerification.status === 'loading'}
23
+ * <p>Verifying...</p>
24
+ * {:else}
25
+ * <p>Not verified</p>
26
+ * {/if}
27
+ * ```
28
+ */
29
+ export declare const timebackVerification: Readable<TimebackVerificationState>;
30
+ /**
31
+ * Force a re-verification of the current user.
32
+ *
33
+ * @example
34
+ * ```svelte
35
+ * <script>
36
+ * import { refreshTimebackVerification } from '@timeback/sdk/svelte'
37
+ * </script>
38
+ *
39
+ * <button on:click={refreshTimebackVerification}>Refresh Verification</button>
40
+ * ```
41
+ */
42
+ export declare function refreshTimebackVerification(): void;
43
+ //# sourceMappingURL=verification.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verification.d.ts","sourceRoot":"","sources":["../../../../../src/client/adapters/svelte/stores/verification.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAOH,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAA;AACzD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAc5C,eAAO,MAAM,iBAAiB,4DAA6D,CAAA;AA8D3F;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,oBAAoB,EAAE,QAAQ,CAAC,yBAAyB,CAAqB,CAAA;AAE1F;;;;;;;;;;;GAWG;AACH,wBAAgB,2BAA2B,IAAI,IAAI,CAGlD"}
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Timeback Verification Store
3
+ *
4
+ * Svelte store for user verification state.
5
+ */
6
+
7
+ import { getVerifyCache, verifyOnce } from '../../../lib/user-cache'
8
+ import { clientStore, getClientInstance } from './client'
9
+ import { writable } from 'svelte/store'
10
+
11
+ import type { TimebackVerifyResult } from '../../../../shared/types'
12
+ import type { TimebackVerificationState } from '../types'
13
+ import type { Readable } from 'svelte/store'
14
+
15
+ /**
16
+ * Convert a verify result into a store-friendly state machine.
17
+ *
18
+ * @param result - Raw verify result from the SDK client
19
+ * @returns Mapped verification state
20
+ */
21
+ function toVerificationState(result: TimebackVerifyResult): TimebackVerificationState {
22
+ return result.verified
23
+ ? { status: 'verified', timebackId: result.timebackId }
24
+ : { status: 'unverified' }
25
+ }
26
+
27
+ export const verificationStore = writable<TimebackVerificationState>({ status: 'loading' })
28
+ let verificationInitialized = false
29
+
30
+ /**
31
+ * Check if we're running in the browser.
32
+ *
33
+ * @returns True if in browser
34
+ */
35
+ function isBrowser(): boolean {
36
+ return typeof window !== 'undefined'
37
+ }
38
+
39
+ /**
40
+ * Run the verification check.
41
+ *
42
+ * @param force - If true, bypass cache and force a fresh verification
43
+ */
44
+ async function runVerification(force: boolean): Promise<void> {
45
+ const client = getClientInstance()
46
+ if (!client) return
47
+
48
+ if (!force) {
49
+ const cached = getVerifyCache(client)
50
+ if (cached) {
51
+ verificationStore.set(toVerificationState(cached))
52
+ return
53
+ }
54
+ }
55
+
56
+ verificationStore.set({ status: 'loading' })
57
+
58
+ try {
59
+ const result = await verifyOnce(client, force)
60
+ verificationStore.set(toVerificationState(result))
61
+ } catch (err) {
62
+ verificationStore.set({
63
+ status: 'error',
64
+ message: err instanceof Error ? err.message : 'Failed to verify Timeback user',
65
+ })
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Initialize verification when client becomes available.
71
+ */
72
+ function initVerification(): void {
73
+ if (verificationInitialized) return
74
+ verificationInitialized = true
75
+
76
+ // Subscribe to client changes to auto-verify
77
+ clientStore.subscribe(client => {
78
+ if (client && isBrowser()) {
79
+ void runVerification(false)
80
+ }
81
+ })
82
+ }
83
+
84
+ // Auto-init in browser
85
+ if (isBrowser()) {
86
+ initVerification()
87
+ }
88
+
89
+ /**
90
+ * Store containing the Timeback verification state.
91
+ *
92
+ * Use this to check if the current user is verified in Timeback.
93
+ *
94
+ * @example
95
+ * ```svelte
96
+ * <script>
97
+ * import { timebackVerification, refreshTimebackVerification } from '@timeback/sdk/svelte'
98
+ * </script>
99
+ *
100
+ * {#if $timebackVerification.status === 'verified'}
101
+ * <p>Verified! Timeback ID: {$timebackVerification.timebackId}</p>
102
+ * {:else if $timebackVerification.status === 'loading'}
103
+ * <p>Verifying...</p>
104
+ * {:else}
105
+ * <p>Not verified</p>
106
+ * {/if}
107
+ * ```
108
+ */
109
+ export const timebackVerification: Readable<TimebackVerificationState> = verificationStore
110
+
111
+ /**
112
+ * Force a re-verification of the current user.
113
+ *
114
+ * @example
115
+ * ```svelte
116
+ * <script>
117
+ * import { refreshTimebackVerification } from '@timeback/sdk/svelte'
118
+ * </script>
119
+ *
120
+ * <button on:click={refreshTimebackVerification}>Refresh Verification</button>
121
+ * ```
122
+ */
123
+ export function refreshTimebackVerification(): void {
124
+ if (!isBrowser()) return
125
+ void runVerification(true)
126
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Svelte Types
3
+ *
4
+ * Types for Svelte-specific stores.
5
+ */
6
+ import type { TimebackProfile } from '../../../shared/types';
7
+ /**
8
+ * Verification state for the current user.
9
+ */
10
+ export type TimebackVerificationState = {
11
+ status: 'loading';
12
+ } | {
13
+ status: 'verified';
14
+ timebackId: string;
15
+ } | {
16
+ status: 'unverified';
17
+ } | {
18
+ status: 'error';
19
+ message: string;
20
+ };
21
+ /**
22
+ * Profile state for the current user.
23
+ */
24
+ export type TimebackProfileState = {
25
+ status: 'idle';
26
+ } | {
27
+ status: 'loading';
28
+ } | {
29
+ status: 'loaded';
30
+ profile: TimebackProfile;
31
+ } | {
32
+ status: 'error';
33
+ message: string;
34
+ };
35
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/client/adapters/svelte/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAE5D;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAClC;IAAE,MAAM,EAAE,SAAS,CAAA;CAAE,GACrB;IAAE,MAAM,EAAE,UAAU,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,GAC1C;IAAE,MAAM,EAAE,YAAY,CAAA;CAAE,GACxB;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAA;AAEvC;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAC7B;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,GAClB;IAAE,MAAM,EAAE,SAAS,CAAA;CAAE,GACrB;IAAE,MAAM,EAAE,QAAQ,CAAC;IAAC,OAAO,EAAE,eAAe,CAAA;CAAE,GAC9C;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAA"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * useTimebackProfile
3
+ *
4
+ * Vue composable for fetching the current user's Timeback profile.
5
+ *
6
+ * This composable uses `useTimebackVerification` internally and only fetches
7
+ * the profile when the user is verified (i.e., exists in Timeback).
8
+ */
9
+ import type { ComputedRef, Ref } from 'vue';
10
+ import type { TimebackProfileState, UseTimebackProfileOptions } from '../types';
11
+ /**
12
+ * Fetch the current user's Timeback profile.
13
+ *
14
+ * This composable internally uses `useTimebackVerification` to ensure the user
15
+ * is verified before fetching. When `auto: true`, it automatically fetches
16
+ * the profile once verified. Otherwise, call `fetchProfile()` manually.
17
+ *
18
+ * @param options - Composable options
19
+ * @param options.enabled - If false, the composable does nothing and stays in idle state. Defaults to true.
20
+ * @param options.auto - If true, automatically fetch once verified. Defaults to false.
21
+ * @returns Profile state and fetch methods
22
+ *
23
+ * @example
24
+ * ```vue
25
+ * <script setup>
26
+ * import { useTimebackProfile } from '@timeback/sdk/vue'
27
+ *
28
+ * // Manual fetch
29
+ * const { state, canFetch, fetchProfile } = useTimebackProfile()
30
+ *
31
+ * // Or auto-fetch
32
+ * // const { state } = useTimebackProfile({ auto: true })
33
+ * </script>
34
+ *
35
+ * <template>
36
+ * <div v-if="state.status === 'loaded'">
37
+ * XP: {{ state.profile.xp }}
38
+ * </div>
39
+ * <button v-else @click="fetchProfile" :disabled="!canFetch">
40
+ * {{ state.status === 'loading' ? 'Loading...' : 'Load Profile' }}
41
+ * </button>
42
+ * </template>
43
+ * ```
44
+ */
45
+ export declare function useTimebackProfile(options?: UseTimebackProfileOptions): {
46
+ state: Ref<TimebackProfileState>;
47
+ canFetch: ComputedRef<boolean>;
48
+ fetchProfile: () => void;
49
+ refresh: () => void;
50
+ };
51
+ //# sourceMappingURL=useTimebackProfile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTimebackProfile.d.ts","sourceRoot":"","sources":["../../../../../src/client/adapters/vue/composables/useTimebackProfile.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAQH,OAAO,KAAK,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,KAAK,CAAA;AAC3C,OAAO,KAAK,EAAE,oBAAoB,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAA;AAE/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,yBAA8B,GAAG;IAC5E,KAAK,EAAE,GAAG,CAAC,oBAAoB,CAAC,CAAA;IAChC,QAAQ,EAAE,WAAW,CAAC,OAAO,CAAC,CAAA;IAC9B,YAAY,EAAE,MAAM,IAAI,CAAA;IACxB,OAAO,EAAE,MAAM,IAAI,CAAA;CACnB,CAgIA"}