Skip to content

Commit

Permalink
perf(plugin): improve type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
mohatt committed Mar 22, 2021
1 parent 7df3960 commit 9f68375
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 100 deletions.
25 changes: 24 additions & 1 deletion src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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', () => {
Expand Down
42 changes: 30 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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(
Expand All @@ -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()
Expand All @@ -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) {
Expand All @@ -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)
}
Expand Down
94 changes: 38 additions & 56 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<PaginationProps>;
export const Link: React.ComponentType<LinkProps>;

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<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 const Pagination: React.ComponentType<PaginationProps>
export const Link: React.ComponentType<LinkProps>

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<RouteParams>
export function generatePath(route: string, params?: RouteParams, scope?: string, ignorePrefix?: boolean): string
export function navigate(to: string, params?: RouteParams, scope?: string, options?: {}): void
38 changes: 10 additions & 28 deletions types/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -58,26 +54,12 @@ export type PathGeneratorFunction = (
declare export var Pagination: React.ComponentType<PaginationProps>;
declare export var Link: React.ComponentType<LinkProps>;

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;

0 comments on commit 9f68375

Please sign in to comment.