Skip to content

Commit

Permalink
docs(readme): update type docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mohatt committed Mar 22, 2021
1 parent 9f68375 commit 5bc7966
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 53 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,34 +407,34 @@ Returns a function to be used to generate paths for a specific route.
Extends Gatsby's [navigate](https://www.gatsbyjs.org/docs/gatsby-link/#how-to-use-the-navigate-helper-function) to allow passing route names and params.

### getActivatedRoute
> `getActivatedRoute(): ActivatedRoute`
> `getActivatedRoute(): Route`

Gets the current active route based on `@reach/router` location history.

### getMatchingRoute
> `getMatchingRoute(path: string): ActivatedRoute`
> `getMatchingRoute(path: string, ignorePrefix?: boolean): Route`

Gets the route that matches a given path. *Note: The provided path should be prefixed with `pathPrefix` if any.*
Gets the route that matches a given path.

### isActivatedRoute
> `isActivatedRoute(route: string): boolean`

Checks whether a given route is currently active.

### getRoutes
> `getRoutes(): Array`
> `getRoutes(parent?: string): Route[]`

Gets an array of all routes.

### routeExists
> `routeExists(route: string): boolean`

Checks if there is a route defined with the given name.
Gets an array of all routes or routes nested under a given parent route.

### getRoute
> `getRoute(route: string): Route`

Gets a specific route.
Gets the Route object of a given route name.

### routeExists
> `routeExists(route: string): boolean`

Checks if a route is defined with the given name.


## Configuration
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ export function isActivatedRoute (route) {

/**
* Gets the route that matches a specific path
* use `ignorePrefix` if the path provided already contains `pathPrefix`
*/
export function getMatchingRoute (path, ignorePrefix) {
return pick(getRoutes(), ignorePrefix ? path : withPrefix(path))
}

/**
* Returns a function to be used to generate paths for a specific route
* use `ignorePrefix` to ignore adding `pathPrefix` to generated paths
*/
export function getPathGenerator (route, scope, ignorePrefix) {
const ro = getRoute(route)
Expand Down
10 changes: 6 additions & 4 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React from 'react'
import { PathFunction as PathGeneratorFunction } from 'path-to-regexp'

export type RouteScope = 'pagination'

export interface Route {
name: string
path: string
realpath: string
parent: {
name: string
scope: string
scope: RouteScope
} | null
}

Expand Down Expand Up @@ -55,6 +57,6 @@ export function getRoute(route: string): Route
export function getActivatedRoute(): Route
export function isActivatedRoute(route: string): boolean
export function getMatchingRoute(path: string, ignorePrefix?: boolean): Route
export function getPathGenerator(route: string, scope?: string, ignorePrefix?: boolean): PathGeneratorFunction<RouteParams>
export function generatePath(route: string, params?: RouteParams, scope?: string, ignorePrefix?: boolean): string
export function navigate(to: string, params?: RouteParams, scope?: string, options?: {}): void
export function getPathGenerator(route: string, scope?: RouteScope, ignorePrefix?: boolean): PathGeneratorFunction<RouteParams>
export function generatePath(route: string, params?: RouteParams, scope?: RouteScope, ignorePrefix?: boolean): string
export function navigate(to: string, params?: RouteParams, scope?: RouteScope, options?: {}): void
78 changes: 40 additions & 38 deletions types/index.js.flow
Original file line number Diff line number Diff line change
@@ -1,65 +1,67 @@
// @flow
import * as React from "react";
import * as React from "react"

export type RouteScope = 'pagination'

export interface Route {
name: string;
path: string;
realpath: string;
name: string
path: string
realpath: string
parent: {
name: string;
scope: string;
};
name: string
scope: RouteScope
} | null
}

export interface RouteParams {
[index: string]: string;
[index: string]: string
}

export interface PaginationInfo {
itemCount: number;
perPage: number;
pageCount: number;
currentPage: number;
hasNextPage: boolean;
hasPreviousPage: boolean;
itemCount: number
perPage: number
pageCount: number
currentPage: number
hasNextPage: boolean
hasPreviousPage: boolean
}

export interface PaginationProps {
route: string;
params?: RouteParams;
ui?: string;
range?: number;
className?: string;
renderDisabled?: boolean;
pageInfo: PaginationInfo;
route: string
params?: RouteParams
ui?: string
range?: number
className?: string
renderDisabled?: boolean
pageInfo: PaginationInfo
labels?: {
[index: string]: string | React.Node
};
}
theme?: {
[index: string]: string
};
}
}

export interface LinkProps {
to: string;
params?: RouteParams;
scope?: string;
to: string
params?: RouteParams
scope?: string
}

export type PathGeneratorFunction = (
data?: RouteParams,
options?: {}
) => string;
) => string

declare export var Pagination: React.ComponentType<PaginationProps>;
declare export var Link: React.ComponentType<LinkProps>;
declare export var Pagination: React.ComponentType<PaginationProps>
declare export var Link: React.ComponentType<LinkProps>

declare export function getRoutes(parent: string): [Route];
declare export function routeExists(route: string): boolean;
declare export function getRoute(route: string): Route;
declare export function getActivatedRoute(): Route;
declare export function isActivatedRoute(route: string): boolean;
declare export function getMatchingRoute(path: string, ignorePrefix?: boolean): Route;
declare export function getPathGenerator(route: string, scope?: string, ignorePrefix?: boolean): PathGeneratorFunction;
declare export function generatePath(route: string, params?: RouteParams, scope?: string, ignorePrefix?: boolean): string;
declare export function navigate(to: string, params?: RouteParams, scope?: string, options?: {}): void;
declare export function getRoutes(parent: string): [Route]
declare export function routeExists(route: string): boolean
declare export function getRoute(route: string): Route
declare export function getActivatedRoute(): Route
declare export function isActivatedRoute(route: string): boolean
declare export function getMatchingRoute(path: string, ignorePrefix?: boolean): Route
declare export function getPathGenerator(route: string, scope?: RouteScope, ignorePrefix?: boolean): PathGeneratorFunction
declare export function generatePath(route: string, params?: RouteParams, scope?: RouteScope, ignorePrefix?: boolean): string
declare export function navigate(to: string, params?: RouteParams, scope?: RouteScope, options?: {}): void

0 comments on commit 5bc7966

Please sign in to comment.