diff --git a/types/redux-first-router/index.d.ts b/types/redux-first-router/index.d.ts index 02ed943048..27483c03b5 100644 --- a/types/redux-first-router/index.d.ts +++ b/types/redux-first-router/index.d.ts @@ -3,20 +3,30 @@ // Definitions by: Valbrand // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -import { Dispatch, Store } from 'redux' +import { + Dispatch, + Store, + Reducer, + Middleware, + GenericStoreEnhancer +} from 'redux' -interface StateGetter { - (): S +interface StateGetter { + (): S } export type RouteString = string +export interface RouteThunk { + (dispatch: Dispatch, getState: StateGetter): any | Promise +} + export interface RouteObject { path: string capitalizedWords?: boolean - toPath?: (param: string, key?: string) => string - fromPath?: (path: string, key?: string) => string - thunk?: (dispatch: Dispatch, getState: StateGetter) => any | Promise + toPath?(param: string, key?: string): string + fromPath?(path: string, key?: string): string + thunk?: RouteThunk navKey?: string } @@ -45,6 +55,17 @@ export interface Location { payload: Object } +export interface LocationState { + pathname: string + type: string + payload: Object + prev: Location + kind: string | void + history: HistoryData | void + routesMap: RoutesMap + hasSSR?: boolean +} + export interface ActionMetaLocation { current: Location prev: Location @@ -76,18 +97,40 @@ export interface Action { navKey?: string } -// FIXME -export interface History { +export interface HistoryLocation { + pathname: string +} +export type HistoryAction = string + +export interface Listener { + (location: HistoryLocation, action: HistoryAction): void +} + +export interface History { + listen(listener: Listener): void + push(pathname: string): void + replace(pathname: string): void + goBack(): void + goForward(): void + go(n: number): void + canGo(n: number): boolean + + entries: Array<{ pathname: string }> + index: number + length: number + location: { + pathname: string + } } export type ScrollBehavior = Object export interface Router { - getStateForActionOriginal: (action: Object, state: Object | void) => Object | void - getStateForAction: (action: Object, state: Object | void) => Object | void - getPathAndParamsForState: (state: Object) => { path: string | void, params: Object | void } - getActionForPathAndParams: (path: string) => Object | void + getStateForActionOriginal(action: Object, state: Object | void): Object | void + getStateForAction(action: Object, state: Object | void): Object | void + getPathAndParamsForState(state: Object): { path: string | void, params: Object | void } + getActionForPathAndParams(path: string): Object | void } export interface Navigator { @@ -100,20 +143,20 @@ export interface Navigators { export interface NavigatorsConfig { navigators: Navigators - patchNavigators: (navigators: Navigators) => void + patchNavigators(navigators: Navigators): void - actionToNavigation: ( + actionToNavigation( navigators: Navigators, action: Object, // TODO check this navigationAction: NavigationAction | void, route: Route | void - ) => Object - navigationToAction: ( + ): Object + navigationToAction( navigators: Navigators, store: Store, routesMap: RoutesMap, action: Object - ) => { + ): { action: Object, navigationAction: NavigationAction | void } @@ -124,10 +167,10 @@ export interface Options { location?: string notFoundPath?: string scrollTop?: boolean - onBeforeChange?: (dispatch: Dispatch, getState: StateGetter) => void - onAfterChange?: (dispatch: Dispatch, getState: StateGetter) => void - onBackNext?: (dispatch: Dispatch, getState: StateGetter) => void - restoreScroll?: (history: History) => ScrollBehavior + onBeforeChange?: (dispatch: Dispatch, getState: StateGetter) => void + onAfterChange?: (dispatch: Dispatch, getState: StateGetter) => void + onBackNext?: (dispatch: Dispatch, getState: StateGetter) => void + restoreScroll?(history: History): ScrollBehavior navigators?: NavigatorsConfig } @@ -145,8 +188,12 @@ export function canGoBack(): boolean; export function canGoForward(): boolean; -// FIXME -export function connectRoutes(history: History, routesMap: RoutesMap, options: Options): any; +export function connectRoutes(history: History, routesMap: RoutesMap, options?: Options): { + reducer: Reducer + middleware: Middleware, + thunk(store: Store): Promise, + enhancer: GenericStoreEnhancer +}; export function go(n: number): void; diff --git a/types/redux-first-router/redux-first-router-tests.ts b/types/redux-first-router/redux-first-router-tests.ts index e69de29bb2..cb3fd4ef70 100644 --- a/types/redux-first-router/redux-first-router-tests.ts +++ b/types/redux-first-router/redux-first-router-tests.ts @@ -0,0 +1,37 @@ +import { connectRoutes, History, LocationState } from 'redux-first-router' +import { + createStore, + applyMiddleware, + Middleware, + MiddlewareAPI, + Store, + Dispatch, + compose, + Action +} from 'redux' + +declare var console: any + +declare var history: History + +const routesMap = { + HOME: '/' +} + +const { + reducer, + middleware, + enhancer +} = connectRoutes(history, routesMap) + +const logger: Middleware = (store: MiddlewareAPI) => (next: Dispatch) => (action: Action) => { + console.log(`Dispatching action ${action.type}...`) + + next(action) +} + +const composedMiddleware = applyMiddleware(middleware, logger) + +const store = createStore(reducer, compose(enhancer, composedMiddleware)) + +store.getState()