mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-05 20:02:05 +08:00
[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:
52
types/redux-pack/index.d.ts
vendored
Normal file
52
types/redux-pack/index.d.ts
vendored
Normal 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;
|
||||
5
types/redux-pack/package.json
Normal file
5
types/redux-pack/package.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"redux": "^3.6.0"
|
||||
}
|
||||
}
|
||||
71
types/redux-pack/redux-pack-tests.ts
Normal file
71
types/redux-pack/redux-pack-tests.ts
Normal 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,
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
22
types/redux-pack/tsconfig.json
Normal file
22
types/redux-pack/tsconfig.json
Normal 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"
|
||||
]
|
||||
}
|
||||
1
types/redux-pack/tslint.json
Normal file
1
types/redux-pack/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user