@tiun/types 0.2.0 → 0.4.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.
Files changed (3) hide show
  1. package/README.md +15 -0
  2. package/index.d.ts +90 -7
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -34,6 +34,21 @@ The global `tiun` object is typed:
34
34
 
35
35
  ```typescript
36
36
  tiun.init({ snippetId: 'your-snippet-id' });
37
+
38
+ // Authentication
39
+ tiun.login();
40
+ tiun.logout();
41
+
42
+ const { isAuthenticated, user } = tiun.getUser();
43
+
44
+ // Events
45
+ tiun.on('userChange', (event) => {
46
+ console.log(event.isAuthenticated, event.user);
47
+ });
48
+ tiun.on('login', (event) => console.log(event.user.email));
49
+ tiun.on('logout', () => {});
50
+
51
+ // Paywall
37
52
  tiun.on('paywallShow', ({ isConnected }) => {});
38
53
  tiun.checkout({ productId: 'prod_123' });
39
54
  ```
package/index.d.ts CHANGED
@@ -16,7 +16,14 @@
16
16
 
17
17
  export type ContentType = 'active' | 'inactive' | 'paused';
18
18
  export type MediaType = 'text' | 'audio' | 'video';
19
- export type TiunEventType = 'ready' | 'paywallShow' | 'paywallHide' | 'error';
19
+ export type TiunEventType =
20
+ | 'ready'
21
+ | 'paywallShow'
22
+ | 'paywallHide'
23
+ | 'error'
24
+ | 'userChange'
25
+ | 'login'
26
+ | 'logout';
20
27
 
21
28
  // =============================================================================
22
29
  // Configuration
@@ -71,6 +78,21 @@ export interface TiunConfig {
71
78
  */
72
79
  onPaywallHide?: (data: PaywallHideEvent) => void;
73
80
 
81
+ /**
82
+ * Called on user state changes (init + every subsequent change)
83
+ */
84
+ onUserChange?: (data: UserChangeEvent) => void;
85
+
86
+ /**
87
+ * Called when user logs in
88
+ */
89
+ onLogin?: (data: LoginEvent) => void;
90
+
91
+ /**
92
+ * Called when user logs out
93
+ */
94
+ onLogout?: () => void;
95
+
74
96
  /**
75
97
  * Called on any error
76
98
  */
@@ -106,6 +128,27 @@ export interface TiunError {
106
128
  details?: unknown;
107
129
  }
108
130
 
131
+ export interface UserInfo {
132
+ userId: string;
133
+ email: string;
134
+ productAccess: string[];
135
+ }
136
+
137
+ export interface UserChangeEvent {
138
+ event: 'init' | 'login' | 'checkout' | 'logout' | 'update';
139
+ isAuthenticated: boolean;
140
+ user: UserInfo | null;
141
+ }
142
+
143
+ export interface LoginEvent {
144
+ user: UserInfo;
145
+ }
146
+
147
+ export interface GetUserResponse {
148
+ isAuthenticated: boolean;
149
+ user: UserInfo | null;
150
+ }
151
+
109
152
  // =============================================================================
110
153
  // Checkout Options
111
154
  // =============================================================================
@@ -115,11 +158,6 @@ export interface CheckoutOptions {
115
158
  * Specific product ID to checkout (optional, for future use)
116
159
  */
117
160
  productId?: string;
118
-
119
- /**
120
- * Custom metadata to attach to this checkout
121
- */
122
- metadata?: Record<string, string>;
123
161
  }
124
162
 
125
163
  // =============================================================================
@@ -159,7 +197,12 @@ export interface TiunSDK {
159
197
  init(config?: TiunConfig): TiunSDK;
160
198
 
161
199
  /**
162
- * Open the checkout flow.
200
+ * Open the time-based connect flow (no specific product).
201
+ */
202
+ start(): Promise<void>;
203
+
204
+ /**
205
+ * Open the checkout flow for a specific product.
163
206
  */
164
207
  checkout(options?: CheckoutOptions): Promise<void>;
165
208
 
@@ -168,6 +211,28 @@ export interface TiunSDK {
168
211
  */
169
212
  setContent(options: ContentOptions): Promise<void>;
170
213
 
214
+ /**
215
+ * Open the OTP login modal for returning subscribers.
216
+ */
217
+ login(): Promise<void>;
218
+
219
+ /**
220
+ * Clear session and fire logout + userChange events.
221
+ */
222
+ logout(): void;
223
+
224
+ /**
225
+ * Returns the cached user state. Does not call the backend —
226
+ * the snippet keeps user info in sync automatically.
227
+ */
228
+ getUser(): GetUserResponse;
229
+
230
+ /**
231
+ * Returns a signed JWT for server-to-server user verification.
232
+ * Valid for 5 minutes. Returns null if the user is not authenticated.
233
+ */
234
+ getUserVerificationToken(): Promise<string | null>;
235
+
171
236
  /**
172
237
  * Subscribe to an event. Returns unsubscribe function.
173
238
  */
@@ -181,6 +246,12 @@ export interface TiunSDK {
181
246
  callback: (data: PaywallHideEvent) => void
182
247
  ): () => void;
183
248
  on(event: 'error', callback: (error: TiunError) => void): () => void;
249
+ on(
250
+ event: 'userChange',
251
+ callback: (data: UserChangeEvent) => void
252
+ ): () => void;
253
+ on(event: 'login', callback: (data: LoginEvent) => void): () => void;
254
+ on(event: 'logout', callback: () => void): () => void;
184
255
 
185
256
  /**
186
257
  * Subscribe to an event once (auto-unsubscribes after first fire).
@@ -195,6 +266,12 @@ export interface TiunSDK {
195
266
  callback: (data: PaywallHideEvent) => void
196
267
  ): () => void;
197
268
  once(event: 'error', callback: (error: TiunError) => void): () => void;
269
+ once(
270
+ event: 'userChange',
271
+ callback: (data: UserChangeEvent) => void
272
+ ): () => void;
273
+ once(event: 'login', callback: (data: LoginEvent) => void): () => void;
274
+ once(event: 'logout', callback: () => void): () => void;
198
275
 
199
276
  /**
200
277
  * Destroy the SDK and cleanup all listeners.
@@ -214,6 +291,12 @@ export interface TiunSDK {
214
291
 
215
292
  /** Whether the snippet is ready */
216
293
  readonly isReady: boolean;
294
+
295
+ /** Whether the user is identified (has valid session) */
296
+ readonly isAuthenticated: boolean;
297
+
298
+ /** Current user info or null */
299
+ readonly user: UserInfo | null;
217
300
  }
218
301
 
219
302
  // =============================================================================
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiun/types",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "TypeScript type definitions for tiun SDK (for script tag users)",
5
5
  "types": "./index.d.ts",
6
6
  "files": [