Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
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.0.8 to 1.0.9

2

package.json
{
"name": "next-persist",
"version": "1.0.8",
"version": "1.0.9",
"description": "Bridging the gap between client-side persistence and server-side rendering",

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

@@ -1,13 +0,30 @@

/* eslint-disable prettier/prettier */
import cookie from 'cookie'
import Cookie from 'js-cookie'
/**
* ************************************
*
* @module next-persist
* @author most-js
* @description object that contains methods to set and get cookies containing state
*
* ************************************
*/
const cookie = require('cookie');
const Cookie = require('js-cookie');
const nextPersistCookie = {};
nextPersistCookie.setCookie = (nextPersistConfig, state) => {
const { key, allowList } = nextPersistConfig;
/*
sets a cookie containing state
arguments:
cookieConfig(object) - object with reserved keywords 'key' (string) and 'allowList' (array).
state(object) - object from application containing state
*/
nextPersistCookie.setCookie = (cookieConfig, state) => {
const { key, allowList } = cookieConfig;
// if allowList was not defined in cookieConfig, sets cookie containing entire state
if (!allowList) {
Cookie.set(key, state);
} else {
// sets cookie containing only properties listed in allowList
const allowedState = allowList.reduce((acc, cur) => {

@@ -19,12 +36,14 @@ acc[cur] = state[cur];

}
};
nextPersistCookie.getCookie = (req) => {
if (req) {
return cookie.parse(req ? req.ctx.req.headers.cookie || "" : global.cookie);
}
return cookie.parse(document.cookie);
};
/*
retrieves cookie containing state
arguments:
req (object) - request object from the context object from Next.js data-fetching methods
*/
// if application is running server-side, parse and return cookie from request object
// if application is running client-side, parse and return cooking from document object
nextPersistCookie.getCookie = (req) => cookie.parse(req ? req.ctx.req.headers.cookie || '' : document.cookie);
module.exports = nextPersistCookie;

@@ -0,11 +1,30 @@

/**
* ************************************
*
* @module next-persist
* @author most-js
* @description object that contains methods to set and get state from localStorage
*
* ************************************
*/
const nextPersist = {};
// writes to local storage
nextPersist.writeStorage = (nextPersistConfig, state) => {
const { key, allowList } = nextPersistConfig;
/*
sets state in localStorage
arguments:
storageConfig(object) - object with reserved keywords 'key' (string) and 'allowList' (array).
state(object) - object from application containing state
*/
nextPersist.setStorage = (storageConfig, state) => {
const { key, allowList } = storageConfig;
// if application is running client-side, localStorage is accessible
if (typeof window !== 'undefined') {
// if allowList was not defined in persistConfig, set all propertiesfrom state to localStorage
if (!allowList) {
localStorage.setItem(key, JSON.stringify(state));
} else {
// only sets properties listed in allowList to localStorage
const allowedState = allowList.reduce((acc, cur) => {

@@ -17,2 +36,3 @@ acc[cur] = state[cur];

}
// if application is not running-client side, localStorage is inaccessible
} else {

@@ -23,6 +43,16 @@ return { err: 'LocalStorage not found.' };

// retrieves from local storage
/*
retrieves state from localStorage
arguments:
key(string) - name state was saved under in localStorage
state(object) - object from application containing state
*/
nextPersist.getStorage = (key, state) => {
// if application is running client-side, localStorage is accessible
if (typeof window !== 'undefined') {
const clientState = localStorage.getItem(key);
// if localStorage contains the key user queries,
// parse the data and return an updated state object
if (clientState) {

@@ -36,2 +66,4 @@ const parsedClientState = JSON.parse(clientState);

}
// if localStorage doesn't contain the key user queries or application isn't running client-side,
// return original state that was passed in
return state;

@@ -38,0 +70,0 @@ };

@@ -0,4 +1,17 @@

/**
* ************************************
*
* @module next-persist
* @author most-js
* @description a component that persists state to localStorage or cookies.
* this component should wrap Next.js components
* and be wrapped by Redux's Provider component
*
* ************************************
*/
import { Component } from 'react';
import { connect } from 'react-redux';
import { writeStorage } from './next-persist';
import { setCookie } from './next-persist-cookies';
import { setStorage } from './next-persist';

@@ -11,2 +24,12 @@ const mapStateToProps = (state) => ({

render() {
// determines method to persist state
let method;
if (this.props.wrapperConfig.method === 'localStorage') {
method = setStorage;
} else if (this.props.wrapperConfig.method === 'cookies') {
method = setCookie;
}
// 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) {

@@ -20,4 +43,7 @@ const { allowedKeys } = this.props.wrapperConfig;

};
writeStorage(nextPersistConfig, this.props.state[allowedReducer]);
method(nextPersistConfig, this.props.state[allowedReducer]);
});
// otherwise, the single reducer's state gets saved to local storage
// according to allowList configuration
} else {

@@ -28,3 +54,3 @@ const nextPersistConfig = {

};
writeStorage(nextPersistConfig, this.props.state);
method(nextPersistConfig, this.props.state);
}

@@ -35,4 +61,5 @@ return this.props.children;

// connects wrapper to redux store
const PersistWrapper = connect(mapStateToProps)(NextPersistWrapper);
export default PersistWrapper;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc