🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

next-persist

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-persist - npm Package Compare versions

Comparing version
1.1.0
to
1.1.1
+1
-1
package.json
{
"name": "next-persist",
"version": "1.1.0",
"version": "1.1.1",
"description": "Bridging the gap between client-side persistence and server-side rendering",

@@ -5,0 +5,0 @@ "main": "src/next-persist.js",

@@ -24,6 +24,7 @@ /**

nextPersistCookie.setCookie = (cookieConfig, state) => {
const { key, allowList } = cookieConfig;
const key = Object.keys(cookieConfig)[0];
const allowList = Object.values(cookieConfig)[0];
// if allowList was not defined in cookieConfig, sets cookie containing entire state
if (!allowList) {
Cookie.set(key, state);
if (allowList.length === 0) {
Cookie.set(key, JSON.stringify(state));
} else {

@@ -35,3 +36,3 @@ // sets cookie containing only properties listed in allowList

}, {});
Cookie.set(key, allowedState);
Cookie.set(key, JSON.stringify(allowedState));
}

@@ -52,11 +53,11 @@ };

nextPersistCookie.getCookie = (req, state, cookieName) => {
nextPersistCookie.getCookie = (req, cookieName, state) => {
// if application is running client-side, parse and return cookie from browser
if (!req) {
const stateFromCookie = Cookie.get(cookieName);
return stateFromCookie ? JSON.parse(stateFromCookie) : state
return stateFromCookie ? JSON.parse(stateFromCookie) : state;
} // if application is running server-side, parse and return cooking from request body
else return cookie.parse(req.ctx.req.headers.cookie || '');
}
};
module.exports = nextPersistCookie;

@@ -21,3 +21,4 @@ /**

nextPersist.setStorage = (storageConfig, state) => {
const { key, allowList } = storageConfig;
const key = Object.keys(storageConfig)[0];
const allowList = Object.values(storageConfig)[0];

@@ -27,3 +28,3 @@ // if application is running client-side, localStorage is accessible

// if allowList was not defined in persistConfig, set all propertiesfrom state to localStorage
if (!allowList) {
if (allowList.length === 0) {
localStorage.setItem(key, JSON.stringify(state));

@@ -30,0 +31,0 @@ } else {

@@ -32,23 +32,18 @@ /**

// if redux combines reducers, each reducer's state gets saved to local storage
// under a unique key specified in wrapperConfig
if (this.props.wrapperConfig.combinedReducers) {
const { allowedKeys } = this.props.wrapperConfig;
const { allowedReducers } = this.props.wrapperConfig;
const { allowList } = this.props.wrapperConfig;
const nextPersistConfig = {};
allowedReducers.forEach((allowedReducer, index) => {
const nextPersistConfig = {
key: allowedKeys[index],
};
// if no allowlist provided save all state to their corresponding keys
if (!allowList) {
const key = Object.keys(this.props.state)[0];
nextPersistConfig[key] = [];
method(nextPersistConfig, this.props.state[key]);
}
// if allowlist exists pass subconfigs of allowed reducers into storage method
else {
const allowedReducers = Object.keys(allowList);
allowedReducers.forEach((allowedReducer) => {
nextPersistConfig[allowedReducer] = allowList[allowedReducer];
method(nextPersistConfig, this.props.state[allowedReducer]);
});
// otherwise, the single reducer's state gets saved to local storage
// according to allowList configuration
} else {
const nextPersistConfig = {
key: this.props.wrapperConfig.key,
allowList: this.props.wrapperConfig.allowList,
};
method(nextPersistConfig, this.props.state);
}

@@ -55,0 +50,0 @@ return this.props.children;

/**
* @jest-environment jsdom
*/
const nextPersist = require('../src/next-persist');
const { writeStorage, getStorage } = require('../src/next-persist');
let state = {};
let nextPersistConfig = {};
describe('writeStorage tests', () => {
beforeEach(() => {
state.foo = [1, 2, 3];
state.bar = {a : true, b : false};
state.baz = 'Hello world!'
nextPersistConfig.key = 'state';
nextPersistConfig.allowList = null;
})
afterEach(() => {
state = {};
nextPersistConfig = {};
})
it('Saves the entire state to localStorage', () => {
writeStorage(nextPersistConfig, state);
expect(localStorage[nextPersistConfig.key]).toEqual(JSON.stringify(state));
});
it('Saves only the properties of state whitelisted on allowList to localStorage', () => {
nextPersistConfig.allowList = ['foo', 'bar'];
writeStorage(nextPersistConfig, state);
delete state.baz;
expect(localStorage[nextPersistConfig.key]).toEqual(JSON.stringify(state));
});
// change jest-environment on line 2 from jsdom to node -> probably some jest configuration that automates this
xit('Returns an error if `window` is undefined', () => {
const error = writeStorage(nextPersistConfig, state);
expect(error.err).toBe('LocalStorage not found.');
})
});
describe('getStorage tests', () => {
beforeEach(() => {
state.foo = [1, 2, 3];
state.bar = {a : true, b : false};
state.baz = 'Hello world!'
nextPersistConfig.key = 'state';
nextPersistConfig.allowList = null;
})
afterEach(() => {
state = {};
nextPersistConfig = {};
})
it('Retrieves the entire persisted state from localStorage and returns it', () => {
localStorage.setItem(nextPersistConfig.key, JSON.stringify(state));
const stateFromLocalStorage = getStorage(nextPersistConfig, state);
expect(stateFromLocalStorage).toEqual(state);
});
it('Retrieves only the properties of state whitelisted on allowList from localStorage', () => {
nextPersistConfig.allowList = ['foo', 'bar'];
localStorage.setItem(nextPersistConfig.key, JSON.stringify(state));
delete state.baz;
const stateFromLocalStorage = getStorage(nextPersistConfig, state);
expect(stateFromLocalStorage).toEqual(state);
})
// change jest-environment on line 2 from jsdom to node -> probably some jest configuration that automates this
xit('Returns the same state passed in if `window` is undefined', () => {
const sameState = getStorage(nextPersistConfig, state);
expect(sameState).toEqual(state);
});
});