@proveanything/smartlinks 1.3.6 → 1.3.9

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.
@@ -1,4 +1,4 @@
1
- import { CollectionResponse, CollectionCreateRequest, CollectionUpdateRequest } from "../types/collection";
1
+ import { CollectionResponse, CollectionCreateRequest, CollectionUpdateRequest, AppsConfigResponse } from "../types/collection";
2
2
  export declare namespace collection {
3
3
  /**
4
4
  * Retrieves a single Collection by its ID.
@@ -28,6 +28,13 @@ export declare namespace collection {
28
28
  * @returns Promise resolving to the settings object
29
29
  */
30
30
  function getSettings(collectionId: string, settingGroup: string, admin?: boolean): Promise<any>;
31
+ /**
32
+ * Retrieve all configured app module definitions for a collection (public endpoint).
33
+ * @param collectionId – Identifier of the collection
34
+ * @returns Promise resolving to an AppsConfigResponse containing all app configurations
35
+ * @throws ErrorResponse if the request fails
36
+ */
37
+ function getAppsConfig(collectionId: string): Promise<AppsConfigResponse>;
31
38
  /**
32
39
  * Update a specific settings group for a collection (admin endpoint).
33
40
  * @param collectionId – Identifier of the collection
@@ -49,6 +49,17 @@ export var collection;
49
49
  return request(path);
50
50
  }
51
51
  collection.getSettings = getSettings;
52
+ /**
53
+ * Retrieve all configured app module definitions for a collection (public endpoint).
54
+ * @param collectionId – Identifier of the collection
55
+ * @returns Promise resolving to an AppsConfigResponse containing all app configurations
56
+ * @throws ErrorResponse if the request fails
57
+ */
58
+ async function getAppsConfig(collectionId) {
59
+ const path = `/public/collection/${encodeURIComponent(collectionId)}/apps-config`;
60
+ return request(path);
61
+ }
62
+ collection.getAppsConfig = getAppsConfig;
52
63
  /**
53
64
  * Update a specific settings group for a collection (admin endpoint).
54
65
  * @param collectionId – Identifier of the collection
@@ -0,0 +1,32 @@
1
+ export interface CacheOptions {
2
+ /** TTL in milliseconds (default: 5 minutes) */
3
+ ttl?: number;
4
+ /** Storage backend */
5
+ storage?: 'memory' | 'session' | 'local';
6
+ }
7
+ /**
8
+ * Get cached value or fetch fresh.
9
+ *
10
+ * @param key - Cache key
11
+ * @param fetcher - Async function to fetch fresh data
12
+ * @param options - Cache options
13
+ * @returns Cached or fresh value
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const apps = await cache.getOrFetch(
18
+ * `apps:${collectionId}`,
19
+ * () => collection.getApps(collectionId),
20
+ * { ttl: 5 * 60 * 1000, storage: 'session' }
21
+ * );
22
+ * ```
23
+ */
24
+ export declare function getOrFetch<T>(key: string, fetcher: () => Promise<T>, options?: CacheOptions): Promise<T>;
25
+ /**
26
+ * Invalidate a cached key.
27
+ */
28
+ export declare function invalidate(key: string): void;
29
+ /**
30
+ * Clear all cached data.
31
+ */
32
+ export declare function clear(): void;
package/dist/cache.js ADDED
@@ -0,0 +1,125 @@
1
+ // =============================================================================
2
+ // Cache Utilities - TTL-based caching for browser and Node environments
3
+ // =============================================================================
4
+ // In-memory cache store
5
+ const memoryCache = new Map();
6
+ /**
7
+ * Get cached value or fetch fresh.
8
+ *
9
+ * @param key - Cache key
10
+ * @param fetcher - Async function to fetch fresh data
11
+ * @param options - Cache options
12
+ * @returns Cached or fresh value
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const apps = await cache.getOrFetch(
17
+ * `apps:${collectionId}`,
18
+ * () => collection.getApps(collectionId),
19
+ * { ttl: 5 * 60 * 1000, storage: 'session' }
20
+ * );
21
+ * ```
22
+ */
23
+ export async function getOrFetch(key, fetcher, options = {}) {
24
+ const { ttl = 5 * 60 * 1000, storage = 'memory' } = options;
25
+ const now = Date.now();
26
+ // Try to get from cache
27
+ const cached = getFromStorage(key, storage);
28
+ if (cached && cached.expiresAt > now) {
29
+ return cached.value;
30
+ }
31
+ // Fetch fresh data
32
+ const value = await fetcher();
33
+ // Store in cache
34
+ const entry = {
35
+ value,
36
+ expiresAt: now + ttl,
37
+ };
38
+ setToStorage(key, entry, storage);
39
+ return value;
40
+ }
41
+ /**
42
+ * Invalidate a cached key.
43
+ */
44
+ export function invalidate(key) {
45
+ // Remove from all storage backends
46
+ memoryCache.delete(key);
47
+ try {
48
+ if (typeof sessionStorage !== 'undefined') {
49
+ sessionStorage.removeItem(`smartlinks:cache:${key}`);
50
+ }
51
+ if (typeof localStorage !== 'undefined') {
52
+ localStorage.removeItem(`smartlinks:cache:${key}`);
53
+ }
54
+ }
55
+ catch (_a) {
56
+ // Storage may not be available
57
+ }
58
+ }
59
+ /**
60
+ * Clear all cached data.
61
+ */
62
+ export function clear() {
63
+ memoryCache.clear();
64
+ try {
65
+ if (typeof sessionStorage !== 'undefined') {
66
+ // Clear only smartlinks cache keys
67
+ const sessionKeys = Object.keys(sessionStorage).filter(k => k.startsWith('smartlinks:cache:'));
68
+ sessionKeys.forEach(k => sessionStorage.removeItem(k));
69
+ }
70
+ if (typeof localStorage !== 'undefined') {
71
+ const localKeys = Object.keys(localStorage).filter(k => k.startsWith('smartlinks:cache:'));
72
+ localKeys.forEach(k => localStorage.removeItem(k));
73
+ }
74
+ }
75
+ catch (_a) {
76
+ // Storage may not be available
77
+ }
78
+ }
79
+ // =============================================================================
80
+ // Internal Helpers
81
+ // =============================================================================
82
+ function getFromStorage(key, storage) {
83
+ try {
84
+ if (storage === 'memory') {
85
+ return memoryCache.get(key) || null;
86
+ }
87
+ const storageKey = `smartlinks:cache:${key}`;
88
+ let stored = null;
89
+ if (storage === 'session' && typeof sessionStorage !== 'undefined') {
90
+ stored = sessionStorage.getItem(storageKey);
91
+ }
92
+ else if (storage === 'local' && typeof localStorage !== 'undefined') {
93
+ stored = localStorage.getItem(storageKey);
94
+ }
95
+ if (stored) {
96
+ return JSON.parse(stored);
97
+ }
98
+ }
99
+ catch (_a) {
100
+ // Storage errors - return null
101
+ }
102
+ return null;
103
+ }
104
+ function setToStorage(key, entry, storage) {
105
+ try {
106
+ if (storage === 'memory') {
107
+ memoryCache.set(key, entry);
108
+ return;
109
+ }
110
+ const storageKey = `smartlinks:cache:${key}`;
111
+ const serialized = JSON.stringify(entry);
112
+ if (storage === 'session' && typeof sessionStorage !== 'undefined') {
113
+ sessionStorage.setItem(storageKey, serialized);
114
+ }
115
+ else if (storage === 'local' && typeof localStorage !== 'undefined') {
116
+ localStorage.setItem(storageKey, serialized);
117
+ }
118
+ // Also keep in memory for faster access
119
+ memoryCache.set(key, entry);
120
+ }
121
+ catch (_a) {
122
+ // Storage quota exceeded or not available - keep in memory only
123
+ memoryCache.set(key, entry);
124
+ }
125
+ }