{"id":15090151,"url":"https://github.com/lstkz/action-creators","last_synced_at":"2025-10-05T23:31:34.827Z","repository":{"id":57172759,"uuid":"92157642","full_name":"lstkz/action-creators","owner":"lstkz","description":"deprecated ","archived":true,"fork":false,"pushed_at":"2017-05-25T14:43:27.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-25T12:44:32.110Z","etag":null,"topics":["logging","redux","validation"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lstkz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-23T09:59:09.000Z","updated_at":"2023-01-28T20:27:13.000Z","dependencies_parsed_at":"2022-08-24T14:41:07.415Z","dependency_job_id":null,"html_url":"https://github.com/lstkz/action-creators","commit_stats":null,"previous_names":["lsentkiewicz/action-creators"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lstkz%2Faction-creators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lstkz%2Faction-creators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lstkz%2Faction-creators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lstkz%2Faction-creators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lstkz","download_url":"https://codeload.github.com/lstkz/action-creators/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235461364,"owners_count":18994067,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["logging","redux","validation"],"created_at":"2024-09-25T09:22:26.139Z","updated_at":"2025-10-05T23:31:29.535Z","avatar_url":"https://github.com/lstkz.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# action-creators\n[![Build Status](https://travis-ci.org/lsentkiewicz/action-creators.svg?branch=master)](https://travis-ci.org/lsentkiewicz/action-creators)\n[![codecov](https://codecov.io/gh/lsentkiewicz/action-creators/branch/master/graph/badge.svg)](https://codecov.io/gh/lsentkiewicz/action-creators)\n\nAction creators utilities for Redux.  \nFeatures:\n- Parameter validation using [joi](https://www.npmjs.com/package/joi).\n- Debug created actions using [debug](https://www.npmjs.com/package/debug).\n- No need to define constants.\n- No duplicated namespaces.\n- No duplicated action names.  \n\nNotes:\n- It doesn't work with [redux-thunk](https://www.npmjs.com/package/redux-thunk), but it's designed to work with [redux-logic](https://www.npmjs.com/package/redux-logic).\n\n## Installation\n```bash\nnpm i --save action-creators joi joi-browser\n```\nAdd to webpack.config.js\n```js\nresolve: {\n  alias: {\n    joi: 'joi-browser'\n  }\n}\n```\n\n## Enable debugging\nAdd to your your app.\n```js\nif (process.env.NODE_ENV !== 'production') {\n  process.env.DEBUG = 'ac:*';\n}\n```\nOr in webpack.config.js\n```js\nplugins: [\n  new webpack.DefinePlugin({\n    'process.env': {\n      DEBUG: JSON.stringify(process.env.NODE_ENV !== 'production' ? 'ac:*' : ''),\n    },\n  }),\n],\n```\n\n## Quick demo\n\n```js\n// user-actions.js\n\nimport createNamespace from 'action-creators';\n\nconst {createAction} = createNamespace('USERS');\n\nconst usersLoaded = createAction('USERS_LOADED',\n  ['keyword', 'items', 'pageNumber', 'pageSize'],\n  {\n    keyword: Joi.string().required(),\n    items: Joi.array().required(),\n    pageNumber: Joi.number().required(),\n    pageSize: Joi.number().required(),\n  }\n);\nexpect(usersLoaded('john', [{id: 1, name: 'john'}], 1, 10)).toEqual({\n  type: 'USERS/USERS_LOADED',\n  payload: {\n    keyword: 'john',\n    items: [{id: 1, name: 'john'}],\n    pageNumber: 1,\n    pageSize: 10,\n  },\n});\n```\n\nConsole output:\n```\nac:USERS USERS_LOADED { keyword: 'john', items: [ { id: 1, name: 'john' } ], pageNumber: 1, pageSize: 10 } +0ms\n```\n\nError reporting\n```js\nusersLoaded('john', -2, 1, 10)\n```\n\nthrows an error\n```\nValidationError: Validation failed for: \"USERS/USERS_LOADED\" {\n  \"keyword\": \"john\",\n  \"pageNumber\": 1,\n  \"pageSize\": 10,\n  \"items\" [1]: -2\n}\n\n[1] \"items\" must be an array\n\n```\n\n## Usage with reducers\n\n```js\n// user-reducer.js\nimport {usersLoaded} from './user-actions';\n\nfunction reducer(state = {}, action) {\n  switch (action.type) {\n    case usersLoaded.toString(): \n      return {\n        ...state,\n        ...action.payload,\n      }\n    default:\n      return state;\n  }\n}\n```\n\nYou can also use `handleActions` from [redux-actions](http://npmjs.com/package/redux-actions).\n\n## Motivation\n- During development, action creators can be called with invalid or missing arguments, and it usually causes errors in the reducer function.  \n- When using `createAction` from `redux-actions`, it's not obvious if the action creator requires any arguments.  \n   For example:  \n   `increment = createAction('INCREMENT');`  \n   You don't know if you should call `increment()` or `increment(something)`. In such situation, you always must check the expected payload in the reducer.\n- I needed a fast way to debug created actions. There are existing libraries for logging like [redux-logger](http://npmjs.com/package/redux-logger),\n  but it can sometimes be not convenience. You must expand 3 levels of an object to see the action payload.\n  It's much more readable if the action payload is logged in a single line.\n\n## API\n- `createNamespace(namespace)`\n  - Parameters:  \n     - `namespace: String` The namespace prefix for all action types. All namespaces must be unique otherwise an error will be thrown.  \n   - Returns:  \n    `{createAction: Function}` Return an object with a `createAction` property.\n- `createAction(type, argNames, schema, transform)`  \n  - Parameters:\n     - `type: String` The action type. All namespaces must be unique otherwise an error will be thrown.\n     - `argNames: Array` An array with arguments.\n     - `schema: Object` A Joi schema. Must be an object containing all props from the `argNames` array.  \n     - `transform: Function(Object)` An optional function to transform the created action. You can use it to change payload or metadata.\n  - Returns:\n     - `Function` The action creator\n\n\n\nMIT License\n\nCopyright (c) 2017 Łukasz Sentkiewicz","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flstkz%2Faction-creators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flstkz%2Faction-creators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flstkz%2Faction-creators/lists"}