{"_id":"@cromwellian/redux-promise-middleware-actions","_rev":"2-7f37dcdb1fcdb26f7b7aef44c10c9cbc","name":"@cromwellian/redux-promise-middleware-actions","dist-tags":{"latest":"2.1.1"},"versions":{"2.1.0":{"name":"@cromwellian/redux-promise-middleware-actions","version":"2.1.0","description":"Redux action creator for making async actions with redux-promise-middleware","main":"lib/index.js","types":"lib/index.d.ts","scripts":{"lint":"tslint '{src,test}/**/*.ts' --format stylish","prepublishOnly":"tsc","test":"npm run lint && nyc mocha"},"repository":{"type":"git","url":"git+https://github.com/cromwellian/redux-promise-middleware-actions.git"},"keywords":["redux","actions","promise","async"],"author":{"name":"actions"},"license":"MIT","bugs":{"url":"https://github.com/omichelsen/redux-promise-middleware-actions/issues"},"homepage":"https://github.com/omichelsen/redux-promise-middleware-actions#readme","devDependencies":{"@types/mocha":"^5.2.5","@types/node":"^10.7.1","mocha":"^5.2.0","nyc":"^12.0.2","ts-node":"^7.0.1","tslint":"^5.11.0","typescript":"^3.0.1"},"nyc":{"extension":[".ts"],"include":["src/**/*.ts"],"reporter":["lcov","text-summary"],"sourceMap":true,"instrument":true},"gitHead":"001b24ad9c94f9787cc5d53c2d65219e12d98567","_id":"@cromwellian/redux-promise-middleware-actions@2.1.0","_npmVersion":"6.4.1","_nodeVersion":"10.13.0","_npmUser":{"name":"cromwellian","email":"cromwellian@gmail.com"},"dist":{"integrity":"sha512-Hq3Aix5imfCDaYNwWa936lrve0H1w6+1BdvJZEDgWc0l2TUvELI0fT6RSGxCBOizI8nHTY4HP9E45EF927jHqg==","shasum":"0f2a62d871db2860540b183c4528442641593889","tarball":"https://registry.npmjs.org/@cromwellian/redux-promise-middleware-actions/-/redux-promise-middleware-actions-2.1.0.tgz","fileCount":23,"unpackedSize":27581,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJb6gFmCRA9TVsSAnZWagAA7l0P/2RRSMGYxhxtGuv0ybYA\nnx6807Q9eml4RSnmGNBd91Zi5Yoj5zQFanLxiA+K5c6ZYcrj417j45NqftU1\nWnl8YfEz2zhuWOJThRWnyDzwrrCA7ff3gD6RYZ2Lmu4htyuKtf51XmiXlUE+\n2HXIaaOhb2OcX9/LaHb/FxNGtVgDMVE32HYuDGX7D26219bxe8IhIrNITddJ\nx48Xxc3NLd+iwuYoltkJvoJH1ALY6IXuAbqgdGnZNgO7E9CBn9vDc4AwBQjN\nWxroLY8PE+GB+AKW4oiUcMtdENrqt/J0CZbAjE2k7qoHq9w8QGwZnx72bofk\ny6lgICsqhD3b722thNFuNti01xufcfBS/AnrzL6Rm6l15D6m4fWhScqcZQDz\nwwm0CrazTWD1JYJ1QLG26Gom3DvAI5umvgdlwPVBVQqyWRYS79U/Nj79Yu2l\nkjrpM1TiBqIJXZFEgybOdH61kgHOoTlKFKmrki40afgf8ZZCWNIHYV5rl0FE\nJnQ6HUr3ckuIUBpwqrTJPxKcVseKgJ+AL9b3zIYUEEdsFLDufajV8iWWgPE0\nRHb6t0fAJo/Wk5rUAQ/IuxY79C8fglFRLZakZ1hW5cl7TKbblDpSEFdpl8v9\nJ02FRKXfaODZpJ+EA7H2Gg9At0fsP6cfJNkiEF46y2P0499VgfciYKJpiCDu\nS1Fe\r\n=rLRH\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFD+JabH8sh9Ol96WDPx515LyN8jeO3f8W7OJTd69raiAiBHgyyoZmVMjTiBv03+CgoYNm9XymNECjKNzmWFtW9UUA=="}]},"maintainers":[{"name":"cromwellian","email":"cromwellian@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/redux-promise-middleware-actions_2.1.0_1542062437829_0.8993410700131406"},"_hasShrinkwrap":false},"2.1.1":{"name":"@cromwellian/redux-promise-middleware-actions","version":"2.1.1","description":"Redux action creator for making async actions with redux-promise-middleware","main":"lib/index.js","types":"lib/index.d.ts","scripts":{"lint":"tslint '{src,test}/**/*.ts' --format stylish","prepublishOnly":"tsc","test":"npm run lint && nyc mocha"},"repository":{"type":"git","url":"git+https://github.com/cromwellian/redux-promise-middleware-actions.git"},"keywords":["redux","actions","promise","async"],"author":{"name":"actions"},"license":"MIT","bugs":{"url":"https://github.com/omichelsen/redux-promise-middleware-actions/issues"},"homepage":"https://github.com/omichelsen/redux-promise-middleware-actions#readme","devDependencies":{"@types/mocha":"^5.2.5","@types/node":"^10.7.1","mocha":"^5.2.0","nyc":"^12.0.2","ts-node":"^7.0.1","tslint":"^5.11.0","typescript":"^3.0.1"},"nyc":{"extension":[".ts"],"include":["src/**/*.ts"],"reporter":["lcov","text-summary"],"sourceMap":true,"instrument":true},"gitHead":"a5cbe12130026403ccd40f5360287bb58872c8fe","_id":"@cromwellian/redux-promise-middleware-actions@2.1.1","_npmVersion":"6.4.1","_nodeVersion":"10.13.0","_npmUser":{"name":"cromwellian","email":"cromwellian@gmail.com"},"dist":{"integrity":"sha512-nWIaKYjVByJzqM1HIOKOn9EP4uxn2hF+v0mfTjFbuFkaPZ7DjKIxWuyQljnhkkh8CZ5KuwQGIpy20GY7YUmq+Q==","shasum":"35ed67b7d2183fbb1cba0ff912deaa753533c738","tarball":"https://registry.npmjs.org/@cromwellian/redux-promise-middleware-actions/-/redux-promise-middleware-actions-2.1.1.tgz","fileCount":23,"unpackedSize":27215,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJb6gJsCRA9TVsSAnZWagAAMsIP/32ikGYqm6H+yY3B20xM\nsURv0nzoCnyC/hAjEJm391cwzhlLZ9A0619bEJ9qnaXnd2lV+QNF9YFokwdD\n03i994xbcw+znqWGmzExzqiHo+f5uosu2fv8jTq1yLZ8pjJv0PwAmghpfFuo\ne0JceCS96vcFybt5B4UfEOig4EMO2K1PEh0V3mJSRPB5gzFA12InyJJBM6O2\nVOgR8tK35aR22vrHnGlqyIDJhOgSfUtYR44xvZO+V223oGY4aIThC5LUDlim\nuIfMzM0yBh9yx3//D5bs5t1dJXqjNT6RzcdrPWu5SdtmsHm29gHjHDJxR7Gn\nFcUZ0rtYlBrmFsnbDS4vSTUmw+7S0qJDbQXoQcUVLWG50ooG9lJ6A5kpQSEq\nqt0mRg1KnSlVcMcqya6A4DH0zWieKQQ8FLbCE46fXFIWNbwdrkRaLpEtA8sK\nsVSZz253BPWEscDZjLsA85vUENj6DJNVlSaGKN8BaG4jNJYgEQjdHYGVxodZ\nNtOofR6ic7YC1ySjo4YS6JNHHvWtXdd4p4qUFCaqAcAvA1EQGW1uGF41HdLd\nsJS0FVSgTInFAk/mu6hzlNs9YP81W+HWBK0LscgVKwaRK8KWjBlMYhuHuVHZ\n5J8k08Lp6A6+/NEmMJj5Vci0i7a/R9nAF7GCIXO7VzT9KoNtplL12fKSeGjn\nR3bq\r\n=VWQT\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCID3xpuWTsV9PDR+LjJBLcQ+g66tm+8qT1nytBtPiEEQiAiEAom+s1SJdgfdHogeW3txtdajHF+Sw6BxpH6fbdZoZoIg="}]},"maintainers":[{"name":"cromwellian","email":"cromwellian@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/redux-promise-middleware-actions_2.1.1_1542062700126_0.8841217472378609"},"_hasShrinkwrap":false}},"time":{"created":"2018-11-12T22:40:37.662Z","2.1.0":"2018-11-12T22:40:37.949Z","modified":"2022-04-05T01:51:38.143Z","2.1.1":"2018-11-12T22:45:00.384Z"},"maintainers":[{"name":"cromwellian","email":"cromwellian@gmail.com"}],"description":"Redux action creator for making async actions with redux-promise-middleware","homepage":"https://github.com/omichelsen/redux-promise-middleware-actions#readme","keywords":["redux","actions","promise","async"],"repository":{"type":"git","url":"git+https://github.com/cromwellian/redux-promise-middleware-actions.git"},"author":{"name":"actions"},"bugs":{"url":"https://github.com/omichelsen/redux-promise-middleware-actions/issues"},"license":"MIT","readme":"# redux-promise-middleware-actions\n\n[![Build Status](https://img.shields.io/travis/omichelsen/redux-promise-middleware-actions/master.svg)](https://travis-ci.org/omichelsen/redux-promise-middleware-actions)\n[![Coverage Status](https://coveralls.io/repos/omichelsen/redux-promise-middleware-actions/badge.svg?branch=master&service=github)](https://coveralls.io/github/omichelsen/redux-promise-middleware-actions?branch=master)\n[![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/redux-promise-middleware-actions.svg)](https://bundlephobia.com/result?p=redux-promise-middleware-actions)\n\nCreate Redux actions with a `type` and `payload` in a standardized way. Inspired by [redux-actions](https://www.npmjs.com/package/redux-actions) but simpler and with special support for asynchronous actions (promises).\n\nHas no dependencies and is tiny (~680 bytes gzipped). First class TypeScript support.\n\nWorks with [redux-promise-middleware](https://www.npmjs.com/package/redux-promise-middleware) to handle asynchronous actions by dispatching `pending`, `fulfilled` and `rejected` events based on the state of the input promise.\n\nGoals of this library:\n\n* Reference action creators directly - no need to maintain an action type enum/list\n* Automatically generate actions for pending, fulfilled and rejected outcomes of a promise payload\n* Have statically typed access to all action types - no need to manually add a type suffix like \"_PENDING\"\n* TypeScript support so asynchronous actions can't be confused for normal synchronous actions\n\nNote: If you are using TypeScript this library requires TypeScript 3. For TypeScript 2 use version 1 of this library.\n\n## Installation\n\nYou need to install this library as well as [redux-promise-middleware](https://www.npmjs.com/package/redux-promise-middleware).\n\n```\nnpm install redux-promise-middleware-actions redux-promise-middleware\n```\n\nInclude redux-promise-middleware when you create your store:\n\n```js\nimport promiseMiddleware from 'redux-promise-middleware';\n\ncomposeStoreWithMiddleware = applyMiddleware(\n  promiseMiddleware(),\n)(createStore);\n```\n\n## Usage\n\n### Synchronous action\n\nSynchronous actions works exactly like redux-actions. You supply a function that returns whatever payload the action should have (if any).\n\n```js\nimport { createAction } from 'redux-promise-middleware-actions';\n\nexport const foo = createAction('FOO', (num) => num);\n\ndispatch(foo(5)); // { type: 'FOO', payload: 5 }\n```\n\nWhen handling the action in a reducer, you simply cast the action function to a string to return the type. This ensures type safety (no spelling errors) and you can use code navigation to find all uses of an action.\n\n```js\nconst fooType = String(foo); // 'FOO'\n```\n\n### Asynchronous action\n\nWhen you create an asynchronous action you need to return a promise payload. If your action is called `FOO` the following events will be dispatched:\n\n1. `FOO_PENDING` is dispatched immediately\n2. `FOO_FULFILLED` is dispatched when the promise is resolved\n    * ... or `FOO_REJECTED` is dispatched instead if the promise is rejected\n\n```js\nimport { createAsyncAction } from 'redux-promise-middleware-actions';\n\nexport const fetchData = createAsyncAction('FETCH_DATA', async () => {\n  const res = await fetch(...);\n  return res.json();\n});\n\ndispatch(fetchData()); // { type: 'FETCH_DATA_PENDING' }\n```\n\nAn async action function has three properties to access the possible outcome actions: `pending`, `fulfilled` and `rejected`. You can dispatch them directly (in tests etc.):\n\n```js\ndispatch(fetchData.pending());          // { type: 'FETCH_DATA_PENDING' }\ndispacth(fetchData.fulfilled(payload)); // { type: 'FETCH_DATA_FULFILLED', payload: ... }\ndispacth(fetchData.rejected(err));      // { type: 'FETCH_DATA_REJECTED', payload: err, error: true }\n```\n\nBut normally you only need them when you are writing reducers:\n\n```js\ncase String(fetchData.pending):   // 'FETCH_DATA_PENDING'\ncase String(fetchData.fulfilled): // 'FETCH_DATA_FULFILLED'\ncase String(fetchData.rejected):  // 'FETCH_DATA_REJECTED'\n```\n\nNote that if you try and use the base function in a reducer, an error will be thrown to ensure you are not listening for an action that will never happen:\n\n```js\ncase String(fetchData): // throws an error\n```\n\n### Async reducer\n\nYou can now handle the different events in your reducer by referencing the possible outcome states:\n\n```js\nimport { fetchData } from './actions';\n\nexport default (state, action) => {\n  switch (action.type) {\n    case String(fetchData.pending):\n      return {\n        ...state,\n        pending: true,\n      };\n    case String(fetchData.fulfilled):\n      return {\n        ...state,\n        data: action.payload,\n        error: undefined,\n        pending: false,\n      };\n    case String(fetchData.rejected):\n      return {\n        ...state,\n        error: action.payload,\n        pending: false,\n      };\n    default:\n      return state;\n  }\n};\n```\n\n#### Async reducer helper\n\nIt can get tedious writing the same reducer for every single async action so we've included a simple reducer that does the same as the example above:\n\n```js\nimport { asyncReducer } from 'redux-promise-middleware-actions';\nimport { fetchData } from './actions';\n\nexport default asyncReducer(fetchData);\n```\n\nYou can also combine it with an existing reducer:\n\n```js\nimport { asyncReducer } from 'redux-promise-middleware-actions';\nimport { fetchData } from './actions';\n\nconst fetchReducer = asyncReducer(fetchData);\n\nexport default (state, action) => {\n  const newState = fetchReducer(state, action);\n\n  switch (action.type) {\n    case 'SOME_OTHER_ACTION':\n      return { ... };\n    default:\n      return newState;\n  }\n};\n```\n\n### Metadata\n\nYou can add metadata to any action by supplying an additional metadata creator function. The metadata creator will receive the same arguments as the payload creator:\n\n#### `createAction(type, payloadCreator, metadataCreator)`\n\n```js\nexport const foo = createAction(\n  'FOO',\n  (num) => num,\n  (num) => num + num\n);\n\ndispatch(foo(5)); // { type: 'FOO', meta: 10, payload: 5 }\n```\n\n#### `createAsyncAction(type, payloadCreator, metadataCreator)`\n\n```js\nexport const fetchData = createAsyncAction(\n  'FETCH_DATA',\n  (n: number) => fetch(...),\n  (n: number) => ({ n })\n);\n\ndispatch(fetchData(42));\n// { type: 'FETCH_DATA_PENDING', meta: { n: 42 } }\n// { type: 'FETCH_DATA_FULFILLED', meta: { n: 42 }, payload: Promise<...> }\n// { type: 'FETCH_DATA_REJECTED', meta: { n: 42 }, payload: Error(...) }\n```\n","readmeFilename":"README.md"}