diff --git a/history/v3/index.d.ts b/history/v3/index.d.ts new file mode 100644 index 0000000000..027ca883b9 --- /dev/null +++ b/history/v3/index.d.ts @@ -0,0 +1,79 @@ +// Type definitions for history 3.2 +// Project: https://github.com/mjackson/history +// Definitions by: Sergey Buturlakin , Nathan Brown , Karol Janyst +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export as namespace History; + +export type Action = "POP" | "PUSH" | "REPLACE"; +export type BeforeUnloadHook = () => string | boolean; +export type CreateHistory = (options?: HistoryOptions & O) => History & H; +export type CreateHistoryEnhancer = (createHistory: CreateHistory) => CreateHistory; + +export interface History { + listenBefore(hook: TransitionHook): () => void; + listen(listener: LocationListener): () => void; + transitionTo(location: Location): void; + push(path: LocationDescriptor): void; + replace(path: LocationDescriptor): void; + go(n: number): void; + goBack(): void; + goForward(): void; + createKey(): LocationKey; + createPath(path: LocationDescriptor): Path; + createHref(path: LocationDescriptor): Href; + createLocation(path?: LocationDescriptor, action?: Action, key?: LocationKey): Location; + getCurrentLocation(): Location; +} + +export interface HistoryOptions { + getUserConfirmation?: (message: string, callback: (result: boolean) => void) => void; + keyLength?: number; +} + +export type Hash = string; +export type Href = string; + +export interface Location { + pathname: Pathname; + search: Search; + query: Query; + state: LocationState; + action: Action; + key: LocationKey; +} + +export interface LocationDescriptorObject { + pathname?: Pathname; + search?: Search; + query?: Query; + state?: LocationState; +} + +export type LocationDescriptor = LocationDescriptorObject | Path; + +export type LocationKey = string; + +export type LocationListener = (location: Location) => void; + +export type LocationState = any; + +export type Path = string; // Pathname + Search; +export type Pathname = string; +export type Query = any; +export type Search = string; + +export type TransitionHook = (location: Location, callback: (result: any) => void) => any; + +export { default as createHistory } from "./lib/createBrowserHistory"; +export { default as createHashHistory } from "./lib/createHashHistory"; +export { default as createMemoryHistory, MemoryHistory, MemoryHistoryOptions } from "./lib/createMemoryHistory"; + +export { default as useBasename, Basename, HistoryBasename, HistoryBasenameOptions } from "./lib/useBasename"; +export { default as useBeforeUnload, HistoryBeforeUnload } from "./lib/useBeforeUnload"; +export { default as useQueries, HistoryQueries } from "./lib/useQueries"; + +import * as Actions from "./lib/actions"; +export { Actions } + +export { locationsAreEqual } from "./lib/LocationUtils" diff --git a/history/v3/lib/LocationUtils.d.ts b/history/v3/lib/LocationUtils.d.ts new file mode 100644 index 0000000000..e4f8020925 --- /dev/null +++ b/history/v3/lib/LocationUtils.d.ts @@ -0,0 +1,3 @@ +import { Location } from "../index"; + +export function locationsAreEqual(a: Location, b: Location): boolean; diff --git a/history/v3/lib/actions.d.ts b/history/v3/lib/actions.d.ts new file mode 100644 index 0000000000..22fd29f61e --- /dev/null +++ b/history/v3/lib/actions.d.ts @@ -0,0 +1,9 @@ +export const PUSH: string; +export const REPLACE: string; +export const POP: string; + +export default { + PUSH, + REPLACE, + POP +}; diff --git a/history/v3/lib/createBrowserHistory.d.ts b/history/v3/lib/createBrowserHistory.d.ts new file mode 100644 index 0000000000..5d6b5d1797 --- /dev/null +++ b/history/v3/lib/createBrowserHistory.d.ts @@ -0,0 +1,5 @@ +import { CreateHistory } from '../index'; + +declare const createBrowserHistory: CreateHistory; + +export default createBrowserHistory; diff --git a/history/v3/lib/createHashHistory.d.ts b/history/v3/lib/createHashHistory.d.ts new file mode 100644 index 0000000000..a5a9f001f2 --- /dev/null +++ b/history/v3/lib/createHashHistory.d.ts @@ -0,0 +1,5 @@ +import { CreateHistory } from '../index'; + +declare const createHashHistory: CreateHistory; + +export default createHashHistory; diff --git a/history/v3/lib/createLocation.d.ts b/history/v3/lib/createLocation.d.ts new file mode 100644 index 0000000000..4d24e6e5ac --- /dev/null +++ b/history/v3/lib/createLocation.d.ts @@ -0,0 +1,2 @@ +import { Path, LocationState, Action, LocationKey, Location } from '../index'; +export default function createLocation(path?: Path, state?: LocationState, action?: Action, key?: LocationKey): Location; diff --git a/history/v3/lib/createMemoryHistory.d.ts b/history/v3/lib/createMemoryHistory.d.ts new file mode 100644 index 0000000000..cc03c8a111 --- /dev/null +++ b/history/v3/lib/createMemoryHistory.d.ts @@ -0,0 +1,14 @@ +import { CreateHistory, History } from '../index'; + +export interface MemoryHistoryOptions { + entries?: string | [string]; + current?: string; +} + +export interface MemoryHistory { + canGo(n: number): boolean; +} + +declare const createMemoryHistory: CreateHistory; + +export default createMemoryHistory; diff --git a/history/v3/lib/useBasename.d.ts b/history/v3/lib/useBasename.d.ts new file mode 100644 index 0000000000..073b69ac9a --- /dev/null +++ b/history/v3/lib/useBasename.d.ts @@ -0,0 +1,13 @@ +import { CreateHistory, HistoryOptions } from "../index"; + +export type Basename = string; + +export interface HistoryBasenameOptions { + basename?: Basename; +} + +export interface HistoryBasename { + basename?: Basename; +} + +export default function useBasename(createHistory: CreateHistory): CreateHistory; diff --git a/history/v3/lib/useBeforeUnload.d.ts b/history/v3/lib/useBeforeUnload.d.ts new file mode 100644 index 0000000000..7bb86dc226 --- /dev/null +++ b/history/v3/lib/useBeforeUnload.d.ts @@ -0,0 +1,7 @@ +import { BeforeUnloadHook, CreateHistory } from "../index"; + +export interface HistoryBeforeUnload { + listenBeforeUnload(hook: BeforeUnloadHook): () => void; +} + +export default function useBeforeUnload(createHistory: CreateHistory): CreateHistory; diff --git a/history/v3/lib/useQueries.d.ts b/history/v3/lib/useQueries.d.ts new file mode 100644 index 0000000000..640cb9c550 --- /dev/null +++ b/history/v3/lib/useQueries.d.ts @@ -0,0 +1,10 @@ +import { CreateHistory, Href, LocationState, Path, Pathname, Query } from '../index'; + +export interface HistoryQueries { + pushState(state: LocationState, pathname: Pathname, query?: Query): void; + replaceState(state: LocationState, pathname: Pathname, query?: Query): void; + createPath(path: Path, query?: Query): Path; + createHref(path: Path, query?: Query): Href; +} + +export default function useQueries(createHistory: CreateHistory): CreateHistory; diff --git a/history/v3/tsconfig.json b/history/v3/tsconfig.json new file mode 100644 index 0000000000..05d4e3d48d --- /dev/null +++ b/history/v3/tsconfig.json @@ -0,0 +1,36 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "paths": { + "history": ["history/v3"], + "history/*": ["history/v3/*"] + }, + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "history-tests.ts", + "lib/actions.d.ts", + "lib/createBrowserHistory.d.ts", + "lib/createHashHistory.d.ts", + "lib/createLocation.d.ts", + "lib/createMemoryHistory.d.ts", + "lib/DOMUtils.d.ts", + "lib/useBasename.d.ts", + "lib/useBeforeUnload.d.ts", + "lib/useQueries.d.ts" + ] +} diff --git a/react-router/index.d.ts b/react-router/index.d.ts index 84b9f2c152..a588c5f979 100644 --- a/react-router/index.d.ts +++ b/react-router/index.d.ts @@ -4,27 +4,11 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.1 -/* Replacement from old history definitions */ -export interface HistoryOptions { - getCurrentLocation?(): Location; - getUserConfirmation?(message: string, callback: (result: boolean) => void): void; - pushLocation?(nextLocation: Location): void; - replaceLocation?(nextLocation: Location): void; - go?(n: number): void; - keyLength?: number; -} - -export type CreateHistory = (options?: HistoryOptions) => T; -export type CreateHistoryEnhancer = (createHistory: CreateHistory) => CreateHistory; - export { - Basename, ChangeHook, EnterHook, InjectedRouter, LeaveHook, - Location, - LocationDescriptor, ParseQueryString, RouteComponent, RouteComponents, @@ -34,8 +18,7 @@ export { RouterProps, RouterState, RedirectFunction, - StringifyQuery, - Query + StringifyQuery } from "react-router/lib/Router"; export { LinkProps } from "react-router/lib/Link"; export { IndexLinkProps } from "react-router/lib/IndexLink"; diff --git a/react-router/lib/IndexLink.d.ts b/react-router/lib/IndexLink.d.ts index 0c62010262..3eb022f4c3 100644 --- a/react-router/lib/IndexLink.d.ts +++ b/react-router/lib/IndexLink.d.ts @@ -1,5 +1,5 @@ import { ComponentClass, CSSProperties, HTMLProps } from "react"; -import { Location, LocationDescriptor } from "react-router/lib/Router"; +import { Location, LocationDescriptor } from "history"; type ToLocationFunction = (location: Location) => LocationDescriptor; diff --git a/react-router/lib/IndexRedirect.d.ts b/react-router/lib/IndexRedirect.d.ts index 3b8187d09b..da0b88cf34 100644 --- a/react-router/lib/IndexRedirect.d.ts +++ b/react-router/lib/IndexRedirect.d.ts @@ -1,5 +1,6 @@ import { ComponentClass, ClassAttributes } from "react"; -import { RoutePattern, Query } from "react-router"; +import { RoutePattern } from "react-router"; +import { Query } from "history"; export interface IndexRedirectProps extends ClassAttributes { to: RoutePattern; diff --git a/react-router/lib/Redirect.d.ts b/react-router/lib/Redirect.d.ts index bb8254b4f5..118c2264ad 100644 --- a/react-router/lib/Redirect.d.ts +++ b/react-router/lib/Redirect.d.ts @@ -1,6 +1,7 @@ import { ComponentClass, ClassAttributes } from "react"; -import { RoutePattern, Query } from "react-router"; +import { RoutePattern } from "react-router"; import { IndexRedirectProps } from "react-router/lib/IndexRedirect"; +import { Query } from "history"; export interface RedirectProps extends IndexRedirectProps { from: RoutePattern; diff --git a/react-router/lib/Router.d.ts b/react-router/lib/Router.d.ts index 3870f538fc..02550026b9 100644 --- a/react-router/lib/Router.d.ts +++ b/react-router/lib/Router.d.ts @@ -1,20 +1,19 @@ import { Component, ComponentClass, ClassAttributes, ReactNode, StatelessComponent } from "react"; import { Action, - Hash, History, Href, + Location, + LocationDescriptor, LocationKey, LocationState, Path, Pathname, + Query, Search } from "history"; import { PlainRoute } from "react-router"; -/* Replacement from old history definitions */ -export type Basename = string; -export type Query = any; export interface Params { [key: string]: string; } @@ -36,24 +35,6 @@ export type LeaveHook = (prevState: RouterState) => any; export type ChangeHook = (prevState: RouterState, nextState: RouterState, replace: RedirectFunction, callback?: AnyFunction) => any; export type RouteHook = (nextLocation?: Location) => any; -export interface Location { - pathname: Pathname; - search: Search; - query: Query; - state: LocationState; - action: Action; - key: LocationKey; -} - -export interface LocationDescriptorObject { - pathname?: Pathname; - query?: Query; - hash?: Hash; - state?: LocationState; -} - -export type LocationDescriptor = Path | LocationDescriptorObject; - export interface RedirectFunction { (location: LocationDescriptor): void; (state: LocationState, pathname: Pathname | Path, query?: Query): void; diff --git a/react-router/lib/createMemoryHistory.d.ts b/react-router/lib/createMemoryHistory.d.ts index 038e707e6b..e4bbb74648 100644 --- a/react-router/lib/createMemoryHistory.d.ts +++ b/react-router/lib/createMemoryHistory.d.ts @@ -1,6 +1,2 @@ -import { History } from "history"; -import { CreateHistory } from "react-router"; - -declare const createMemoryHistory: CreateHistory; - -export default createMemoryHistory; +import { default as createMemoryHistory } from "history/lib/createMemoryHistory"; +export default createMemoryHistory diff --git a/react-router/lib/match.d.ts b/react-router/lib/match.d.ts index 407bcc80c2..e9fd936ad9 100644 --- a/react-router/lib/match.d.ts +++ b/react-router/lib/match.d.ts @@ -1,5 +1,5 @@ -import { History } from "history"; -import { Basename, LocationDescriptor, ParseQueryString, RouteConfig, StringifyQuery } from "react-router"; +import { Basename, History, LocationDescriptor } from "history"; +import { ParseQueryString, RouteConfig, StringifyQuery } from "react-router"; interface MatchArgs { routes: RouteConfig; diff --git a/react-router/lib/useRouterHistory.d.ts b/react-router/lib/useRouterHistory.d.ts index 7dc2bdd3ab..fdc82c9981 100644 --- a/react-router/lib/useRouterHistory.d.ts +++ b/react-router/lib/useRouterHistory.d.ts @@ -1,6 +1,3 @@ -import { History } from "history"; -import { CreateHistoryEnhancer } from "react-router"; +import { CreateHistory, HistoryBasename, HistoryBasenameOptions, HistoryQueries } from "history"; -declare const useRouterHistory: CreateHistoryEnhancer; - -export default useRouterHistory; +export default function useRouterHistory(createHistory: CreateHistory): CreateHistory; diff --git a/react-router/react-router-tests.tsx b/react-router/react-router-tests.tsx index 59186914e4..0d7b8d9e32 100644 --- a/react-router/react-router-tests.tsx +++ b/react-router/react-router-tests.tsx @@ -9,6 +9,7 @@ import { hashHistory, match, createMemoryHistory, + useRouterHistory, withRouter, routerShape, Router, @@ -21,6 +22,9 @@ import { RedirectFunction, RouteComponentProps } from "react-router"; +import { createHistory } from "history"; + +const routerHistory = useRouterHistory(createHistory)({ basename: "/test" }) const NavLink = (props: LinkProps) => ( diff --git a/react-router/tsconfig.json b/react-router/tsconfig.json index e2a4e054ad..301ed3f651 100644 --- a/react-router/tsconfig.json +++ b/react-router/tsconfig.json @@ -11,6 +11,10 @@ "jsx": "react", "baseUrl": "../", "typeRoots": ["../"], + "paths": { + "history": ["history/v3"], + "history/*": ["history/v3/*"] + }, "types": [], "noEmit": true, "forceConsistentCasingInFileNames": true