Détail du package

deox

thebrodmann6.2kMIT3.3.1

Functional Type-safe Flux Standard Utilities

reducer, action, typesafe, redux

readme

Deox

Functional type-safe Flux standard utilities

License Build Status Coverage Status Semantic Release Latest Release PRs welcome

The only completely functional type-safe approach to Flux that its main goals are to diminish types verbosity, repetition and complexity without losing any type information (type-safe alternative of redux-actions).

Behold, the art of Deox:

Deox counter example

Highlights

  • Minimalist (almost no import cost) - checkout Bundle Phobia.
  • Simple - focused on self-declarative API.
  • Secure - complete test-suits for all of the edge and corners.

Motivation

The most common complaint about Flux is how it makes you write a lot of boilerplate. Also, existing solutions like redux-actions or redux-starter-kit are not type-safe by the idea (although there are some other ideas with classes 😱).

So, this is where Deox takes place to make maintenance of Flux architecture simpler and more readable by sticking to functional programming paradigm.

Installation

You can install Deox package by running:

# YARN
yarn add deox

# NPM
npm install deox

Typescript tip: notice that Deox internally uses some ES2015 type definitions to represent better developer experience. So if you are using Typescript and targeting es5, be sure es2015 lib has been added in tsconfig.json:

{
  "compilerOptions": {
    ...
    "target": "es5",
    "lib": ["es2015"]
  }
}

The Deox NPM package contains a CommonJS build that can be use with Node.js or module bundlers (e.g. Rollup, Webpack, etc.). it also includes an ESM build that works well with tree-shaking.

If you don't use module bundler, it's also fine. The Deox NPM package also includes a production minified UMD build that makes Deox available as global variable called window.Deox; you can add it simply to your page via following script tag:

<script src="https://unpkg.com/deox@latest"></script>

Usage

import { createActionCreator, createReducer } from 'deox'

const increment = createActionCreator('INCREMENT')
const decrement = createActionCreator('DECREMENT')
const reset = createActionCreator('RESET', resolve => (count: number) =>
  resolve(count)
)

const defaultState = 0

const counterReducer = createReducer(defaultState, handleAction => [
  handleAction(increment, state => state + 1),
  handleAction(decrement, state => state - 1),
  handleAction(reset, (_state, { payload }) => payload),
])

counterReducer(undefined, increment()) //=> 1
counterReducer(undefined, decrement()) //=> -1
counterReducer(3, reset(0)) //=> 0

Documentation

FAQ

Why not redux-actions, redux-starter-kit ?

Both redux-actions and redux-starter-kit are neat and almost similar to each other. Actually deox is similar to those projects in the idea, but not in implementation and promise. The main goal of deox is to use the full power of type-safety and type inferring in typescript. If you have some experience with those libraries, the following piece of code should be familiar for you:

type Actions
  = ReturnType<typeof addTodo>
  | ReturnType<typeof removeTodo>
  | ReturnType<typeof editTodo>

const todosReducer = createReducer<State, Actions>(...)

This is horrible; Why define a type like actions that a reducer can handle?! It's completely obvious which actions a reducer handles.

On another hand there is a big problem with the pattern that redux-actions and redux-starter-kit follows. it's lack of correct type for action handler:

const todosReducer = createReducer<State, Actions>(defaultState, {
  [addTodo]: (state, action) => {...}, // action: Actions
  [removeTodo]: (state, action) => {...}, // action: Actions
  [editTodo]: (state, action) => {...}, // action: Actions
})

Type of action parameter in addTodo action handler is overall Actions type. It's inaccurate!

And this is where Deox comes in action and practice:

const todosReducer = createReducer(defaultState, handleAction => [
  handleAction(addTodo, (state, action) => {...}), // action: AddTodoAction
  handleAction(removeTodo, (state, action) => {...}), // action: RemoveTodoAction
  handleAction(editTodo, (state, action) => {...}) // action: EditTodoAction
])

That's it. Thanks to typescript type inferring there is no type verbosity at all. You can be sure todos reducer have the proper type of state and actions that it can handle. And every action handler's type is just what it should be. It's completely safe and correct!

What's the difference with typesafe-actions ?

The typesafe-actions is a great project that Deox carries huge inspiration from that. But typesafe-actions doesn't have any plan for a complete set of utilities (specially reducers); It's all about actions and action creators.

Versioning

Deox uses Semantic Versioning 2.0.0

Contributing

Please read through our contributing guidelines.

