Skip to content

Commit

Permalink
fix: add storages options, allow change storage key
Browse files Browse the repository at this point in the history
BREAKING CHANGE: changed storages options param
  • Loading branch information
MatthewPattell committed Aug 21, 2024
1 parent d3b3cc3 commit 0209089
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
30 changes: 20 additions & 10 deletions src/storages/cookie-storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IStorage } from '../types';

interface ICookiesStorageOptions {
interface ICookiesStorageAttributes {
expires?: number | Date | undefined;
path?: string | undefined;
domain?: string | undefined;
Expand All @@ -11,32 +11,42 @@ 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
*/
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';
}

/**
Expand All @@ -54,14 +64,14 @@ class CookieStorage implements IStorage {
* @inheritDoc
*/
public flush(): void | Promise<any> {
return this.storage.remove(this.globalKey, this.options);
return this.storage.remove(this.globalKey, this.cookieAttr);
}

/**
* @inheritDoc
*/
public set(value: Record<string, any> | undefined): void {
return this.storage.set(this.globalKey, JSON.stringify(value || '{}'), this.options);
return this.storage.set(this.globalKey, JSON.stringify(value || '{}'), this.cookieAttr);
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/storages/local-storage.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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';
}

/**
Expand Down

0 comments on commit 0209089

Please sign in to comment.