@claspo/common 7.0.2 → 7.0.3-rc1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@claspo/common",
3
- "version": "7.0.2",
3
+ "version": "7.0.3-rc1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "engines": {
@@ -0,0 +1,13 @@
1
+ interface CacheEntry {
2
+ value: string | null;
3
+ expiration: number;
4
+ }
5
+ export default abstract class BaseCachingStorage {
6
+ cache: Record<string, CacheEntry>;
7
+ cacheTimeMs: number;
8
+ protected _process(operationCallback: () => string | null): string | null;
9
+ protected _getFromCache(key: string): string | null;
10
+ protected _setToCache(key: string, value: string | null): void;
11
+ protected _removeFromCache(key: string): void;
12
+ }
13
+ export {};
@@ -0,0 +1 @@
1
+ export default class BaseCachingStorage{constructor(){this.cache={},this.cacheTimeMs=2e3}_process(e){try{return e()}catch(e){return null}}_getFromCache(e){const t=Date.now(),c=this.cache[e];return c&&t<c.expiration?c.value:null}_setToCache(e,t){this.cache[e]={value:t,expiration:Date.now()+this.cacheTimeMs}}_removeFromCache(e){delete this.cache[e]}}
@@ -0,0 +1,13 @@
1
+ import BaseCachingStorage from './BaseCachingStorage';
2
+ interface CookieOptionsI {
3
+ path?: string;
4
+ expires?: Date | string;
5
+ [key: string]: string | Date | boolean | undefined;
6
+ }
7
+ declare class CachingCookieStorage extends BaseCachingStorage {
8
+ get(name: string): string | null;
9
+ set(name: string, value: string, options?: CookieOptionsI): void;
10
+ remove(name: string): void;
11
+ }
12
+ declare const _default: CachingCookieStorage;
13
+ export default _default;
@@ -0,0 +1 @@
1
+ import BaseCachingStorage from"./BaseCachingStorage";class CachingCookieStorage extends BaseCachingStorage{get(e){const o=this._getFromCache(e);return null!==o?o:this._process(()=>{let o=document.cookie.match(new RegExp("(?:^|; )"+e.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,"\\$1")+"=([^;]*)"));const t=o?decodeURIComponent(o[1]):null;return this._setToCache(e,t),t})}set(e,o,t={}){const n=Object.assign({path:"/",expires:new Date((new Date).getFullYear()+5,0)},t);n.expires instanceof Date&&(n.expires=n.expires.toUTCString());let s=encodeURIComponent(e)+"="+encodeURIComponent(o);for(const e in n){s+="; "+e;const o=n[e];!0!==o&&(s+="="+o)}const c="string"==typeof o?o:this._process(()=>JSON.stringify(o));this._setToCache(e,c),this._process(()=>(document.cookie=s,null))}remove(e){this._removeFromCache(e),this._process(()=>(document.cookie=encodeURIComponent(e)+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/",null))}}export default new CachingCookieStorage;
@@ -0,0 +1,8 @@
1
+ import BaseCachingStorage from './BaseCachingStorage';
2
+ declare class CachingLocalStorage extends BaseCachingStorage {
3
+ get(key: string): string | null;
4
+ set(key: string, value: string | object): void;
5
+ remove(key: string): void;
6
+ }
7
+ declare const _default: CachingLocalStorage;
8
+ export default _default;
@@ -0,0 +1 @@
1
+ import BaseCachingStorage from"./BaseCachingStorage";class CachingLocalStorage extends BaseCachingStorage{get(e){const t=this._getFromCache(e);return null!==t?t:this._process(()=>{const t=localStorage.getItem(e);return this._setToCache(e,t),t})}set(e,t){const o="string"==typeof t?t:this._process(()=>JSON.stringify(t));this._setToCache(e,o),this._process(()=>(localStorage.setItem(e,o),null))}remove(e){this._removeFromCache(e),this._process(()=>(localStorage.removeItem(e),null))}}export default new CachingLocalStorage;