{"_id":"redux-define","_rev":"6-26a74a6eee4797a86131b9d2f31f13f3","name":"redux-define","description":"Define action constants for Redux","dist-tags":{"latest":"1.1.1"},"versions":{"1.0.0":{"name":"redux-define","version":"1.0.0","description":"Define action constants for Redux","main":"lib/index.js","scripts":{"build:commonjs":"babel src --out-dir lib --ignore *-test.js","build:umd":"cross-env NODE_ENV=development webpack","build:umd:min":"cross-env NODE_ENV=production webpack","build":"npm run clean && npm run build:commonjs && npm run build:umd && npm run build:umd:min","clean":"rimraf lib","lint":"esw src webpack.config --color","lint:fix":"npm run lint -- --fix","lint:watch":"npm run lint -- --watch","prepublish":"npm run lint && npm run test && npm run build","test":"mocha --compilers js:babel-register src/**/*-test.js","test:watch":"npm run test -- --watch src/**/*-test.js"},"files":["lib","dist"],"keywords":["flux","redux","fsa","actions","define","constants"],"author":{"name":"Stephan Meijer","email":"stephan@meijer.ws"},"bugs":{"url":"https://github.com/smeijer/redux-define/issues"},"homepage":"https://github.com/smeijer/redux-define","repository":{"type":"git","url":"git+https://github.com/smeijer/redux-define.git"},"license":"MIT","devDependencies":{"babel-cli":"^6.7.7","babel-core":"^6.7.7","babel-eslint":"^6.1.1","babel-loader":"^6.2.4","babel-preset-es2015":"^6.6.0","babel-preset-stage-0":"^6.5.0","babel-register":"^6.7.2","chai":"^3.0.0","cross-env":"^2.0.0","eslint":"^2.8.0","eslint-config-airbnb-base":"^1.0.3","eslint-plugin-import":"^1.5.0","eslint-watch":"^2.1.13","mocha":"^2.2.5","rimraf":"^2.5.3","webpack":"^1.13.1"},"dependencies":{},"gitHead":"282229c4371fe0073f4b2a02f3e71a185601d1c4","_id":"redux-define@1.0.0","_shasum":"5e9b79fa0913649850561ac4ddc9ada8eb10dff7","_from":".","_npmVersion":"3.10.6","_nodeVersion":"4.5.0","_npmUser":{"name":"smeijer","email":"stephan.meijer@gmail.com"},"dist":{"shasum":"5e9b79fa0913649850561ac4ddc9ada8eb10dff7","tarball":"https://registry.npmjs.org/redux-define/-/redux-define-1.0.0.tgz","integrity":"sha512-OTEr+vTw6XlmBXs7DiCNp836L9rH24z6YrscyD+UtMcTsWh0+ekWozwcX3glKL9xqZhdQhvorPBgp9NyqYB7+g==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD4HwCJmQAcsU852yAeue66jbNy+YQ3l8YPQ9VCCBegJwIgLZA3+nNlCJOz+lyHZ5wqT82soPQ60YQVCpI7lUPGqrM="}]},"maintainers":[{"name":"smeijer","email":"stephan.meijer@gmail.com"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/redux-define-1.0.0.tgz_1479579903680_0.06190875358879566"}},"1.1.0":{"name":"redux-define","version":"1.1.0","description":"Define action constants for Redux","main":"lib/index.js","scripts":{"build:commonjs":"babel src --out-dir lib --ignore *-test.js","build:umd":"cross-env NODE_ENV=development webpack","build:umd:min":"cross-env NODE_ENV=production webpack","build":"npm run clean && npm run build:commonjs && npm run build:umd && npm run build:umd:min","clean":"rimraf lib","lint":"esw src webpack.config --color","lint:fix":"npm run lint -- --fix","lint:watch":"npm run lint -- --watch","prepublish":"npm run lint && npm run test && npm run build","test":"mocha --compilers js:babel-register src/**/*-test.js","test:watch":"npm run test -- --watch src/**/*-test.js"},"files":["lib","dist"],"keywords":["flux","redux","fsa","actions","define","constants"],"author":{"name":"Stephan Meijer","email":"stephan@meijer.ws"},"bugs":{"url":"https://github.com/smeijer/redux-define/issues"},"homepage":"https://github.com/smeijer/redux-define","repository":{"type":"git","url":"git+https://github.com/smeijer/redux-define.git"},"license":"MIT","devDependencies":{"babel-cli":"^6.7.7","babel-core":"^6.7.7","babel-eslint":"^6.1.1","babel-loader":"^6.2.4","babel-preset-es2015":"^6.6.0","babel-preset-stage-0":"^6.5.0","babel-register":"^6.7.2","chai":"^3.0.0","cross-env":"^2.0.0","eslint":"^2.8.0","eslint-config-airbnb-base":"^1.0.3","eslint-plugin-import":"^1.5.0","eslint-watch":"^2.1.13","mocha":"^2.2.5","rimraf":"^2.5.3","webpack":"^1.13.1"},"dependencies":{},"gitHead":"7b3efa177300f8a61bdbc27d3b513d526ed8827c","_id":"redux-define@1.1.0","_shasum":"76075c503e2fc13e4b9103f62e38f16c93b8be7b","_from":".","_npmVersion":"3.10.6","_nodeVersion":"4.5.0","_npmUser":{"name":"smeijer","email":"stephan.meijer@gmail.com"},"dist":{"shasum":"76075c503e2fc13e4b9103f62e38f16c93b8be7b","tarball":"https://registry.npmjs.org/redux-define/-/redux-define-1.1.0.tgz","integrity":"sha512-nwf3XNcjTYtjYMrEmGJaLE10GTCuV/8Lnw3TBbqYz2yGM+DqvWQqS8fwBRsQFVlkFwdcBKXKa/7Aw53t1qUgPA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEOu8TtEvmAvEjS4BHNpAA5jzgw7EHxBC+v7blsQLyHVAiARAvMB4sEWauxgg2FPhg36LLJwtMpA2kMBia10uVOKxQ=="}]},"maintainers":[{"name":"smeijer","email":"stephan.meijer@gmail.com"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/redux-define-1.1.0.tgz_1479650650611_0.6716893224511296"}},"1.1.1":{"name":"redux-define","version":"1.1.1","description":"Define action constants for Redux","main":"lib/index.js","scripts":{"build:commonjs":"babel src --out-dir lib --ignore *-test.js","build:umd":"cross-env NODE_ENV=development webpack","build:umd:min":"cross-env NODE_ENV=production webpack","build":"npm run clean && npm run build:commonjs && npm run build:umd && npm run build:umd:min","clean":"rimraf lib","lint":"esw src webpack.config --color","lint:fix":"npm run lint -- --fix","lint:watch":"npm run lint -- --watch","prepublish":"npm run lint && npm run test && npm run build","test":"mocha --compilers js:babel-register src/**/*-test.js","test:watch":"npm run test -- --watch src/**/*-test.js"},"files":["lib","dist"],"keywords":["flux","redux","fsa","actions","define","constants"],"author":{"name":"Stephan Meijer","email":"stephan@meijer.ws"},"bugs":{"url":"https://github.com/smeijer/redux-define/issues"},"homepage":"https://github.com/smeijer/redux-define","repository":{"type":"git","url":"git+https://github.com/smeijer/redux-define.git"},"license":"MIT","devDependencies":{"babel-cli":"^6.7.7","babel-core":"^6.7.7","babel-eslint":"^6.1.1","babel-loader":"^6.2.4","babel-plugin-transform-object-assign":"^6.22.0","babel-preset-es2015":"^6.6.0","babel-preset-stage-0":"^6.5.0","babel-register":"^6.7.2","chai":"^3.0.0","cross-env":"^2.0.0","eslint":"^2.8.0","eslint-config-airbnb-base":"^1.0.3","eslint-plugin-import":"^1.5.0","eslint-watch":"^2.1.13","mocha":"^2.2.5","rimraf":"^2.5.3","webpack":"^1.13.1"},"dependencies":{},"gitHead":"eaa7e586bf00e0a0e69bd72a8a0810014db6f68e","_id":"redux-define@1.1.1","_shasum":"98f97bfa45219a1d646a76b8512f421a96c289bf","_from":".","_npmVersion":"3.10.9","_nodeVersion":"4.6.2","_npmUser":{"name":"smeijer","email":"stephan.meijer@gmail.com"},"dist":{"shasum":"98f97bfa45219a1d646a76b8512f421a96c289bf","tarball":"https://registry.npmjs.org/redux-define/-/redux-define-1.1.1.tgz","integrity":"sha512-oBZCijWX8lbI0Yow/xT6Y6BHnWvGRLpv+qkxZBuF3b9M0ver9sowCPO+KWVrLjrUUJfOs4Ka2wAUoCihunB2HQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIH6/3zvtIAIFhSFM7u6Ufnwk4xN3jZ6nWojnERPC2pGTAiA0lvXVsmJmJWOvjcP2CkmGeIZPwmu9HJoFajJyRdZSOA=="}]},"maintainers":[{"name":"smeijer","email":"stephan.meijer@gmail.com"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/redux-define-1.1.1.tgz_1493024588246_0.5635502825025469"}}},"readme":"# redux-define\n\n[![Join the chat at https://gitter.im/smeijer/redux-define](https://badges.gitter.im/smeijer/redux-define.svg)](https://gitter.im/smeijer/redux-define?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)\n\n[![build status](https://img.shields.io/travis/smeijer/redux-define/master.svg?style=flat-square)][1]\n\n[![NPM](https://nodei.co/npm/redux-define.png?downloads=true)][7]\n\n\n## Installation\n\nwith npm:\n```bash\nnpm install --save redux-define\n```\n\nor yarn:\n```bash\nyarn add redux-define\n```\n\nIf you don’t use [npm][2], you may grab the latest [UMD][3] build from [unpkg][4] \n(either a [development][5] or a [production][6] build). The UMD build exports a \nglobal called `window.ReduxDefine` if you add it to your page via a `<script>` tag. \n\nWe *don’t* recommend UMD builds for any serious application, as most of the libraries \ncomplementary to Redux are only available on [npm][8].\n\n## Usage\n\n### `defineAction(type, ?[subactions], ?namespace)`\n\n```js\nimport { defineAction } from 'redux-define';\n```\n\nCreate a redux action type with one or more subactions:\n\n```js\nconst CREATE_TODO = defineAction('CREATE_TODO', ['ERROR', 'SUCCESS']);\n\n// result:\nconsole.log('' + CREATE_TODO);            // CREATE_TODO\nconsole.log('' + CREATE_TODO.ERROR);      // CREATE_TODO_ERROR\nconsole.log('' + CREATE_TODO.SUCCESS);    // CREATE_TODO_SUCCESS;\n```\n\nNamespaces can be used to separate actions through out modules and apps.\n\n```js\nconst CREATE_TODO = defineAction('CREATE_TODO', ['ERROR', 'SUCCESS'], 'my-app');\n\n// result:\nconsole.log('' + CREATE_TODO);            // my-app/CREATE_TODO\nconsole.log('' + CREATE_TODO.ERROR);      // my-app/CREATE_TODO_ERROR\nconsole.log('' + CREATE_TODO.SUCCESS);    // my-app/CREATE_TODO_SUCCESS;\n```\n\nIt's also possible to give in another constant as namespace for the new one.\n\n```js\nconst todos = defineAction('todos', ['LOADING', 'SUCCESS'], 'my-app');\nconst CREATE_TODO = defineAction('CREATE_TODO', ['ERROR', 'SUCCESS'], todos);\n\n// result:\nconsole.log('' + CREATE_TODO);            // my-app/todos/CREATE_TODO\nconsole.log('' + CREATE_TODO.ERROR);      // my-app/todos/CREATE_TODO_ERROR\nconsole.log('' + CREATE_TODO.SUCCESS);    // my-app/todos/CREATE_TODO_SUCCESS;\n```\n\nTo integrate better with other `redux` libraries, a special `ACTION` property is\nadded to the constant. [`redux-actions`][9] and [`redux-saga`][11] for example \ntreat actionTypes other than `string` specially. \n\nExtra benefit of this little feature, is that it makes the separation between\nuser actions and status updates more clear. Read more about this under \n[best practice](#best-practice) and [integrations](#integrations)\n\n```js\nconst CREATE_TODO = defineAction('CREATE_TODO', ['ERROR', 'SUCCESS']);\n\n// result:\nconsole.log('' + CREATE_TODO);            // CREATE_TODO\nconsole.log('' + CREATE_TODO.ACTION);     // CREATE_TODO\nconsole.log('' + CREATE_TODO.ERROR);      // CREATE_TODO_ERROR\nconsole.log('' + CREATE_TODO.SUCCESS);    // CREATE_TODO_SUCCESS;\n```\n\n### `actionType.defineAction(type, ?[subactions])`\n\nAs alternative syntax, we can use the `defineAction` method on defined constants.\nConstants defined in this way inherit their namespace. Making the namespace\nargument obsolete.\n\n```js\nconst myApp = defineAction('my-app');\nconst todos = myApp.defineAction('todos', ['LOADING', 'SUCCESS']);\nconst CREATE = todos.defineAction('CREATE', ['ERROR', 'SUCCESS']);\n```\n\nThis is the same as writing:\n\n```js\nconst myApp = defineAction('my-app');\nconst todos = defineAction('todos', ['LOADING', 'SUCCESS'], 'my-app');\nconst CREATE = todos.defineAction('CREATE', ['ERROR', 'SUCCESS'], todos);\n```\n\nOr if you only need the `CREATE` constant:\n\n```js\nconst CREATE = todos.defineAction('CREATE', ['ERROR', 'SUCCESS'], 'my-app/todos');\n```\n\nResult in these cases is the same. Except in the third case, where we only defined\nthe `CREATE` constant:\n\n```js\nconsole.log('' + myApp);                  // my-app\n\nconsole.log('' + todos);                  // my-app/todos\nconsole.log('' + todos.LOADING);          // my-app/todos_LOADING\nconsole.log('' + todos.SUCCESS);          // my-app/todos_SUCCESS\n\nconsole.log('' + CREATE);                 // my-app/todos/CREATE\nconsole.log('' + CREATE.ERROR);           // my-app/todos/CREATE_ERROR;\nconsole.log('' + CREATE.SUCCESS);         // my-app/todos/CREATE_SUCCESS;\n```\n\n### Best practice\nExtract general state constants into a separate file so they can easily be\nimported and shared across different modules:\n\n```js\n// stateConstants.js\nexport const LOADING = 'LOADING';\nexport const ERROR = 'ERROR';\nexport const SUCCESS = 'SUCCESS';\n```\n\n```js\n// app.js\nexport const myApp = defineAction('my-app');\n```\n\nIn the module; we can import the `stateConstants` and optionally parent modules\nto construct a namespace.\n\n```js\n// todos.js\nimport { defineAction } from 'redux-define';\nimport { LOADING, ERROR, SUCCESS } from './stateConstants';\nimport { myApp } from './app';\n\nconst todos = defineAction('todos', [LOADING, SUCCESS], myApp);\nconst CREATE = defineAction('CREATE', [ERROR, SUCCESS], todos);\n\n// result:\nconsole.log('' + myApp);                  // my-app\n\nconsole.log('' + todos);                  // my-app/todos\nconsole.log('' + todos.LOADING);          // my-app/todos_LOADING\nconsole.log('' + todos.SUCCESS);          // my-app/todos_SUCCESS\n\nconsole.log('' + CREATE);                 // my-app/todos/CREATE\nconsole.log('' + CREATE.ACTION);          // my-app/todos/CREATE\nconsole.log('' + CREATE.ERROR);           // my-app/todos/CREATE_ERROR\nconsole.log('' + CREATE.SUCCESS);         // my-app/todos/CREATE_SUCCESS\n```\n\nUse the `ACTION` constant in `dispatch` and in `saga watchers`. This makes it\nclear that an user or system `ACTION` is being handled. All other subtypes \nshould be `status` updates. They should be handled trough `thunks` or `sagas`, \nbut never dispatched by a user.  Although it is possible to handle user actions \nin the reducer directly, the advice is to not do this. Keep clear separation \nbetween user actions and reducer actions.\n\n### Implementation example\n\n##### stateConstants.js\n\n```js\nexport const CANCELLED = 'CANCELLED';\nexport const ERROR     = 'ERROR';\nexport const PENDING   = 'PENDING';\nexport const SUCCESS   = 'SUCCESS';\n```\n\n##### actionTypes.js\n\n```js\nimport { defineAction } from 'redux-define';\nimport { CANCELLED, ERROR, PENDING, SUCCESS } from './stateConstants';\n\nexport const DELETE_COMMENT = defineAction('DELETE_COMMENT',\n\t[CANCELLED, ERROR, PENDING, SUCCESS], 'comments');\n```\n\n##### actions.js\n\n```js\nimport { createAction } from 'redux-actions';\nimport { DELETE_COMMENT } from './actionTypes';\n\nexport const deleteComment = createAction(DELETE_COMMENT.ACTION);\n```\n\n##### reducer.js\n\n```js\nimport { handleActions, combineActions } from 'redux-actions';\nimport { DELETE_COMMENT } from './actionTypes';\n\nconst initialState = {\n  isDeleting: false,\n};\n\nconst reducer = handleActions({\n  [DELETE_COMMENT.PENDING]: state => ({\n    ...state,\n    isDeleting: true,\n  }),\n\n  [combineActions(\n    DELETE_COMMENT.CANCELLED,\n    DELETE_COMMENT.SUCCESS,\n    DELETE_COMMENT.ERROR,\n  )]: state => ({\n    ...state,\n    isDeleting: false,\n  }),\n}, initialState);\n```\n\n##### sagas.js\n\n```js\nimport { call, put, take } from 'redux-saga/effects';\nimport deleteAPI from 'somewhere-out-of-this-scope';\nimport { DELETE_COMMENT } from './actionTypes';\n\nexport function* deleteComment({ payload }) {\n  try {\n    yield put({ type: DELETE_COMMENT.PENDING });\n    const { data } = yield call(deleteAPI, payload);\n    yield put({ type: DELETE_COMMENT.SUCCESS, payload: data });\n  }\n  catch (error) {\n    yield put({ type: DELETE_COMMENT.ERROR, payload: { error: error.message } });\n  }\n}\n```\n\n##### watchers.js\n\n```js\nimport { takeEvery } from 'redux-saga';\nimport { fork } from 'redux-saga/effects';\n\nimport { DELETE_COMMENT } from './actionTypes';\nimport * as s from './sagas';\n\nfunction* deleteCommentWatcher() {\n  yield* takeEvery(DELETE_COMMENT.ACTION, s.deleteComment);\n}\n\nexport default function* () {\n  yield [\n    fork(deleteCommentWatcher),\n  ];\n}\n\n```\n\n### Why use `redux-define`? \nThis library reduces a lot of the boilerplate that comes with defining redux\naction types. This library is created as solution to [organizing large ducks][10]\nLet's show the difference here. See above for a full [implementation example](#implementation-example).\nWhen using `ducks`, some of the files in the example above should be joined into\na single duck file.\n\nWithout using `redux-define`\n\n```js\nconst CREATE_TODO = 'CREATE_TODO';\nconst CREATE_TODO_PENDING = 'CREATE_TODO_PENDING';\nconst CREATE_TODO_ERROR = 'CREATE_TODO_ERROR';\nconst CREATE_TODO_SUCCESS = 'CREATE_TODO_SUCCESS';\n\nconst DELETE_TODO = 'DELETE_TODO';\nconst DELETE_TODO_PENDING = 'DELETE_TODO_PENDING';\nconst DELETE_TODO_CANCELLED = 'DELETE_TODO_CANCELLED';\nconst DELETE_TODO_ERROR = 'DELETE_TODO_ERROR';\nconst DELETE_TODO_SUCCESS = 'DELETE_TODO_SUCCESS';\n```\n\nWith `redux-define`\n\n```js\nimport { defineAction } from 'redux-define';\nimport { PENDING,  CANCELLED, ERROR, SUCCESS } from '/lib/stateConstants.js';\n\nconst CREATE_TODO = defineAction('CREATE_TODO', [PENDING, ERROR, SUCCESS]);\nconst DELETE_TODO = defineAction('DELETE_TODO', [PENDING, CANCELLED, ERROR, SUCCESS]);\n```\n\n### Integrations\n\nCreated constants can be directly used in [`sagas`][11] `reducers`, or together \nwith [`redux-actions`][9].\n\nSee [implementation example](#implementation-example) in this readme for implementation\ndetails. We handle [`redux-actions`][9] in [actions.js](#actionsjs) and \n[reducer.js](#reducerjs) and [`redux-saga`][11] in [watchers.js](#watchersjs)\nand [sagas.js](#sagasjs).\n\n[1]: https://travis-ci.org/smeijer/redux-define\n[2]: https://www.npmjs.com\n[3]: https://unpkg.com/redux-define@latest/dist\n[4]: https://unpkg.com\n[5]: https://unpkg.com/redux-define@latest/dist/redux-define.js\n[6]: https://unpkg.com/redux-define@latest/dist/redux-define.min.js\n[7]: https://nodei.co/npm/redux-define\n[8]: https://www.npmjs.com/search?q=redux\n[9]: https://github.com/acdlite/redux-actions\n[10]: https://github.com/erikras/ducks-modular-redux/issues/16\n[11]: https://github.com/yelouafi/redux-saga\n","maintainers":[{"name":"smeijer","email":"stephan.meijer@gmail.com"}],"time":{"modified":"2022-06-26T10:05:00.626Z","created":"2016-11-19T18:25:05.870Z","1.0.0":"2016-11-19T18:25:05.870Z","1.1.0":"2016-11-20T14:04:12.478Z","1.1.1":"2017-04-24T09:03:09.068Z"},"homepage":"https://github.com/smeijer/redux-define","keywords":["flux","redux","fsa","actions","define","constants"],"repository":{"type":"git","url":"git+https://github.com/smeijer/redux-define.git"},"author":{"name":"Stephan Meijer","email":"stephan@meijer.ws"},"bugs":{"url":"https://github.com/smeijer/redux-define/issues"},"license":"MIT","readmeFilename":"README.md","users":{"smeijer":true}}