Compare commits

..

4 Commits

Author SHA1 Message Date
b21f462d35 Added clear function to cache
All checks were successful
Build / Build NPM Project (push) Successful in 37s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Successful in 35s
2024-10-14 21:07:59 -04:00
0f10aebfd2 bubble up all fetch errors in the http helper
All checks were successful
Build / Build NPM Project (push) Successful in 40s
Build / Tag Version (push) Successful in 8s
Build / Publish Documentation (push) Successful in 36s
2024-10-14 20:46:32 -04:00
1af23ac544 Fixed cache localstorage
All checks were successful
Build / Build NPM Project (push) Successful in 37s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Successful in 34s
2024-10-14 20:04:29 -04:00
494cfaaccd Added localStorage support to cache
All checks were successful
Build / Build NPM Project (push) Successful in 41s
Build / Tag Version (push) Successful in 8s
Build / Publish Documentation (push) Successful in 41s
2024-10-14 18:51:24 -04:00
5 changed files with 79 additions and 5802 deletions

5753
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.19.2", "version": "0.20.3",
"description": "Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",
@ -38,8 +38,5 @@
}, },
"files": [ "files": [
"dist" "dist"
], ]
"dependencies": {
"var-persist": "^1.0.1"
}
} }

View File

@ -1,3 +1,12 @@
export type CacheOptions = {
/** Delete keys automatically after x amount of seconds */
ttl?: number;
/** Storage to persist cache */
storage?: Storage;
/** Key cache will be stored under */
storageKey?: string;
}
/** /**
* Map of data which tracks whether it is a complete collection & offers optional expiry of cached values * Map of data which tracks whether it is a complete collection & offers optional expiry of cached values
*/ */
@ -13,9 +22,18 @@ export class Cache<K extends string | number | symbol, T> {
* Create new cache * Create new cache
* *
* @param {keyof T} key Default property to use as primary key * @param {keyof T} key Default property to use as primary key
* @param {number} ttl Default expiry in milliseconds * @param options
*/ */
constructor(public readonly key?: keyof T, public ttl?: number) { constructor(public readonly key?: keyof T, public readonly options: CacheOptions = {}) {
if(options.storageKey && !options.storage)
options.storage = localStorage;
if(options.storageKey && options.storage) {
const stored = options.storage.getItem(options.storageKey);
if(stored) {
try { Object.assign(this.store, JSON.parse(stored)); }
catch { }
}
}
return new Proxy(this, { return new Proxy(this, {
get: (target: this, prop: string | symbol) => { get: (target: this, prop: string | symbol) => {
if (prop in target) return (target as any)[prop]; if (prop in target) return (target as any)[prop];
@ -69,6 +87,13 @@ export class Cache<K extends string | number | symbol, T> {
return this; return this;
} }
/**
* Remove all keys from cache
*/
clear() {
this.store = <Record<K, T>>{};
}
/** /**
* Delete an item from the cache * Delete an item from the cache
* *
@ -76,6 +101,8 @@ export class Cache<K extends string | number | symbol, T> {
*/ */
delete(key: K) { delete(key: K) {
delete this.store[key]; delete this.store[key];
if(this.options.storageKey && this.options.storage)
this.options.storage.setItem(this.options.storageKey, JSON.stringify(this.store));
} }
/** /**
@ -118,15 +145,17 @@ export class Cache<K extends string | number | symbol, T> {
* *
* @param {K} key Key item will be cached under * @param {K} key Key item will be cached under
* @param {T} value Item to cache * @param {T} value Item to cache
* @param {number | undefined} ttl Override default expiry * @param {number | undefined} ttl Override default expiry in seconds
* @return {this} * @return {this}
*/ */
set(key: K, value: T, ttl = this.ttl): this { set(key: K, value: T, ttl = this.options.ttl): this {
this.store[key] = value; this.store[key] = value;
if(this.options.storageKey && this.options.storage)
this.options.storage.setItem(this.options.storageKey, JSON.stringify(this.store));
if(ttl) setTimeout(() => { if(ttl) setTimeout(() => {
this.complete = false; this.complete = false;
this.delete(key); this.delete(key);
}, ttl); }, ttl * 1000);
return this; return this;
} }

View File

@ -75,6 +75,7 @@ export class Http {
// Send request // Send request
return new PromiseProgress((res, rej, prog) => { return new PromiseProgress((res, rej, prog) => {
try {
fetch(url, { fetch(url, {
headers, headers,
method: opts.method || (opts.body ? 'POST' : 'GET'), method: opts.method || (opts.body ? 'POST' : 'GET'),
@ -100,6 +101,7 @@ export class Http {
push(); push();
}).catch((error: any) => controller.error(error)); }).catch((error: any) => controller.error(error));
} }
push(); push();
} }
}); });
@ -115,7 +117,10 @@ export class Http {
if(resp.ok) res(resp); if(resp.ok) res(resp);
else rej(resp); else rej(resp);
}) }).catch(err => rej(err));
} catch(err) {
rej(err);
}
}); });
} }
} }

View File

@ -16,4 +16,3 @@ export * from './promise-progress';
export * from './string'; export * from './string';
export * from './time'; export * from './time';
export * from './types'; export * from './types';
export * from 'var-persist';