redux-artificer
v0.1.0
Published
[[https://www.npmjs.org/package/redux-artificer][file:https://img.shields.io/npm/v/redux-artificer.svg?style=flat-square]] [[https://travis-ci.org/jupl/redux-artificer][file:https://img.shields.io/travis/jupl/redux-artificer.svg?label=travis&style=flat-sq
Downloads
5
Readme
#+HTML: Redux Artificer Redux builder [[https://www.npmjs.org/package/redux-artificer][file:https://img.shields.io/npm/v/redux-artificer.svg?style=flat-square]] [[https://travis-ci.org/jupl/redux-artificer][file:https://img.shields.io/travis/jupl/redux-artificer.svg?label=travis&style=flat-square]] [[https://codecov.io/gh/jupl/redux-artificer][file:https://img.shields.io/codecov/c/github/jupl/redux-artificer.svg?style=flat-square]] [[https://david-dm.org/jupl/redux-artificer?type=peer][file:https://img.shields.io/david/peer/jupl/redux-artificer.svg?style=flat-square]] [[https://david-dm.org/jupl/redux-artificer?type=dev][file:https://img.shields.io/david/dev/jupl/redux-artificer.svg?style=flat-square]]
** About [[https://redux.js.org/][Redux]] builder to construct [[https://www.typescriptlang.org/][TypeScript]]-backed strognly typed pieces easily to consume with [[types/README.org#readme][types]]:
- Reducer
- Actions
- Selectors
** Installation #+BEGIN_EXAMPLE npm install redux redux-artificer #+END_EXAMPLE
** Example #+BEGIN_SRC typescript import {createStore} from 'redux' import {Builders, Types} from 'redux-artificer'
interface Alert { id: number message: string timestamp: number }
const {actions, initialState, reducer, selectors} = Builders.build({ alerts: { list: Types.Records.New<Alert, number>({getId: ({id}) => id}), show: Types.Flag.New(), }, user: { accessToken: Types.Optional.New(), signedIn: Types.Flag.New(), type: Types.Value.New<'admin' | 'regular' | 'guest'>({ initialState: 'guest', }), }, })
type State = typeof initialState
const store = createStore(reducer) const hasAccessToken = selectors.user.accessToken.isSet(store.getState()) const alert1 = selectors.alerts.list.getAt(store.getState(), 1) store.dispatch(actions.alerts.list.clear()) store.dispatch(actions.user.accessToken.unset()) store.dispatch(actions.user.signedIn.reset()) #+END_SRC
State is of type: #+BEGIN_SRC typescript type State = { alerts: { list: Alert[] show: boolean } user: { accessToken: string | null signedIn: boolean type: 'admin' | 'regular' | 'guest' } } #+END_SRC
Initial state is: #+BEGIN_SRC json { "alerts": { "list": [], "show": false }, "user": { "accessToken": null, "signedIn": false, "type": "guest" } } #+END_SRC
** Notes
- While =null= may be used for state, it is only for the benefit of Redux. =undefined= is used for non existant values for the selectors.