[redux-pack] Add first types for redux-pack

First version of types for redux-pack:
https://github.com/lelandrichardson/redux-pack/tree/v0.1.5
This commit is contained in:
Frank Tan
2017-08-18 07:20:31 -04:00
parent a529685fe3
commit 8aad2e6f48
5 changed files with 151 additions and 0 deletions

52
types/redux-pack/index.d.ts vendored Normal file
View File

@@ -0,0 +1,52 @@
// Type definitions for redux-pack 0.1
// Project: https://github.com/lelandrichardson/redux-pack
// Definitions by: tansongyang <https://github.com/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<S> {
start?: Reducer<S>;
finish?: Reducer<S>;
failure?: Reducer<S>;
success?: Reducer<S>;
always?: Reducer<S>;
}
export type GetState<S> = () => S;
export interface ActionMeta<TState = {}, TSuccessPayload = {}, TErrorPayload = {}, TStartPayload = {}> {
startPayload?: TStartPayload;
onStart?(payload: TStartPayload, getState: GetState<TState>): void;
onFinish?(resolved: boolean, getState: GetState<TState>): void;
onSuccess?(response: TSuccessPayload, getState: GetState<TState>): void;
onFailure?(error: TErrorPayload, getState: GetState<TState>): void;
['redux-pack/LIFECYCLE']?: keyof LIFECYCLEValues;
['redux-pack/TRANSACTION']?: string;
}
export interface Action<TState = {}, TSuccessPayload = {}, TErrorPayload = {}, TStartPayload = {}> extends ReduxAction {
promise?: Promise<TSuccessPayload>;
payload?: TSuccessPayload | TErrorPayload | TStartPayload;
meta?: ActionMeta<TState, TSuccessPayload, TErrorPayload, TStartPayload>;
}
export function handle<TState, TSuccessPayload, TErrorPayload, TStartPayload>(
state: TState,
action: Action<TState, TSuccessPayload, TErrorPayload, TStartPayload>,
handlers: Handlers<TState>)
: TState;

View File

@@ -0,0 +1,5 @@
{
"dependencies": {
"redux": "^3.6.0"
}
}

View File

@@ -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<Foo>;
};
declare function logSuccess(foo: Foo): void;
function loadFoo(id: string): Action<FooState, Foo> {
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<FooState, Foo, string | null>) {
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<Foo>;
declare function sendAnalytics(action: string, data: {
userId: string,
fooId: string,
}): void;
function userDoesFoo(): Action<FooState, Foo> {
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,
});
}
}
};
}

View File

@@ -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"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }