Add definitions for history v3 and use it in react-router v3

This commit is contained in:
Karol Janyst
2017-02-13 11:43:21 +09:00
parent a01e02004b
commit 2f045dc94b
21 changed files with 206 additions and 56 deletions
+79
View File
@@ -0,0 +1,79 @@
// Type definitions for history 3.2
// Project: https://github.com/mjackson/history
// Definitions by: Sergey Buturlakin <https://github.com/sergey-buturlakin>, Nathan Brown <https://github.com/ngbrown>, Karol Janyst <https://github.com/LKay>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export as namespace History;
export type Action = "POP" | "PUSH" | "REPLACE";
export type BeforeUnloadHook = () => string | boolean;
export type CreateHistory<O, H> = (options?: HistoryOptions & O) => History & H;
export type CreateHistoryEnhancer<O, H, E> = (createHistory: CreateHistory<O, H>) => CreateHistory<O, H & E>;
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"
+3
View File
@@ -0,0 +1,3 @@
import { Location } from "../index";
export function locationsAreEqual(a: Location, b: Location): boolean;
+9
View File
@@ -0,0 +1,9 @@
export const PUSH: string;
export const REPLACE: string;
export const POP: string;
export default {
PUSH,
REPLACE,
POP
};
+5
View File
@@ -0,0 +1,5 @@
import { CreateHistory } from '../index';
declare const createBrowserHistory: CreateHistory<any, any>;
export default createBrowserHistory;
+5
View File
@@ -0,0 +1,5 @@
import { CreateHistory } from '../index';
declare const createHashHistory: CreateHistory<any, any>;
export default createHashHistory;
+2
View File
@@ -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;
+14
View File
@@ -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<MemoryHistoryOptions, MemoryHistory>;
export default createMemoryHistory;
+13
View File
@@ -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<O, H>(createHistory: CreateHistory<O, H>): CreateHistory<O & HistoryBasenameOptions, H & HistoryBasename>;
+7
View File
@@ -0,0 +1,7 @@
import { BeforeUnloadHook, CreateHistory } from "../index";
export interface HistoryBeforeUnload {
listenBeforeUnload(hook: BeforeUnloadHook): () => void;
}
export default function useBeforeUnload<O, H>(createHistory: CreateHistory<O, H>): CreateHistory<O, H & HistoryBeforeUnload>;
+10
View File
@@ -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<O, H>(createHistory: CreateHistory<O, H>): CreateHistory<O, H & HistoryQueries>;
+36
View File
@@ -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"
]
}
+1 -18
View File
@@ -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<T> = (options?: HistoryOptions) => T;
export type CreateHistoryEnhancer<T> = (createHistory: CreateHistory<T>) => CreateHistory<T>;
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";
+1 -1
View File
@@ -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;
+2 -1
View File
@@ -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<any> {
to: RoutePattern;
+2 -1
View File
@@ -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;
+3 -22
View File
@@ -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;
+2 -6
View File
@@ -1,6 +1,2 @@
import { History } from "history";
import { CreateHistory } from "react-router";
declare const createMemoryHistory: CreateHistory<History>;
export default createMemoryHistory;
import { default as createMemoryHistory } from "history/lib/createMemoryHistory";
export default createMemoryHistory
+2 -2
View File
@@ -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;
+2 -5
View File
@@ -1,6 +1,3 @@
import { History } from "history";
import { CreateHistoryEnhancer } from "react-router";
import { CreateHistory, HistoryBasename, HistoryBasenameOptions, HistoryQueries } from "history";
declare const useRouterHistory: CreateHistoryEnhancer<History>;
export default useRouterHistory;
export default function useRouterHistory<O, H>(createHistory: CreateHistory<O, H>): CreateHistory<O & HistoryBasenameOptions, H & HistoryBasename & HistoryQueries>;
+4
View File
@@ -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) => (
<Link {...props} activeClassName="active" />
+4
View File
@@ -11,6 +11,10 @@
"jsx": "react",
"baseUrl": "../",
"typeRoots": ["../"],
"paths": {
"history": ["history/v3"],
"history/*": ["history/v3/*"]
},
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true