diff --git a/README.md b/README.md index 539cb69..679ad3a 100644 --- a/README.md +++ b/README.md @@ -196,8 +196,8 @@ API reference 2. **rememberedKeys** *(required)* - an array of persistable keys - if an empty array is provided nothing will get persisted; 3. **options** *(optional)* - plain object of extra options: - **prefix**: storage key prefix *(default: `'@@remember-'`)*; - - **serialize** - a plain function that takes unserialized store state and its key (`serialize(state, keyStr)`) and returns serialized state to be persisted *(default: `JSON.stringify`)*; - - **unserialize** - a plain function that takes serialized persisted state and its key (`serialize(serializedStr, keyStr)`) and returns unserialized to be set in the store *(default: `JSON.parse`)*; + - **serialize** - a plain function that takes unserialized store state and its key (`serialize(state, stateKey)`) and returns serialized state to be persisted *(default: `JSON.stringify`)*; + - **unserialize** - a plain function that takes serialized persisted state and its key (`serialize(state, stateKey)`) and returns unserialized to be set in the store *(default: `JSON.parse`)*; - **persistThrottle** - how much time should the persistence be throttled in milliseconds *(default: `100`)* - **persistWholeStore** - a boolean which specifies if the whole store should be persisted at once. Generally only use this if you're using your own storage driver which has gigabytes of storage limits. Don't use this when using window.localStorage, window.sessionStorage or AsyncStorage as their limits are quite small. When using this option, key won't be passed to `serialize` nor `unserialize` functions - *(default: `false`)*; - Returns - an enhancer to be used with Redux diff --git a/index.d.ts b/index.d.ts index 823607c..d39bc19 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,8 +1,8 @@ import { Action, AnyAction, Reducer, StoreEnhancer } from 'redux'; declare const REMEMBER_REHYDRATED = "@@REMEMBER_REHYDRATED"; declare const REMEMBER_PERSISTED = "@@REMEMBER_PERSISTED"; -type SerializeFunction = (data: any, key?: string) => any; -type UnserializeFunction = (data: any, key?: string) => any; +type SerializeFunction = (data: any, key: string) => any; +type UnserializeFunction = (data: any, key: string) => any; type Driver = { getItem: (key: string) => any; setItem: (key: string, value: any) => any; diff --git a/package-lock.json b/package-lock.json index f86b9cf..e163879 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "redux-remember", - "version": "2.1.4", + "version": "2.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 48d4064..d981c27 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "redux-remember", - "version": "2.1.4", + "version": "2.2.0", "description": "Saves and loads your redux state from a key-value store of your choice", "main": "lib/index.js", "module": "es/index.js", diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index d747dbc..1e0a14e 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -1,7 +1,7 @@ import * as indexModule from '../index'; import * as actionTypes from '../action-types'; -import { AnyAction, Reducer, StoreEnhancer } from 'redux'; -import { Driver, Options } from '../types'; +import { Reducer, StoreCreator } from 'redux'; +import { Options } from '../types'; describe('index.js', () => { let mockRehydrate = { @@ -111,8 +111,7 @@ describe('index.js', () => { it('calls createStore function and returns its result', () => { const store = 'dummy store!!!'; - const createStore = jest.fn(() => store); - + const createStore = jest.fn(() => store) as StoreCreator; const rootReducer = () => 'beep'; const initialState = 'dummyInitialState'; const enhancer: any = 'dummyEnhancer'; @@ -125,7 +124,8 @@ describe('index.js', () => { [] ); - const res = enhancerInstance(createStore)( + const storeMaker: StoreCreator = enhancerInstance(createStore); + const res = storeMaker( rootReducer, initialState, enhancer ); @@ -158,7 +158,11 @@ describe('index.js', () => { unserialize: (o) => o }; - index.rememberEnhancer(driver, rememberedKeys, opts)(() => store)( + const storeMaker: StoreCreator = index.rememberEnhancer( + driver, rememberedKeys, opts + )((() => store) as StoreCreator); + + storeMaker( rootReducer, initialState, enhancer ); @@ -174,7 +178,7 @@ describe('index.js', () => { mockInit.mockImplementationOnce((store, rememberedKeys, opts) => { optionDefaults = opts; }); - const store = 'the store!!!'; + const store = 'some-store'; const driver = { getItem() {}, @@ -187,7 +191,11 @@ describe('index.js', () => { const initialState = 'yup, initial state'; const enhancer: any = 'another enhancer'; - index.rememberEnhancer(driver, rememberedKeys)(() => store)( + const storeMaker: StoreCreator = index.rememberEnhancer( + driver, rememberedKeys + )((() => store) as StoreCreator); + + storeMaker( rootReducer, initialState, enhancer ); @@ -196,12 +204,12 @@ describe('index.js', () => { rememberedKeys, { driver, ...optionDefaults } ) - + const stringifySpy = jest.spyOn(JSON, 'stringify'); const parseSpy = jest.spyOn(JSON, 'parse'); - + expect(optionDefaults).toMatchObject({ - prefix : '@persist-', + prefix : '@@remember-', persistThrottle : 100, persistWholeStore : false }); diff --git a/src/__tests__/persist.test.ts b/src/__tests__/persist.test.ts index 0572458..902d357 100644 --- a/src/__tests__/persist.test.ts +++ b/src/__tests__/persist.test.ts @@ -99,7 +99,7 @@ describe('persist.js', () => { { prefix: 'yada.', driver: mockDriver, - serialize: JSON.stringify + serialize: (string, key) => JSON.stringify(string) } ); @@ -154,8 +154,9 @@ describe('persist.js', () => { state, oldState, { + prefix: 'bla', driver: mockDriver, - serialize() {} + serialize(state, key) {} } ); @@ -189,7 +190,7 @@ describe('persist.js', () => { } ); - expect(mockSerialize).toBeCalledWith(state); + expect(mockSerialize).toBeCalledWith(state, '@@yada-state@@'); }); it('calls driver.setItem()', async () => { @@ -247,15 +248,7 @@ describe('persist.js', () => { { key1: 'old' }, { driver: mockDriver, - serialize: (o: any) => o - } - ); - - await mod.persist( - { key1: 'new' }, - { key1: 'old' }, - { - driver: mockDriver, + prefix: 'one', persistWholeStore: false, serialize: (o: any) => o } @@ -266,6 +259,7 @@ describe('persist.js', () => { { key1: 'old' }, { driver: mockDriver, + prefix: 'two', persistWholeStore: true, serialize: (o: any) => o } @@ -278,6 +272,7 @@ describe('persist.js', () => { undefined, { driver: mockDriver, + prefix: 'three', persistWholeStore: false, serialize: (o: any) => o } @@ -288,6 +283,7 @@ describe('persist.js', () => { undefined, { driver: mockDriver, + prefix: 'four', persistWholeStore: true, serialize: (o: any) => o } @@ -304,6 +300,7 @@ describe('persist.js', () => { const error2 = 'DUMMY ERROR 2!!!'; const mockDriver = { + getItem: (key: string) => {}, setItem: ( jest.fn() .mockRejectedValueOnce(error1) @@ -318,6 +315,8 @@ describe('persist.js', () => { { key1: 'yay' }, { key1: 'cool' }, { + prefix: 'beep', + persistWholeStore: false, driver: mockDriver, serialize: (o: any) => o } @@ -327,6 +326,7 @@ describe('persist.js', () => { { key1: 'yay' }, { key1: 'cool' }, { + prefix: 'boop', driver: mockDriver, persistWholeStore: true, serialize: (o: any) => o diff --git a/src/index.ts b/src/index.ts index 5a6269e..b0d4dd4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,7 +51,7 @@ const rememberEnhancer = ( driver: Driver, rememberedKeys: string[], { - prefix = '@persist-', + prefix = '@@remember-', serialize = (data) => JSON.stringify(data), unserialize = (data) => JSON.parse(data), persistThrottle = 100, diff --git a/src/persist.ts b/src/persist.ts index 56fe1e2..e4177db 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -14,7 +14,7 @@ export const saveAll = ( if (!isEqual(state, oldState)) { return driver.setItem( `${prefix}state@@`, - serialize(state) + serialize(state, `${prefix}state@@`) ); } }; diff --git a/src/rehydrate.ts b/src/rehydrate.ts index 8076156..5a13fbb 100644 --- a/src/rehydrate.ts +++ b/src/rehydrate.ts @@ -21,16 +21,15 @@ export const loadAll = async ({ prefix, unserialize }: LoadAllOptions) => { - const data = await driver.getItem( - `${prefix}state@@` - ); + const key = `${prefix}state@@`; + const data = await driver.getItem(key); if (data === null || data == undefined) { return {}; } return pick( - unserialize(data), + unserialize(data, key), rememberedKeys ); }; diff --git a/src/types.ts b/src/types.ts index 73395c1..8b2a11f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,5 @@ -export type SerializeFunction = (data: any, key?: string) => any; -export type UnserializeFunction = (data: any, key?: string) => any; +export type SerializeFunction = (data: any, key: string) => any; +export type UnserializeFunction = (data: any, key: string) => any; export type Driver = { getItem: (key: string) => any;