Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a41fb2e
Showing
10 changed files
with
410 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["es2015"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# IntelliJ IDEA | ||
.idea | ||
*.iml | ||
|
||
# NPM | ||
node_modules | ||
npm-*.log | ||
|
||
# OS X | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Daniel Lytkin | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
# Redux TypeScript Actions | ||
|
||
A simple Action Creator library for TypeScript. Its goal is to provide simple | ||
yet type-safe experience with Redux actions. | ||
Created actions are FSA-compliant: | ||
|
||
```ts | ||
interface Action<P> { | ||
type: string; | ||
payload?: P; | ||
error?: boolean; | ||
meta?: Object; | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
### Basic | ||
|
||
```ts | ||
import actionCreatorFactory from 'redux-typescript-actions'; | ||
|
||
const actionCreator = actionCreatorFactory(); | ||
|
||
// Specify payload shape as generic type argument. | ||
const somethingHappened = actionCreator<{foo: string}>('SOMETHING_HAPPENED'); | ||
|
||
// Get action creator type. | ||
console.log(somethingHappened.type); // SOMETHING_HAPPENED | ||
|
||
// Create action. | ||
const action = somethingHappened({foo: 'bar'}); | ||
console.log(action); // {type: 'SOMETHING_HAPPENED', payload: {foo: 'bar'}} | ||
``` | ||
|
||
### Async Action Creators | ||
|
||
Async Action Creators are objects with properties `started`, `done` and | ||
`failed` whose values are action creators. | ||
|
||
```ts | ||
import actionCreatorFactory from 'redux-typescript-actions'; | ||
|
||
const actionCreator = actionCreatorFactory(); | ||
|
||
// specify parameters and result shapes as generic type arguments | ||
const doSomething = | ||
actionCreator.async<{foo: string}, {bar: number}>('DO_SOMETHING'); | ||
|
||
console.log(doSomething.started({foo: 'lol'})); | ||
// {type: 'DO_SOMETHING_STARTED', payload: {foo: 'lol'}} | ||
|
||
console.log(doSomething.done({ | ||
params: {foo: 'lol'}, | ||
result: {bar: 42}, | ||
}); | ||
// {type: 'DO_SOMETHING_DONE', payload: { | ||
// params: {foo: 'lol'}, | ||
// result: {bar: 42}, | ||
// }} | ||
|
||
console.log(doSomething.failed({ | ||
params: {foo: 'lol'}, | ||
error: {code: 42}, | ||
}); | ||
// {type: 'DO_SOMETHING_FAILED', payload: { | ||
// params: {foo: 'lol'}, | ||
// error: {code: 42}, | ||
// }, error: true} | ||
``` | ||
### Actions With Type Prefix | ||
You can specify a prefix that will be prepended to all action types. This is | ||
useful to namespace library actions as well as for large projects where it's | ||
convenient to keep actions near the component that dispatches them. | ||
```ts | ||
// MyComponent.actions.ts | ||
import actionCreatorFactory from 'redux-typescript-actions'; | ||
|
||
const actionCreator = actionCreatorFactory('MyComponent'); | ||
|
||
const somethingHappened = actionCreator<{foo: string}>('SOMETHING_HAPPENED'); | ||
|
||
const action = somethingHappened({foo: 'bar'}); | ||
console.log(action); | ||
// {type: 'MyComponent/SOMETHING_HAPPENED', payload: {foo: 'bar'}} | ||
``` | ||
### Reducers | ||
```ts | ||
// actions.ts | ||
import actionCreatorFactory from 'redux-typescript-actions'; | ||
|
||
const actionCreator = actionCreatorFactory(); | ||
|
||
export const somethingHappened = | ||
actionCreator<{foo: string}>('SOMETHING_HAPPENED'); | ||
|
||
|
||
// reducer.ts | ||
import {Action} from 'redux'; | ||
import {isType, Action} from 'redux-typescript-actions'; | ||
import {somethingHappened} from './actions'; | ||
|
||
type State = {bar: string}; | ||
|
||
const reducer = (state: State, action: Action): State => { | ||
if (isType(action, somethingHappened)) { | ||
// action.payload is inferred as {foo: string}; | ||
|
||
action.payload.bar; // error | ||
|
||
return {bar: action.payload.foo}; | ||
} | ||
|
||
return state; | ||
}; | ||
``` | ||
## API | ||
### `actionCreatorFactory(prefix?: string): ActionCreatorFactory` | ||
Creates Action Creator factory with optional prefix for action types. | ||
* `prefix?: string`: Prefix to be prepended to action types. | ||
### `isType(action: Action, actionCreator: ActionCreator): boolean` | ||
Returns `true` if action has the same type as action creator. Defines | ||
[Type Guard](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards) | ||
that lets TypeScript know `payload` type inside blocks where `isType` returned | ||
`true`: | ||
```ts | ||
const somethingHappened = actionCreator<{foo: string}>('SOMETHING_HAPPENED'); | ||
|
||
if (isType(action, somethingHappened)) { | ||
// action.payload has type {foo: string}; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export function isType(action, actionCreator) { | ||
return action.type === actionCreator.type; | ||
} | ||
export default function actionCreatorFactory(prefix) { | ||
const actionTypes = {}; | ||
function actionCreator(type, commonMeta, error) { | ||
if (actionTypes[type]) | ||
throw new Error(`Duplicate action type: ${type}`); | ||
actionTypes[type] = true; | ||
const fullType = prefix ? `${prefix}/${type}` : type; | ||
return Object.assign((payload, meta) => { | ||
const action = { | ||
type: fullType, | ||
payload, | ||
meta: Object.assign({}, commonMeta, meta), | ||
}; | ||
if (error) | ||
action.error = error; | ||
return action; | ||
}, { type: fullType }); | ||
} | ||
function asyncActionCreators(type, commonMeta) { | ||
return { | ||
type: prefix ? `${prefix}/${type}` : type, | ||
started: actionCreator(`${type}_STARTED`, commonMeta), | ||
done: actionCreator(`${type}_DONE`, commonMeta), | ||
failed: actionCreator(`${type}_FAILED`, commonMeta, true), | ||
}; | ||
} | ||
return Object.assign(actionCreator, { async: asyncActionCreators }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Action as ReduxAction } from "redux"; | ||
export interface Action<P> extends ReduxAction { | ||
type: string; | ||
payload?: P; | ||
error?: boolean; | ||
meta?: Object; | ||
} | ||
export declare function isType<P>(action: ReduxAction, actionCreator: ActionCreator<P>): action is Action<P>; | ||
export interface ActionCreator<P> { | ||
type: string; | ||
(payload?: P, meta?: Object): Action<P>; | ||
} | ||
export interface AsyncActionCreators<P, R> { | ||
type: string; | ||
started: ActionCreator<P>; | ||
done: ActionCreator<{ | ||
params: P; | ||
result: R; | ||
}>; | ||
failed: ActionCreator<{ | ||
params: P; | ||
error: any; | ||
}>; | ||
} | ||
export interface ActionCreatorFactory { | ||
<P>(type: string, commonMeta?: Object, error?: boolean): ActionCreator<P>; | ||
async<P, S>(type: string, commonMeta?: Object): AsyncActionCreators<P, S>; | ||
} | ||
export default function actionCreatorFactory(prefix?: string): ActionCreatorFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.isType = isType; | ||
exports.default = actionCreatorFactory; | ||
function isType(action, actionCreator) { | ||
return action.type === actionCreator.type; | ||
} | ||
function actionCreatorFactory(prefix) { | ||
var actionTypes = {}; | ||
function actionCreator(type, commonMeta, error) { | ||
if (actionTypes[type]) throw new Error("Duplicate action type: " + type); | ||
actionTypes[type] = true; | ||
var fullType = prefix ? prefix + "/" + type : type; | ||
return Object.assign(function (payload, meta) { | ||
var action = { | ||
type: fullType, | ||
payload: payload, | ||
meta: Object.assign({}, commonMeta, meta) | ||
}; | ||
if (error) action.error = error; | ||
return action; | ||
}, { type: fullType }); | ||
} | ||
function asyncActionCreators(type, commonMeta) { | ||
return { | ||
type: prefix ? prefix + "/" + type : type, | ||
started: actionCreator(type + "_STARTED", commonMeta), | ||
done: actionCreator(type + "_DONE", commonMeta), | ||
failed: actionCreator(type + "_FAILED", commonMeta, true) | ||
}; | ||
} | ||
return Object.assign(actionCreator, { async: asyncActionCreators }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "redux-typescript-actions", | ||
"version": "1.0.0", | ||
"description": "Type-safe action creator utilities", | ||
"keywords": [ | ||
"redux" | ||
], | ||
"main": "lib/index.js", | ||
"jsnext:main": "es6/index.js", | ||
"typings": "./index.d.ts", | ||
"files": [ | ||
"es6", | ||
"lib", | ||
"index.d.ts" | ||
], | ||
"scripts": { | ||
"build:es6": "tsc", | ||
"build:commonjs": "babel es6 --out-dir lib", | ||
"build": "npm run build:es6 && npm run build:commonjs" | ||
}, | ||
"author": "Daniel Lytkin <dan.lytkin@gmail.com>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"redux": "^3.6.0" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "^6.17.0", | ||
"babel-preset-es2015": "^6.16.0", | ||
"typescript": "^2.0.3" | ||
} | ||
} |
Oops, something went wrong.