@soham20/smart-offline-sdk 0.2.1 → 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.
package/src/index.d.ts CHANGED
@@ -1,77 +1,345 @@
1
- export interface UsageData {
2
- url: string;
3
- count: number;
4
- lastAccessed: number;
5
- }
1
+ /**
2
+ * SmartOffline SDK - TypeScript Definitions
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+
7
+ // ============================================================================
8
+ // CONFIGURATION TYPES
9
+ // ============================================================================
10
+
11
+ export interface SmartOfflineConfig {
12
+ /**
13
+ * URL patterns for pages to cache (supports * wildcard)
14
+ * @example ['/admin/*', '/dashboard', '/products/*']
15
+ */
16
+ pages: string[];
17
+
18
+ /**
19
+ * URL patterns for API endpoints to cache
20
+ * @example ['/api/v1/*', '/graphql']
21
+ */
22
+ apis: string[];
23
+
24
+ /**
25
+ * Enable debug logging in console
26
+ * @default false in production, true in development
27
+ */
28
+ debug: boolean;
29
+
30
+ /**
31
+ * Number of accesses before URL is considered high priority
32
+ * @default 3
33
+ */
34
+ frequencyThreshold: number;
35
+
36
+ /**
37
+ * Time in ms within which access is considered "recent"
38
+ * @default 86400000 (24 hours)
39
+ */
40
+ recencyThreshold: number;
41
+
42
+ /**
43
+ * Maximum resource size in bytes to cache (skip larger files)
44
+ * @default 10485760 (10MB)
45
+ */
46
+ maxResourceSize: number;
47
+
48
+ /**
49
+ * Network quality detection mode
50
+ * - 'auto': Detect from navigator.connection
51
+ * - 'slow': Treat as slow network (only cache high priority)
52
+ * - 'fast': Treat as fast network (cache everything)
53
+ * @default 'auto'
54
+ */
55
+ networkQuality: "auto" | "slow" | "fast";
56
+
57
+ /**
58
+ * Override priority for specific URL patterns
59
+ * @example { '/critical/*': 'high', '/logs/*': 'low' }
60
+ */
61
+ significance: Record<string, "high" | "low">;
62
+
63
+ /**
64
+ * Weights for priority calculation
65
+ * Higher weight = more influence on final score
66
+ */
67
+ weights: {
68
+ frequency: number;
69
+ recency: number;
70
+ size: number;
71
+ };
6
72
 
7
- export interface CacheWeights {
8
- /** Weight for frequency score (default: 1) */
9
- frequency?: number;
10
- /** Weight for recency score (default: 1) */
11
- recency?: number;
12
- /** Weight for size consideration (default: 1) */
13
- size?: number;
73
+ /**
74
+ * Custom function to calculate priority (0-100)
75
+ * If provided, overrides the default algorithm
76
+ */
77
+ customPriorityFn:
78
+ | ((
79
+ usage: UsageData | null,
80
+ url: string,
81
+ config: SmartOfflineConfig,
82
+ ) => number)
83
+ | null;
84
+
85
+ /**
86
+ * Enable detailed event logging (for debugging/visualization)
87
+ * @default false
88
+ */
89
+ enableDetailedLogs: boolean;
90
+
91
+ /**
92
+ * Callback when cache events occur
93
+ */
94
+ onCacheEvent?: (event: CacheEvent) => void;
95
+
96
+ /**
97
+ * Path to the service worker file
98
+ * @default '/smart-offline-sw.js'
99
+ */
100
+ serviceWorkerPath: string;
101
+
102
+ /**
103
+ * Service worker scope
104
+ * @default '/'
105
+ */
106
+ serviceWorkerScope: string;
14
107
  }
15
108
 
109
+ // ============================================================================
110
+ // EVENT TYPES
111
+ // ============================================================================
112
+
16
113
  export interface CacheEvent {
17
- type: 'CACHE_CACHE' | 'CACHE_SKIP' | 'CACHE_SERVE' | 'CACHE_CLEAR' | 'CACHE_ERROR';
114
+ type: "CACHE_CACHE" | "CACHE_SKIP" | "CACHE_SERVE" | "CACHE_ERROR";
18
115
  url: string;
19
116
  reason: string;
20
- metadata?: any;
117
+ metadata: Record<string, unknown>;
21
118
  timestamp: number;
22
119
  }
23
120
 
