diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..f543624 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,123 @@ +import { + Reducer, + Action, + AnyAction, + ReducersMapObject, + MiddlewareAPI, + StoreEnhancer, + bindActionCreators, +} from "redux"; + +import { History } from "history"; + +export interface Dispatch { + (action: T): Promise | T; +} + +export interface onActionFunc { + (api: MiddlewareAPI): void; +} + +export interface ReducerEnhancer { + (reducer: Reducer): void; +} + +export interface Hooks { + onError?: (e: Error, dispatch: Dispatch) => void; + onAction?: onActionFunc | onActionFunc[]; + onStateChange?: () => void; + onReducer?: ReducerEnhancer; + onEffect?: () => void; + onHmr?: () => void; + extraReducers?: ReducersMapObject; + extraEnhancers?: StoreEnhancer[]; +} + +export type DvaOption = Hooks & { + namespacePrefixWarning?: boolean; + initialState?: Object; + history?: Object; +}; + +export interface EffectsCommandMap { + put: (action: A) => any; + call: Function; + select: Function; + take: Function; + cancel: Function; + [key: string]: any; +} + +export type Effect = (action: AnyAction, effects: EffectsCommandMap) => void; +export type EffectType = "takeEvery" | "takeLatest" | "watcher" | "throttle"; +export type EffectWithType = [Effect, { type: EffectType }]; +export type Subscription = (api: SubscriptionAPI, done: Function) => void; +export type ReducersMapObjectWithEnhancer = [ + ReducersMapObject, + ReducerEnhancer +]; + +export interface EffectsMapObject { + [key: string]: Effect | EffectWithType; +} + +export interface SubscriptionAPI { + history: History; + dispatch: Dispatch; +} + +export interface SubscriptionsMapObject { + [key: string]: Subscription; +} + +export interface Model { + namespace: string; + state?: any; + reducers?: ReducersMapObject | ReducersMapObjectWithEnhancer; + effects?: EffectsMapObject; + subscriptions?: SubscriptionsMapObject; +} + +export interface DvaInstance { + /** + * Register an object of hooks on the application. + * + * @param hooks + */ + use: (hooks: Hooks) => void; + + /** + * Register a model. + * + * @param model + */ + model: (model: Model) => void; + + /** + * Unregister a model. + * + * @param namespace + */ + unmodel: (namespace: string) => void; + + /** + * Start the application. Selector is optional. If no selector + * arguments, it will return a function that return JSX elements. + * + * @param selector + */ + start: (selector?: React.ReactNode, opts?: { forwardRef: boolean }) => any; +} + +export default function dva(opts?: DvaOption): DvaInstance; + +export { bindActionCreators }; + +export { + connect, + connectAdvanced, + useSelector, + useDispatch, + useStore, + shallowEqual, +} from "react-redux"; diff --git a/index.js b/index.js index 4388194..8ff05e0 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ import React from 'react'; import { Provider, connect, connectAdvanced, useSelector, useDispatch, useStore, shallowEqual } from 'react-redux'; import { bindActionCreators } from 'redux'; -import { utils, create, saga } from 'dva-core'; +import { create, saga } from 'dva-core'; export default function(opts = {}) { const createOpts = { @@ -34,8 +34,6 @@ export default function(opts = {}) { return app; } -function getProvider(store, app) {} - export { connect, connectAdvanced, useSelector, useDispatch, useStore, shallowEqual }; export { bindActionCreators }; export { saga };