diff --git a/types/redux-pack/index.d.ts b/types/redux-pack/index.d.ts new file mode 100644 index 0000000000..72e2d79d10 --- /dev/null +++ b/types/redux-pack/index.d.ts @@ -0,0 +1,52 @@ +// Type definitions for redux-pack 0.1 +// Project: https://github.com/lelandrichardson/redux-pack +// Definitions by: tansongyang +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 +import { + Action as ReduxAction, + Middleware, + Reducer, +} from 'redux'; + +export const KEY: { + readonly LIFECYCLE: 'redux-pack/LIFECYCLE' + readonly TRANSACTION: 'redux-pack/TRANSACTION' +}; + +export const LIFECYCLE: { + readonly START: 'start' + readonly SUCCESS: 'success' + readonly FAILURE: 'failure' +}; +export type LIFECYCLEValues = 'start'| 'succes'| 'failure'; + +export const middleware: Middleware; + +export interface Handlers { + start?: Reducer; + finish?: Reducer; + failure?: Reducer; + success?: Reducer; + always?: Reducer; +} +export type GetState = () => S; +export interface ActionMeta { + startPayload?: TStartPayload; + onStart?(payload: TStartPayload, getState: GetState): void; + onFinish?(resolved: boolean, getState: GetState): void; + onSuccess?(response: TSuccessPayload, getState: GetState): void; + onFailure?(error: TErrorPayload, getState: GetState): void; + ['redux-pack/LIFECYCLE']?: keyof LIFECYCLEValues; + ['redux-pack/TRANSACTION']?: string; +} +export interface Action extends ReduxAction { + promise?: Promise; + payload?: TSuccessPayload | TErrorPayload | TStartPayload; + meta?: ActionMeta; +} +export function handle( + state: TState, + action: Action, + handlers: Handlers) + : TState; diff --git a/types/redux-pack/package.json b/types/redux-pack/package.json new file mode 100644 index 0000000000..36ce503807 --- /dev/null +++ b/types/redux-pack/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "redux": "^3.6.0" + } +} diff --git a/types/redux-pack/redux-pack-tests.ts b/types/redux-pack/redux-pack-tests.ts new file mode 100644 index 0000000000..ff1746bdbf --- /dev/null +++ b/types/redux-pack/redux-pack-tests.ts @@ -0,0 +1,71 @@ +import { handle, Action } from 'redux-pack'; + +interface Foo { + id: string; +} +interface FooState { + foo: Foo | null; + error: string | null; + isLoading: boolean; + currentUser: { + id: string; + }; +} + +// https://github.com/lelandrichardson/redux-pack/tree/v0.1.5#logging-beforeafter +declare const Api: { + getFoo(id: string): Promise; +}; +declare function logSuccess(foo: Foo): void; +function loadFoo(id: string): Action { + return { + type: LOAD_FOO, + promise: Api.getFoo(id), + meta: { + onSuccess: logSuccess + }, + }; +} + +// https://github.com/lelandrichardson/redux-pack/tree/v0.1.5#using-the-handle-helper +const LOAD_FOO = 'LOAD_FOO'; +declare const initialState: FooState; +function fooReducer(state = initialState, action: Action) { + const { type, payload } = action; + switch (type) { + case LOAD_FOO: + return handle(state, action, { + start: prevState => ({ ...prevState, isLoading: true, error: null, foo: null }), + finish: prevState => ({ ...prevState, isLoading: false }), + failure: prevState => ({ ...prevState, error: payload as string }), + success: prevState => ({ ...prevState, foo: payload as Foo }), + always: prevState => prevState, // unnecessary, for the sake of example + }); + default: + return state; + } +} + +// https://github.com/lelandrichardson/redux-pack/tree/v0.1.5#adding-side-effects-with-event-hooks +const DO_FOO = 'DO_FOO'; +declare function doFoo(): Promise; +declare function sendAnalytics(action: string, data: { + userId: string, + fooId: string, +}): void; +function userDoesFoo(): Action { + return { + type: DO_FOO, + promise: doFoo(), + meta: { + onSuccess: (result, getState) => { + const userId = getState().currentUser.id; + const fooId = result.id; + sendAnalytics('USER_DID_FOO', { + userId, + fooId, + }); + } + } + }; +} diff --git a/types/redux-pack/tsconfig.json b/types/redux-pack/tsconfig.json new file mode 100644 index 0000000000..cd4b958eb7 --- /dev/null +++ b/types/redux-pack/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-pack-tests.ts" + ] +} diff --git a/types/redux-pack/tslint.json b/types/redux-pack/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/redux-pack/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }