@sixbell-telco/sdk 1.3.0 → 1.5.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/CHANGELOG.md +81 -0
- package/README.md +437 -472
- package/_index.css +109 -11
- package/assets/styles/toast.css +8 -0
- package/components/forms/date-picker/constants/date-picker.constants.d.ts +13 -0
- package/components/forms/date-picker/index.d.ts +5 -0
- package/components/forms/date-picker/public-api.d.ts +3 -0
- package/components/forms/date-picker/src/date-picker.component.d.ts +323 -0
- package/components/forms/date-picker/types/date-picker.types.d.ts +39 -0
- package/components/{forms/datepicker → sonner}/index.d.ts +1 -1
- package/components/sonner/public-api.d.ts +2 -0
- package/components/sonner/src/sonner.component.d.ts +39 -0
- package/components/sonner/toaster/index.d.ts +5 -0
- package/components/sonner/toaster/public-api.d.ts +1 -0
- package/components/sonner/toaster/src/toaster.component.d.ts +89 -0
- package/components/toast/public-api.d.ts +9 -0
- package/components/toast/services/toast.service.d.ts +3 -0
- package/components/toast/toast-message/toast-message.component.d.ts +3 -0
- package/components/toast/toasts-container/toasts-container.component.d.ts +3 -0
- package/fesm2022/sixbell-telco-sdk-components-forms-date-picker.mjs +885 -0
- package/fesm2022/sixbell-telco-sdk-components-forms-date-picker.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-components-sonner-toaster.mjs +132 -0
- package/fesm2022/sixbell-telco-sdk-components-sonner-toaster.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-components-sonner.mjs +179 -0
- package/fesm2022/sixbell-telco-sdk-components-sonner.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-components-toast.mjs +14 -0
- package/fesm2022/sixbell-telco-sdk-components-toast.mjs.map +1 -1
- package/fesm2022/sixbell-telco-sdk-utils-date.mjs +90 -2
- package/fesm2022/sixbell-telco-sdk-utils-date.mjs.map +1 -1
- package/fesm2022/sixbell-telco-sdk-utils-logger.mjs +578 -0
- package/fesm2022/sixbell-telco-sdk-utils-logger.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-utils-resource-loader.mjs +1222 -0
- package/fesm2022/sixbell-telco-sdk-utils-resource-loader.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-utils-theme.mjs +1434 -180
- package/fesm2022/sixbell-telco-sdk-utils-theme.mjs.map +1 -1
- package/fesm2022/sixbell-telco-sdk-utils-translation.mjs +734 -182
- package/fesm2022/sixbell-telco-sdk-utils-translation.mjs.map +1 -1
- package/package.json +68 -51
- package/utils/date/src/date-utils.d.ts +44 -0
- package/utils/logger/README.md +539 -0
- package/utils/logger/index.d.ts +5 -0
- package/utils/logger/public-api.d.ts +3 -0
- package/utils/logger/src/constants.d.ts +15 -0
- package/utils/logger/src/logger.service.d.ts +123 -0
- package/utils/logger/src/models/logger.d.ts +25 -0
- package/utils/logger/src/providers/logger.provider.d.ts +3 -0
- package/utils/resource-loader/README.md +460 -0
- package/utils/resource-loader/index.d.ts +5 -0
- package/utils/resource-loader/public-api.d.ts +1 -0
- package/utils/resource-loader/src/cache/index.d.ts +2 -0
- package/utils/resource-loader/src/cache/layers/factory.d.ts +38 -0
- package/utils/resource-loader/src/cache/layers/index.d.ts +3 -0
- package/utils/resource-loader/src/cache/layers/indexed-db-cache.d.ts +118 -0
- package/utils/resource-loader/src/cache/layers/local-storage-cache.d.ts +86 -0
- package/utils/resource-loader/src/cache/layers/memory-cache.d.ts +76 -0
- package/utils/resource-loader/src/cache/types.d.ts +107 -0
- package/utils/resource-loader/src/config/index.d.ts +1 -0
- package/utils/resource-loader/src/config/load-config.d.ts +32 -0
- package/utils/resource-loader/src/constants.d.ts +38 -0
- package/utils/resource-loader/src/http/index.d.ts +1 -0
- package/utils/resource-loader/src/http/retryable-client.d.ts +43 -0
- package/utils/resource-loader/src/index.d.ts +1 -0
- package/utils/resource-loader/src/resource-loader.d.ts +67 -0
- package/utils/resource-loader/src/types.d.ts +152 -0
- package/utils/resource-loader/src/utils/cache-validator.d.ts +72 -0
- package/utils/resource-loader/src/utils/helpers.d.ts +15 -0
- package/utils/resource-loader/src/utils/index.d.ts +2 -0
- package/utils/theme/README.md +1054 -109
- package/utils/theme/public-api.d.ts +9 -1
- package/utils/theme/src/constants.d.ts +74 -0
- package/utils/theme/src/models/theme.d.ts +288 -0
- package/utils/theme/src/providers/constants.d.ts +7 -0
- package/utils/theme/src/providers/theme.provider.d.ts +13 -0
- package/utils/theme/src/providers/theme.provider.types.d.ts +29 -0
- package/utils/theme/src/theme-assets/theme-assets.service.d.ts +214 -0
- package/utils/theme/src/theme-assets/theme-assets.types.d.ts +10 -0
- package/utils/theme/src/theme-dom/theme-dom.service.d.ts +87 -0
- package/utils/theme/src/theme-scheme/theme-scheme.service.d.ts +44 -0
- package/utils/theme/src/theme-storage/theme-storage.service.d.ts +47 -0
- package/utils/theme/src/theme.service.d.ts +40 -186
- package/utils/theme/src/utils/utils.d.ts +86 -0
- package/utils/translation/README.md +1414 -176
- package/utils/translation/public-api.d.ts +6 -0
- package/utils/translation/src/constants.d.ts +52 -0
- package/utils/translation/src/models/translation.d.ts +59 -0
- package/utils/translation/src/providers/constants.d.ts +11 -0
- package/utils/translation/src/providers/translation.provider.d.ts +73 -0
- package/utils/translation/src/providers/translation.provider.types.d.ts +26 -0
- package/utils/translation/src/translation-dom/translation-dom.service.d.ts +26 -0
- package/utils/translation/src/translation-storage/translation-storage.service.d.ts +34 -0
- package/utils/translation/src/translation.service.d.ts +19 -166
- package/components/forms/datepicker/constants/datepicker.constants.d.ts +0 -5
- package/components/forms/datepicker/public-api.d.ts +0 -3
- package/components/forms/datepicker/src/datepicker.component.d.ts +0 -165
- package/components/forms/datepicker/types/datepicker.types.d.ts +0 -7
- package/fesm2022/sixbell-telco-sdk-components-forms-datepicker.mjs +0 -487
- package/fesm2022/sixbell-telco-sdk-components-forms-datepicker.mjs.map +0 -1
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
# ResourceLoader
|
|
2
|
+
|
|
3
|
+
Intelligent resource loading with multi-layer caching, retry logic, and fallback support. Perfect for loading configuration files, translations, themes, and other assets reliably in the browser.
|
|
4
|
+
|
|
5
|
+
## Quick Start
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { ResourceLoader } from '@sixbell-telco/sdk/utils/resource-loader';
|
|
9
|
+
|
|
10
|
+
const loader = new ResourceLoader({
|
|
11
|
+
cacheName: 'my-resources',
|
|
12
|
+
cacheTTL: 1000 * 60 * 60 * 24, // 24 hours
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const result = await loader.load<MyConfig>('/assets/config.json', {
|
|
16
|
+
fallbackData: DEFAULT_CONFIG,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
console.log(result.data); // Config object
|
|
20
|
+
console.log(result.source); // 'network', 'cache', or 'fallback'
|
|
21
|
+
console.log(result.cacheStatus); // 'fresh', 'stale', or 'miss'
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
✅ **Multi-layer Caching** - IndexedDB (persistent) → localStorage → memory (fast)
|
|
27
|
+
✅ **Automatic Fallback** - Gracefully degrade to fallback data on errors
|
|
28
|
+
✅ **Smart Retry** - Configurable exponential backoff with request timeouts
|
|
29
|
+
✅ **Stale-While-Revalidate** - Serve cached data immediately, refresh in background
|
|
30
|
+
✅ **Request Deduplication** - Multiple identical requests share a single network call
|
|
31
|
+
✅ **Fresh on Navigation** - Optionally reload on every page navigation
|
|
32
|
+
✅ **Offline Support** - Works with cached data when offline
|
|
33
|
+
✅ **Structured Logging** - Built-in debug logging with LoggerService
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
The ResourceLoader is part of `@sixbell-telco/sdk`:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { ResourceLoader } from '@sixbell-telco/sdk/utils/resource-loader';
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## API
|
|
44
|
+
|
|
45
|
+
### Constructor
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
const loader = new ResourceLoader(defaults?, logger?);
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
**Parameters:**
|
|
52
|
+
|
|
53
|
+
- `defaults` - Global default configuration applied to all loads (optional)
|
|
54
|
+
- `logger` - LoggerService instance for debug output (optional)
|
|
55
|
+
|
|
56
|
+
### load(url, options)
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
const result = await loader.load<T>(url, options?);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**Parameters:**
|
|
63
|
+
|
|
64
|
+
- `url` - URL or path to fetch the resource from
|
|
65
|
+
- `options` - Request-specific options (overrides defaults)
|
|
66
|
+
|
|
67
|
+
**Returns:** `Promise<ResourceLoadResult<T>>`
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
interface ResourceLoadResult<T> {
|
|
71
|
+
data: T; // The loaded data
|
|
72
|
+
source: ResourceSource; // 'network', 'cache', or 'fallback'
|
|
73
|
+
cacheStatus: ResourceCacheStatus; // 'fresh', 'stale', or 'miss'
|
|
74
|
+
revalidate?: Promise<void>; // Background revalidation promise (if enabled)
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Configuration
|
|
79
|
+
|
|
80
|
+
### Default Configuration
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
{
|
|
84
|
+
cacheName: 'resilient-resource', // Cache storage name
|
|
85
|
+
storageOrder: ['indexedDB', 'localStorage', 'memory'], // Layer priority
|
|
86
|
+
cacheTTL: 24 * 60 * 60 * 1000, // 24 hours
|
|
87
|
+
freshOnNavigation: true, // Fetch fresh on every load
|
|
88
|
+
staleWhileRevalidate: true, // Serve stale, refresh in background
|
|
89
|
+
useStaleOnError: true, // Use stale cache if fetch fails
|
|
90
|
+
revalidateOnFresh: false, // Background refresh when cache is fresh
|
|
91
|
+
retries: 3, // Retry attempts on failure
|
|
92
|
+
retryDelayMs: 250, // Delay between retries (ms)
|
|
93
|
+
timeoutMs: 8000, // Request timeout (ms)
|
|
94
|
+
seedCacheWithFallback: true, // Cache fallback data for future use
|
|
95
|
+
logErrors: true, // Log errors with logger
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Common Scenarios
|
|
100
|
+
|
|
101
|
+
#### 1. Always Fresh (Default)
|
|
102
|
+
|
|
103
|
+
Always fetch fresh with fallback to cache. Prevents stale data but requires network.
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
const result = await loader.load('/config.json', {
|
|
107
|
+
fallbackData: DEFAULT_CONFIG,
|
|
108
|
+
});
|
|
109
|
+
// First attempt: Fetch from network
|
|
110
|
+
// On error: Use cache if available, then fallback
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
#### 2. Aggressive Caching
|
|
114
|
+
|
|
115
|
+
Use cache first, only fetch if stale. Good for high-latency networks.
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
const result = await loader.load('/config.json', {
|
|
119
|
+
freshOnNavigation: false, // Don't skip cache on navigation
|
|
120
|
+
staleWhileRevalidate: false, // Don't revalidate in background
|
|
121
|
+
fallbackData: DEFAULT_CONFIG,
|
|
122
|
+
});
|
|
123
|
+
// Use cache immediately if available
|
|
124
|
+
// Only fetch when cache expires
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
#### 3. Background Revalidation
|
|
128
|
+
|
|
129
|
+
Serve cache immediately, refresh in background.
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
const result = await loader.load('/config.json', {
|
|
133
|
+
staleWhileRevalidate: true,
|
|
134
|
+
revalidateOnFresh: true, // Refresh even fresh cache
|
|
135
|
+
fallbackData: DEFAULT_CONFIG,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// Optional: Wait for background refresh
|
|
139
|
+
if (result.revalidate) {
|
|
140
|
+
await result.revalidate;
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
#### 4. Custom Parser
|
|
145
|
+
|
|
146
|
+
Parse non-JSON responses (XML, CSV, etc.).
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
const result = await loader.load('/data.xml', {
|
|
150
|
+
parser: async (response: Response) => {
|
|
151
|
+
const text = await response.text();
|
|
152
|
+
return parseXml(text);
|
|
153
|
+
},
|
|
154
|
+
fallbackData: DEFAULT_DATA,
|
|
155
|
+
});
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Real-World Examples
|
|
159
|
+
|
|
160
|
+
### Loading Theme Configuration
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
import { ResourceLoader } from '@sixbell-telco/sdk/utils/resource-loader';
|
|
164
|
+
|
|
165
|
+
// In your theme provider
|
|
166
|
+
export function provideThemeConfig() {
|
|
167
|
+
return provideAppInitializer(async () => {
|
|
168
|
+
const loader = new ResourceLoader(
|
|
169
|
+
{
|
|
170
|
+
cacheName: 'theme-config',
|
|
171
|
+
cacheTTL: 1000 * 60 * 60 * 24, // Cache 24 hours
|
|
172
|
+
},
|
|
173
|
+
inject(LoggerService),
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
const result = await loader.load('/assets/config/themes.json', {
|
|
177
|
+
fallbackData: DEFAULT_THEME_CONFIG,
|
|
178
|
+
freshOnNavigation: true, // Hot-reload on navigation
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
applyTheme(result.data);
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Loading Translations
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import { ResourceLoader } from '@sixbell-telco/sdk/utils/resource-loader';
|
|
190
|
+
|
|
191
|
+
// In your translation provider
|
|
192
|
+
const loader = new ResourceLoader(
|
|
193
|
+
{
|
|
194
|
+
cacheName: 'translation-files',
|
|
195
|
+
cacheTTL: 1000 * 60 * 60 * 12, // Cache 12 hours
|
|
196
|
+
},
|
|
197
|
+
logger,
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
const result = await loader.load(`/assets/i18n/${lang}.json`, {
|
|
201
|
+
fallbackData: {},
|
|
202
|
+
retries: 3,
|
|
203
|
+
retryDelayMs: 400,
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
loadTranslations(result.data);
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Loading Multiple Paths with Fallback
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
import { ResourceLoader } from '@sixbell-telco/sdk/utils/resource-loader';
|
|
213
|
+
|
|
214
|
+
const loader = new ResourceLoader();
|
|
215
|
+
|
|
216
|
+
// Try multiple paths, use first successful load
|
|
217
|
+
const paths = ['/assets/config/custom.json', '/assets/config/default.json'];
|
|
218
|
+
|
|
219
|
+
let config = null;
|
|
220
|
+
for (const path of paths) {
|
|
221
|
+
try {
|
|
222
|
+
const result = await loader.load(path, {
|
|
223
|
+
fallbackData: null,
|
|
224
|
+
useStaleOnError: false,
|
|
225
|
+
});
|
|
226
|
+
if (result.data) {
|
|
227
|
+
config = result.data;
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
} catch (error) {
|
|
231
|
+
// Try next path
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (!config) {
|
|
236
|
+
config = DEFAULT_CONFIG;
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Cache Layers
|
|
241
|
+
|
|
242
|
+
ResourceLoader uses three cache layers, checked in priority order:
|
|
243
|
+
|
|
244
|
+
### 1. IndexedDB
|
|
245
|
+
|
|
246
|
+
- **Capacity:** ~50MB+ (depends on browser/user settings)
|
|
247
|
+
- **Persistence:** Survives page reload and browser session
|
|
248
|
+
- **Speed:** Asynchronous (slightly slower than localStorage)
|
|
249
|
+
- **Use Case:** Primary persistent storage
|
|
250
|
+
|
|
251
|
+
### 2. localStorage
|
|
252
|
+
|
|
253
|
+
- **Capacity:** ~5-10MB per domain
|
|
254
|
+
- **Persistence:** Survives page reload and browser session
|
|
255
|
+
- **Speed:** Synchronous, but slower than memory
|
|
256
|
+
- **Use Case:** Fallback when IndexedDB unavailable
|
|
257
|
+
- **Limitations:** Subject to quota limits, fails in private browsing
|
|
258
|
+
|
|
259
|
+
### 3. Memory
|
|
260
|
+
|
|
261
|
+
- **Capacity:** Unlimited (depends on available RAM)
|
|
262
|
+
- **Persistence:** Cleared on page reload/session close
|
|
263
|
+
- **Speed:** Fastest (in-memory operations)
|
|
264
|
+
- **Use Case:** Session-specific data, fallback for storage-unavailable scenarios
|
|
265
|
+
|
|
266
|
+
## Storage Order Configuration
|
|
267
|
+
|
|
268
|
+
Control which cache layers to use and their priority:
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
// Use all layers (default)
|
|
272
|
+
const result = await loader.load('/config.json', {
|
|
273
|
+
storageOrder: ['indexedDB', 'localStorage', 'memory'],
|
|
274
|
+
fallbackData: {},
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
// Use only memory (fastest, session-only)
|
|
278
|
+
const result = await loader.load('/config.json', {
|
|
279
|
+
storageOrder: ['memory'],
|
|
280
|
+
fallbackData: {},
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
// Skip localStorage (if quota issues)
|
|
284
|
+
const result = await loader.load('/config.json', {
|
|
285
|
+
storageOrder: ['indexedDB', 'memory'],
|
|
286
|
+
fallbackData: {},
|
|
287
|
+
});
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
## Error Handling
|
|
291
|
+
|
|
292
|
+
### Try-Catch Pattern
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
try {
|
|
296
|
+
const result = await loader.load('/config.json', {
|
|
297
|
+
retries: 3,
|
|
298
|
+
fallbackData: null, // Will throw if no fallback
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
if (result.source === 'fallback') {
|
|
302
|
+
console.warn('Using fallback data, network may be unavailable');
|
|
303
|
+
}
|
|
304
|
+
} catch (error) {
|
|
305
|
+
console.error('Failed to load config:', error);
|
|
306
|
+
// Handle critical error
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### With Fallback
|
|
311
|
+
|
|
312
|
+
```typescript
|
|
313
|
+
const result = await loader.load('/config.json', {
|
|
314
|
+
fallbackData: DEFAULT_CONFIG, // Never throws
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
// Always has data from network, cache, or fallback
|
|
318
|
+
console.log(result.data);
|
|
319
|
+
console.log(result.source); // Check where data came from
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
## Logging & Debugging
|
|
323
|
+
|
|
324
|
+
Enable debug logging:
|
|
325
|
+
|
|
326
|
+
```typescript
|
|
327
|
+
import { LoggerService } from '@sixbell-telco/sdk/utils/logger';
|
|
328
|
+
|
|
329
|
+
const logger = inject(LoggerService);
|
|
330
|
+
logger.configure({
|
|
331
|
+
enableLogging: true,
|
|
332
|
+
logLevel: 'debug', // See all debug messages
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
const loader = new ResourceLoader(defaults, logger);
|
|
336
|
+
|
|
337
|
+
// Now ResourceLoader logs all operations:
|
|
338
|
+
// - "Starting load"
|
|
339
|
+
// - "Cache lookup results"
|
|
340
|
+
// - "Network fetch"
|
|
341
|
+
// - "Using fallback"
|
|
342
|
+
// - etc.
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
## Performance Tips
|
|
346
|
+
|
|
347
|
+
1. **Use appropriate cache TTL:**
|
|
348
|
+
|
|
349
|
+
- Config files: 24 hours
|
|
350
|
+
- Translations: 12 hours
|
|
351
|
+
- Assets: Based on change frequency
|
|
352
|
+
|
|
353
|
+
2. **Enable stale-while-revalidate for config:**
|
|
354
|
+
|
|
355
|
+
```typescript
|
|
356
|
+
{
|
|
357
|
+
staleWhileRevalidate: true,
|
|
358
|
+
revalidateOnFresh: true, // Refresh even fresh cache
|
|
359
|
+
}
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
3. **Use custom storage order for specific scenarios:**
|
|
363
|
+
|
|
364
|
+
- Mobile with limited storage: `['memory']`
|
|
365
|
+
- Server-side rendering: `['memory']` (IndexedDB unavailable)
|
|
366
|
+
|
|
367
|
+
4. **Batch requests:**
|
|
368
|
+
- ResourceLoader automatically deduplicates concurrent identical requests
|
|
369
|
+
- Multiple components can call `load()` with same URL safely
|
|
370
|
+
|
|
371
|
+
## Troubleshooting
|
|
372
|
+
|
|
373
|
+
### Cache not persisting between page reloads?
|
|
374
|
+
|
|
375
|
+
- Check `storageOrder` includes `'indexedDB'` or `'localStorage'`
|
|
376
|
+
- Verify IndexedDB/localStorage is not disabled in browser
|
|
377
|
+
- Check browser privacy mode (storage may be disabled)
|
|
378
|
+
|
|
379
|
+
### Getting stale data?
|
|
380
|
+
|
|
381
|
+
- Set `freshOnNavigation: true` to bypass cache on navigation
|
|
382
|
+
- Reduce `cacheTTL` for more frequent refreshes
|
|
383
|
+
- Use `revalidateOnFresh: true` for background updates
|
|
384
|
+
|
|
385
|
+
### Network timeouts?
|
|
386
|
+
|
|
387
|
+
- Increase `timeoutMs` (default: 8000ms)
|
|
388
|
+
- Increase `retries` for unreliable connections
|
|
389
|
+
- Increase `retryDelayMs` for congested networks
|
|
390
|
+
|
|
391
|
+
### High memory usage?
|
|
392
|
+
|
|
393
|
+
- Switch to memory-only caching for less critical data: `['memory']`
|
|
394
|
+
- Reduce `cacheTTL` to evict stale entries sooner
|
|
395
|
+
- Use multiple loaders with different `cacheName` values
|
|
396
|
+
|
|
397
|
+
## API Reference
|
|
398
|
+
|
|
399
|
+
### Types
|
|
400
|
+
|
|
401
|
+
```typescript
|
|
402
|
+
type ResourceSource = 'network' | 'cache' | 'fallback';
|
|
403
|
+
type ResourceCacheStatus = 'fresh' | 'stale' | 'miss';
|
|
404
|
+
type CacheLayerName = 'indexedDB' | 'localStorage' | 'memory';
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
### Interfaces
|
|
408
|
+
|
|
409
|
+
```typescript
|
|
410
|
+
interface ResourceLoadResult<T> {
|
|
411
|
+
data: T;
|
|
412
|
+
source: ResourceSource;
|
|
413
|
+
cacheStatus: ResourceCacheStatus;
|
|
414
|
+
revalidate?: Promise<void>;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
interface ResourceLoaderDefaults {
|
|
418
|
+
cacheName?: string;
|
|
419
|
+
storageOrder?: CacheLayerName[];
|
|
420
|
+
cacheTTL?: number;
|
|
421
|
+
freshOnNavigation?: boolean;
|
|
422
|
+
staleWhileRevalidate?: boolean;
|
|
423
|
+
useStaleOnError?: boolean;
|
|
424
|
+
revalidateOnFresh?: boolean;
|
|
425
|
+
retries?: number;
|
|
426
|
+
retryDelayMs?: number;
|
|
427
|
+
timeoutMs?: number;
|
|
428
|
+
seedCacheWithFallback?: boolean;
|
|
429
|
+
logErrors?: boolean;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
interface ResourceLoaderRequestOptions<T> extends ResourceLoaderDefaults {
|
|
433
|
+
cacheKey?: string;
|
|
434
|
+
fallbackData?: T;
|
|
435
|
+
parser?: (response: Response) => Promise<T>;
|
|
436
|
+
defaultCacheKey?: string;
|
|
437
|
+
}
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
## Best Practices
|
|
441
|
+
|
|
442
|
+
✅ **Do:**
|
|
443
|
+
|
|
444
|
+
- Provide `fallbackData` for critical resources
|
|
445
|
+
- Use `freshOnNavigation: true` for config files that may change
|
|
446
|
+
- Set appropriate `cacheTTL` based on expected change frequency
|
|
447
|
+
- Enable logging during development
|
|
448
|
+
- Handle `source` and `cacheStatus` in your UI (show loading state, offline indicator)
|
|
449
|
+
|
|
450
|
+
❌ **Don't:**
|
|
451
|
+
|
|
452
|
+
- Rely on cache alone without `fallbackData` for critical data
|
|
453
|
+
- Use very short `cacheTTL` (causes excessive network requests)
|
|
454
|
+
- Ignore `revalidate` promise (miss background updates)
|
|
455
|
+
- Use aggressive caching for security-sensitive data
|
|
456
|
+
- Store sensitive data in IndexedDB/localStorage (unencrypted)
|
|
457
|
+
|
|
458
|
+
## License
|
|
459
|
+
|
|
460
|
+
Part of `@sixbell-telco/sdk` - See LICENSE file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src/index';
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { CacheLayer, CacheLayerFactoryOptions } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Factory for creating cache layer instances based on layer name.
|
|
4
|
+
* Decouples cache layer creation from usage throughout the application.
|
|
5
|
+
* Makes it easy to swap or customize cache backends.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const cacheLayer = CacheLayerFactory.create({
|
|
10
|
+
* layerName: 'indexedDB',
|
|
11
|
+
* cacheName: 'my-app-cache'
|
|
12
|
+
* });
|
|
13
|
+
*
|
|
14
|
+
* // All three layer types can be created the same way
|
|
15
|
+
* const memory = CacheLayerFactory.create({ layerName: 'memory', cacheName: 'app' });
|
|
16
|
+
* const local = CacheLayerFactory.create({ layerName: 'localStorage', cacheName: 'app' });
|
|
17
|
+
* const idb = CacheLayerFactory.create({ layerName: 'indexedDB', cacheName: 'app' });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class CacheLayerFactory {
|
|
21
|
+
/**
|
|
22
|
+
* Create a cache layer instance based on the specified layer name.
|
|
23
|
+
*
|
|
24
|
+
* @param options - Factory options including layer name and cache name
|
|
25
|
+
* @returns A new cache layer instance
|
|
26
|
+
*
|
|
27
|
+
* @throws Error if the layer name is not recognized
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const layer = CacheLayerFactory.create({
|
|
32
|
+
* layerName: 'indexedDB',
|
|
33
|
+
* cacheName: 'themes'
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
static create(options: CacheLayerFactoryOptions): CacheLayer;
|
|
38
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { CacheEntry, CacheLayer } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* IndexedDB-backed cache layer implementation.
|
|
4
|
+
* Most persistent and largest capacity (~50MB+ depending on browser).
|
|
5
|
+
* Survives page reload and browser session; user must explicitly clear.
|
|
6
|
+
* Async operations with transactions ensure data integrity.
|
|
7
|
+
*
|
|
8
|
+
* @template T The type of data stored
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const indexedDbCache = new IndexedDbCacheLayer<Config>('my-app', 'sixbell-theme-resources');
|
|
13
|
+
* await indexedDbCache.set('config', { theme: 'dark' }, 3600000);
|
|
14
|
+
* const entry = await indexedDbCache.get('config');
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare class IndexedDbCacheLayer<T = unknown> implements CacheLayer<T> {
|
|
18
|
+
private cacheName;
|
|
19
|
+
private databaseName;
|
|
20
|
+
private dbPromise;
|
|
21
|
+
constructor(cacheName: string, databaseName?: string);
|
|
22
|
+
/**
|
|
23
|
+
* Retrieve a cached entry from IndexedDB.
|
|
24
|
+
*
|
|
25
|
+
* @param key - The cache key
|
|
26
|
+
* @returns The cached entry if found and fresh, null otherwise
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const entry = await indexedDbCache.get('theme-config');
|
|
31
|
+
* if (entry) {
|
|
32
|
+
* console.log('Found in IndexedDB:', entry.value);
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
get(key: string): Promise<CacheEntry<T> | null>;
|
|
37
|
+
/**
|
|
38
|
+
* Store a value in IndexedDB.
|
|
39
|
+
*
|
|
40
|
+
* @param key - The cache key
|
|
41
|
+
* @param value - The data to cache
|
|
42
|
+
* @param ttl - Time-to-live in milliseconds
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* await indexedDbCache.set('theme', { mode: 'dark' }, 3600000);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
set(key: string, value: T, ttl: number): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Remove a cached entry from IndexedDB.
|
|
52
|
+
*
|
|
53
|
+
* @param key - The cache key
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* await indexedDbCache.remove('theme-config');
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
remove(key: string): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Clear all entries from IndexedDB with this cache name prefix.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* await indexedDbCache.clear();
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
clear(): Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* Remove all expired entries from IndexedDB.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* await indexedDbCache.cleanup();
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
cleanup(): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Get or create the IndexedDB database connection.
|
|
81
|
+
*
|
|
82
|
+
* @private
|
|
83
|
+
* @returns Promise that resolves to the IDBDatabase instance
|
|
84
|
+
*/
|
|
85
|
+
private getDatabase;
|
|
86
|
+
/**
|
|
87
|
+
* Get a value from an IndexedDB object store.
|
|
88
|
+
*
|
|
89
|
+
* @private
|
|
90
|
+
*/
|
|
91
|
+
private getFromStore;
|
|
92
|
+
/**
|
|
93
|
+
* Put a value into an IndexedDB object store.
|
|
94
|
+
*
|
|
95
|
+
* @private
|
|
96
|
+
*/
|
|
97
|
+
private putInStore;
|
|
98
|
+
/**
|
|
99
|
+
* Delete a value from an IndexedDB object store.
|
|
100
|
+
*
|
|
101
|
+
* @private
|
|
102
|
+
*/
|
|
103
|
+
private deleteFromStore;
|
|
104
|
+
/**
|
|
105
|
+
* Get all keys from an IndexedDB object store.
|
|
106
|
+
*
|
|
107
|
+
* @private
|
|
108
|
+
*/
|
|
109
|
+
private getAllKeysFromStore;
|
|
110
|
+
/**
|
|
111
|
+
* Get the full storage key with namespace prefix.
|
|
112
|
+
*
|
|
113
|
+
* @private
|
|
114
|
+
* @param key - The logical cache key
|
|
115
|
+
* @returns The full storage key with prefix
|
|
116
|
+
*/
|
|
117
|
+
private getStorageKey;
|
|
118
|
+
}
|