diff --git a/src/storages/cookie-storage.ts b/src/storages/cookie-storage.ts index 8b07ae5..14a8bdc 100644 --- a/src/storages/cookie-storage.ts +++ b/src/storages/cookie-storage.ts @@ -1,6 +1,6 @@ import type { IStorage } from '../types'; -interface ICookiesStorageOptions { +interface ICookiesStorageAttributes { expires?: number | Date | undefined; path?: string | undefined; domain?: string | undefined; @@ -11,15 +11,24 @@ interface ICookiesStorageOptions { interface ICookieStorage { get: (key: string) => string | null | undefined; - set: (key: string, value: string, options: ICookiesStorageOptions) => any; - remove: (key: string, options: ICookiesStorageOptions) => any; + set: (key: string, value: string, options: ICookiesStorageAttributes) => any; + remove: (key: string, options: ICookiesStorageAttributes) => any; +} + +interface ICookiesStorageOptions { + storage: ICookieStorage; + globalKey?: string; + cookieAttr?: ICookiesStorageAttributes; } /** * Cookie storage for mobx store manager */ class CookieStorage implements IStorage { - globalKey = 'stores'; + /** + * Cookie storage key + */ + protected globalKey: string; /** * @protected @@ -27,16 +36,17 @@ class CookieStorage implements IStorage { protected storage: ICookieStorage; /** - * Cookie options + * Cookie attributes */ - protected options: ICookiesStorageOptions; + protected cookieAttr: ICookiesStorageAttributes; /** * @constructor */ - constructor(storage: ICookieStorage, options: ICookiesStorageOptions = {}) { + constructor({ storage, cookieAttr, globalKey }: ICookiesStorageOptions) { this.storage = storage; - this.options = options; + this.cookieAttr = cookieAttr ?? {}; + this.globalKey = globalKey ?? 'stores'; } /** @@ -54,14 +64,14 @@ class CookieStorage implements IStorage { * @inheritDoc */ public flush(): void | Promise { - return this.storage.remove(this.globalKey, this.options); + return this.storage.remove(this.globalKey, this.cookieAttr); } /** * @inheritDoc */ public set(value: Record | undefined): void { - return this.storage.set(this.globalKey, JSON.stringify(value || '{}'), this.options); + return this.storage.set(this.globalKey, JSON.stringify(value || '{}'), this.cookieAttr); } } diff --git a/src/storages/local-storage.ts b/src/storages/local-storage.ts index 251be0a..ae924d2 100644 --- a/src/storages/local-storage.ts +++ b/src/storages/local-storage.ts @@ -1,10 +1,18 @@ import type { IStorage } from '../types'; +interface ILocalStorageOptions { + globalKey?: string; + storage?: Storage; +} + /** * Local storage for mobx store manager */ class LocalStorage implements IStorage { - globalKey = 'stores'; + /** + * Local storage key + */ + protected globalKey: string; /** * @protected @@ -14,8 +22,9 @@ class LocalStorage implements IStorage { /** * @constructor */ - constructor(storage: Storage = localStorage) { - this.storage = storage; + constructor({ storage, globalKey }: ILocalStorageOptions = {}) { + this.storage = storage ?? localStorage; + this.globalKey = globalKey ?? 'stores'; } /**