@sixbell-telco/sdk 1.4.0 → 1.6.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 +76 -0
- package/README.md +410 -472
- package/_index.css +26 -0
- package/components/dropdown/dropdown-chevron/dropdown-chevron.component.d.ts +6 -1
- package/components/dropdown/dropdown-content/dropdown-content.component.d.ts +1 -1
- package/components/dropdown/dropdown-item/dropdown-item.component.d.ts +6 -1
- package/components/dropdown/dropdown-item-checkbox/dropdown-item-checkbox.component.d.ts +7 -1
- package/components/dropdown/dropdown-item-radio/dropdown-item-radio.component.d.ts +7 -1
- package/components/dropdown/dropdown-trigger/dropdown-trigger.component.d.ts +30 -0
- package/components/dropdown/public-api.d.ts +0 -1
- package/components/dropdown/src/dropdown.component.d.ts +31 -7
- package/components/forms/combobox/src/combobox.component.d.ts +24 -0
- package/components/forms/select/src/select.component.d.ts +24 -0
- package/fesm2022/sixbell-telco-sdk-components-dropdown.mjs +472 -341
- package/fesm2022/sixbell-telco-sdk-components-dropdown.mjs.map +1 -1
- package/fesm2022/sixbell-telco-sdk-components-forms-combobox.mjs +58 -2
- package/fesm2022/sixbell-telco-sdk-components-forms-combobox.mjs.map +1 -1
- package/fesm2022/sixbell-telco-sdk-components-forms-select.mjs +52 -2
- package/fesm2022/sixbell-telco-sdk-components-forms-select.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 +54 -46
- 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/dropdown/src/services/dropdown.service.d.ts +0 -22
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { CacheEntry, CacheLayer } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* localStorage-backed cache layer implementation.
|
|
4
|
+
* Persistent across page reloads and browser sessions (capacity ~5-10MB per domain).
|
|
5
|
+
* Falls back gracefully if localStorage is unavailable (private browsing, quota exceeded).
|
|
6
|
+
*
|
|
7
|
+
* Stores entries as JSON strings with a namespace prefix to avoid collisions.
|
|
8
|
+
*
|
|
9
|
+
* @template T The type of data stored
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const localCache = new LocalStorageCacheLayer<Config>('my-app');
|
|
14
|
+
* await localCache.set('config', { theme: 'dark' }, 3600000);
|
|
15
|
+
* const entry = await localCache.get('config');
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare class LocalStorageCacheLayer<T = unknown> implements CacheLayer<T> {
|
|
19
|
+
private cacheName;
|
|
20
|
+
constructor(cacheName: string);
|
|
21
|
+
/**
|
|
22
|
+
* Retrieve a cached entry from localStorage.
|
|
23
|
+
*
|
|
24
|
+
* @param key - The cache key
|
|
25
|
+
* @returns The cached entry if found and fresh, null otherwise
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const entry = await localCache.get('theme-config');
|
|
30
|
+
* if (entry) {
|
|
31
|
+
* console.log('Found in localStorage:', entry.value);
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
get(key: string): Promise<CacheEntry<T> | null>;
|
|
36
|
+
/**
|
|
37
|
+
* Store a value in localStorage.
|
|
38
|
+
*
|
|
39
|
+
* @param key - The cache key
|
|
40
|
+
* @param value - The data to cache
|
|
41
|
+
* @param ttl - Time-to-live in milliseconds
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* await localCache.set('theme', { mode: 'dark' }, 3600000);
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
set(key: string, value: T, ttl: number): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Remove a cached entry from localStorage.
|
|
51
|
+
*
|
|
52
|
+
* @param key - The cache key
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* await localCache.remove('theme-config');
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
remove(key: string): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Clear all entries from localStorage with this cache name prefix.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* await localCache.clear();
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
clear(): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Remove all expired entries from localStorage.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* await localCache.cleanup();
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
cleanup(): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Get the full storage key with namespace prefix.
|
|
80
|
+
*
|
|
81
|
+
* @private
|
|
82
|
+
* @param key - The logical cache key
|
|
83
|
+
* @returns The full storage key with prefix
|
|
84
|
+
*/
|
|
85
|
+
private getStorageKey;
|
|
86
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { CacheEntry, CacheLayer } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* In-memory cache layer implementation.
|
|
4
|
+
* Fastest but volatile—cleared on page reload and browser session close.
|
|
5
|
+
*
|
|
6
|
+
* Uses a Map to store entries with O(1) lookup and retrieval.
|
|
7
|
+
*
|
|
8
|
+
* @template T The type of data stored
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const memoryCache = new MemoryCacheLayer<Config>();
|
|
13
|
+
* await memoryCache.set('config', { theme: 'dark' }, 3600000);
|
|
14
|
+
* const entry = await memoryCache.get('config');
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare class MemoryCacheLayer<T = unknown> implements CacheLayer<T> {
|
|
18
|
+
private store;
|
|
19
|
+
/**
|
|
20
|
+
* Retrieve a cached entry from memory.
|
|
21
|
+
*
|
|
22
|
+
* @param key - The cache key
|
|
23
|
+
* @returns The cached entry if found and fresh, null otherwise
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const entry = await memoryCache.get('theme-config');
|
|
28
|
+
* if (entry) {
|
|
29
|
+
* console.log('Found in memory:', entry.value);
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
get(key: string): Promise<CacheEntry<T> | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Store a value in memory cache.
|
|
36
|
+
*
|
|
37
|
+
* @param key - The cache key
|
|
38
|
+
* @param value - The data to cache
|
|
39
|
+
* @param ttl - Time-to-live in milliseconds
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* await memoryCache.set('theme', { mode: 'dark' }, 3600000);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
set(key: string, value: T, ttl: number): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Remove a cached entry from memory.
|
|
49
|
+
*
|
|
50
|
+
* @param key - The cache key
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* await memoryCache.remove('theme-config');
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
remove(key: string): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Clear all entries from memory cache.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* await memoryCache.clear();
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
clear(): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Remove all expired entries from memory cache.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* await memoryCache.cleanup();
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
cleanup(): Promise<void>;
|
|
76
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { CacheLayerName } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a single cache entry with metadata.
|
|
4
|
+
*
|
|
5
|
+
* @template T The type of cached data
|
|
6
|
+
*
|
|
7
|
+
* @property value - The actual cached data
|
|
8
|
+
* @property timestamp - When the entry was created (milliseconds since epoch)
|
|
9
|
+
* @property ttl - Time-to-live in milliseconds from creation
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const entry: CacheEntry<Config> = {
|
|
14
|
+
* value: { theme: 'dark' },
|
|
15
|
+
* timestamp: Date.now(),
|
|
16
|
+
* ttl: 3600000, // 1 hour
|
|
17
|
+
* };
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export interface CacheEntry<T> {
|
|
21
|
+
value: T;
|
|
22
|
+
timestamp: number;
|
|
23
|
+
ttl: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Abstract interface for cache storage layers.
|
|
27
|
+
* Each layer (IndexedDB, localStorage, memory) implements this interface,
|
|
28
|
+
* allowing the loader to swap between storage backends seamlessly.
|
|
29
|
+
*
|
|
30
|
+
* @template T The type of data stored
|
|
31
|
+
*/
|
|
32
|
+
export interface CacheLayer<T = unknown> {
|
|
33
|
+
/**
|
|
34
|
+
* Retrieve a cached entry by key.
|
|
35
|
+
*
|
|
36
|
+
* @param key - The cache key
|
|
37
|
+
* @returns The cached entry, or null if not found or expired
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const entry = await cacheLayer.get('theme-config');
|
|
42
|
+
* if (entry) {
|
|
43
|
+
* console.log('Found:', entry.value);
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
get(key: string): Promise<CacheEntry<T> | null>;
|
|
48
|
+
/**
|
|
49
|
+
* Store a value in the cache.
|
|
50
|
+
*
|
|
51
|
+
* @param key - The cache key
|
|
52
|
+
* @param value - The data to cache
|
|
53
|
+
* @param ttl - Time-to-live in milliseconds
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* await cacheLayer.set('theme-config', themeData, 3600000);
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
set(key: string, value: T, ttl: number): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Remove a cached entry by key.
|
|
63
|
+
*
|
|
64
|
+
* @param key - The cache key
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* await cacheLayer.remove('theme-config');
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
remove(key: string): Promise<void>;
|
|
72
|
+
/**
|
|
73
|
+
* Clear all entries from this cache layer.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* await cacheLayer.clear();
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
clear(): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Cleanup expired entries (called during maintenance cycles).
|
|
83
|
+
* For in-memory caches, this might remove stale entries.
|
|
84
|
+
* For persistent layers (IndexedDB, localStorage), this removes entries past TTL.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* await cacheLayer.cleanup();
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
cleanup(): Promise<void>;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Configuration for creating cache layers.
|
|
95
|
+
*/
|
|
96
|
+
export interface CacheLayerConfig {
|
|
97
|
+
cacheName: string;
|
|
98
|
+
storageName?: string;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Factory options for creating cache layers.
|
|
102
|
+
*/
|
|
103
|
+
export interface CacheLayerFactoryOptions {
|
|
104
|
+
layerName: CacheLayerName;
|
|
105
|
+
cacheName: string;
|
|
106
|
+
indexedDBName?: string;
|
|
107
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { LoadConfig } from './load-config';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ResourceLoaderDefaults, ResourceLoaderRequestOptions } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Merges global defaults with per-request options into a single configuration object.
|
|
4
|
+
* Eliminates scattered config assignments throughout the codebase.
|
|
5
|
+
*
|
|
6
|
+
* @template T The expected data type
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const config = new LoadConfig<MyData>(defaults, requestOptions);
|
|
11
|
+
* console.log(config.cacheTTL); // Value from requestOptions or defaults
|
|
12
|
+
* console.log(config.retries); // Value from requestOptions or defaults
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare class LoadConfig<T = unknown> {
|
|
16
|
+
readonly cacheName: string;
|
|
17
|
+
readonly storageOrder: string[];
|
|
18
|
+
readonly cacheTTL: number;
|
|
19
|
+
readonly staleWhileRevalidate: boolean;
|
|
20
|
+
readonly useStaleOnError: boolean;
|
|
21
|
+
readonly revalidateOnFresh: boolean;
|
|
22
|
+
readonly retries: number;
|
|
23
|
+
readonly retryDelayMs: number;
|
|
24
|
+
readonly timeoutMs: number;
|
|
25
|
+
readonly parser: (response: Response) => Promise<unknown>;
|
|
26
|
+
readonly seedCacheWithFallback: boolean;
|
|
27
|
+
readonly logErrors: boolean;
|
|
28
|
+
readonly freshOnNavigation: boolean;
|
|
29
|
+
readonly cacheKey: string;
|
|
30
|
+
readonly fallbackData?: T;
|
|
31
|
+
constructor(defaults: ResourceLoaderDefaults | undefined, options: ResourceLoaderRequestOptions<T> | undefined, url: string);
|
|
32
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { CacheLayerName, ResourceLoaderDefaults } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Default storage layer order for cache lookups.
|
|
4
|
+
* IndexedDB is tried first (largest capacity), then localStorage, then memory (fastest but volatile).
|
|
5
|
+
*/
|
|
6
|
+
export declare const DEFAULT_STORAGE_ORDER: CacheLayerName[];
|
|
7
|
+
/**
|
|
8
|
+
* Default cache TTL: 24 hours (in milliseconds)
|
|
9
|
+
*/
|
|
10
|
+
export declare const DEFAULT_CACHE_TTL: number;
|
|
11
|
+
/**
|
|
12
|
+
* IndexedDB database and store names
|
|
13
|
+
*/
|
|
14
|
+
export declare const INDEXED_DB_NAME = "resilient-resources";
|
|
15
|
+
export declare const INDEXED_DB_STORE_NAME = "resources";
|
|
16
|
+
/**
|
|
17
|
+
* Default configuration for ResourceLoader instances.
|
|
18
|
+
* These values are applied to all load operations unless explicitly overridden.
|
|
19
|
+
*
|
|
20
|
+
* Note: freshOnNavigation defaults to true, which is the recommended pattern:
|
|
21
|
+
* - Skip cache on every load, always fetch fresh with network retry fallback to cache
|
|
22
|
+
* - Prevents stale data problems and layout shifts
|
|
23
|
+
* - Use fallbackData for offline scenarios
|
|
24
|
+
* - Disable it (set to false) only if you need aggressive caching (rare cases)
|
|
25
|
+
*/
|
|
26
|
+
export declare const RESOURCE_LOADER_DEFAULTS: ResourceLoaderDefaults;
|
|
27
|
+
/**
|
|
28
|
+
* Maximum number of retry attempts for failed requests
|
|
29
|
+
*/
|
|
30
|
+
export declare const MAX_RETRIES = 5;
|
|
31
|
+
/**
|
|
32
|
+
* Cleanup interval for cache maintenance (24 hours)
|
|
33
|
+
*/
|
|
34
|
+
export declare const CACHE_CLEANUP_INTERVAL: number;
|
|
35
|
+
/**
|
|
36
|
+
* Maximum cleanup task duration (5 minutes)
|
|
37
|
+
*/
|
|
38
|
+
export declare const MAX_CLEANUP_TASK_DURATION: number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { RetryableHttpClient } from './retryable-client';
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encapsulates HTTP request logic with built-in retry and timeout support.
|
|
3
|
+
* Centralizes fetch implementation to eliminate duplication across the codebase.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* const client = new RetryableHttpClient(3, 250, 8000);
|
|
8
|
+
* const response = await client.fetch('https://example.com/data');
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export declare class RetryableHttpClient {
|
|
12
|
+
readonly retries: number;
|
|
13
|
+
readonly retryDelayMs: number;
|
|
14
|
+
readonly timeoutMs: number;
|
|
15
|
+
constructor(retries: number, retryDelayMs: number, timeoutMs: number);
|
|
16
|
+
/**
|
|
17
|
+
* Fetch a resource with automatic retry and timeout support.
|
|
18
|
+
*
|
|
19
|
+
* @param url - The resource URL
|
|
20
|
+
* @returns The Response object
|
|
21
|
+
* @throws Error if all retries are exhausted or fetch fails
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* try {
|
|
26
|
+
* const response = await client.fetch('https://api.example.com/config');
|
|
27
|
+
* const data = await response.json();
|
|
28
|
+
* } catch (error) {
|
|
29
|
+
* console.error('Failed to fetch:', error);
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
fetch(url: string): Promise<Response>;
|
|
34
|
+
/**
|
|
35
|
+
* Single fetch attempt with timeout.
|
|
36
|
+
*
|
|
37
|
+
* @private
|
|
38
|
+
* @param url - The resource URL
|
|
39
|
+
* @returns The Response object
|
|
40
|
+
* @throws Error if fetch times out or fails
|
|
41
|
+
*/
|
|
42
|
+
private attemptFetch;
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './resource-loader';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { LoggerService } from '@sixbell-telco/sdk/utils/logger';
|
|
2
|
+
export type { CacheLayerName, ResourceCacheStatus, ResourceLoadResult, ResourceLoaderDefaults, ResourceLoaderRequestOptions, ResourceSource, } from './types';
|
|
3
|
+
export type { CacheEntry, CacheLayer, CacheLayerConfig, CacheLayerFactoryOptions } from './cache';
|
|
4
|
+
export { IndexedDbCacheLayer, LocalStorageCacheLayer, MemoryCacheLayer } from './cache/layers';
|
|
5
|
+
export { CacheLayerFactory } from './cache/layers/factory';
|
|
6
|
+
export { CacheValidator, cloneValue } from './utils';
|
|
7
|
+
export { DEFAULT_CACHE_TTL, DEFAULT_STORAGE_ORDER, RESOURCE_LOADER_DEFAULTS } from './constants';
|
|
8
|
+
import type { ResourceLoadResult, ResourceLoaderDefaults, ResourceLoaderRequestOptions } from './types';
|
|
9
|
+
/**
|
|
10
|
+
* Provides intelligent resource loading with:
|
|
11
|
+
* - **Multi-layer caching**: IndexedDB (persistent) → localStorage → memory, with automatic fallback
|
|
12
|
+
* - **Retry logic**: Configurable retries with exponential backoff and request timeouts
|
|
13
|
+
* - **Stale-while-revalidate**: Serve cached data immediately, refresh in background
|
|
14
|
+
* - **Fallback support**: Gracefully degrade to fallback data when all else fails
|
|
15
|
+
* - **Request deduplication**: Multiple identical requests share a single network call
|
|
16
|
+
*
|
|
17
|
+
* @example Basic usage (theme configuration):
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const loader = new ResourceLoader({
|
|
20
|
+
* cacheName: 'theme-cache',
|
|
21
|
+
* cacheTTL: 1000 * 60 * 60 * 24, // 24 hours
|
|
22
|
+
* storageOrder: ['indexedDB', 'localStorage', 'memory'],
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* const result = await loader.load<ThemeConfig>('assets/themes/config.json', {
|
|
26
|
+
* fallbackData: DEFAULT_THEME_CONFIG,
|
|
27
|
+
* parser: async (response) => (await response.json()) as ThemeConfig,
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* console.log(result.data); // Theme configuration
|
|
31
|
+
* console.log(result.source); // 'cache', 'network', or 'fallback'
|
|
32
|
+
* console.log(result.cacheStatus); // 'fresh', 'stale', or 'miss'
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare class ResourceLoader {
|
|
36
|
+
private readonly defaults;
|
|
37
|
+
private readonly layerPool;
|
|
38
|
+
private readonly inFlightLoads;
|
|
39
|
+
private logger;
|
|
40
|
+
private indexedDBName;
|
|
41
|
+
/**
|
|
42
|
+
* Creates a new ResourceLoader instance.
|
|
43
|
+
*
|
|
44
|
+
* @param defaults Global default configuration applied to all load operations
|
|
45
|
+
* @param logger Optional logger instance for debug output
|
|
46
|
+
*/
|
|
47
|
+
constructor(defaults?: ResourceLoaderDefaults, logger?: LoggerService);
|
|
48
|
+
/**
|
|
49
|
+
* Loads a resource with intelligent caching, retry, and fallback support.
|
|
50
|
+
*
|
|
51
|
+
* @template T The expected data type to be loaded
|
|
52
|
+
* @param url The URL or path to fetch the resource from
|
|
53
|
+
* @param options Request-specific options (override defaults)
|
|
54
|
+
* @returns A promise resolving to a ResourceLoadResult containing data, source, and cache status
|
|
55
|
+
*/
|
|
56
|
+
load<T = unknown>(url: string, options?: ResourceLoaderRequestOptions<T>): Promise<ResourceLoadResult<T>>;
|
|
57
|
+
private loadInternal;
|
|
58
|
+
private lookupCache;
|
|
59
|
+
private logCacheStatus;
|
|
60
|
+
private returnCacheResult;
|
|
61
|
+
private returnCacheWithRevalidation;
|
|
62
|
+
private performBackgroundRefresh;
|
|
63
|
+
private fetchAndHandleError;
|
|
64
|
+
private performFetch;
|
|
65
|
+
private buildLayers;
|
|
66
|
+
private writeToLayers;
|
|
67
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions and interfaces for ResourceLoader
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Indicates where the resource data originated.
|
|
6
|
+
* - `network`: Freshly fetched from the URL
|
|
7
|
+
* - `cache`: Retrieved from one of the cache layers (IndexedDB, localStorage, or memory)
|
|
8
|
+
* - `fallback`: Used the provided fallback data (no cache, network error)
|
|
9
|
+
*/
|
|
10
|
+
export type ResourceSource = 'network' | 'cache' | 'fallback';
|
|
11
|
+
/**
|
|
12
|
+
* Describes the freshness status of cached data.
|
|
13
|
+
* - `fresh`: Cache entry is within the TTL window
|
|
14
|
+
* - `stale`: Cache entry is older than the TTL (but still served if `staleWhileRevalidate` is enabled)
|
|
15
|
+
* - `miss`: No cache entry existed (new load or fallback used)
|
|
16
|
+
*/
|
|
17
|
+
export type ResourceCacheStatus = 'fresh' | 'stale' | 'miss';
|
|
18
|
+
/**
|
|
19
|
+
* Names of the supported cache storage layers.
|
|
20
|
+
* - `indexedDB`: Persistent, large capacity; survives page reload and browser session
|
|
21
|
+
* - `localStorage`: Persistent, limited capacity (~5-10MB); survives page reload and browser session
|
|
22
|
+
* - `memory`: In-memory only; cleared on page reload
|
|
23
|
+
*/
|
|
24
|
+
export type CacheLayerName = 'indexedDB' | 'localStorage' | 'memory';
|
|
25
|
+
/**
|
|
26
|
+
* Result returned by the loader after a successful load operation.
|
|
27
|
+
*
|
|
28
|
+
* @template T The data type being loaded
|
|
29
|
+
*
|
|
30
|
+
* @property data - The loaded resource data
|
|
31
|
+
* @property source - Where the data came from (network, cache, or fallback)
|
|
32
|
+
* @property cacheStatus - Whether the cache entry is fresh, stale, or missed
|
|
33
|
+
* @property revalidate - Optional promise that resolves when background revalidation completes
|
|
34
|
+
* (only present if `revalidateOnFresh` or `staleWhileRevalidate` is enabled)
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const result = await loader.load<ThemeConfig>('config.json', options);
|
|
39
|
+
* console.log(result.data); // Theme configuration
|
|
40
|
+
* console.log(result.source); // 'cache' or 'network'
|
|
41
|
+
* if (result.revalidate) {
|
|
42
|
+
* await result.revalidate; // Wait for background refresh to complete
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export interface ResourceLoadResult<T> {
|
|
47
|
+
data: T;
|
|
48
|
+
source: ResourceSource;
|
|
49
|
+
cacheStatus: ResourceCacheStatus;
|
|
50
|
+
revalidate?: Promise<void>;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Default configuration options for the ResourceLoader.
|
|
54
|
+
* These values are applied globally to all load operations unless overridden per-request.
|
|
55
|
+
*
|
|
56
|
+
* @property cacheName - Namespace for cache entries (default: `'resilient-resource'`)
|
|
57
|
+
* @property indexedDBName - IndexedDB database name for this loader instance (default: `'resilient-resources'`)
|
|
58
|
+
* @property defaultCacheKey - Default cache key if not provided in load options
|
|
59
|
+
* @property storageOrder - Ordered list of cache layers to use (default: `['indexedDB', 'localStorage', 'memory']`)
|
|
60
|
+
* @property cacheTTL - Time-to-live for cache entries in milliseconds (default: 24 hours)
|
|
61
|
+
* @property staleWhileRevalidate - Return stale cache while fetching a fresh copy in background (default: `true`)
|
|
62
|
+
* @property useStaleOnError - Return stale cache if fetch fails (default: `true`)
|
|
63
|
+
* @property revalidateOnFresh - Even for fresh cache, fetch in background (default: `false`)
|
|
64
|
+
* @property retries - Number of retry attempts on network failure (default: 2, total attempts = retries + 1)
|
|
65
|
+
* @property retryDelayMs - Base delay between retries in milliseconds (default: 250)
|
|
66
|
+
* @property timeoutMs - Request timeout in milliseconds (default: 8000)
|
|
67
|
+
* @property parser - Custom parser function for any file type (default: JSON.parse). Receives Response object,
|
|
68
|
+
* supports JSON, XML, plain text, binary, or custom formats.
|
|
69
|
+
* Example: `(response) => response.json()` for JSON, `(response) => response.text()` for text
|
|
70
|
+
* @property seedCacheWithFallback - Populate caches with fallback data on first failure (default: `true`)
|
|
71
|
+
* @property logErrors - Log warnings and errors to console (default: `true`)
|
|
72
|
+
* @property freshOnNavigation - Skip cache on each load() call for hot-reload pattern. Always tries network first,
|
|
73
|
+
* falls back to cache if network fails, then to fallbackData if all else fails.
|
|
74
|
+
* Recommended default for most use cases (prevents stale data problems).
|
|
75
|
+
* (default: `true`)
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const defaults: ResourceLoaderDefaults = {
|
|
80
|
+
* cacheName: 'my-app-cache',
|
|
81
|
+
* cacheTTL: 1000 * 60 * 5, // 5 minutes
|
|
82
|
+
* staleWhileRevalidate: true,
|
|
83
|
+
* retries: 3,
|
|
84
|
+
* };
|
|
85
|
+
* const loader = new ResourceLoader(defaults);
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export interface ResourceLoaderDefaults {
|
|
89
|
+
cacheName?: string;
|
|
90
|
+
indexedDBName?: string;
|
|
91
|
+
defaultCacheKey?: string;
|
|
92
|
+
storageOrder?: CacheLayerName[];
|
|
93
|
+
cacheTTL?: number;
|
|
94
|
+
staleWhileRevalidate?: boolean;
|
|
95
|
+
useStaleOnError?: boolean;
|
|
96
|
+
revalidateOnFresh?: boolean;
|
|
97
|
+
retries?: number;
|
|
98
|
+
retryDelayMs?: number;
|
|
99
|
+
timeoutMs?: number;
|
|
100
|
+
parser?: (response: Response) => Promise<unknown>;
|
|
101
|
+
seedCacheWithFallback?: boolean;
|
|
102
|
+
logErrors?: boolean;
|
|
103
|
+
freshOnNavigation?: boolean;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Per-request load options, extending global defaults.
|
|
107
|
+
* Any option set here overrides the loader's default configuration for this specific load.
|
|
108
|
+
*
|
|
109
|
+
* @template T The expected data type
|
|
110
|
+
*
|
|
111
|
+
* @property cacheKey - Unique identifier for this resource (default: the URL itself)
|
|
112
|
+
* @property fallbackData - Data to return if all other methods fail (no cache, network error)
|
|
113
|
+
* @property parser - Custom parser supporting any file type (JSON, XML, text, binary, etc).
|
|
114
|
+
* Receives Response object. Examples:
|
|
115
|
+
* - JSON: `(response) => response.json()`
|
|
116
|
+
* - Text: `(response) => response.text()`
|
|
117
|
+
* - Custom: `(response) => customParse(response.arrayBuffer())`
|
|
118
|
+
* @property freshOnNavigation - Skip cache on each call (default: inherits from loader defaults which is now true).
|
|
119
|
+
* Set to false to enable aggressive caching for this specific resource.
|
|
120
|
+
* @property seedCacheWithFallback - Populate caches with fallback data if load fails
|
|
121
|
+
* @property logErrors - Log errors for this request
|
|
122
|
+
* @property ... - Inherits all other defaults from ResourceLoaderDefaults
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* // JSON loading with cache override
|
|
127
|
+
* const options: ResourceLoaderRequestOptions<ThemeConfig> = {
|
|
128
|
+
* cacheKey: 'themes/config',
|
|
129
|
+
* cacheTTL: 1000 * 60 * 2, // Override: 2 minutes instead of default 24h
|
|
130
|
+
* fallbackData: { themes: [], availableThemes: [] },
|
|
131
|
+
* parser: async (response) => (await response.json()) as ThemeConfig,
|
|
132
|
+
* };
|
|
133
|
+
* const result = await loader.load<ThemeConfig>('assets/themes/config.json', options);
|
|
134
|
+
*
|
|
135
|
+
* // XML loading with custom parser
|
|
136
|
+
* const xmlOptions: ResourceLoaderRequestOptions<XMLDocument> = {
|
|
137
|
+
* parser: async (response) => new DOMParser().parseFromString(
|
|
138
|
+
* await response.text(),
|
|
139
|
+
* 'application/xml'
|
|
140
|
+
* ),
|
|
141
|
+
* };
|
|
142
|
+
* const xmlResult = await loader.load<XMLDocument>('assets/data.xml', xmlOptions);
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
export interface ResourceLoaderRequestOptions<T> extends ResourceLoaderDefaults {
|
|
146
|
+
cacheKey?: string;
|
|
147
|
+
fallbackData?: T;
|
|
148
|
+
parser?: (response: Response) => Promise<T>;
|
|
149
|
+
freshOnNavigation?: boolean;
|
|
150
|
+
seedCacheWithFallback?: boolean;
|
|
151
|
+
logErrors?: boolean;
|
|
152
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { CacheEntry } from '../cache/types';
|
|
2
|
+
/**
|
|
3
|
+
* Pure utility class for cache validation logic.
|
|
4
|
+
* Determines whether a cache entry is fresh, stale, or expired.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* const entry = { value: data, timestamp: Date.now() - 1000, ttl: 10000 };
|
|
9
|
+
* if (CacheValidator.isFresh(entry)) {
|
|
10
|
+
* console.log('Cache is fresh');
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare class CacheValidator {
|
|
15
|
+
/**
|
|
16
|
+
* Check if a cache entry is within its TTL (fresh).
|
|
17
|
+
*
|
|
18
|
+
* @param entry - The cache entry to validate
|
|
19
|
+
* @returns true if the entry is fresh, false if expired
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* if (CacheValidator.isFresh(cacheEntry)) {
|
|
24
|
+
* return cacheEntry.value;
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
static isFresh(entry: CacheEntry<unknown>): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Determine if a cache entry should be considered stale (older than TTL but within revalidate window).
|
|
31
|
+
*
|
|
32
|
+
* @param entry - The cache entry to validate
|
|
33
|
+
* @returns true if the entry is stale, false otherwise
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* if (CacheValidator.isStale(cacheEntry)) {
|
|
38
|
+
* console.log('Cache is stale; revalidating...');
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
static isStale(entry: CacheEntry<unknown>): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Check if a cache entry is expired based on age (age > ttl).
|
|
45
|
+
* Expired entries should be removed during cleanup.
|
|
46
|
+
*
|
|
47
|
+
* @param entry - The cache entry to validate
|
|
48
|
+
* @returns true if the entry is expired, false otherwise
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* if (CacheValidator.isExpired(cacheEntry)) {
|
|
53
|
+
* await cacheLayer.remove(key);
|
|
54
|
+
* }
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
static isExpired(entry: CacheEntry<unknown>): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Get the remaining TTL for a cache entry in milliseconds.
|
|
60
|
+
* Negative values indicate the entry is already expired.
|
|
61
|
+
*
|
|
62
|
+
* @param entry - The cache entry to check
|
|
63
|
+
* @returns Milliseconds remaining until expiry (negative if expired)
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const remaining = CacheValidator.getTimeRemaining(entry);
|
|
68
|
+
* console.log(`Cache expires in ${remaining}ms`);
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
static getTimeRemaining(entry: CacheEntry<unknown>): number;
|
|
72
|
+
}
|