@tiun/types 0.1.2 → 0.3.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 +21 -17
  2. package/index.d.ts +97 -34
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -2,16 +2,7 @@
2
2
 
3
3
  TypeScript type definitions for the tiun SDK.
4
4
 
5
- ## When to Use
6
-
7
- | Installation Method | Types Included? | Need This Package? |
8
- | ------------------------ | --------------- | ------------------ |
9
- | `npm install @tiun/sdk` | ✅ Yes | No |
10
- | `<script>` tags (legacy) | ❌ No | **Yes** |
11
-
12
- **NPM users:** Skip this package - types are bundled with `@tiun/sdk`.
13
-
14
- **Script tag users:** Install this for TypeScript support.
5
+ Types are included in `@tiun/sdk`; you do not need this package when using the SDK. This package is an optional standalone reference (e.g. for custom setups).
15
6
 
16
7
  ## Installation
17
8
 
@@ -31,22 +22,35 @@ Add to `tsconfig.json`:
31
22
  }
32
23
  ```
33
24
 
34
- Or add a reference directive to any `.d.ts` file:
25
+ Or in any `.d.ts` file:
35
26
 
36
27
  ```typescript
37
28
  /// <reference types="@tiun/types" />
38
29
  ```
39
30
 
40
- Restart your TypeScript server after setup.
41
-
42
31
  ## Usage
43
32
 
44
- The global `Tiun` object is now typed:
33
+ The global `tiun` object is typed:
45
34
 
46
35
  ```typescript
47
- Tiun.init();
48
- Tiun.on('paywallShow', ({ isConnected }) => {});
49
- Tiun.checkout({ productId: 'prod_123' });
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
52
+ tiun.on('paywallShow', ({ isConnected }) => {});
53
+ tiun.checkout({ productId: 'prod_123' });
50
54
  ```
51
55
 
52
56
  ## Auto-generated
package/index.d.ts CHANGED
@@ -3,8 +3,8 @@
3
3
  *
4
4
  * Install: npm install @tiun/types --save-dev
5
5
  *
6
- * These types are for the global `Tiun` object exposed by the Tiun script.
7
- * Load the script in your HTML, then use Tiun.* with full TypeScript support.
6
+ * These types are for the global `tiun` object exposed by the tiun script.
7
+ * Load the script, then use tiun.* with full TypeScript support.
8
8
  *
9
9
  * @generated from src/sdk/types.ts - Do not edit directly.
10
10
  * Run `npm run generate:types` to regenerate.
@@ -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
@@ -25,7 +32,7 @@ export type TiunEventType = 'ready' | 'paywallShow' | 'paywallHide' | 'error';
25
32
  export interface TiunConfig {
26
33
  /**
27
34
  * Your unique tiun snippet ID.
28
- * Required for NPM package usage.
35
+ * Required when using the NPM package (SDK loads the snippet from backend).
29
36
  * For legacy script tag usage, this is provided via the backend.
30
37
  */
31
38
  snippetId?: string;
@@ -45,30 +52,16 @@ export interface TiunConfig {
45
52
  tone?: 'formal' | 'informal';
46
53
 
47
54
  /**
48
- * Corner style for UI elements.
49
- * - 'rounded': Rounded corners
50
- * - 'pointed': Sharp corners
51
- * @default 'rounded'
52
- */
53
- corners?: 'rounded' | 'pointed';
54
-
55
- /**
56
- * Enable WebView mode (for mobile app WebViews).
55
+ * Enable debug logging to console.
57
56
  * @default false
58
57
  */
59
- webView?: boolean;
60
-
61
- /**
62
- * API base URL. Only needed for custom deployments.
63
- * @default 'https://api.tiun.live/live_api'
64
- */
65
- baseUrl?: string;
58
+ debug?: boolean;
66
59
 
67
60
  /**
68
- * Enable debug logging to console.
61
+ * Use sandbox base URL instead of production.
69
62
  * @default false
70
63
  */
71
- debug?: boolean;
64
+ sandbox?: boolean;
72
65
 
73
66
  /**
74
67
  * Called when the snippet has finished initializing and is ready
@@ -85,6 +78,21 @@ export interface TiunConfig {
85
78
  */
86
79
  onPaywallHide?: (data: PaywallHideEvent) => void;
87
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
+
88
96
  /**
89
97
  * Called on any error
90
98
  */
@@ -120,6 +128,27 @@ export interface TiunError {
120
128
  details?: unknown;
121
129
  }
122
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
+
123
152
  // =============================================================================
124
153
  // Checkout Options
125
154
  // =============================================================================
@@ -159,21 +188,15 @@ export interface ContentOptions {
159
188
  }
160
189
 
161
190
  // =============================================================================
162
- // SDK Interface (for global Tiun object)
191
+ // SDK Interface (for global tiun object)
163
192
  // =============================================================================
164
193
 
165
194
  export interface TiunSDK {
166
195
  /**
167
196
  * Initialize the SDK. Call once at app startup.
168
197
  *
169
- * For NPM package usage, snippetId is required:
170
- * ```typescript
171
- * Tiun.init({ snippetId: 'your-snippet-id' });
172
- * ```
173
- *
174
- * For legacy script tag usage (backend-served), no config needed:
175
198
  * ```typescript
176
- * Tiun.init();
199
+ * tiun.init({ snippetId: 'your-snippet-id' });
177
200
  * ```
178
201
  */
179
202
  init(config?: TiunConfig): TiunSDK;
@@ -188,6 +211,28 @@ export interface TiunSDK {
188
211
  */
189
212
  setContent(options: ContentOptions): Promise<void>;
190
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
+
191
236
  /**
192
237
  * Subscribe to an event. Returns unsubscribe function.
193
238
  */
@@ -201,6 +246,12 @@ export interface TiunSDK {
201
246
  callback: (data: PaywallHideEvent) => void
202
247
  ): () => void;
203
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;
204
255
 
205
256
  /**
206
257
  * Subscribe to an event once (auto-unsubscribes after first fire).
@@ -215,6 +266,12 @@ export interface TiunSDK {
215
266
  callback: (data: PaywallHideEvent) => void
216
267
  ): () => void;
217
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;
218
275
 
219
276
  /**
220
277
  * Destroy the SDK and cleanup all listeners.
@@ -234,6 +291,12 @@ export interface TiunSDK {
234
291
 
235
292
  /** Whether the snippet is ready */
236
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;
237
300
  }
238
301
 
239
302
  // =============================================================================
@@ -242,13 +305,13 @@ export interface TiunSDK {
242
305
 
243
306
  declare global {
244
307
  /**
245
- * Global Tiun SDK instance.
246
- * Available after loading the Tiun script.
308
+ * Global tiun SDK instance.
309
+ * Available after loading the tiun script.
247
310
  */
248
- const Tiun: TiunSDK;
311
+ const tiun: TiunSDK;
249
312
 
250
313
  interface Window {
251
- Tiun: TiunSDK;
314
+ tiun: TiunSDK;
252
315
  }
253
316
  }
254
317
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiun/types",
3
- "version": "0.1.2",
3
+ "version": "0.3.0",
4
4
  "description": "TypeScript type definitions for tiun SDK (for script tag users)",
5
5
  "types": "./index.d.ts",
6
6
  "files": [