From 5b647b682a6bbc3484078e0b1cda74e81510ab2d Mon Sep 17 00:00:00 2001 From: klis87 Date: Fri, 10 Jul 2020 01:25:40 +0200 Subject: [PATCH] Added babel plugin and smart methods --- README.md | 55 +++++++++++++++++++++++++++++++++++++++++++++--- package.json | 2 +- types/index.d.ts | 17 ++++++++++++++- 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index da80830..23752f6 100644 --- a/README.md +++ b/README.md @@ -14,20 +14,21 @@ Redux addon to create actions and thunks with minimum boilerplate Why yet another Redux action creation library? Because: - writing Redux actions without any addon requires writing constants which is very verbose and error-prone - other addons often force conventions about action structure +- other addons still require to pass strings as first argument to define action type - here you won't have to with optional Babel plugin - some addons solve problem with unique types aucomatically, but then they are not determenistic (TODO) - this library also provides thunks creators - you can create thunk like normal action and also forget about types ## Installation To install the package, just run: -``` +```bash $ npm install redux-smart-actions ``` or you can just use CDN: `https://unpkg.com/redux-smart-actions`. ## Usage -### createAction +### `createAction` Let's say you have an action written without any addon: ```js @@ -58,7 +59,7 @@ const doSth = createAction('DO_STH', x => ({ x })); Basically 2nd argument is an action creator, you write it like usually, just you don't need to worry about `type`. -### createThunk +### `createThunk` If you happen to use `redux-thunk`, you might like using `createThunk` from this library. Often you need to use thunks which looks very similar to normal actions, but they need to @@ -92,6 +93,54 @@ So what changed? `doSth.toString() === 'DO_STH'`, so you can use `doSth` in redu like constants didn't even exist. Also notice that we do not dispatch `{ x: state.x }` action, we return it, `createThunk` will add `type` for us and dispatch it automatically. +## Babel plugin + +This plugin it totally optional, but very recommended. With just no work you will be able +to omit first arguments (action types) for both `createAction` and `createThunk` - +they will be taken from action name automatically! + +To install it, just run: +```bash +npm install --save-dev babel-plugin-redux-smart-action +``` + +and add it to babel plugins, for example in `.babelrc`: +```json +{ + "plugins": [ + "redux-smart-actions" + ] +} +``` + +Then, you could use new functions from this library. + +### `createSmartAction` + +```js +import { createSmartAction } from 'redux-smart-actions'; + +const doSth = createSmartAction(); + +const doSthElse = createSmartAction(x => ({ x })); +``` + +which would be the same as: +```js +import { createAction } from 'redux-smart-actions'; + +const doSth = createAction('doSth'); + +const doSthElse = createAction('doSthElse', x => ({ x })); +``` + +which saves you any need to pass action type strings manually. + +### `createSmartThunk` + +The cousin of `createSmartAction`, the usage is the same, just use `createSmartThunk` +instead of `createThunk` and omit the first string argument - it will be again +interpolated from variable name you attach thunk to. ## Licence diff --git a/package.json b/package.json index a969b72..1a3ec50 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "redux-smart-actions", - "version": "0.1.0", + "version": "0.2.0", "description": "Redux addon to create actions and thunks with minimum boilerplate", "main": "lib/index.js", "module": "es/index.js", diff --git a/types/index.d.ts b/types/index.d.ts index e428883..5812317 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -5,7 +5,22 @@ export function createAction( export function createThunk( name: string, - thunk?: ( + thunk: ( + ...params: Input + ) => (dispatch: (action: any) => any, getState: () => any) => Output, +): ( + ...params: Input +) => ( + dispatch: (action: any) => any, + getState: () => any, +) => Output & { type: string }; + +export function createSmartAction( + action?: (...params: Input) => Output, +): (...params: Input) => Output & { type: string }; + +export function createSmartThunk( + thunk: ( ...params: Input ) => (dispatch: (action: any) => any, getState: () => any) => Output, ): (