redux-create-actiontype
Micro library for easy Action Types generation in Redux/React.
Install
$ npm install redux-create-actiontype --save
Usage
Basic Usage
Create a basic object with Action Types:
; const types = 'LOGIN' 'LOGOUT' 'IS_FETCHING' 'CREATE_ACTION' 'USER': api: true ; /* it is going to generateconst types = { LOGIN: 'LOGIN', LOGOUT: 'LOGOUT', IS_FETCHING: 'IS_FETCHING', CREATE_ACTION: 'CREATE_ACTION', USER_FETCHING: 'USER_FETCHING', USER_SUCCESS: 'USER_SUCCESS', USER_ERROR: 'USER_ERROR'};*/
A nested example which is closer to a real world project:
const types = 'login' 'logout' 'fetch': api: true 'dialog': postfixes: 'open' 'close' 'send email' 'eat borscht' ; /* it is going to generateconst types = { MY_APP_LOGIN: 'MY_APP_LOGIN', MY_APP_LOGOUT: 'MY_APP_LOGOUT', MY_APP_USERS_FETCH_FETCHING: 'MY_APP_USERS_FETCH_FETCHING', MY_APP_USERS_FETCH_SUCCESS: 'MY_APP_USERS_FETCH_SUCCESS', MY_APP_USERS_FETCH_ERROR: 'MY_APP_USERS_FETCH_ERROR', MY_APP_USERS_DIALOG_OPEN: 'MY_APP_USERS_DIALOG_OPEN', MY_APP_USERS_DIALOG_CLOSE: 'MY_APP_USERS_DIALOG_CLOSE', MY_APP_USERS_SEND_EMAIL: 'MY_APP_USERS_SEND_EMAIL', MY_APP_USERS_EAT_BORSCHT: 'MY_APP_USERS_EAT_BORSCHT'};*/
Advanced Usage:
Generating API Action Types
const types = 'login' 'logout' 'user': api: true ; /* it is going to generateconst types = { MY_APP_LOGIN: 'MY_APP_LOGIN', MY_APP_LOGOUT: 'MY_APP_LOGOUT', MY_APP_USER_FETCHING: 'MY_APP_USER_FETCHING', MY_APP_USER_SUCCESS: 'MY_APP_USER_SUCCESS', MY_APP_USER_ERROR: 'MY_APP_USER_ERROR',};*/
Pre-define custom API postfixes
const types = 'login' 'logout' 'user': api: true 'items': api: true ; /* it is going to generateconst types = { MY_APP_LOGIN: 'MY_APP_LOGIN', MY_APP_LOGOUT: 'MY_APP_LOGOUT', MY_APP_USER_OK: 'MY_APP_USER_OK', MY_APP_USER_BAD: 'MY_APP_USER_BAD', MY_APP_ITEMS_OK: 'MY_APP_ITEMS_OK', MY_APP_ITEMS_BAD: 'MY_APP_ITEMS_BAD'};*/
Custom postfixes for a custom Action Type
const types = 'login' 'logout' 'dialog': postfixes: 'open' 'close' ; /* it is going to generateconst types = { MY_APP_LOGIN: 'MY_APP_LOGIN', MY_APP_LOGOUT: 'MY_APP_LOGOUT', MY_APP_DIALOG_OPEN: 'MY_APP_DIALOG_OPEN', MY_APP_DIALOG_CLOSE: 'MY_APP_DIALOG_CLOSE'};*/ // OR// the following code yields the same resultsconst types = 'login' 'logout' 'open' 'close' ;
(support for another style) Set a lower-case prefix with / as a separator
const types = 'login' 'logout'; /* it is going to generateconst types = { 'my_app/LOGIN': 'my_app/LOGIN', 'my_app/LOGOUT': 'my_app/LOGOUT'};*/
deep structured actions
const types = 'login' 'logout' 'open' 'close' 'change' 'on focus' ; /* it is going to generateconst types = { MY_APP_LOGIN: 'MY_APP_LOGIN', MY_APP_LOGOUT: 'MY_APP_LOGOUT', MY_APP_DIALOG_OPEN: 'MY_APP_DIALOG_OPEN', MY_APP_DIALOG_CLOSE: 'MY_APP_DIALOG_CLOSE', MY_APP_DIALOG_INPUT_CHANGE: 'MY_APP_DIALOG_INPUT_CHANGE', MY_APP_DIALOG_INPUT_ON_FOCUS: 'MY_APP_DIALOG_INPUT_ON_FOCUS'};*/
Options
const types = arrayOfTypes;
Default API Postfixes
const defaultAPIPostfixes = 'FETCHING' 'ERROR' 'SUCCESS';
Full list of examples
You can find more examples in my test file.
Feedback
For any ideas, suggestion or bugs feel free to ping me or create a ticket right here.
License
MIT license; see LICENSE.