diff --git a/types/redux-doghouse/index.d.ts b/types/redux-doghouse/index.d.ts new file mode 100644 index 0000000000..deef70d81a --- /dev/null +++ b/types/redux-doghouse/index.d.ts @@ -0,0 +1,41 @@ +// Type definitions for redux-doghouse 1.0 +// Project: https://github.com/DataDog/redux-doghouse#readme +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export as namespace ReduxDoghouse; + +import { ActionCreator, ActionCreatorsMapObject, bindActionCreators, Dispatch, Reducer } from 'redux'; + +export class ScopedActionFactory | ActionCreatorsMapObject> { + constructor(actionCreator: A); + + scope(id: string | number): A; +} + +export interface ActionFactoriesMap { + [key: string]: ScopedActionFactory | ActionCreator | ActionFactoriesMap; +} + +export function bindActionCreatorsDeep(actionFactories: M, + dispatch: Dispatch): M; +export function bindActionCreatorsDeep, S>(actionFactories: A, + dispatch: Dispatch): A; +export function bindActionCreatorsDeep | ActionCreatorsMapObject, + F extends ScopedActionFactory, S>(actionFactories: F, + dispatch: Dispatch): F; + +export function bindScopedActionFactories(actionFactories: M, + dispatch: Dispatch, + bindFn?: typeof bindActionCreators): M; +export function bindScopedActionFactories, S>(actionFactories: A, + dispatch: Dispatch, + bindFn?: typeof bindActionCreators): A; +export function bindScopedActionFactories | ActionCreatorsMapObject, + F extends ScopedActionFactory, S>(actionFactories: F, + dispatch: Dispatch, + bindFn?: typeof bindActionCreators): F; + +export function scopeActionCreators | ActionCreatorsMapObject>(actionCreator: A, scopeID: string | number): A; + +export function scopeReducers(reducers?: { [key: string]: Reducer }): Reducer; diff --git a/types/redux-doghouse/package.json b/types/redux-doghouse/package.json new file mode 100644 index 0000000000..0954d220bd --- /dev/null +++ b/types/redux-doghouse/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "redux": "^3.7.1" + } +} diff --git a/types/redux-doghouse/redux-doghouse-tests.ts b/types/redux-doghouse/redux-doghouse-tests.ts new file mode 100644 index 0000000000..fb18d83405 --- /dev/null +++ b/types/redux-doghouse/redux-doghouse-tests.ts @@ -0,0 +1,79 @@ +import { Action, ActionCreator, createStore, Dispatch } from 'redux'; +import { + bindActionCreatorsDeep, + bindScopedActionFactories, + scopeActionCreators, + ScopedActionFactory, + scopeReducers +} from 'redux-doghouse'; + +// ==== scopeActionCreators +function fooActionCreator(bar: string): { [key: string]: any } { + return {}; +} + +const actionCreators = { + foo: fooActionCreator +}; + +scopeActionCreators(actionCreators, 'a').foo('bar'); +scopeActionCreators(fooActionCreator, 'a')('bar'); +scopeActionCreators(fooActionCreator, 1)('bar'); + +// ==== ScopedActionFactory +const scopeableActionsFromCreators = new ScopedActionFactory(actionCreators); +const actionCreatorsScopedToA = scopeableActionsFromCreators.scope('a'); + +actionCreatorsScopedToA.foo('bar'); + +const scopeableActionsFromFooCreator = new ScopedActionFactory(fooActionCreator); +const actionCreatorScopedToB = scopeableActionsFromFooCreator.scope('b'); + +actionCreatorScopedToB('bar'); + +// ==== bindScopedActionFactories +const store = createStore((state: any, action: Action) => state); +const scopeableActionsFactories = { + myComponentActions: new ScopedActionFactory(actionCreators), + otherActions: { + fooAction: new ScopedActionFactory(fooActionCreator), + plainFooAction: fooActionCreator, + }, +}; +const boundScopeableActions = bindScopedActionFactories(scopeableActionsFactories, store.dispatch); +boundScopeableActions['myComponentActions'].scope('bar'); +boundScopeableActions['otherActions']['plainFooAction']('bar'); + +const boundActions = bindScopedActionFactories(actionCreators, store.dispatch); +boundActions.foo('bar'); + +bindScopedActionFactories(fooActionCreator, store.dispatch, (a: ActionCreator, d: Dispatch) => a)('bar'); + +// ==== bindActionCreatorsDeep +const boundScopeableActionFactories = bindActionCreatorsDeep(scopeableActionsFactories, store.dispatch); +boundScopeableActionFactories['myComponentActions'].scope('bar'); +boundScopeableActionFactories['otherActions']['plainFooAction']('bar'); + +const boundActionFactories = bindActionCreatorsDeep(actionCreators, store.dispatch); +boundActionFactories.foo('bar'); + +bindActionCreatorsDeep(fooActionCreator, store.dispatch)('bar'); + +// ==== scopeReducers +const reducers = { + foo: (state = 0, action: Action) => { + return state; + }, +}; +const actionCreatorsA = scopeActionCreators({ + incrementFoo: () => ({type: 'INCREMENT_FOO'}) +}, 'a'); + +const scopedReducers = scopeReducers(reducers); +const state = { + a: {foo: 0}, + b: {foo: 2} +}; +const newState = scopedReducers(state, actionCreatorsA.incrementFoo()); +const aScopedFoo: number = newState.a.foo; +const bScopedFoo: number = newState.b.foo; diff --git a/types/redux-doghouse/tsconfig.json b/types/redux-doghouse/tsconfig.json new file mode 100644 index 0000000000..122bb36672 --- /dev/null +++ b/types/redux-doghouse/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "redux-doghouse-tests.ts" + ] +} diff --git a/types/redux-doghouse/tslint.json b/types/redux-doghouse/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/redux-doghouse/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }