mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-11 20:40:20 +00:00
Add definitions for history v3 and use it in react-router v3
This commit is contained in:
Vendored
+79
@@ -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"
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
import { Location } from "../index";
|
||||
|
||||
export function locationsAreEqual(a: Location, b: Location): boolean;
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
export const PUSH: string;
|
||||
export const REPLACE: string;
|
||||
export const POP: string;
|
||||
|
||||
export default {
|
||||
PUSH,
|
||||
REPLACE,
|
||||
POP
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import { CreateHistory } from '../index';
|
||||
|
||||
declare const createBrowserHistory: CreateHistory<any, any>;
|
||||
|
||||
export default createBrowserHistory;
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import { CreateHistory } from '../index';
|
||||
|
||||
declare const createHashHistory: CreateHistory<any, any>;
|
||||
|
||||
export default createHashHistory;
|
||||
Vendored
+2
@@ -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
@@ -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;
|
||||
Vendored
+13
@@ -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>;
|
||||
Vendored
+7
@@ -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>;
|
||||
Vendored
+10
@@ -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>;
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
Vendored
+1
-18
@@ -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";
|
||||
|
||||
Vendored
+1
-1
@@ -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;
|
||||
|
||||
|
||||
Vendored
+2
-1
@@ -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;
|
||||
|
||||
Vendored
+2
-1
@@ -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;
|
||||
|
||||
Vendored
+3
-22
@@ -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
@@ -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
|
||||
|
||||
Vendored
+2
-2
@@ -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
@@ -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>;
|
||||
|
||||
@@ -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" />
|
||||
|
||||
@@ -11,6 +11,10 @@
|
||||
"jsx": "react",
|
||||
"baseUrl": "../",
|
||||
"typeRoots": ["../"],
|
||||
"paths": {
|
||||
"history": ["history/v3"],
|
||||
"history/*": ["history/v3/*"]
|
||||
},
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
|
||||
Reference in New Issue
Block a user