diff --git a/src/__tests__/__snapshots__/index.js.snap b/src/__tests__/__snapshots__/index.js.snap index 7fbe7be2..af4a21f8 100644 --- a/src/__tests__/__snapshots__/index.js.snap +++ b/src/__tests__/__snapshots__/index.js.snap @@ -69,6 +69,20 @@ Array [ `; exports[`API correctly fetchs routes 2`] = ` +Array [ + Object { + "name": "blog.pagination", + "parent": Object { + "name": "blog", + "scope": "pagination", + }, + "path": "/blog/page/:page", + "realpath": "/site/blog/page/:page", + }, +] +`; + +exports[`API correctly fetchs routes 3`] = ` Object { "name": "blog.post", "parent": null, @@ -103,6 +117,15 @@ Object { `; exports[`API picks the correct matching route 3`] = ` +Object { + "name": "home", + "parent": null, + "path": "/", + "realpath": "/site/", +} +`; + +exports[`API picks the correct matching route 4`] = ` Object { "name": "blog.tag.pagination", "parent": Object { @@ -114,7 +137,7 @@ Object { } `; -exports[`API picks the correct matching route 4`] = ` +exports[`API picks the correct matching route 5`] = ` Object { "name": "blog.post", "parent": null, diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 020cb35a..bdac3b50 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -25,6 +25,7 @@ router.useLocation = jest.fn().mockReturnValue({ describe('API', () => { it('correctly fetchs routes', () => { expect(getRoutes()).toMatchSnapshot() + expect(getRoutes('blog')).toMatchSnapshot() expect(routeExists('blog.post')).toBe(true) expect(routeExists('invalid')).toBe(false) expect(getRoute('blog.post')).toMatchSnapshot() @@ -38,9 +39,10 @@ describe('API', () => { expect(isActivatedRoute('home')).toBe(false) expect(isActivatedRoute('blog')).toBe(true) expect(() => isActivatedRoute('invalid')).toThrow() - expect(getMatchingRoute(withPrefix('/'))).toMatchSnapshot() - expect(getMatchingRoute(withPrefix('/blog/tag/test/page/4'))).toMatchSnapshot() - expect(getMatchingRoute(withPrefix('/blog/post/hi'))).toMatchSnapshot() + expect(getMatchingRoute('/')).toMatchSnapshot() + expect(getMatchingRoute(withPrefix('/'), true)).toMatchSnapshot() + expect(getMatchingRoute('/blog/tag/test/page/4')).toMatchSnapshot() + expect(getMatchingRoute('/blog/post/hi')).toMatchSnapshot() }) it('correctly generates paths', () => { diff --git a/src/index.js b/src/index.js index 65bf24f1..af0eedaf 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,9 @@ import { pick, compile } from './lib/route-compiler' import { useLocation } from '@reach/router' import routes from 'gatsby-plugin-advanced-pages-cache/routes.json' -// Gets an array of all routes +/** + * Gets an array of all routes or routes nested under a given parent route + */ export function getRoutes (parent) { if (!getRoutes.tree) { const tree = [] @@ -31,7 +33,9 @@ export function getRoutes (parent) { : getRoutes.tree } -// Gets a specific route +/** + * Gets the Route object of a given route name + */ export function getRoute (route) { if (typeof route !== 'string' || !route) { throw new TypeError( @@ -47,17 +51,23 @@ export function getRoute (route) { return ro } -// Checks if there is a route defined with the given name +/** + * Checks if a route is defined with the given name + */ export function routeExists (route) { return getRoutes().find(r => r.name === route) !== undefined } -// Gets the current active route based on `@reach/router` location history +/** + * Gets the current active route based on `@reach/router` location history + */ export function getActivatedRoute () { - return getMatchingRoute(useLocation().pathname) + return getMatchingRoute(useLocation().pathname, true) } -// Checks whether a given route is currently active +/** + * Checks whether a given route is currently active + */ export function isActivatedRoute (route) { const ro = getRoute(route) const activeRo = getActivatedRoute() @@ -66,12 +76,16 @@ export function isActivatedRoute (route) { : false } -// Gets the route that matches a specific path -export function getMatchingRoute (path) { - return pick(getRoutes(), path) +/** + * Gets the route that matches a specific path + */ +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 +/** + * Returns a function to be used to generate paths for a specific route + */ export function getPathGenerator (route, scope, ignorePrefix) { const ro = getRoute(route) if (!scope) { @@ -86,12 +100,16 @@ export function getPathGenerator (route, scope, ignorePrefix) { return compile(ignorePrefix ? childRo.path : childRo.realpath) } -// Generates a path for a specific route based on the given parameters. +/** + * Generates a path for a specific route based on the given parameters + */ export function generatePath (route, params = {}, scope, ignorePrefix) { return getPathGenerator(route, scope, ignorePrefix)(params) } -// Extends Gatsby's navigate to allow route names +/** + * Extends Gatsby's navigate to allow route names + */ export function navigate (to, params = {}, scope, options) { return gatsbyNavigate(generatePath(to, params, scope), options) } diff --git a/types/index.d.ts b/types/index.d.ts index eeedfca0..8d76aac8 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,78 +1,60 @@ -import * as React from 'react'; +import React from 'react' import { PathFunction as PathGeneratorFunction } from 'path-to-regexp' export interface Route { - name: string; - path: string; - scopes: { - [index: string]: string; - } -} - -export interface ActivatedRoute { - name: string; - path: string; - scope: string; + name: string + path: string + realpath: string + parent: { + name: string + scope: string + } | 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.ReactNode - }; + } theme?: { [index: string]: string - }; + } } export interface LinkProps { - to: string; - params?: RouteParams; + to: string + params?: RouteParams scope?: string } -export const Pagination: React.ComponentType; -export const Link: React.ComponentType; - -export function getRoutes(): [Route]; -export function routeExists(route: string): boolean; -export function getRoute(route: string): Route; -export function getActivatedRoute(): ActivatedRoute; -export function isActivatedRoute(route: string): boolean; -export function getMatchingRoute(path: string): ActivatedRoute; -export function getPathGenerator( - route: string, - scope?: string, - ignorePrefix?: boolean -): PathGeneratorFunction; -export function generatePath( - route: string, - params?: RouteParams, - scope?: string, - ignorePrefix?: boolean -): string; -export function navigate( - to: string, - params?: RouteParams, - scope?: string, - options?: {} -): void; +export const Pagination: React.ComponentType +export const Link: React.ComponentType + +export function getRoutes(parent?: string): [Route] +export function routeExists(route: string): boolean +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 +export function generatePath(route: string, params?: RouteParams, scope?: string, ignorePrefix?: boolean): string +export function navigate(to: string, params?: RouteParams, scope?: string, options?: {}): void diff --git a/types/index.js.flow b/types/index.js.flow index c70c8e85..553d58e7 100644 --- a/types/index.js.flow +++ b/types/index.js.flow @@ -4,17 +4,13 @@ import * as React from "react"; export interface Route { name: string; path: string; - scopes: { - [index: string]: string; + realpath: string; + parent: { + name: string; + scope: string; }; } -export interface ActivatedRoute { - name: string; - path: string; - scope: string; -} - export interface RouteParams { [index: string]: string; } @@ -58,26 +54,12 @@ export type PathGeneratorFunction = ( declare export var Pagination: React.ComponentType; declare export var Link: React.ComponentType; -declare export function getRoutes(): [Route]; +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(): ActivatedRoute; +declare export function getActivatedRoute(): Route; declare export function isActivatedRoute(route: string): boolean; -declare export function getMatchingRoute(path: string): ActivatedRoute; -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 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;