UNPKG

redux-security

Version:
51 lines (42 loc) 1.34 kB
import Promise from 'bluebird' import cuid from 'cuid' import { write, error } from 'redux-journal' import { manager } from 'redux-manager' import { actions } from '../actions' import { SERVICE, TAGS } from '../config' import { select } from '../select' const tags = `${TAGS}.api.local` export const configAPILocal = ( { serviceName = SERVICE } = { serviceName: SERVICE } ) => { const state = () => manager.getStore().getState()[serviceName] const create = ({ userID }) => Promise.try(() => { const sessionID = cuid() insert({ _id: sessionID, userID }) return { sessionID } }) const get = ({ sessionID }) => Promise.try(() => { const sessions = select(state()) const docs = sessions.docs.all() const session = docs.find(doc => doc._id == sessionID ) if (session) { return session } throw new Error('Cannot find session') }) const insert = (payload) => Promise.try(() => { manager.dispatch(actions.insert(payload), serviceName) }) const remove = (payload) => Promise.try(() => { manager.dispatch(actions.remove(payload), serviceName) }) const update = (payload) => Promise.try(() => { manager.dispatch(actions.update(payload), serviceName) }) return { create, get, insert, remove, update } }