PiotrKujawa / redux-deep-persist

Redux Deep Persist creates a redux-persist configuration for any level of a nested state.
MIT License
40 stars 2 forks source link

Alternative to use whitelist and blacklist at the same time in Nested Persist #8

Open samitha9125 opened 1 year ago

samitha9125 commented 1 year ago

First of all, thank you for the amazing library and keep up the good work!

Background

We want to use another storage for secure storing. But with current configurations mentioned in the redux-persist Nested Persist section and redux-deep-persist configurations like below, details will not get stored.

import { getPersistConfig } from 'redux-deep-persist';
import { configureStore } from '@reduxjs/toolkit';
import createEncryptedStorage from 'redux-persist-encrypted-storage';

const securedStorage = createEncryptedStorage();
const securedConfig = getPersistConfig({
    key: 'secured',
    storage: securedStorage,
    timeout: 50000,
    whitelist: ['app.tokens'],
    rootReducer: securedReducer
});

const config = getPersistConfig({
    debug: false,
    key: 'root',
    storage,
    timeout: 50000,
    blacklist: ['secured'],
    rootReducer: combinedReducer
});

const combinedReducer = combineReducers({
    secured: persistReducer<SecuredReducerType>(securedConfig, securedReducer),
    general: rootReducer
});
const reducers = persistReducer<ReturnType<typeof combinedReducer>>(config, combinedReducer);

// Creating Store
const store = configureStore({
    reducer: reducers,
    middleware: getDefaultMiddleware =>
        getDefaultMiddleware({
            serializableCheck: {
                ignoredActions: [
                    FLUSH,
                    REHYDRATE,
                    PAUSE,
                    PERSIST,
                    PURGE,
                    REGISTER,
                ]
            }
        }).prepend(middleware),
    enhancers
});

Expected

Since secured is blacklisted in the config, it should be able to persist some configurations from the general reducer as well.

Actual

Getting this error (You should not define a whitelist and blacklist in parallel.) which is clearly mentioned in the documentation. But what would be the alternative?

PiotrKujawa commented 1 year ago

Hi @samitha9125 it's possible but not with getPersistConfig method. This method is actually a helper to create a configuration for you but behind the scenes, it uses createWhitelist and createBlacklist transforms which can be used together with caution. getPersistConfig prevents incorrect config, using transforms directly you must know what you're doing. I'm away from my desk and getting back next week. I'll give you an example when I'm back unless you figure this out yourself. As you can see I already use blacklist and whitelist at the same time here:

https://github.com/PiotrKujawa/redux-deep-persist/blob/master/src/index.ts#L221. https://github.com/PiotrKujawa/redux-deep-persist/blob/master/tests/persist.spec.ts#L32

samitha9125 commented 1 year ago

@PiotrKujawa Thank you for the reply. This library is really convenient and I think we can find a solution to add this feature as well, if you are up to discussions :)