24
- export type CustomPriorityFunction = (
25
- usage: UsageData | null,
26
- url: string,
27
- config: SmartOfflineConfig
28
- ) => number; // Return 0-100, >50 is high priority
29
-
30
- export interface SmartOfflineConfig {
31
- /** Array of page URL patterns to cache */
32
- pages?: string[];
33
- /** Array of API URL patterns to cache */
34
- apis?: string[];
35
- /** Enable debug logging */
36
- debug?: boolean;
37
- /** Number of accesses before resource is considered "frequent" (default: 3) */
38
- frequencyThreshold?: number;
39
- /** Milliseconds within which resource is considered "recent" (default: 24h) */
40
- recencyThreshold?: number;
41
- /** Max bytes to cache per resource; larger resources skipped (default: Infinity) */
42
- maxResourceSize?: number;
43
- /** Network quality setting: 'auto' | 'fast' | 'slow' (default: 'auto') */
44
- networkQuality?: "auto" | "fast" | "slow";
45
- /** Manual priority overrides by URL pattern */
46
- significance?: Record<string, "high" | "normal" | "low">;
47
- /** Weights for priority calculation (default: all 1) */
48
- weights?: CacheWeights;
49
- /** Custom priority function for complete control */
50
- customPriorityFn?: CustomPriorityFunction;
51
- /** Enable detailed event logging to IndexedDB */
52
- enableDetailedLogs?: boolean;
53
- /** Callback for all cache events */
54
- onCacheEvent?: (event: CacheEvent) => void;
121
+ export interface UsageData {
122
+ url: string;
123
+ count: number;
124
+ lastAccessed: number;
55
125
  }
56
126
 
57
127
  export interface CacheLog {
58
128
  type: string;
59
129
  url: string;
60
130
  reason: string;
61
- metadata?: any;
131
+ metadata?: Record<string, unknown>;
62
132
  timestamp: number;
63
133
  date: string;
64
134
  }
65
135
 
136
+ // ============================================================================
137
+ // RESULT TYPES
138
+ // ============================================================================
139
+
140
+ export interface SetupResult {
141
+ success: boolean;
142
+ registration?: ServiceWorkerRegistration;
143
+ error?: string;
144
+ }
145
+
146
+ export interface CacheStats {
147
+ cachedItems: number;
148
+ trackedUrls: number;
149
+ cacheSize: number;
150
+ }
151
+
152
+ // ============================================================================
153
+ // TEST UTILITIES TYPES
154
+ // ============================================================================
155
+
156
+ export interface TestResult {
157
+ name: string;
158
+ passed: boolean;
159
+ message: string;
160
+ duration?: number;
161
+ }
162
+
163
+ export interface CacheInspectorData {
164
+ cachedItems: Array<{ url: string; size: number; contentType: string }>;
165
+ usageData: UsageData[];
166
+ logs: Array<{ type: string; url: string; timestamp: number }>;
167
+ }
168
+
169
+ // ============================================================================
170
+ // MAIN API
171
+ // ============================================================================
172
+
173
+ /**
174
+ * Initialize the SmartOffline SDK with complete setup
175
+ */
176
+ export declare function setupSmartOffline(
177
+ config?: Partial<SmartOfflineConfig>,
178
+ ): Promise<SetupResult>;
179
+
180
+ /**
181
+ * Update configuration at runtime
182
+ */
183
+ export declare function updateConfig(
184
+ newConfig: Partial<SmartOfflineConfig>,
185
+ ): Promise<void>;
186
+
187
+ /**
188
+ * Get current configuration
189
+ */
190
+ export declare function getConfig(): SmartOfflineConfig;
191
+
192
+ /**
193
+ * Check if SDK is initialized
194
+ */
195
+ export declare function isSmartOfflineReady(): boolean;
196
+
197
+ /**
198
+ * Get service worker registration
199
+ */
200
+ export declare function getServiceWorkerRegistration(): ServiceWorkerRegistration | null;
201
+
202
+ /**
203
+ * Add event listener for cache events
204
+ */
205
+ export declare function on(
206
+ eventType: "cache" | "skip" | "serve" | "clear" | "error",
207
+ callback: (event: CacheEvent) => void,
208
+ ): void;
209
+
210
+ /**
211
+ * Remove event listener for cache events
212
+ */
213
+ export declare function off(
214
+ eventType: "cache" | "skip" | "serve" | "clear" | "error",
215
+ callback: (event: CacheEvent) => void,
216
+ ): void;
217
+
218
+ /**
219
+ * Get cache logs from IndexedDB
220
+ */
221
+ export declare function getCacheLogs(): Promise<CacheLog[]>;
222
+
223
+ /**
224
+ * Clear cache logs from IndexedDB
225
+ */
226
+ export declare function clearCacheLogs(): Promise<void>;
227
+
228
+ /**
229
+ * Clear all cached data
230
+ */
231
+ export declare function clearAllCache(): Promise<void>;
232
+
233
+ /**
234
+ * Get cache statistics
235
+ */
236
+ export declare function getCacheStats(): Promise<CacheStats>;
237
+
238
+ /**
239
+ * Force update the service worker
240
+ */
241
+ export declare function forceUpdate(): Promise<void>;
242
+
243
+ /**
244
+ * Unregister the service worker and clean up
245
+ */
246
+ export declare function uninstall(): Promise<void>;
247
+
248
+ /**
249
+ * Initialize the SDK (legacy API)
250
+ * @deprecated Use setupSmartOffline() instead
251
+ */
252
+ export declare function init(config?: Partial<SmartOfflineConfig>): void;
253
+
254
+ // ============================================================================
255
+ // MAIN SDK OBJECT
256
+ // ============================================================================
257
+
66
258
  export interface SmartOfflineSDK {
67
- init(config?: SmartOfflineConfig): void;
68
- on(eventType: 'cache' | 'skip' | 'serve' | 'clear' | 'error', callback: (event: CacheEvent) => void): void;
69
- off(eventType: 'cache' | 'skip' | 'serve' | 'clear' | 'error', callback: (event: CacheEvent) => void): void;
70
- getCacheLogs(): Promise<CacheLog[]>;
71
- clearCacheLogs(): Promise<void>;
72
- clearCache(): Promise<void>;
259
+ /** Main setup function (recommended) */
260
+ setup: typeof setupSmartOffline;
261
+ /** Legacy init function */
262
+ init: typeof init;
263
+ /** Update configuration at runtime */
264
+ updateConfig: typeof updateConfig;
265
+ /** Get current configuration */
266
+ getConfig: typeof getConfig;
267
+ /** Check if SDK is initialized */
268
+ isReady: typeof isSmartOfflineReady;
269
+ /** Get service worker registration */
270
+ getRegistration: typeof getServiceWorkerRegistration;
271
+ /** Add event listener */
272
+ on: typeof on;
273
+ /** Remove event listener */
274
+ off: typeof off;
275
+ /** Clear all cache */
276
+ clearCache: typeof clearAllCache;
277
+ /** Get cache statistics */
278
+ getStats: typeof getCacheStats;
279
+ /** Get cache logs */
280
+ getCacheLogs: typeof getCacheLogs;
281
+ /** Clear cache logs */
282
+ clearCacheLogs: typeof clearCacheLogs;
283
+ /** Force update service worker */
284
+ forceUpdate: typeof forceUpdate;
285
+ /** Uninstall SDK */
286
+ uninstall: typeof uninstall;
73
287
  }
74
288
 
75
289
  export declare const SmartOffline: SmartOfflineSDK;
76
290
  export default SmartOffline;
77
291
 
292
+ // ============================================================================
293
+ // TEST UTILITIES
294
+ // ============================================================================
295
+
296
+ /**
297
+ * Matches URL against wildcard pattern
298
+ */
299
+ export declare function matchesPattern(url: string, pattern: string): boolean;
300
+
301
+ /**
302
+ * Calculates if a URL should be treated as HIGH priority
303
+ */
304
+ export declare function isHighPriority(
305
+ usage: UsageData | null,
306
+ url: string,
307
+ config?: Partial<SmartOfflineConfig>,
308
+ ): boolean;
309
+
310
+ /**
311
+ * Determines if a URL should be cached based on pattern matching
312
+ */
313
+ export declare function shouldCacheUrl(
314
+ url: string,
315
+ config?: Partial<SmartOfflineConfig>,
316
+ ): { isPage: boolean; isAPI: boolean };
317
+
318
+ /**
319
+ * Test suite class for SmartOffline SDK
320
+ */
321
+ export declare class SmartOfflineTestSuite {
322
+ constructor(config?: Partial<SmartOfflineConfig>);
323
+ runAll(): Promise<TestResult[]>;
324
+ printResults(): void;
325
+ }
326
+
327
+ /**
328
+ * Cache inspector for debugging
329
+ */
330
+ export declare class CacheInspector {
331
+ getCachedItems(): Promise<
332
+ Array<{ url: string; size: number; contentType: string }>
333
+ >;
334
+ getUsageData(): Promise<UsageData[]>;
335
+ getLogs(): Promise<Array<{ type: string; url: string; timestamp: number }>>;
336
+ showAll(): Promise<void>;
337
+ getAllData(): Promise<CacheInspectorData>;
338
+ }
339
+
340
+ /**
341
+ * Run all SmartOffline tests and print results
342
+ */
343
+ export declare function runSmartOfflineTests(
344
+ config?: Partial<SmartOfflineConfig>,
345
+ ): Promise<TestResult[]>;