redux-artificer v0.1.0
#+HTML: Redux Artificer Redux builder [https://www.npmjs.org/package/redux-artificer] [https://travis-ci.org/jupl/redux-artificer] [https://codecov.io/gh/jupl/redux-artificer] [https://david-dm.org/jupl/redux-artificer?type=peer] [https://david-dm.org/jupl/redux-artificer?type=dev]
** About [https://redux.js.org/] builder to construct [https://www.typescriptlang.org/]-backed strognly typed pieces easily to consume with [types/README.org#readme]:
- 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.
6 years ago