@koloseum/utils 0.2.16 → 0.2.18
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/dist/utils.d.ts +40 -0
- package/dist/utils.js +73 -0
- package/package.json +10 -10
package/dist/utils.d.ts
CHANGED
|
@@ -289,3 +289,43 @@ export declare const Utility: {
|
|
|
289
289
|
*/
|
|
290
290
|
validateSocialMediaHandle: (handle: string) => boolean;
|
|
291
291
|
};
|
|
292
|
+
export declare const Cache: {
|
|
293
|
+
/**
|
|
294
|
+
* Delete cached data from Valkey.
|
|
295
|
+
* @param valkey - The Valkey client instance
|
|
296
|
+
* @param cachePrefix - The cache prefix
|
|
297
|
+
* @param key - The cache key
|
|
298
|
+
*/
|
|
299
|
+
deleteData: (valkey: any, cachePrefix: string, key: string) => Promise<void>;
|
|
300
|
+
/**
|
|
301
|
+
* Generate cache key with consistent formatting
|
|
302
|
+
* @param prefix - The key prefix
|
|
303
|
+
* @param params - Additional parameters to include in the key
|
|
304
|
+
* @returns The formatted cache key
|
|
305
|
+
*/
|
|
306
|
+
generateDataKey: (prefix: string, ...params: (string | number)[]) => string;
|
|
307
|
+
/**
|
|
308
|
+
* Get cached data from Valkey.
|
|
309
|
+
* @param valkey - The Valkey client instance
|
|
310
|
+
* @param cachePrefix - The cache prefix
|
|
311
|
+
* @param key - The cache key
|
|
312
|
+
* @returns The cached data, or `null` if not found
|
|
313
|
+
*/
|
|
314
|
+
getData: <T>(valkey: any, cachePrefix: string, key: string) => Promise<T | null>;
|
|
315
|
+
/**
|
|
316
|
+
* Invalidate cache by pattern
|
|
317
|
+
* @param valkey - The Valkey client instance
|
|
318
|
+
* @param cachePrefix - The cache prefix
|
|
319
|
+
* @param pattern - The pattern to match keys against (e.g., "app-config*")
|
|
320
|
+
*/
|
|
321
|
+
invalidateDataByPattern: (valkey: any, cachePrefix: string, pattern: string) => Promise<void>;
|
|
322
|
+
/**
|
|
323
|
+
* Set data in Valkey cache.
|
|
324
|
+
* @param valkey - The Valkey client instance
|
|
325
|
+
* @param cachePrefix - The cache prefix
|
|
326
|
+
* @param key - The cache key
|
|
327
|
+
* @param data - The data to cache
|
|
328
|
+
* @param ttl - The time to live in seconds; defaults to 1 hour
|
|
329
|
+
*/
|
|
330
|
+
setData: <T>(valkey: any, cachePrefix: string, key: string, data: T, ttl?: number) => Promise<void>;
|
|
331
|
+
};
|
package/dist/utils.js
CHANGED
|
@@ -1507,3 +1507,76 @@ export const Utility = {
|
|
|
1507
1507
|
!handle.startsWith("@") &&
|
|
1508
1508
|
Boolean(handle.match(Utility.socialMediaHandleRegex))
|
|
1509
1509
|
};
|
|
1510
|
+
/* CACHE FUNCTIONS */
|
|
1511
|
+
export const Cache = {
|
|
1512
|
+
/**
|
|
1513
|
+
* Delete cached data from Valkey.
|
|
1514
|
+
* @param valkey - The Valkey client instance
|
|
1515
|
+
* @param cachePrefix - The cache prefix
|
|
1516
|
+
* @param key - The cache key
|
|
1517
|
+
*/
|
|
1518
|
+
deleteData: async (valkey, cachePrefix, key) => {
|
|
1519
|
+
try {
|
|
1520
|
+
await valkey.del(`${cachePrefix}${key}`);
|
|
1521
|
+
}
|
|
1522
|
+
catch (error) {
|
|
1523
|
+
console.error("Cache delete error:", error);
|
|
1524
|
+
}
|
|
1525
|
+
},
|
|
1526
|
+
/**
|
|
1527
|
+
* Generate cache key with consistent formatting
|
|
1528
|
+
* @param prefix - The key prefix
|
|
1529
|
+
* @param params - Additional parameters to include in the key
|
|
1530
|
+
* @returns The formatted cache key
|
|
1531
|
+
*/
|
|
1532
|
+
generateDataKey: (prefix, ...params) => `${prefix}:${params.join(":")}`,
|
|
1533
|
+
/**
|
|
1534
|
+
* Get cached data from Valkey.
|
|
1535
|
+
* @param valkey - The Valkey client instance
|
|
1536
|
+
* @param cachePrefix - The cache prefix
|
|
1537
|
+
* @param key - The cache key
|
|
1538
|
+
* @returns The cached data, or `null` if not found
|
|
1539
|
+
*/
|
|
1540
|
+
getData: async (valkey, cachePrefix, key) => {
|
|
1541
|
+
try {
|
|
1542
|
+
const cached = await valkey.get(`${cachePrefix}${key}`);
|
|
1543
|
+
return cached ? JSON.parse(cached) : null;
|
|
1544
|
+
}
|
|
1545
|
+
catch (error) {
|
|
1546
|
+
console.error("Cache get error:", error);
|
|
1547
|
+
return null;
|
|
1548
|
+
}
|
|
1549
|
+
},
|
|
1550
|
+
/**
|
|
1551
|
+
* Invalidate cache by pattern
|
|
1552
|
+
* @param valkey - The Valkey client instance
|
|
1553
|
+
* @param cachePrefix - The cache prefix
|
|
1554
|
+
* @param pattern - The pattern to match keys against (e.g., "app-config*")
|
|
1555
|
+
*/
|
|
1556
|
+
invalidateDataByPattern: async (valkey, cachePrefix, pattern) => {
|
|
1557
|
+
try {
|
|
1558
|
+
const keys = await valkey.keys(`${cachePrefix}${pattern}`);
|
|
1559
|
+
if (keys.length > 0)
|
|
1560
|
+
await valkey.del(...keys);
|
|
1561
|
+
}
|
|
1562
|
+
catch (error) {
|
|
1563
|
+
console.error("Cache pattern invalidation error:", error);
|
|
1564
|
+
}
|
|
1565
|
+
},
|
|
1566
|
+
/**
|
|
1567
|
+
* Set data in Valkey cache.
|
|
1568
|
+
* @param valkey - The Valkey client instance
|
|
1569
|
+
* @param cachePrefix - The cache prefix
|
|
1570
|
+
* @param key - The cache key
|
|
1571
|
+
* @param data - The data to cache
|
|
1572
|
+
* @param ttl - The time to live in seconds; defaults to 1 hour
|
|
1573
|
+
*/
|
|
1574
|
+
setData: async (valkey, cachePrefix, key, data, ttl = 3600) => {
|
|
1575
|
+
try {
|
|
1576
|
+
await valkey.setex(`${cachePrefix}${key}`, ttl, JSON.stringify(data));
|
|
1577
|
+
}
|
|
1578
|
+
catch (error) {
|
|
1579
|
+
console.error("Cache set error:", error);
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koloseum/utils",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.18",
|
|
4
4
|
"author": "Koloseum Technologies Limited",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Utility logic for use across Koloseum web apps (TypeScript)",
|
|
@@ -29,23 +29,23 @@
|
|
|
29
29
|
"test": "vitest --run"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@supabase/supabase-js": "^2.
|
|
33
|
-
"@sveltejs/kit": "^2.
|
|
32
|
+
"@supabase/supabase-js": "^2.57.4",
|
|
33
|
+
"@sveltejs/kit": "^2.41.0",
|
|
34
34
|
"kenya-administrative-divisions": "^0.0.18",
|
|
35
35
|
"postgres-interval": "^4.0.2",
|
|
36
36
|
"sanitize-html": "^2.17.0",
|
|
37
|
-
"uuid": "^
|
|
37
|
+
"uuid": "^13.0.0",
|
|
38
38
|
"validator": "^13.15.15"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@koloseum/types": "^0.2.
|
|
42
|
-
"@playwright/test": "^1.
|
|
43
|
-
"@suprsend/web-components": "^0.
|
|
41
|
+
"@koloseum/types": "^0.2.6",
|
|
42
|
+
"@playwright/test": "^1.55.0",
|
|
43
|
+
"@suprsend/web-components": "^0.4.0",
|
|
44
44
|
"@types/sanitize-html": "^2.16.0",
|
|
45
45
|
"@types/uuid": "^10.0.0",
|
|
46
|
-
"@types/validator": "^13.15.
|
|
47
|
-
"prettier": "^3.6.
|
|
48
|
-
"typescript": "^5.
|
|
46
|
+
"@types/validator": "^13.15.3",
|
|
47
|
+
"prettier": "^3.6.2",
|
|
48
|
+
"typescript": "^5.9.2",
|
|
49
49
|
"vitest": "^3.2.4"
|
|
50
50
|
}
|
|
51
51
|
}
|