@traffical/svelte 0.1.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/README.md +365 -0
- package/dist/TrafficalProvider.svelte +34 -0
- package/dist/TrafficalProvider.svelte.d.ts +12 -0
- package/dist/TrafficalProvider.svelte.d.ts.map +1 -0
- package/dist/context.svelte.d.ts +44 -0
- package/dist/context.svelte.d.ts.map +1 -0
- package/dist/context.svelte.js +192 -0
- package/dist/hooks.svelte.d.ts +155 -0
- package/dist/hooks.svelte.d.ts.map +1 -0
- package/dist/hooks.svelte.js +371 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +83 -0
- package/dist/index.test.d.ts +7 -0
- package/dist/index.test.d.ts.map +1 -0
- package/dist/index.test.js +184 -0
- package/dist/sveltekit.d.ts +73 -0
- package/dist/sveltekit.d.ts.map +1 -0
- package/dist/sveltekit.js +111 -0
- package/dist/sveltekit.test.d.ts +5 -0
- package/dist/sveltekit.test.d.ts.map +1 -0
- package/dist/sveltekit.test.js +170 -0
- package/dist/types.d.ts +206 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/package.json +67 -0
- package/src/TrafficalProvider.svelte +34 -0
- package/src/context.svelte.ts +232 -0
- package/src/hooks.svelte.ts +445 -0
- package/src/index.test.ts +221 -0
- package/src/index.ts +144 -0
- package/src/sveltekit.test.ts +218 -0
- package/src/sveltekit.ts +139 -0
- package/src/types.ts +296 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @traffical/svelte - Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* TypeScript types for the Svelte 5 SDK.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { TrafficalClient, TrafficalPlugin } from "@traffical/js-client";
|
|
8
|
+
import type {
|
|
9
|
+
ConfigBundle,
|
|
10
|
+
Context,
|
|
11
|
+
DecisionResult,
|
|
12
|
+
ParameterValue,
|
|
13
|
+
} from "@traffical/core";
|
|
14
|
+
|
|
15
|
+
// =============================================================================
|
|
16
|
+
// Provider Configuration
|
|
17
|
+
// =============================================================================
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Configuration for the TrafficalProvider component.
|
|
21
|
+
*/
|
|
22
|
+
export interface TrafficalProviderConfig {
|
|
23
|
+
// ==========================================================================
|
|
24
|
+
// Required
|
|
25
|
+
// ==========================================================================
|
|
26
|
+
|
|
27
|
+
/** Organization ID */
|
|
28
|
+
orgId: string;
|
|
29
|
+
/** Project ID */
|
|
30
|
+
projectId: string;
|
|
31
|
+
/** Environment (e.g., "production", "staging") */
|
|
32
|
+
env: string;
|
|
33
|
+
/** API key for authentication */
|
|
34
|
+
apiKey: string;
|
|
35
|
+
|
|
36
|
+
// ==========================================================================
|
|
37
|
+
// Optional - Connection
|
|
38
|
+
// ==========================================================================
|
|
39
|
+
|
|
40
|
+
/** Base URL for the SDK API (edge worker) */
|
|
41
|
+
baseUrl?: string;
|
|
42
|
+
/** Local config bundle for offline fallback */
|
|
43
|
+
localConfig?: ConfigBundle;
|
|
44
|
+
/** Refresh interval in milliseconds (default: 60000) */
|
|
45
|
+
refreshIntervalMs?: number;
|
|
46
|
+
|
|
47
|
+
// ==========================================================================
|
|
48
|
+
// Optional - Identity
|
|
49
|
+
// ==========================================================================
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Function to get the unit key value.
|
|
53
|
+
* If not provided, the SDK will use automatic stable ID generation.
|
|
54
|
+
*/
|
|
55
|
+
unitKeyFn?: () => string;
|
|
56
|
+
/** Function to get additional context (optional) */
|
|
57
|
+
contextFn?: () => Context;
|
|
58
|
+
|
|
59
|
+
// ==========================================================================
|
|
60
|
+
// Optional - Decision Tracking
|
|
61
|
+
// ==========================================================================
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Whether to automatically track decision events (default: true).
|
|
65
|
+
* When enabled, every call to decide() automatically sends a DecisionEvent
|
|
66
|
+
* to the control plane, enabling intent-to-treat analysis.
|
|
67
|
+
*/
|
|
68
|
+
trackDecisions?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Decision deduplication TTL in milliseconds (default: 1 hour).
|
|
71
|
+
* Same user+assignment combination won't be tracked again within this window.
|
|
72
|
+
*/
|
|
73
|
+
decisionDeduplicationTtlMs?: number;
|
|
74
|
+
|
|
75
|
+
// ==========================================================================
|
|
76
|
+
// Optional - Exposure Tracking
|
|
77
|
+
// ==========================================================================
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Exposure deduplication session TTL in milliseconds (default: 30 minutes).
|
|
81
|
+
* Same user seeing same variant won't trigger multiple exposure events.
|
|
82
|
+
*/
|
|
83
|
+
exposureSessionTtlMs?: number;
|
|
84
|
+
|
|
85
|
+
// ==========================================================================
|
|
86
|
+
// Optional - Plugins
|
|
87
|
+
// ==========================================================================
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Plugins to register with the client.
|
|
91
|
+
* The DecisionTrackingPlugin is included by default unless trackDecisions is false.
|
|
92
|
+
*/
|
|
93
|
+
plugins?: TrafficalPlugin[];
|
|
94
|
+
|
|
95
|
+
// ==========================================================================
|
|
96
|
+
// Optional - Event Batching
|
|
97
|
+
// ==========================================================================
|
|
98
|
+
|
|
99
|
+
/** Max events before auto-flush (default: 10) */
|
|
100
|
+
eventBatchSize?: number;
|
|
101
|
+
/** Auto-flush interval in ms (default: 30000) */
|
|
102
|
+
eventFlushIntervalMs?: number;
|
|
103
|
+
|
|
104
|
+
// ==========================================================================
|
|
105
|
+
// Optional - SSR/Hydration
|
|
106
|
+
// ==========================================================================
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Pre-fetched config bundle from server-side load function.
|
|
110
|
+
* When provided, the SDK is immediately ready without fetching.
|
|
111
|
+
*/
|
|
112
|
+
initialBundle?: ConfigBundle | null;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Pre-resolved params from SSR for immediate hydration.
|
|
116
|
+
* Used to prevent FOOC (Flash of Original Content).
|
|
117
|
+
*/
|
|
118
|
+
initialParams?: Record<string, unknown>;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// =============================================================================
|
|
122
|
+
// Context Types
|
|
123
|
+
// =============================================================================
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Internal context value shared via Svelte's setContext/getContext.
|
|
127
|
+
*/
|
|
128
|
+
export interface TrafficalContextValue {
|
|
129
|
+
/** The Traffical client instance (null during SSR) */
|
|
130
|
+
readonly client: TrafficalClient | null;
|
|
131
|
+
/** Whether the client is ready (config loaded) */
|
|
132
|
+
readonly ready: boolean;
|
|
133
|
+
/** Any initialization error */
|
|
134
|
+
readonly error: Error | null;
|
|
135
|
+
/** The current config bundle */
|
|
136
|
+
readonly bundle: ConfigBundle | null;
|
|
137
|
+
/** Function to get the unit key */
|
|
138
|
+
getUnitKey: () => string;
|
|
139
|
+
/** Function to get the full context */
|
|
140
|
+
getContext: () => Context;
|
|
141
|
+
/** Initial params from SSR for hydration */
|
|
142
|
+
initialParams?: Record<string, unknown>;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// =============================================================================
|
|
146
|
+
// Hook Types
|
|
147
|
+
// =============================================================================
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Options for the useTraffical hook.
|
|
151
|
+
*/
|
|
152
|
+
export interface UseTrafficalOptions<
|
|
153
|
+
T extends Record<string, ParameterValue> = Record<string, ParameterValue>,
|
|
154
|
+
> {
|
|
155
|
+
/** Default parameter values (required for type inference) */
|
|
156
|
+
defaults: T;
|
|
157
|
+
|
|
158
|
+
/** Additional context to merge (optional) */
|
|
159
|
+
context?: Context;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Tracking mode (default: "full")
|
|
163
|
+
* - "full": Track decision + exposure (default, recommended for UI)
|
|
164
|
+
* - "decision": Track decision only, manual exposure control
|
|
165
|
+
* - "none": No tracking (SSR, internal logic, tests)
|
|
166
|
+
*/
|
|
167
|
+
tracking?: "full" | "decision" | "none";
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Options for the bound track function returned by useTraffical.
|
|
172
|
+
*/
|
|
173
|
+
export interface BoundTrackOptions {
|
|
174
|
+
/** Additional event properties */
|
|
175
|
+
properties?: Record<string, unknown>;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @deprecated Use BoundTrackOptions instead.
|
|
180
|
+
* Options for the bound trackReward function returned by useTraffical.
|
|
181
|
+
*/
|
|
182
|
+
export interface BoundTrackRewardOptions {
|
|
183
|
+
/** The reward value (e.g., revenue amount, conversion count) */
|
|
184
|
+
reward: number;
|
|
185
|
+
/** Type of reward (e.g., "revenue", "conversion", "engagement") */
|
|
186
|
+
rewardType?: string;
|
|
187
|
+
/** Multiple reward values keyed by type */
|
|
188
|
+
rewards?: Record<string, number>;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Return value from the useTraffical hook.
|
|
193
|
+
* All properties are reactive via Svelte 5 runes.
|
|
194
|
+
*/
|
|
195
|
+
export interface UseTrafficalResult<
|
|
196
|
+
T extends Record<string, ParameterValue> = Record<string, ParameterValue>,
|
|
197
|
+
> {
|
|
198
|
+
/** Resolved parameter values (reactive) */
|
|
199
|
+
readonly params: T;
|
|
200
|
+
/** The full decision result (null when tracking="none") */
|
|
201
|
+
readonly decision: DecisionResult | null;
|
|
202
|
+
/** Whether the client is ready (config loaded) */
|
|
203
|
+
readonly ready: boolean;
|
|
204
|
+
/** Any error that occurred */
|
|
205
|
+
readonly error: Error | null;
|
|
206
|
+
/** Function to manually track exposure (no-op when tracking="none") */
|
|
207
|
+
trackExposure: () => void;
|
|
208
|
+
/**
|
|
209
|
+
* Track a user event. The decisionId is automatically bound.
|
|
210
|
+
* No-op if tracking="none" or no decision is available.
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* track('purchase', { value: 99.99, orderId: 'ord_123' });
|
|
214
|
+
* track('add_to_cart', { itemId: 'sku_456' });
|
|
215
|
+
*/
|
|
216
|
+
track: (event: string, properties?: Record<string, unknown>) => void;
|
|
217
|
+
/**
|
|
218
|
+
* @deprecated Use track() instead.
|
|
219
|
+
* Track a reward for this decision. The decisionId is automatically bound.
|
|
220
|
+
* No-op if tracking="none" or no decision is available.
|
|
221
|
+
*/
|
|
222
|
+
trackReward: (options: BoundTrackRewardOptions) => void;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Options for tracking an event with the standalone hook.
|
|
227
|
+
*/
|
|
228
|
+
export interface TrackEventOptions {
|
|
229
|
+
/** Event name (e.g., 'purchase', 'add_to_cart') */
|
|
230
|
+
event: string;
|
|
231
|
+
/** Additional event properties */
|
|
232
|
+
properties?: Record<string, unknown>;
|
|
233
|
+
/** Reference to the decision (optional, for attribution) */
|
|
234
|
+
decisionId?: string;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* @deprecated Use TrackEventOptions instead.
|
|
239
|
+
* Options for tracking a reward.
|
|
240
|
+
*/
|
|
241
|
+
export interface TrackRewardOptions {
|
|
242
|
+
/** Reference to the decision (optional, will use last decision if not provided) */
|
|
243
|
+
decisionId?: string;
|
|
244
|
+
/** The reward value (e.g., revenue amount, conversion count) */
|
|
245
|
+
reward: number;
|
|
246
|
+
/** Type of reward (e.g., "revenue", "conversion", "engagement") */
|
|
247
|
+
rewardType?: string;
|
|
248
|
+
/** Multiple reward values keyed by type */
|
|
249
|
+
rewards?: Record<string, number>;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// =============================================================================
|
|
253
|
+
// SvelteKit Types
|
|
254
|
+
// =============================================================================
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Options for loading the Traffical config bundle in a SvelteKit load function.
|
|
258
|
+
*/
|
|
259
|
+
export interface LoadTrafficalBundleOptions {
|
|
260
|
+
/** Organization ID */
|
|
261
|
+
orgId: string;
|
|
262
|
+
/** Project ID */
|
|
263
|
+
projectId: string;
|
|
264
|
+
/** Environment (e.g., "production", "staging") */
|
|
265
|
+
env: string;
|
|
266
|
+
/** API key for authentication */
|
|
267
|
+
apiKey: string;
|
|
268
|
+
/** SvelteKit's fetch function (from load context) */
|
|
269
|
+
fetch: typeof globalThis.fetch;
|
|
270
|
+
/** Base URL for the SDK API (optional, defaults to https://sdk.traffical.io) */
|
|
271
|
+
baseUrl?: string;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Result from loading the Traffical config bundle.
|
|
276
|
+
*/
|
|
277
|
+
export interface LoadTrafficalBundleResult {
|
|
278
|
+
/** The fetched config bundle, or null if fetch failed */
|
|
279
|
+
bundle: ConfigBundle | null;
|
|
280
|
+
/** Error message if fetch failed */
|
|
281
|
+
error?: string;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// =============================================================================
|
|
285
|
+
// Re-exports for convenience
|
|
286
|
+
// =============================================================================
|
|
287
|
+
|
|
288
|
+
export type {
|
|
289
|
+
ConfigBundle,
|
|
290
|
+
Context,
|
|
291
|
+
DecisionResult,
|
|
292
|
+
ParameterValue,
|
|
293
|
+
TrafficalClient,
|
|
294
|
+
TrafficalPlugin,
|
|
295
|
+
};
|
|
296
|
+
|