redux-persist-cookie-storage
Advanced tools
Comparing version 0.4.0 to 1.0.0-beta
@@ -1,1 +0,2 @@ | ||
module.exports = require('./src/redux-persist-cookie-storage'); | ||
module.exports.CookieStorage = require('./src/redux-persist-cookie-storage'); | ||
module.exports.NodeCookiesWrapper = require('./src/node-cookies-wrapper'); |
{ | ||
"name": "redux-persist-cookie-storage", | ||
"version": "0.4.0", | ||
"version": "1.0.0-beta", | ||
"description": "redux-persist storage adapter for cookies", | ||
@@ -19,8 +19,7 @@ "main": "index.js", | ||
"chai-spies": "^0.7.1", | ||
"cookies-js": "^1.2.3", | ||
"cookies": "^0.7.1", | ||
"jsdom": "^9.5.0", | ||
"mocha": "^3.0.2" | ||
}, | ||
"dependencies": { | ||
"cookies-js": "^1.2.2" | ||
} | ||
} |
172
README.md
@@ -15,51 +15,98 @@ # Redux Persist Cookie Storage Adapter | ||
#### Pure Cookie mode | ||
```js | ||
import { persistStore, autoRehydrate } from 'redux-persist' | ||
import CookieStorage from 'redux-persist-cookie-storage' | ||
import { persistStore, persistCombineReducers } from 'redux-persist' | ||
import { CookieStorage } from 'redux-persist-cookie-storage' | ||
import Cookies from 'cookies-js' | ||
const store = createStore(reducer, undefined, autoRehydrate()) | ||
// Cookies.defaults.domain = ... | ||
// By default, session cookies are used | ||
persistStore(store, { storage: new CookieStorage() }) | ||
const persistConfig = { | ||
key: "root", | ||
storage: new CookieStorage(Cookies/*, options */) | ||
} | ||
// Expiration time can be set via options | ||
persistStore(store, { storage: new CookieStorage({ | ||
expiration: { | ||
'default': 365 * 86400 // Cookies expire after one year | ||
} | ||
}) | ||
}) | ||
const rootReducer = persistCombineReducers(persistConfig, reducers) | ||
// Default expiration time can be overridden for specific parts of the store: | ||
persistStore(store, { storage: new CookieStorage({ | ||
expiration: { | ||
'default': null, // Session cookies used by default | ||
'storeKey': 600 // State in key `storeKey` expires after 10 minutes | ||
} | ||
}) | ||
}) | ||
const store = createStore(rootReducer, undefined) | ||
const persistor = persistStore(store, {}) | ||
``` | ||
#### Bootstrap from preloaded state in window object | ||
```js | ||
import { persistStore, persistCombineReducers } from 'redux-persist' | ||
import { CookieStorage } from 'redux-persist-cookie-storage' | ||
import Cookies from 'cookies-js' | ||
const persistConfig = { | ||
key: "root", | ||
storage: new CookieStorage(Cookies/*, options */), | ||
stateReconciler(inboundState, originalState) { | ||
// Ignore state from cookies, only use preloadedState from window object | ||
return originalState | ||
} | ||
} | ||
const rootReducer = persistCombineReducers(persistConfig, reducers) | ||
const store = createStore(rootReducer) | ||
const persistor = persistStore(store, window.PRELOADED_STATE) | ||
``` | ||
### Server | ||
```js | ||
// Read-only mode: Use plain object output of cookie parser | ||
import { persistStore, autoRehydrate } from 'redux-persist' | ||
import CookieStorage from 'redux-persist-cookie-storage' | ||
import cookieParser from 'cookie-parser' | ||
// Read-only mode: Use getStoredState method | ||
import { persistStore, getStoredState } from 'redux-persist' | ||
import { CookieStorage, NodeCookiesWrapper } from 'redux-persist-cookie-storage' | ||
import Cookies from 'cookies' | ||
const app = new Express() | ||
app.use(cookieParser()) | ||
app.use(Cookies.express()) | ||
app.use((req, res) => { | ||
const store = createStore(reducer, undefined, autoRehydrate()) | ||
const cookies = req.cookies | ||
persistStore(store, { storage: new CookieStorage({ cookies }) }) | ||
app.use(async (req, res) => { | ||
const cookieJar = new NodeCookiesWrapper(new Cookies(req, res)) | ||
const persistConfig = { | ||
key: "root", | ||
storage: new CookieStorage(cookieJar/*, options */), | ||
stateReconciler(inboundState, originalState) { | ||
// Ignore state from cookies, only use preloadedState from window object | ||
return originalState | ||
} | ||
} | ||
let preloadedState | ||
try { | ||
preloadedState = await getStoredState(persistConfig) | ||
} | ||
catch (e) { | ||
// getStoredState implementation fails when index storage item is not set. | ||
preloadedState = {} | ||
} | ||
const rootReducer = persistCombineReducers(persistConfig, reducers) | ||
const store = createStore(rootReducer, preloadedState) | ||
}) | ||
// Read-write mode: Use actual cookie jar implementation | ||
import { persistStore, autoRehydrate } from 'redux-persist' | ||
import CookieStorage from 'redux-persist-cookie-storage' | ||
// Read-write mode: Create persistor | ||
import { persistStore, getStoredState } from 'redux-persist' | ||
import { CookieStorage, NodeCookiesWrapper } from 'redux-persist-cookie-storage' | ||
import Cookies from 'cookies' | ||
const configurePersistor = async (store) => { | ||
return new Promise((resolve) => { | ||
const persistor = persistStore(store, {}, () => { | ||
resolve(persistor) | ||
}) | ||
}) | ||
} | ||
const app = new Express() | ||
@@ -69,11 +116,62 @@ | ||
app.use((req, res) => { | ||
const store = createStore(reducer, undefined, autoRehydrate()) | ||
const cookies = new Cookies(req, res) | ||
persistStore(store, { storage: new CookieStorage({ cookies }) }) | ||
app.use(async (req, res) => { | ||
const cookieJar = new NodeCookiesWrapper(new Cookies(req, res)) | ||
const persistConfig = { | ||
key: "root", | ||
storage: new CookieStorage(cookieJar/*, options */), | ||
stateReconciler(inboundState, originalState) { | ||
// Ignore state from cookies, only use preloadedState from window object | ||
return originalState | ||
} | ||
} | ||
const rootReducer = persistCombineReducers(persistConfig, reducers) | ||
// Initialize store without preloaded state | ||
const store = createStore(rootReducer) | ||
// Wait until persistor has completed deserialization | ||
const persistor = await configurePersistor(store) | ||
// Force cookies to be set | ||
await persistor.flush() | ||
res.send(200, 'Done!') | ||
}) | ||
``` | ||
### Options | ||
```js | ||
// By default, session cookies are used | ||
persistStore(store, { storage: new CookieStorage(Cookies) }) | ||
// Expiration time can be set via options | ||
persistStore(store, { storage: new CookieStorage(Cookies, { | ||
expiration: { | ||
'default': 365 * 86400 // Cookies expire after one year | ||
} | ||
}) | ||
}) | ||
// Default expiration time can be overridden for specific parts of the store: | ||
persistStore(store, { storage: new CookieStorage(Cookies, { | ||
expiration: { | ||
'default': null, // Session cookies used by default | ||
'storeKey': 600 // State in key `storeKey` expires after 10 minutes | ||
} | ||
}) | ||
}) | ||
// Other cookie options like domain, path and secure: | ||
persistStore(store, { storage: new CookieStorage(Cookies, { | ||
setCookieOptions: { | ||
path: '/mypath' | ||
} | ||
}) | ||
}) | ||
``` | ||
N.B.: Custom expiration times are not supported server-side at the moment. | ||
## Development | ||
@@ -80,0 +178,0 @@ |
@@ -1,7 +0,6 @@ | ||
var Cookies = require('cookies-js'); | ||
var FakeCookieJar = require('./fake-cookie-jar'); | ||
function CookieStorage(options) { | ||
function CookieStorage(cookies, options) { | ||
options = options || {}; | ||
this.cookies = cookies; | ||
this.keyPrefix = options.keyPrefix || ''; | ||
@@ -14,20 +13,3 @@ this.indexKey = options.indexKey || 'reduxPersistIndex'; | ||
if (options.windowRef) { | ||
this.cookies = Cookies(options.windowRef); | ||
} else if (typeof window !== 'undefined') { | ||
this.cookies = Cookies; | ||
} else if (options.cookies) { | ||
if ('get' in options.cookies && 'set' in options.cookies && 'expire' in options.cookies) { | ||
this.cookies = options.cookies | ||
} else { | ||
this.cookies = new FakeCookieJar(options.cookies); | ||
} | ||
} | ||
if (options.domain) { | ||
this.cookies.defaults.domain = options.domain; | ||
} | ||
if (options.path) { | ||
this.cookies.defaults.path = options.path; | ||
} | ||
this.setCookieOptions = options.setCookieOptions; | ||
} | ||
@@ -44,3 +26,3 @@ | ||
CookieStorage.prototype.setItem = function (key, value, callback) { | ||
var options = {}; | ||
var options = Object.assign({}, this.setCookieOptions); | ||
@@ -58,3 +40,3 @@ var expires = this.expiration.default; | ||
// Update key index | ||
var indexOptions = {}; | ||
var indexOptions = Object.assign({}, this.setCookieOptions); | ||
if (this.expiration.default) { | ||
@@ -61,0 +43,0 @@ indexOptions["expires"] = this.expiration.default; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10048
0
180
6
102
- Removedcookies-js@^1.2.2
- Removedcookies-js@1.2.3(transitive)