Compare commits

...

8 Commits

Author SHA1 Message Date
b103b6f786 Cache localstorage fix
All checks were successful
Build / Build NPM Project (push) Successful in 38s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Successful in 34s
2024-10-15 16:35:11 -04:00
3b486310de Deleting lock file, seems to mess up build
All checks were successful
Build / Build NPM Project (push) Successful in 1m4s
Build / Tag Version (push) Successful in 11s
Build / Publish Documentation (push) Successful in 46s
2024-10-15 13:51:00 -04:00
8699fb49ff New lock file
Some checks failed
Build / Build NPM Project (push) Failing after 18s
Build / Tag Version (push) Has been skipped
Build / Publish Documentation (push) Has been skipped
2024-10-15 13:49:52 -04:00
fdb29e7984 New lock file
Some checks failed
Build / Tag Version (push) Blocked by required conditions
Build / Publish Documentation (push) Blocked by required conditions
Build / Build NPM Project (push) Has been cancelled
2024-10-15 13:49:17 -04:00
274c22bb83 Fixed localStorage access on node environments
Some checks failed
Build / Build NPM Project (push) Failing after 15s
Build / Tag Version (push) Has been skipped
Build / Publish Documentation (push) Has been skipped
2024-10-15 13:46:11 -04:00
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
3 changed files with 55 additions and 43 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.20.0", "version": "0.20.5",
"description": "Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@ -25,8 +25,8 @@ export class Cache<K extends string | number | symbol, T> {
* @param options * @param options
*/ */
constructor(public readonly key?: keyof T, public readonly options: CacheOptions = {}) { constructor(public readonly key?: keyof T, public readonly options: CacheOptions = {}) {
if(options.storageKey && !options.storage) if(options.storageKey && !options.storage && window?.localStorage)
options.storage = localStorage; options.storage = window.localStorage;
if(options.storageKey && options.storage) { if(options.storageKey && options.storage) {
const stored = options.storage.getItem(options.storageKey); const stored = options.storage.getItem(options.storageKey);
if(stored) { if(stored) {
@ -87,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
* *
@ -95,7 +102,7 @@ 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) if(this.options.storageKey && this.options.storage)
this.options.storage.setItem(this.options.storageKey, JSON.stringify(this.cache)); this.options.storage.setItem(this.options.storageKey, JSON.stringify(this.store));
} }
/** /**
@ -144,7 +151,7 @@ export class Cache<K extends string | number | symbol, T> {
set(key: K, value: T, ttl = this.options.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) if(this.options.storageKey && this.options.storage)
this.options.storage.setItem(this.options.storageKey, JSON.stringify(this.cache)); 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);

View File

@ -75,47 +75,52 @@ export class Http {
// Send request // Send request
return new PromiseProgress((res, rej, prog) => { return new PromiseProgress((res, rej, prog) => {
fetch(url, { try {
headers, fetch(url, {
method: opts.method || (opts.body ? 'POST' : 'GET'), headers,
body: opts.body method: opts.method || (opts.body ? 'POST' : 'GET'),
}).then(async (resp: any) => { body: opts.body
for(let fn of [...Object.values(Http.interceptors), ...Object.values(this.interceptors)]) { }).then(async (resp: any) => {
await new Promise<void>(res => fn(resp, () => res())); for(let fn of [...Object.values(Http.interceptors), ...Object.values(this.interceptors)]) {
} await new Promise<void>(res => fn(resp, () => res()));
const contentLength = resp.headers.get('Content-Length');
const total = contentLength ? parseInt(contentLength, 10) : 0;
let loaded = 0;
const reader = resp.body?.getReader();
const stream = new ReadableStream({
start(controller) {
function push() {
reader?.read().then((event: any) => {
if(event.done) return controller.close();
loaded += event.value.byteLength;
prog(loaded / total);
controller.enqueue(event.value);
push();
}).catch((error: any) => controller.error(error));
}
push();
} }
});
resp.data = new Response(stream); const contentLength = resp.headers.get('Content-Length');
if(opts.decode == null || opts.decode) { const total = contentLength ? parseInt(contentLength, 10) : 0;
const content = resp.headers.get('Content-Type')?.toLowerCase(); let loaded = 0;
if(content?.includes('form')) resp.data = <T>await resp.data.formData();
else if(content?.includes('json')) resp.data = <T>await resp.data.json();
else if(content?.includes('text')) resp.data = <T>await resp.data.text();
else if(content?.includes('application')) resp.data = <T>await resp.data.blob();
}
if(resp.ok) res(resp); const reader = resp.body?.getReader();
else rej(resp); const stream = new ReadableStream({
}) start(controller) {
function push() {
reader?.read().then((event: any) => {
if(event.done) return controller.close();
loaded += event.value.byteLength;
prog(loaded / total);
controller.enqueue(event.value);
push();
}).catch((error: any) => controller.error(error));
}
push();
}
});
resp.data = new Response(stream);
if(opts.decode == null || opts.decode) {
const content = resp.headers.get('Content-Type')?.toLowerCase();
if(content?.includes('form')) resp.data = <T>await resp.data.formData();
else if(content?.includes('json')) resp.data = <T>await resp.data.json();
else if(content?.includes('text')) resp.data = <T>await resp.data.text();
else if(content?.includes('application')) resp.data = <T>await resp.data.blob();
}
if(resp.ok) res(resp);
else rej(resp);
}).catch(err => rej(err));
} catch(err) {
rej(err);
}
}); });
} }
} }