Skip to content
This repository has been archived by the owner on Nov 23, 2020. It is now read-only.

Commit

Permalink
feat(creators): generate action types based on config
Browse files Browse the repository at this point in the history
take a config object and an entity and return action creators and types
  • Loading branch information
Vince Speelman committed Sep 16, 2018
0 parents commit 1917781
Show file tree
Hide file tree
Showing 9 changed files with 8,553 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": [
"env",
"stage-0"
]
}
57 changes: 57 additions & 0 deletions .cz-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module.exports = {
types: [
{
value: 'feat',
name: 'feat: A new feature',
},
{
value: 'fix',
name: 'fix: A bug fix',
},
{
value: 'docs',
name: 'docs: Documentation only changes',
},
{
value: 'style',
name:
'style: Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc)',
},
{
value: 'refactor',
name:
'refactor: A code change that neither fixes a bug nor adds a feature',
},
{
value: 'perf',
name: 'perf: A code change that improves performance',
},
{
value: 'test',
name: 'test: Adding missing tests',
},
{
value: 'chore',
name:
'chore: Changes to the build process or auxiliary tools\n and libraries such as documentation generation',
},
{
value: 'revert',
name: 'revert: Revert to a commit',
},
{
value: 'WIP',
name: 'WIP: Work in progress',
},
],
scopes: [
{ name: 'config' },
{ name: 'creators' },
{ name: 'types' },
{ name: 'dependencies' },
{ name: 'build' },
],
allowCustomScopes: true,
footerPrefix: 'closes ',
allowBreakingChanges: ['feat', 'fix'],
};
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
yarn-error.log
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
language: node_js

node_js:
- 10

jobs:
include:
- stage: test
script: yarn test:fast --ci --colors
- stage: Produce Coverage
node_js: node
script: jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
- stage: build
script: yarn build
- stage: release
node_js: lts/*
deploy:
provider: script
skip_cleanup: true
script:
- yarn semantic-release
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Async Redux Action Creators

[![Travis](https://img.shields.io/travis/VinSpee/async-redux-action-creators.svg?style=flat-square)](https://travis-ci.org/VinSpee/async-redux-action-creators)
[![Tested with Jest](https://img.shields.io/badge/tested_with-jest-99424f.svg?longCache=true&style=flat-square)](https://github.com/facebook/jest)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?longCache=true&style=flat-square)](https://github.com/semantic-release/semantic-release)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?longCache=true&style=flat-square)](http://commitizen.github.io/cz-cli/)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg?longCache=true&style=flat-square)](https://conventionalcommits.org)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg?longCache=true&style=flat-square)](https://github.com/prettier/prettier)
[![license](https://img.shields.io/github/license/VinSpee/async-redux-action-creators.svg?longCache=true&style=flat-square)](https://github.com/VinSpee/async-redux-action-creators/blob/master/LICENSE)
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "async-redux-actions",
"version": "0.0.0-development",
"description": "generate async redux action types and creators",
"main": "dist/async-redux-actions.js",
"umd:main": "dist/async-redux-actions.umd.js",
"module": "dist/async-redux-actions.m.js",
"source": "src/index.ts",
"files": [
"dist"
],
"repository": "http://github.com/vinspee/async-redux-actions",
"author": "Vince Speelman <v@vinspee.me>",
"license": "MIT",
"scripts": {
"build": "microbundle src/index.js",
"start": "microbundle watch src/index.js",
"test": "jest",
"test:watch": "jest --watch",
"semantic-release": "semantic-release",
"travis-deploy-once": "travis-deploy-once"
},
"config": {
"commitizen": {
"path": "node_modules/cz-customizable"
}
},
"devDependencies": {
"babel-preset-env": "1.7.0",
"babel-preset-stage-0": "6.24.1",
"commitizen": "2.10.1",
"coveralls": "3.0.2",
"cz-customizable": "5.2.0",
"jest": "23.6.0",
"microbundle": "0.6.0",
"redux-actions": "2.6.1",
"semantic-release": "^15.9.15",
"travis-deploy-once": "^5.0.7"
},
"peerDependencies": {
"redux-actions": ">=2.0.0"
},
"dependencies": {
"prettier": "1.14.2"
}
}
32 changes: 32 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createActions as act } from 'redux-actions';

const STATES = ['REQUESTED', 'RECEIVED', 'REJECTED'];
const isPromise = value => Promise.resolve(value) === value;

const createActions = (
{ states = STATES, prefix = '' } = {
states: STATES,
prefix: '',
},
) => ({ entity = '' } = { entity: '' }) => actions =>
act(
Object.entries(actions).reduce(
(acc, [k, v]) => ({
...acc,
[k]: isPromise(v) ? () => v : v,
...(v &&
isPromise(v) &&
states.reduce(
(acc, curr) => ({
...acc,
[`${k}/${curr}`]: undefined,
}),
{},
)),
}),
{},
),
{ prefix: `${prefix}${entity !== '' ? `${prefix && '/'}${entity}` : ''}` },
);

export default createActions;
61 changes: 61 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import ca from './';

const DEFAULT_MOCK = ca()()({
PROFILE: new Promise(res => res('profile')),
REGISTRATION: new Promise(res => res('registered')),
EXPLORE_STATUS: () => 'status',
FOO: undefined,
});

test('it should return something', () => {
const actual = DEFAULT_MOCK;
expect(actual).toBeDefined();
});

test('it should return an object of keys which match those given', () => {
const actual = DEFAULT_MOCK;
const expected = ['profile', 'registration', 'exploreStatus', 'foo'];
expect(Object.keys(actual)).toEqual(expect.arrayContaining(expected));
});

test('actions should return creators', () => {
const actual = DEFAULT_MOCK;
const expected = 'actionCreator';
expect(actual.foo.name).toEqual(expected);
expect(actual.profile.name).toEqual(expected);
expect(actual.profile.requested.name).toEqual(expected);
expect(actual.profile.received.name).toEqual(expected);
expect(actual.profile.rejected.name).toEqual(expected);
expect(actual.registration.name).toEqual(expected);
expect(actual.registration.requested.name).toEqual(expected);
expect(actual.registration.received.name).toEqual(expected);
expect(actual.registration.rejected.name).toEqual(expected);
expect(actual.exploreStatus.name).toEqual(expected);
});

test('handles app namespaces', () => {
const actual = ca({ prefix: 'πŸ’Ž' })()({
TEST: undefined,
});
const expected = 'πŸ’Ž/TEST';

expect(actual.test.toString()).toBe(expected);
});

test('handles entity namespaces', () => {
const actual = ca()({ entity: 'DOER' })({
TEST: undefined,
});
const expected = 'DOER/TEST';

expect(actual.test.toString()).toBe(expected);
});

test('handles app and entity namespaces', () => {
const actual = ca({ prefix: 'πŸ’Ž' })({ entity: 'DOER' })({
TEST: undefined,
});
const expected = 'πŸ’Ž/DOER/TEST';

expect(actual.test.toString()).toBe(expected);
});
Loading

0 comments on commit 1917781

Please sign in to comment.