Skip to content

Commit a39a8a8

Browse files
icd2k3jschrader-nr
andauthored
refactor: switch to auto generated type definitions (#110)
* refactor: switch to auto generated type definitions * chore: switch to latest LTS version of node Co-authored-by: Justin Schrader <jschrader@newrelic.com>
1 parent 1db3048 commit a39a8a8

File tree

11 files changed

+1790
-1513
lines changed

11 files changed

+1790
-1513
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
strategy:
1818
matrix:
19-
node-version: [12.16.1]
19+
node-version: [12.18.0]
2020

2121
steps:
2222
- uses: actions/checkout@v2

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v12.16.1
1+
v12.18.0

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
{
22
"name": "react-router-breadcrumbs-hoc",
3-
"version": "3.2.10",
3+
"version": "3.3.0",
44
"description": "small, flexible, higher order component for rendering breadcrumbs with react-router 4.x",
55
"repository": "icd2k3/react-router-breadcrumbs-hoc",
66
"main": "dist/cjs/index.js",
77
"module": "dist/es/index.js",
88
"umd": "dist/umd/index.js",
9-
"types": "types/react-router-breadcrumbs-hoc/index.d.ts",
9+
"types": "dist/index.d.ts",
1010
"scripts": {
1111
"prepublishOnly": "npm run build",
1212
"build": "rollup -c",
1313
"test": "jest",
1414
"test-build": "sh ./scripts/test-build.sh",
15-
"types": "yarn type-src && yarn type-descriptions",
16-
"type-src": "tsc -p tsconfig.json",
17-
"type-descriptions": "tsc -p types/react-router-breadcrumbs-hoc",
15+
"types": "tsc -p tsconfig.json",
1816
"lint": "eslint ./src/**"
1917
},
2018
"husky": {
@@ -66,6 +64,7 @@
6664
"react-router": "^5.1.2",
6765
"rollup": "^2.1.0",
6866
"rollup-plugin-babel": "^4.4.0",
67+
"rollup-plugin-typescript2": "^0.27.1",
6968
"rollup-plugin-uglify": "^6.0.2",
7069
"typescript": "^3.8.3"
7170
},

rollup.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import babel from 'rollup-plugin-babel';
22
import commonjs from '@rollup/plugin-commonjs';
33
import resolve from '@rollup/plugin-node-resolve';
4+
import typescript from 'rollup-plugin-typescript2';
5+
import ts from 'typescript';
46
import { uglify } from 'rollup-plugin-uglify';
57

68
const pkg = require('./package.json');
@@ -10,6 +12,15 @@ const external = Object.keys(pkg.peerDependencies);
1012
const extensions = ['.js', '.tsx'];
1113

1214
const plugins = [
15+
typescript({
16+
useTsconfigDeclarationDir: true,
17+
tsconfigOverride: {
18+
typescript: ts,
19+
compilerOptions: {
20+
module: 'es2015',
21+
},
22+
},
23+
}),
1324
babel({
1425
exclude: 'node_modules/**',
1526
extensions,

src/index.tsx

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,33 @@
2121
import React, { createElement } from 'react';
2222
import { useLocation, matchPath, withRouter } from 'react-router';
2323

24-
// eslint-disable-next-line import/extensions, import/no-unresolved, no-unused-vars
25-
import * as types from '../types/react-router-breadcrumbs-hoc/index';
26-
2724
const DEFAULT_MATCH_OPTIONS = { exact: true };
2825
const NO_BREADCRUMB = 'NO_BREADCRUMB';
2926

27+
export interface Options {
28+
currentSection?: string;
29+
disableDefaults?: boolean;
30+
excludePaths?: string[];
31+
pathSection?: string;
32+
}
33+
34+
export interface Location {
35+
pathname: string
36+
}
37+
38+
export interface MatchOptions {
39+
exact?: boolean;
40+
strict?: boolean;
41+
sensitive?: boolean;
42+
}
43+
44+
export interface BreadcrumbsRoute {
45+
path: string;
46+
breadcrumb?: React.ComponentType | React.ElementType | string;
47+
matchOptions?: MatchOptions;
48+
routes?: BreadcrumbsRoute[];
49+
}
50+
3051
/**
3152
* This method was "borrowed" from https://stackoverflow.com/a/28339742
3253
* we used to use the humanize-string package, but it added a lot of bundle
@@ -49,10 +70,10 @@ const render = ({
4970
}: {
5071
breadcrumb: React.ComponentType | string,
5172
match: { url: string },
52-
location: types.Location
73+
location: Location
5374
}): {
5475
match: { url: string },
55-
location: types.Location,
76+
location: Location,
5677
key: string,
5778
breadcrumb: React.ReactNode
5879
} => {
@@ -75,7 +96,7 @@ const getDefaultBreadcrumb = ({
7596
pathSection,
7697
}: {
7798
currentSection: string,
78-
location: types.Location,
99+
location: Location,
79100
pathSection: string,
80101
}) => {
81102
const match = matchPath(pathSection, { ...DEFAULT_MATCH_OPTIONS, path: pathSection })
@@ -106,7 +127,7 @@ const getBreadcrumbMatch = ({
106127
excludePaths?: string[],
107128
location: { pathname: string },
108129
pathSection: string,
109-
routes: types.BreadcrumbsRoute[]
130+
routes: BreadcrumbsRoute[]
110131
}) => {
111132
let breadcrumb;
112133

@@ -189,9 +210,9 @@ export const getBreadcrumbs = (
189210
location,
190211
options = {},
191212
}: {
192-
routes: types.BreadcrumbsRoute[],
193-
location: types.Location,
194-
options?: types.Options
213+
routes: BreadcrumbsRoute[],
214+
location: Location,
215+
options?: Options
195216
},
196217
): Array<React.ReactNode | string> => {
197218
const matches:Array<React.ReactNode | string> = [];
@@ -238,21 +259,21 @@ export const getBreadcrumbs = (
238259
* Takes a route array and recursively flattens it IF there are
239260
* nested routes in the config.
240261
*/
241-
const flattenRoutes = (routes: types.BreadcrumbsRoute[]) => (routes)
242-
.reduce((arr, route: types.BreadcrumbsRoute): types.BreadcrumbsRoute[] => {
262+
const flattenRoutes = (routes: BreadcrumbsRoute[]) => (routes)
263+
.reduce((arr, route: BreadcrumbsRoute): BreadcrumbsRoute[] => {
243264
if (route.routes) {
244265
return arr.concat([route, ...flattenRoutes(route.routes)]);
245266
}
246267
return arr.concat(route);
247-
}, [] as types.BreadcrumbsRoute[]);
268+
}, [] as BreadcrumbsRoute[]);
248269

249270
/**
250271
* This is the main default HOC wrapper component. There is some
251272
* logic in here for legacy react-router v4 support
252273
*/
253274
export default (
254-
routes?: types.BreadcrumbsRoute[],
255-
options?: types.Options,
275+
routes?: BreadcrumbsRoute[],
276+
options?: Options,
256277
) => (
257278
Component: React.ComponentType<{
258279
breadcrumbs: Array<React.ReactNode | string>
@@ -278,7 +299,7 @@ export default (
278299
// fallback to withRouter for older react-router versions (4.x)
279300
/* istanbul ignore next */
280301
return withRouter(
281-
(props: { location: types.Location }) => {
302+
(props: { location: Location }) => {
282303
// eslint-disable-next-line no-console
283304
console.warn('[react-router-breadcrumbs-hoc]: react-router v4 support will be deprecated in the next major release. Please consider upgrading react-router and react-router-dom to >= 5.1.0');
284305

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"strictFunctionTypes": true,
1818
"strictNullChecks": true,
1919
"strictPropertyInitialization": true,
20-
"target": "es5"
20+
"target": "es5",
21+
"declaration": true,
22+
"declarationDir": "./dist"
2123
},
2224
"files": [
2325
"src/index.tsx"

types/react-router-breadcrumbs-hoc/index-tests.tsx

Lines changed: 0 additions & 8 deletions
This file was deleted.

types/react-router-breadcrumbs-hoc/index.d.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.

types/react-router-breadcrumbs-hoc/react-router-breadcrumbs-hoc-tests.tsx

Lines changed: 0 additions & 39 deletions
This file was deleted.

types/react-router-breadcrumbs-hoc/tsconfig.json

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)