Inspiration

  • redux-actions - Flux Standard Action utilities for Redux
  • typesafe-actions - Typesafe Action Creators for Redux / Flux Architectures (in TypeScript)

License

Deox is released under MIT license.

changelog

Deox Changelog

3.3.1 (2020-10-17)

Bug Fixes

  • use symbol for the others handler (f5b1cee)

3.3.0 (2020-10-01)

Bug Fixes

  • handle.others instead of handle.default (ffe271b)
  • more explicit typing to avoid TS4025 (25b78c3)
  • type for plain action creator. (#144) (f228b81), closes #143

Features

  • default handler in createReducer (0b3dfcf), closes #152

3.3.0 (2020-10-01)

Bug Fixes

  • handle.others instead of handle.default (f9aec53)
  • more explicit typing to avoid TS4025 (e703730)

Features

  • default handler in createReducer (611184d), closes #152

3.2.2 (2020-03-16)

Bug Fixes

3.2.1 (2020-01-09)

Bug Fixes

  • extract action from any, Redux.Action and AnyAction types (18d0a9e), closes #141

3.2.0 (2019-12-09)

Features

  • reuse isOfType helper in ofType RxJS operator (#136) (a5f0648)
  • support mixed keys for ofType RxJS operator (#133) [skip release] (dc21e6d)

3.1.0 (2019-12-08)

Features

  • add isOfType helper (#127) (03e6210), closes #87
  • generalize isOfType keys argument to readonly array (cc01d9d)

3.0.1 (2019-12-07)

Bug Fixes

  • generalize TActionCreator generic type in createHandlerMap (9825477), closes #128

3.0.0 (2019-12-04)

Bug Fixes

  • use tslib and rxjs as peer dependencies (a58a4a9)

BREAKING CHANGES

  • Please make sure you have tslib in your dependencies if you are using TypeScript. Also, make sure you have rxjs in your dependencies if you are using ofType rxjs operator.

2.1.4 (2019-12-06)

Bug Fixes

  • generalize TActionCreator generic type in createHandlerMap (a7dd90a), closes #128

2.1.2 (2019-12-04)

Bug Fixes

  • allow acceptance of any action by reducer created by createReducer (1b39adb)

2.1.1 (2019-12-04)

Bug Fixes

  • make ofType operator typing works with plain action types (fecabda)

2.1.0 (2019-06-25)

Features

  • upgrade redux-starter-kit to version 0.5.1 (644acac)

2.0.1 (2019-06-24)

Bug Fixes

  • remove useless default generic value in createActionCreator (7538ae0)

2.0.0 (2019-06-15)

Features

  • export immutibility type helpers in public API (b723d35)
  • make DeepImmutable optional in input/prev state (0f35d7f), closes #58 #55

BREAKING CHANGES

  • The input/prev state argument in createHandlerMap (AKA handleAction) and returning reducer of createReducer will not obligated to be Immutable data structure by Deox which in turn can lead to changes in handler's and reducer's return type.

1.4.1 (2019-06-09)

Bug Fixes

  • infer return type of reducer from return type of handlers (6b6edaa), closes #55
  • nested DeepImmutable and Immutable types neutralization (d3f9a1a), closes #65

1.4.0 (2019-04-21)

Bug Fixes

  • make rxjs/operators in UMD bundle an external dependency (cf94ab7), closes #41

Features

  • rename createAction to createActionCreator (a855997), closes #30

1.3.1 (2019-04-05)

Bug Fixes

  • tree-shaking: rename browser field to unpkg in package.json (159f2e7), closes #43
  • tree-shaking: set sideEffects field in package.json (dd5889d)

1.3.0 (2019-04-02)

Features

  • add ActionType helper (0b2906a)
  • export ActionType in public API (f349705)
  • export configureStore from redux-starter-kit (2499a02), closes #9

1.2.1 (2019-03-25)

Bug Fixes

  • export DeepImmutable* types (c9d0b3b), closes #21

1.2.0 (2019-03-18)

Features

  • add generic type capability to action creator (4044db2)

1.1.1 (2019-02-20)

Bug Fixes

  • attach non-undefined falsy values to payload and meta of action (1fd8ed8), closes #15

1.1.0 (2019-02-14)

Features

  • add ofType operator (63fb590), closes #11
  • deep immutable reducer type state (de98e30), closes #3

1.0.2 (2019-01-21)

Bug Fixes

  • convert types.d.ts to types.ts (852e196), closes #5

1.0.1 (2019-01-20)

Bug Fixes