Skip to content

Commit

Permalink
Export types for options objects
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Jun 7, 2020
1 parent 90144a7 commit da807d5
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions packages/history/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,6 @@ export interface MemoryHistory<S extends State = State> extends History<S> {
index: number;
}

/**
* Describes an entry in the history stack. Useful when providing entries to
* {@link createMemoryHistory} via its `initialEntries` option.
*/
export type InitialEntry = Path | LocationPieces;

const readOnly: <T extends unknown>(obj: T) => T = __DEV__
? obj => Object.freeze(obj)
: obj => obj;
Expand Down Expand Up @@ -369,14 +363,20 @@ const BeforeUnloadEventType = 'beforeunload';
const HashChangeEventType = 'hashchange';
const PopStateEventType = 'popstate';

/**
* The type of options that are available in {@link createBrowserHistory}.
*/
export type BrowserHistoryOptions = { window?: Window };

/**
* Browser history stores the location in regular URLs. This is the standard for
* most web apps, but it requires some configuration on the server to ensure you
* serve the same app at multiple URLs.
*/
export function createBrowserHistory({
window = document.defaultView as Window
}: { window?: Window } = {}): BrowserHistory {
export function createBrowserHistory(
options: BrowserHistoryOptions = {}
): BrowserHistory {
let { window = document.defaultView! } = options;
let globalHistory = window.history;

function getIndexAndLocation(): [number, Location] {
Expand Down Expand Up @@ -582,15 +582,21 @@ export function createBrowserHistory({
// HASH
////////////////////////////////////////////////////////////////////////////////

/**
* The type of options that are available in {@link createHashHistory}.
*/
export type HashHistoryOptions = { window?: Window };

/**
* Hash history stores the location in window.location.hash. This makes it ideal
* for situations where you don't want to send the location to the server for
* some reason, either because you do cannot configure it or the URL space is
* reserved for something else.
*/
export function createHashHistory({
window = document.defaultView as Window
}: { window?: Window } = {}): HashHistory {
export function createHashHistory(
options: HashHistoryOptions = {}
): HashHistory {
let { window = document.defaultView! } = options;
let globalHistory = window.history;

function getIndexAndLocation(): [number, Location] {
Expand Down Expand Up @@ -836,18 +842,29 @@ export function createHashHistory({
// MEMORY
////////////////////////////////////////////////////////////////////////////////

/**
* Describes an entry in the history stack. Useful when providing entries to
* {@link createMemoryHistory} via its `initialEntries` option.
*/
export type InitialEntry = Path | LocationPieces;

/**
* The type of options that are available in {@link createMemoryHistory}.
*/
export type MemoryHistoryOptions = {
initialEntries?: InitialEntry[];
initialIndex?: number;
};

/**
* Memory history stores the current location in memory. It is designed for use
* in stateful non-browser environments like headless tests (in node.js) and
* React Native.
*/
export function createMemoryHistory({
initialEntries = ['/'],
initialIndex
}: {
initialEntries?: InitialEntry[];
initialIndex?: number;
} = {}): MemoryHistory {
export function createMemoryHistory(
options: MemoryHistoryOptions = {}
): MemoryHistory {
let { initialEntries = ['/'], initialIndex } = options;
let entries: Location[] = initialEntries.map(entry => {
let location = readOnly<Location>({
pathname: '/',
Expand Down

0 comments on commit da807d5

Please sign in to comment.