UNPKG

1.5 kBJavaScriptView Raw
1/**
2 * Imports
3 */
4
5'use strict';
6
7Object.defineProperty(exports, '__esModule', {
8 value: true
9});
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
12
13var _componentCookie = require('component-cookie');
14
15var _componentCookie2 = _interopRequireDefault(_componentCookie);
16
17/**
18 * Cookie middleware
19 */
20
21function cookieMiddleware(cookieMap) {
22 var cookie = cookieMap ? map(cookieMap) : _componentCookie2['default'];
23
24 return function (_ref) {
25 var dispatch = _ref.dispatch;
26 var getState = _ref.getState;
27 return function (next) {
28 return function (action) {
29 return action.type === 'GET_COOKIE' || action.type === 'SET_COOKIE' ? Promise.resolve(handle(cookie, action)) : next(action);
30 };
31 };
32 };
33}
34
35/**
36 * Handle a cookie effect
37 */
38
39function handle(cookie, action) {
40 var _action$payload = action.payload;
41 var name = _action$payload.name;
42 var value = _action$payload.value;
43 var opts = _action$payload.opts;
44
45 switch (action.type) {
46 case 'SET_COOKIE':
47 return cookie(name, value, opts);
48 case 'GET_COOKIE':
49 return cookie(name);
50 }
51}
52
53/**
54 * Use a plain object as the source of cookie data
55 * (e.g. if we're on the server)
56 */
57
58function map(cookieMap) {
59 return function (name, value) {
60 if (arguments.length === 2) {
61 cookieMap[name] = value;
62 }
63
64 return cookieMap[name];
65 };
66}
67
68/**
69 * Exports
70 */
71
72exports['default'] = cookieMiddleware;
73module.exports = exports['default'];
\No newline at end of file